Creating a git alias containing bash command substitution and an argument

安稳与你 提交于 2021-02-05 11:27:07

问题


Got this:

git diff $(git status -s | awk 'FNR == $LINE_NUM {print $2}')

...where line number is the file output by status.

Now I want to create an alias for this command in .gitconfig:

[alias]
diff-num = $WHAT_SHOULD_I_PUT_HERE?

I want this alias to run git's diff command based on the line number argument I'll type in on the command line. I'm new to bash and I'm not sure if I need a bash function to handle this or how to pass the argument into the substituted command.


回答1:


The usual form for this is to use the conventional ! shell escape, define an arbitrarily-named shell function and run that, git supplies it with your args. One of my goto's is lgdo,

git config alias.lgdo '!f() { git log --graph --decorate --oneline "${@---all}"; }; f'

also, I have gr and grl as git config --get-regexp {,--local} aliases:

[alias]
        grl = "!f() { git config --local --get-regexp \"${@-.}\"; }; f"
        gr = "!f() { git config --get-regexp \"${@-.}\"; }; f"



回答2:


OK, got it worked out. Git alias looks like this:

diff-num = ! diff_num

Bash function is this:

diff_num () {
    line=${1:-1}
    git diff $(git status -s | awk 'FNR == '$line' {print $2}')
}

Function must be exported with the following bash command in your bash config file:

export -f diff_num



来源:https://stackoverflow.com/questions/55432750/creating-a-git-alias-containing-bash-command-substitution-and-an-argument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!