Git bash-completion 支持文件名吗?
是否有支持文件名完成的 bash-completion 脚本?我主要使用 Mercurial,我可以输入:
hg diff test/test_<tab>
它将显示/完成所有修改的测试文件。它适用于大多数子命令,即 hg add
将仅列出未跟踪的文件。真的很方便。
git contrib 的 bash 脚本似乎不支持这一点。有没有其他选择,或者如何在命令行上使用 git?
Edit 2015
git-completion.bash
自 ~1.8.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那么,让我们看看 Mercurial bash 完成脚本如何执行此操作。
这是重要部分:
它被称为此处:
因此,它只是对
hg 的调用status -nmar
,并使用输出作为完成的文件列表。我认为将类似的内容修补到 git 补全脚本 - 我们必须在这里修改 __git_diff ,以不执行纯文件名+分支补全,而是调用
git status
反而。命令
(对于
git diff --cached
)和(对于
git diff
)似乎输出了正确的内容(如果没有重命名)。不过,当与 HEAD 以外的任何东西进行比较时,它们都没有用。
更通用的方法是使用
来自已给定 diff 命令行的
commit1
和commit2
(也许还有--cached
)。我在 bash 中实现了上面概述的想法,并修补到 git-completion.bash 中。如果您不想更改
git-completion.bash
,请将这两个函数添加到某个 bash 文件中,并在原始git-completion.bash
之后获取它。它现在应该可以使用像我提交此 作为 git 邮件列表的补丁,让我们看看这会产生什么结果。 (当我在那里得到反馈时,我会更新这个答案。)
更新:看起来这个形式不需要这个补丁,因为当前完成文件的方式对于想要检查的人来说更有用某些子目录是否有更改(例如,当 diff 输出可能为空时完成)。如果链接到某些配置变量(默认为当前行为),则可能会接受它。此外,缩进应适应标准(请参阅
我可能会再试一次,但不能保证在不久的将来会如此。如果其他人想做,请随意拿走我的代码,更改它并再次提交。
So, let's see how the Mercurial bash completion script does this.
This is the important part:
It gets called here:
Thus, it is simply a call of
hg status -nmar
, and using the output as a list of files for completion.I think it would not be too hard to patch something similar into the git completion script - we would have to modify
__git_diff
here to not do a plain filename + branch completion, but callinggit status
instead.The commands
(for
git diff --cached
) and(for
git diff
) seem to output the right thing (if there are no renames).They both are not useful when diffing to anything other than HEAD, though.
A more general way would be to use
where
commit1
andcommit2
(and maybe--cached
) come from the already given diff command line.I implemented the idea outlined above in bash, and patched into
git-completion.bash
. If you don't want to change yourgit-completion.bash
, add these two functions to some bash file and source it after the originalgit-completion.bash
. It should now work with commands likeI submitted this as a patch to the git mailing list, let's see what results from this. (I'll update this answer as I get feedback there.)
Update: Looks like this patch is not wanted in this form, as the current way to complete files is more useful for people which want to check whether there are changes in some subdirectory (e.g. completing when the diff output could be empty). It might be accepted if linked to some configuration variable (with the default being the current behavior). Also, the indenting should be adapted to the standard (see the answer from Junio C Hamano).
I might take another go on it, but can't guarantee this for the near future. If someone else want to do, feel free to take my code, change it and submit it again.
这为我解决了 git diff 问题,将以下内容放入 .bashrc 中:
然后改为执行
gid
git diff
。它可能被简化了,但似乎作为一种快速解决方案效果很好。This solves the
git diff <tab>
problem for me, put the following in .bashrc:And then do
gid <tab>
instead ofgit diff <tab>
. It may be simplified, but seems to work well as a quick fix.这并不是您真正想要的答案,但我想让您知道,很快 Fish(友好的交互式 shell)将为您提供开箱即用的 git 文件名补全支持。目前它处于 master 状态,即将发布 2.3.0 版本。
https://github.com/fish-shell/fish-shell/issues/ 901
https://github.com/fish-shell/fish-shell/pull/ 2364
https://github.com/fish-shell/fish-shell/commit/ c5c59d4acb00674bc37198468b5978f69484c628
如果您的状态如下:
您也可以只输入
README
并点击 Tab,如果它是唯一匹配的,它会为您插入它。真他妈的好!Not really your desired answer but I wanted to let you know that soon fish (friendly interactive shell) will give you git filename completion support out of the box. It is currently in master with a 2.3.0 release coming soon.
https://github.com/fish-shell/fish-shell/issues/901
https://github.com/fish-shell/fish-shell/pull/2364
https://github.com/fish-shell/fish-shell/commit/c5c59d4acb00674bc37198468b5978f69484c628
If you have a status like this:
You can also just type
README
and hit tab and it will insert it for you if it is the only match. Friggin nice!自 2011 年以来,正如 OP 所评论的,Git 从 ~1.8.2 开始支持完整文件名完成。
但在 Git 2.18(2018 年第 2 季度)中,提供路径列表的 shell 补全(在
contrib/
中)已得到一定程度的优化。请参阅 提交 78a2d21(2018 年 4 月 4 日),作者:Clemens Buchacher (
drizzd
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 3a940e9,2018 年 4 月 25 日)目录遍历代码具有冗余递归调用,这使得其性能特征相对于树的深度呈指数级增长,这一点已在 Git 2.27(2020 年第二季度)中得到纠正。
请参阅提交c0af173,提交 95c11ec, 提交 7f45ab2, 提交 1684644, 提交 8d92fb2, 提交 2df179d, 提交 0126d14, 提交 cd129ee, 提交 446f46d, 提交 7260c7b, 提交 ce5c61a(2020 年 4 月 1 日),作者:以利亚纽伦 (
newren
)。请参阅 提交 0bbd0e8(2020 年 4 月 1 日),作者:Derrick Stolee (
derrickstolee
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 6eacc39,2020 年 4 月 29 日)警告:Git 2.29 (2020 年第四季度)修复了 2.27 周期期间引入的回归。
请参阅提交 cada730(7 月 20 日 ) 2020)由 Martin Ågren(
无
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 82fafc7,2020 年 7 月 30 日)Git 2.38(2022 年第 3 季度)修复了 2.27 周期中引入的另一个回归,这可能会影响 git-bash 的完成。
在非裸存储库中,当 core.worktree 配置变量指向以存储库作为其子目录的目录时,Git 的行为在 Git 2.27 天中回归。
请参阅 提交 d6c9a71,提交 2712899(2022 年 6 月 16 日),作者:高斯·格佩特 (
ggossdev
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 dc6315e,2022 年 7 月 14 日)Since 2011, as the OP comments, Git supports full filename completion since ~1.8.2.
But with Git 2.18 (Q2 2018), the shell completion (in
contrib/
) that gives list of paths have been optimized somewhat.See commit 78a2d21 (04 Apr 2018) by Clemens Buchacher (
drizzd
).(Merged by Junio C Hamano --
gitster
-- in commit 3a940e9, 25 Apr 2018)The directory traversal code had redundant recursive calls which made its performance characteristics exponential with respect to the depth of the tree, which was corrected with Git 2.27 (Q2 2020).
See commit c0af173, commit 95c11ec, commit 7f45ab2, commit 1684644, commit 8d92fb2, commit 2df179d, commit 0126d14, commit cd129ee, commit 446f46d, commit 7260c7b, commit ce5c61a (01 Apr 2020) by Elijah Newren (
newren
).See commit 0bbd0e8 (01 Apr 2020) by Derrick Stolee (
derrickstolee
).(Merged by Junio C Hamano --
gitster
-- in commit 6eacc39, 29 Apr 2020)Warning: Git 2.29 (Q4 2020) fixes a regression introduced during 2.27 cycle.
See commit cada730 (20 Jul 2020) by Martin Ågren (
none
).(Merged by Junio C Hamano --
gitster
-- in commit 82fafc7, 30 Jul 2020)Git 2.38 (Q3 2022) fixes another regression introduced during 2.27 cycle, which can impact git-bash completion.
In a non-bare repository, the behavior of Git when the
core.worktree
configuration variable points at a directory that has a repository as its subdirectory, regressed in Git 2.27 days.See commit d6c9a71, commit 2712899 (16 Jun 2022) by Goss Geppert (
ggossdev
).(Merged by Junio C Hamano --
gitster
-- in commit dc6315e, 14 Jul 2022)