是否可以配置git diff来尊重缩进和语法?我不是在谈论忽略缩进和空格,而是使用空行,缩进级别和可能的括号,以帮助将旧行与新行匹配.
例如,git diff经常切换函数及其docblock,如下所示:
class C {
/**
+ * Goes to the bar.
+ */
+ function bar() {
+ return 'bar';
+ }
+
+ /**
* Gets your foo up to date.
*/
function foo() {
Run Code Online (Sandbox Code Playgroud)
当我愿意的时候
class C {
+
+ /**
+ * Goes to the bar.
+ */
+ function bar() {
+ return 'bar';
+ }
/**
* Gets your foo up to date.
*/
function foo() {
Run Code Online (Sandbox Code Playgroud)
在这个例子中它仍然是非常无害的,但是有些例子中函数及其docblock由于贪婪和天真的diff实现而被撕裂.
注意:我已经配置*.php diff=php了 …
我想使用Git的耐心差异算法(如果你git diff用--patience参数调用你得到的算法)git add -p.我怎样才能做到这一点?
背景:我正在使用一些XML文件,并且由于"未对齐"的进入/退出标记,git diff正常算法会产生相当差的差异.如果我运行git diff --patience,我会得到更多有用的差异,但没有明显的方法来使用这些差异git add -p.
作为我的发布过程的一部分,我必须比较我的应用程序使用的一些JSON配置数据.作为第一次尝试,我只是漂亮地打印了JSON并对它们进行了差异化(使用kdiff3或者只是diff).
然而,随着数据的增长,kdiff3会混淆输出中的不同部分,使得添加看起来像巨型修改,奇数删除等.这使得很难弄清楚什么是不同的.我也尝试过其他差异工具(meld,kompare,diff,还有其他一些),但它们都有同样的问题.
尽管我付出了最大努力,但我似乎无法以diff工具可以理解的方式格式化JSON.
示例数据:
[
{
"name": "date",
"type": "date",
"nullable": true,
"state": "enabled"
},
{
"name": "owner",
"type": "string",
"nullable": false,
"state": "enabled",
}
...lots more...
]
Run Code Online (Sandbox Code Playgroud)
以上可能不会导致问题(当开始有数百行时会出现问题),但这就是所比较的要点.
这只是一个样本; 完整对象是4-5个属性,有些属性中有4-5个属性.属性名称非常统一,但它们的值非常不同.
通常,似乎所有的diff工具都会将结束"}"与关闭"}"的下一个对象混淆.我似乎无法打破这种习惯.
我尝试添加空格,更改缩进,并在各个对象之前和之后添加一些"BEGIN"和"END"字符串,但该工具仍然感到困惑.
所以,我已经遇到过几次这个问题了.在一轮更改中,我删除functionA()并添加functionB()到同一个地方.当我运行时diff,我最终会遇到一系列可怕的混乱变化,它会尝试将两个函数匹配在它们的常用括号上,而不是将所有这些functionA作为删除而全部functionB作为补充.有关简化示例:
int functionA(int a, bool b)
{
int c;
bool d;
if (a == b)
{
//do stuff
}
//do more stuff
}
Run Code Online (Sandbox Code Playgroud)
换成了
void functionB()
{
// do different stuff
for (int x=0; x<10; x++)
{
//do more different stuff
}
//do even more different stuff
}
Run Code Online (Sandbox Code Playgroud)
差异可能会产生
-int functionA(int a, bool b)
+void functionB()
{
- int c;
- bool d;
- if (a == b)
+ // …Run Code Online (Sandbox Code Playgroud) 我想要一个带有交错线的差异,即"帅哥"不超过一行.
例如,而不是
-t1 = "Christmas 2013"
-t2 = "Easter 2013"
-t3 = "Thanksgiving 2013"
+t1 = "Christmas 2014"
+t2 = "Easter 2014"
+t3 = "Thanksgiving 2014"
Run Code Online (Sandbox Code Playgroud)
我要这个:
-t1 = "Christmas 2013"
+t1 = "Christmas 2014"
-t2 = "Easter 2013"
+t2 = "Easter 2014"
-t3 = "Thanksgiving 2013"
+t3 = "Thanksgiving 2014"
Run Code Online (Sandbox Code Playgroud)
到目前为止我有
git diff -U0 --ignore-space-at-eol before after holidays.ini
Run Code Online (Sandbox Code Playgroud)
我尝试过设置--break-rewrites=0%/0%,--break-rewrites=100%/0%等等,但它没有改变任何东西(我甚至不知道它是否与我的问题相关).
编辑:我添加了一些我认为不必要的信息,但事实并非如此.我有两个分支,A和B.在A中进行三次提交后更改了file.c我想将它们挑选到B中,还有一个在A~1中更改的file.h
> git cherry-pick A~2
Success
> git cherry-pick A~1
error: could not apply 81e0723...
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
> git status
You are currently cherry-picking commit 81e0723.
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: some/unrelated/file.txt
both modified: file.c
Run Code Online (Sandbox Code Playgroud)
现在,在查看某些/不相关的/ file.txt时,它包含对中间某处的file.h的更改.所以这看起来像是git中的一个bug.所以我现在将手动撤消一些/不相关/ file.txt的更改并将它们添加到file.h.
Stackoverflow上的这个问题似乎是耐心差异算法应用的一个很好的候选者.然而,在测试我的潜在答案时,我发现这git diff --patience不符合我的期望(在这种情况下,与默认的diff算法没有区别):
$ cat a
/**
* Function foo description.
*/
function foo() {}
/**
* Function bar description.
*/
function bar() {}
$ cat b
/**
* Function bar description.
*/
function bar() {}
$ git diff --no-index --patience a b
diff --git a/a b/b
index 3064e15..a93bad0 100644
--- a/a
+++ b/b
@@ -1,9 +1,4 @@
/**
- * Function foo description.
- */
-function foo() {}
-
-/**
* Function bar description.
*/
function …Run Code Online (Sandbox Code Playgroud) 当我合并大的更改时,git经常被无可救药地混淆,因为它没有使用足够的上下文行.它在两个不同子程序的相似外观之间混淆,两个子程序都碰巧以:
.
return 1;
.
}
Run Code Online (Sandbox Code Playgroud)
(此处用于表示空白行的点)
当我使用'git diff'时,我可以说-U20看到20行上下文.但是我可以告诉git在合并时使用这个标志吗?
答案可能与合并策略/选项有关,例如:
git merge -s recursive -X patience [branch]
Run Code Online (Sandbox Code Playgroud) 我希望获得高质量的差异,我不担心需要多长时间,例如
git merge --strategy-option=diff-algorithm=minimal develop
Run Code Online (Sandbox Code Playgroud)
从文档中,不清楚哪一个最适合哪种情况?
默认情况下,迈尔斯基本的贪心差异算法.目前,这是默认值.
最小花费额外的时间来确保产生尽可能小的差异.
耐心在生成补丁时使用"耐心差异"算法.
直方图该算法将耐心算法扩展为"支持低发生的共同元素".
--strategy-option=patience与--strategy-option=diff-algorithm=patience相同呢?我找不到任何真实的例子来--patience给出与正常策略不同的结果。
根据这个答案,如果我有这个文件:
.foo1 {
margin: 0;
}
.bar {
margin: 0;
}
Run Code Online (Sandbox Code Playgroud)
我以这种方式更新它:
.bar {
margin: 0;
}
.foo1 {
margin: 0;
color: green;
}
Run Code Online (Sandbox Code Playgroud)
我应该看到两个不同的差异取决于我使用的算法。
但对我来说这些命令的输出总是相同的
git diff --diff-algorithm=patience
git diff
Run Code Online (Sandbox Code Playgroud)
diff --git a/foo.bar b/foo.bar
index 453dcb1..42cd4b4 100644
--- a/foo.bar
+++ b/foo.bar
@@ -1,7 +1,8 @@
-.foo1 {
+.bar {
margin: 0;
}
-.bar {
+.foo1 {
margin: 0;
+ color: green;
}
\ No newline at end of file
Run Code Online (Sandbox Code Playgroud)
我在用着git version 2.14.2
如果我有一些像这样的代码
/**
* blah blah blah
*/
...some codes...
/**
* yadda yadda
Run Code Online (Sandbox Code Playgroud)
然后我补充说
/**
* blah blah blah
*/
...some codes...
/**
* blah blah blah
*/
...some codes...
Run Code Online (Sandbox Code Playgroud)
在"yadda yadda"commanet之前,git diff将显示我添加了:
+ * blah blah blah
+ */
+ ...some codes...
+ /**
Run Code Online (Sandbox Code Playgroud)
有没有办法告诉git,"嘿,那不对.再试一次."?我知道,--patience但这似乎只是为了git diff和我的生活,它永远不会正常工作.我知道它不是非常重要,但它使得差异和提交日志,特别是在GitHub上,更加干净.