如何使用自定义别名替换内部git命令(例如git status) [英] How to replace internal git commands, such as `git status`, with custom aliases

查看:219
本文介绍了如何使用自定义别名替换内部git命令(例如git status)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案教了如何在git别名中执行自定义bash命令,本文(强大的Git别名的一个怪异技巧)教得很漂亮。但是,当我为内部git命令加上别名时,它似乎不起作用。如何用自定义脚本替换内部git命令?

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?

例如:我有一个自定义python脚本:
custom_script.py

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

print("hello")

在我的 project_name / .git / config 文件中,添加以下别名:

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

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

然后运行 git status 完美的作品!我看到打印出 hello,然后显示 git status返回消息。

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

但是,如果我将别名从 statuss 重命名为 status ,它不再起作用。

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"

我这样做是为了通过简单地调用 git status ,它 first 调用我的 custom_script.py,然后然后运行 git status

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

注意:


  • 理想情况下,它可以在Windows,Mac或Linux上运行,但是Linux是我最关心的,因此仅Linux解决方案是可以的。

  • 理想情况下,我想通过git别名来做到这一点,但是如果我需要使用bash别名或其他Linux /计算机技巧,那总比没有好。

推荐答案

Git不允许您覆盖别名中的内部命令,因为这样做会破坏任何使用那些期望它们起作用的内部命令的脚本像平常一样。这一点特别重要,因为某些Git命令是shell脚本,而覆盖这些命令可能会破坏Git的shell脚本。

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.

您可以通过编写称为<$ c的shell脚本来覆盖它。 $ c> git 放置在 $ PATH 中的目录中,这是完成此操作的最简单方法。但是,请注意, git 的第一个参数不必是命令: git 需要大量的选项,在命令之前,例如 -c -C ,您的脚本将需要解析这些命令以避免破坏可能调用 git 的其他任何脚本(例如,可能包括您的编辑器)。

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.

但是,如果您只想将其用于交互式使用,可以在您的 $ PATH 中创建一个脚本,称为 git-wrapper ,然后在内部执行类似的操作:

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 "$@"

然后可以在〜/ .bash_profile 或<$ c中运行 alias git = git-wrapper $ c>〜/ .zshrc (但不是〜/ .bashrc 〜/ .zshenv )。这只会影响您专门编写 git status 的情况,而不会影响任何脚本用途。这也许对您足够好。

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.

这篇关于如何使用自定义别名替换内部git命令(例如git status)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆