问题描述
我正在研究git交互功能,对于我来说,最重要的是帅哥的合并和选择。它的效果非常好,但我需要小块。处理源代码文件时,大块通常包含两个不相关的不同更改(通常彼此之间相隔几行)。
这可能是因为git可以识别具有给定配置的粗线,例如行距或字符数之类的东西。
这是正确的,并且如果是,是否有任何方法可以更改/重新配置此行为?
非常感谢
如果您仅指的是 git diff
,则块上下文大小是可配置的:可通过设置- U
选项(如果已指定),否则通过 diff.context
设置(如果已设置)。如果所有这些都失败,则默认值为三行上下文(上下两行)。
因此:
$ git config --global diff.context 1
将diff上下文缩小为每个方向上的一行,而不是3.还有一个-inter-hunk-context
选项值,可用于将更多的块合并在一起(这与您想要的相反,因此我在此提及仅出于完整性考虑;请参见 diff.interHunkContext
(在Git 2.12和更高版本中可用);另请参见-function-context
aka -W
)。
但是,运行 git add --interactive
git diff-files
或 git diff-index
而不是普通的 git diff
,并作为:
(强调黑体字)。
尽管这是关于 diff.autoRefreshIndex
设置,它适用于 all 的所有可配置项: git diff
读取并遵循您的配置,但是 git diff -index
和 git diff-files
等(故意,这样脚本应该使用管道命令而不是瓷器来获取)
最后这意味着如果您专门指的是 git,请添加--interactive
,答案是否定的,或者,不是没有一点点的工作。交互式add Perl脚本使用管道命令,而不使用瓷器 git diff
,因此它必须自己获取并使用任何此类设置。它确实获取并使用了您的 diff.algorithm
和 diff.compactionHeuristic
设置,但它不使用您的 diff.context
设置。
这样做很容易,但是需要修改Git
diff --git a / usr / local / libexec / git-core / git-add-interactive b / tmp / git- add--interactive
索引235fb88..ba001a1 100755
--- a / usr / local / libexec / git-core / git-add--interactive
+++ b / tmp / git-add--interactive
@@ -47,2 +47,3 @@ my $ normal_color = $ repo-> get_color(, reset);
+我的$ diff_context_size = $ repo-> config(’diff.context’);
我的$ diff_algorithm = $ repo-> config(’diff.algorithm’);
@@ -753,2 +754,6 @@ sub parse_diff {
}
+ if(defined $ diff_context_size){
+我的$ Uarg = sprintf(- U%d,$ diff_context_size);
+拼接@diff_cmd,1,0, $ Uarg;
+}
if($ diff_compaction_heuristic){
(可能有更好的设置方法 -U
参数;我的Perl很笨拙)。如果要遵循您的 diff.context
,交互式补丁(或 git add -p
)模式需要添加此代码段设置。
当然,您可以简单地分割
( s
)a在交互式添加过程中出现了粗体,因为链接到,因此实际上并不需要。不过,如果包含它,可能会很好。
I am investigating the the git interactive features, from which the most important for me is the hunks merging and selection. It works very good but I need the hunks to be smaller. When working with source code files, very often the hunk contains two different changes that are not related (normally a few lines apart from each other).This is probably because git recognizes the hunks with a given configuration, like lines apart, or ammount of chars or something like that.
Is this correct, and if so, is there any way to change / reconfigure this behavior?
Thanks a lot,
If you are referring to just plain git diff
, the hunk context size is configurable: it is set via the -U
option, if specified, otherwise from your diff.context
setting, if set. If all of those fail the default is three lines of context (both above and below).
Hence:
$ git config --global diff.context 1
reduces the diff context to one line in each direction, instead of 3. There is also an --inter-hunk-context
option value that you can use to fuse more hunks together (this is the opposite of what you want so I mention it only for completeness; see diff.interHunkContext
, available in Git 2.12 and later; and see also --function-context
aka -W
).
However, git add --interactive
runs git diff-files
or git diff-index
rather than plain git diff
, and as the git config
documentation cleverly hides:
(bold-face emphasis mine).
Although this talks about the diff.autoRefreshIndex
setting, it applies to all of the configurable items: git diff
reads and obeys your configuration, but git diff-index
and git diff-files
and so on do not (intentionally, so that scripts, which should use plumbing commands rather than porcelain, can get the correct behavior regardless of newly added options).
What this means in the end is that if you are referring specifically to git add --interactive
, the answer is no, or rather, not without a little bit of work. The interactive add Perl script uses the plumbing commands, not the porcelain git diff
, so it must obtain and use any such setting itself. It does obtain and use your diff.algorithm
and diff.compactionHeuristic
settings, but it does not use your diff.context
setting.
Making it do so is easy but requires modifying Git slightly:
diff --git a/usr/local/libexec/git-core/git-add--interactive b/tmp/git-add--interactive
index 235fb88..ba001a1 100755
--- a/usr/local/libexec/git-core/git-add--interactive
+++ b/tmp/git-add--interactive
@@ -47,2 +47,3 @@ my $normal_color = $repo->get_color("", "reset");
+my $diff_context_size = $repo->config('diff.context');
my $diff_algorithm = $repo->config('diff.algorithm');
@@ -753,2 +754,6 @@ sub parse_diff {
}
+ if (defined $diff_context_size) {
+ my $Uarg = sprintf("-U%d", $diff_context_size);
+ splice @diff_cmd, 1, 0, "$Uarg";
+ }
if ($diff_compaction_heuristic) {
(there may be a better way to set up the -U
argument; my Perl is clumsy). The interactive patch (or git add -p
) mode needs this snippet added if it is to obey your diff.context
setting.
Of course, you can simply split
(s
) a hunk during interactive add, as pedrorijo91 reminded us in a comment linking to Can I modify git-add's hunk size?, so there's no real need for this. It might be nice if it were included, though.
这篇关于GIT:如何配置大块头识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!