Git的别名 - 分支名称的命令行自动完成 [英] Git aliases - command line autocompletion of branch names
问题描述
如果我运行一个普通的Git命令,如 git的结帐
击中TAB键时,我得到分支名称有益的自动完成。
我有采取分支名称作为参数几git的别名,我想知道是否有获取分支名自动完成与他们合作的一种方式?
的 修改的
只是为了从评论的讨论提供一些澄清,别名有直接映射做工精细,即:
CI =提交
共检出=
它的那些更复杂一些,并使用 $ 1
作为例如不这样做,一个参数:
tagarchive = F(){git的变量归档/ $ 1原点/ $ 1安培;&安培;混帐推产地:$ 1安培;&安培;混帐推产地存档/ $ 1安培;&安培; git的分支-d $ 1; }; F
有关git的别名,为Git的命令自动完成功能( __ git的()
)使用到一个呼叫混帐配置--get的别名。$ 1
来确定相当于自动完成功能。这适用于简单的映射,但会窒息更复杂的别名。
要解决这个问题,与您的别名,即相匹配的名称来定义的自动完成功能 _git_tagarchive()
。对于git的自动完成功能,应该采摘并用它来自动完成。
例如:
[我@ home的] $ git的tagarchive<标签><标签>
作者gentleSelect / LICENSE的.gitignore test_multiple.html
的cron / git的/ index.html的README.md
[我@ home的] $ _git_tagarchive(){
> _git_branch#重复使用的git分支
> }
[我@ home的] $ git的tagarchive<标签><标签>
enable_multiple主产地/ GH-V0.1页v0.1.3
FETCH_HEAD ORIG_HEAD产地/ HEAD v0.1.1 v0.1.3.1
HEAD产地/ enable_multiple产地/大师v0.1.2
有关更永久的解决方案简单函数的定义添加到您的的.bashrc
文件。例如:
_git_tagarchive()
{
_git_branch
}
请注意,我已经简单地重复使用的自动完成功能,的Git分支
;你不妨把这个改变的东西更适合或写你自己的。
更多信息
此溶液基础上确定 /etc/bash_completion.d/git
的探索。
通常情况下,别名git的命令是由 __ git_aliased_commands(),它解析
来决定的自动完成功能使用。使用更复杂的shell命令作为别名目标将挫败理解这种做法。混帐配置--get的输出
函数处理的别名。$ 1
进一步看,它似乎对git的自动完成功能( _git()
)的自动完成功能链由简单的$ P $子ppending与功能 _git _
(带破折号( - 替换为下划线
)的命令)。这是前 __做git_aliased_command()
被选中所以这是我们可以使用。
_git()
{
#.....
当地completion_func =_ _混帐$ {//命令 - / _}
声明-f $ completion_func>的/ dev / null的&放大器;&安培; $ completion_func&放大器;&安培;返回 本地扩展= $(__ git_aliased_command$指令)
如果[-n$扩张];然后
completion_func =_ _混帐$ {//扩张 - / _}
声明-f $ completion_func>的/ dev / null的&放大器;&安培; $ completion_func
科幻}
我已经为方针,因此,以确保您的别名相匹配函数存在,即 _git_tagarchive()
。
If I run a regular git command such as git checkout
I get helpful autocompletion of branch names when hitting the tab key.
I have a few git aliases which take branch names as parameters, and I'm wondering if there's a way of getting the branch name autocompletion to work with them?
Edit:
Just to provide some clarification from the discussion in the comments, aliases with a direct mapping work fine, i.e.:
ci = commit
co = checkout
It's ones that are a bit more involved and use $1
as a parameter that don't, for example:
tagarchive = !f() { git tag archive/$1 origin/$1 && git push origin :$1 && git push origin archive/$1 && git branch -d $1; }; f
For git aliases, the autocomplete function for the git command (__git()
) uses a call to git config --get "alias.$1"
to determine that equivalent autocomplete function. This works for simple mappings but will choke on more complex aliases.
To get around this, define an autocomplete function with a name that matches your alias, i.e. _git_tagarchive()
. The autocomplete function for git should pick that up and use it for autocompletion.
For example:
[me@home]$ git tagarchive <TAB><TAB>
AUTHORS gentleSelect/ .gitignore LICENSE test_multiple.html
cron/ .git/ index.html README.md
[me@home]$ _git_tagarchive() {
> _git_branch # reuse that of git branch
> }
[me@home]$ git tagarchive <TAB><TAB>
enable_multiple master origin/gh-pages v0.1 v0.1.3
FETCH_HEAD ORIG_HEAD origin/HEAD v0.1.1 v0.1.3.1
HEAD origin/enable_multiple origin/master v0.1.2
For a more permanent solution simply add the function definition to your bashrc
file. Eg:
_git_tagarchive()
{
_git_branch
}
Note that I've simply reused the autocomplete function for git branch
; you may wish to change this to something more suitable or write your own.
More info
This solution was identified based on an exploration of /etc/bash_completion.d/git
.
Typically, aliased git commands are handled by the __git_aliased_commands()
function which parses the output of git config --get "alias.$1"
to decide on the autocomplete function to use. Using a more complex shell command as the alias target would understandably foil this approach.
Looking further, it appears the autocomplete function for git (_git()
) chains in autocomplete function for subcommands by simple prepending the function with _git_
(with dashes (-
) in the command replaced by underscores). This is done before __git_aliased_command()
is checked so this is something we could use.
_git ()
{
# .....
local completion_func="_git_${command//-/_}"
declare -f $completion_func >/dev/null && $completion_func && return
local expansion=$(__git_aliased_command "$command")
if [ -n "$expansion" ]; then
completion_func="_git_${expansion//-/_}"
declare -f $completion_func >/dev/null && $completion_func
fi
}
The approach I've gone for is therefore to ensure that a function that matches your alias exists, i.e. _git_tagarchive()
.
这篇关于Git的别名 - 分支名称的命令行自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!