Gre*_*rdt 4 git bash autocomplete tab-completion
我正在编写一个 shell 脚本来向 git 添加一个“review”子命令,以便更轻松地进行代码审查。git diff它基本上是和命令的精美包装git difftool,但需要较少的输入。
一些用法示例:
# Lists added, deleted, renamed and modified files between master and current branch
git review master -l
# Opens difftool for files modified between master and current branch
git review -m
Run Code Online (Sandbox Code Playgroud)
我想在我的 shell 脚本中启用分支自动完成,但我无法弄清楚如何做到这一点。这就是我真正追求的:
git review ma<tab><tab>
Run Code Online (Sandbox Code Playgroud)
然后它的行为应该像git checkout ma<tab><tab>.
我的外壳脚本:
# Lists added, deleted, renamed and modified files between master and current branch
git review master -l
# Opens difftool for files modified between master and current branch
git review -m
Run Code Online (Sandbox Code Playgroud)
确实,由于我正在输入命令,我怀疑我的 shell 脚本与解决方案有任何关系。
其他一些有用的信息:
有没有办法将 git 分支自动完成添加到自定义 git 扩展命令中?
Linux 的一个有趣的相关问题:custom git command autocompletion。
让我们假设您的脚本的最小版本,例如
#!/usr/bin/env bash
git diff "$@"
Run Code Online (Sandbox Code Playgroud)
这是可执行的并且位于您的$PATH. 我们就这样称呼它吧gitreview。
要获取它的别名,您可以将其添加到您的.gitconfig:
[alias]
review = "!f() { gitreview \"$@\"; }; f"
Run Code Online (Sandbox Code Playgroud)
review当您输入时,这会让您完成git。现在,要使其行为与 相同git checkout,您可以使用 null 命令,如下所示:
review = "!f() { : git checkout ; gitreview \"$@\"; }; f"
Run Code Online (Sandbox Code Playgroud)
它会完成相同的git checkout!checkout请注意,和之间;需要有空格。
这在评论中提到git-completion.bash:
# If you use complex aliases of form '!f() { ... }; f', you can use the null
# command ':' as the first command in the function body to declare the desired
# completion style. For example '!f() { : git commit ; ... }; f' will
# tell the completion to use commit completion. This also works with aliases
# of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".
Run Code Online (Sandbox Code Playgroud)
或者,您可以通过命名脚本git-review并将其保留在$PATH. 要获得它的补全(假设您有 Bash 和 Git 补全),请将其添加到您的~/.bash_completion文件中(如果不存在则创建它):
_git_review() { _git_checkout; }
Run Code Online (Sandbox Code Playgroud)
Git 完成(对于 Git 2.18 或更高版本)检查名为 的完成函数_git_$command,通过执行此操作,您可以说“只需调用_git_checkout”。
| 归档时间: |
|
| 查看次数: |
784 次 |
| 最近记录: |