How to replace internal git commands, such as `git status`, with custom aliases

独自空忆成欢 提交于 2019-12-14 03:07:26

问题


This answer teaches how to do custom bash commands inside git aliases, and this article (One weird trick for powerful Git aliases) teaches it beautifully. However, it doesn't seem to work when I alias internal git commands. How can I replace internal git commands with custom scripts?

Ex: I have a custom python script: custom_script.py

print("hello")

In my project_name/.git/config file I add this alias:

[alias]
    statuss = "!f() { \
        python3 custom_script.py && git status; \
    }; f"

I then run git statuss and it works perfectly! I see "hello" printed out, followed by the "git status" return messages.

HOWEVER, if I rename my alias name from statuss to status, it no longer works.

[alias]
    status = "!f() { \
        python3 custom_script.py && git status; \
    }; f"

How can I do this so that by simply calling git status, it first calls my "custom_script.py" and then runs git status?

Notes:

  • Ideally, this would work in Windows, Mac, or Linux, but Linux is what I care about most, so a Linux-only solution would be ok.
  • Ideally, I'd like to do this via a git alias, but if I need to use a bash alias or other Linux/computer trick, that's better than nothing.

回答1:


Git doesn't allow you to override internal commands in aliases, because doing so would break any scripting that uses those internal commands that expected them to function as normal. This is especially important because some Git commands are shell scripts, and overriding those commands could break Git's shell scripts.

You can override this by writing a shell script called git which you place into a directory in $PATH, and that is the easiest way to accomplish it. However, be aware that the first argument to git need not be a command: git takes a large number of options which precede the command, such as -c and -C, and your script would need to parse those in order to avoid breaking any other scripts that might call git (which might, for example, include your editor).

So while this is possible, it's very tricky, and any correct solution would be fairly lengthy (which is why I haven't attempted it here). Generally, the recommended solution is to use an alias which doesn't mirror a builtin name, which is much simpler.

However, if you want this only for interactive use, it's possible to create a script in your $PATH called, say, git-wrapper, and do something like this inside:

#!/bin/sh

if [ "$1" = status ]
then
    python3 custom_script.py
fi
exec git "$@"

You can then run alias git=git-wrapper in your ~/.bash_profile or ~/.zshrc (but not ~/.bashrc or ~/.zshenv). This will affect only cases where you specifically write git status, but not any scripting uses. This might be good enough for you, or not.



来源:https://stackoverflow.com/questions/52123145/how-to-replace-internal-git-commands-such-as-git-status-with-custom-aliases

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