Notgnoshi / dotfiles

Personal dotfiles and shell scripts
https://agill.xyz
0 stars 0 forks source link

Create .git-blame-ignore-revs if it isn't present #123

Closed Notgnoshi closed 6 months ago

Notgnoshi commented 7 months ago

I don't think this is possible without forking either fugitive or git because fugitive is invoking git like:

-C /home/nots/.config/dotfiles --literal-pathspecs --no-literal-pathspecs --no-pager -c blame.coloring=none -c blame.blankBoundary=false blame --show-number -- st
owdir/.vim/vimrc

which means I'd have to write an argument parser instead of making a git shim like:

#!/bin/bash

## Add a .git-blame-ignore-revs file if it doesn't exist
__create_git_blame_ignore_revs() {
    local repo_dir
    repo_dir="$(git rev-parse --show-toplevel)"
    local ignore_file="$repo_dir/.git-blame-ignore-revs"
    local exclude_file="$repo_dir/.git/info/exclude"
    if [[ ! -f "$ignore_file" ]]; then
        touch "$ignore_file"
        if ! grep --fixed-strings ".git-blame-ignore-revs" "$exclude_file"; then
            echo ".git-blame-ignore-revs" >>"$exclude_file"
        fi
    fi
}

## A (hopefully) completely transparent wrapper around the real git that allows me to add hooks
## around certain commands
main() {
    local subcommand="$1"
    case "$subcommand" in
    blame)
        __create_git_blame_ignore_revs
        /usr/bin/git "$@"
        ;;
    *)
        /usr/bin/git "$@"
        ;;
    esac
}

main "$@"

Perhaps a better (more pragmatic?) way is to pass --ignore-revs-file if .git-blame-ignore-revs exists. It's not a perfect solution since that filename could be anything, but it's the filename that all of the projects I know of use ...

But I guess that has the same problem - I can't reliably provide my own shim for git-blame, because it's an internal command provided by the git executable, which parses CLI arguments before looking for git-* scripts in the PATH.