我需要编辑一个杂乱的提交提交,它只更改了几个后续行中的一个单词,保留其中一些更改并删除其他更改。
在 git diff --word-diff
中很容易看到这些变化,在那种格式中,我可以轻松地编辑大块头来做我想做的事情,但现在我有一个这样的文件
diff --git a/cldf/forms.csv b/cldf/forms.csv
index 46c12a4..0374ece 100644
--- a/cldf/forms.csv
+++ b/cldf/forms.csv
@@ -1783,8 +1783,8 @@ ID,Lect_ID,Concept_ID,Form_according_to_Source,Form,Local_Orthography,Segments,C
1782,adan1251-lawah,day,dilɛlɛ,dilɛlɛ,dilele,d i l ɛ l ɛ,Lit. 'all day'.,datasets_Adang_Lawahing_tsv
1783,adan1251-lawah,day,wɛd saha,wɛd_saha,wed saha,w ɛ d _ s a h a,midday' lit. 'hot sun',datasets_Adang_Lawahing_tsv
1784,adan1251-lawah,morning,lalami,lalami,lalami,l a l a m i,,datasets_Adang_Lawahing_tsv
1785,adan1251-lawah,yesterday,ʔu:mi,ʔuːmi,[-umi-]{+'umi+},ʔ uː m i,,datasets_Adang_Lawahing_tsv
1786,adan1251-lawah,day_before_yesterday,ʔotariŋ alumi,ʔotariŋ_alumi,[-otaring-]{+'otaring+} alumi,ʔ o t a r i ŋ _ a l u m i,,datasets_Adang_Lawahing_tsv
1787,adan1251-lawah,tomorrow,dilɛlɛ,dilɛlɛ,dilele,d i l ɛ l ɛ,,datasets_Adang_Lawahing_tsv
1788,adan1251-lawah,day_after_tomorrow,a:lu,aːlu,alu,aː l u,,datasets_Adang_Lawahing_tsv
1789,adan1251-lawah,twilight_dawn,lalami,lalami,lalami,l a l a m i,"(lit, 'early morning')",datasets_Adang_Lawahing_tsv
我想用它作为
git apply
的补丁.然而, Vanilla
git apply words.diff
失败并返回 fatal: corrupt patch at line 6
– 普通的 diff 文件会在未受影响的行中以空格开头 – 我看不到任何可能使 git apply
的内容在其联机帮助页中接受 word-diff 文件。怎么说服
git apply
将这种格式的文件作为补丁?或者如何轻松地将此文件转换为有效的补丁?
最佳答案
我找不到可行的解决方案,因此我编写了一个脚本,可将 word-diff 转换为可以应用的常规 diff:
#!/usr/bin/env perl
# convert-word-diff.pl -- rev. 2, this script is licensed under WTFPLv2
my (@minus, @plus);
sub flush_diff {
print join("", map { "-$_" } @minus);
print join("", map { "+$_" } @plus);
@minus = (); @plus = ();
}
while (my $line = <>) {
if ($line =~ /^(?:index |diff |\+\+\+ |\-\-\- |@@ )/) {
flush_diff();
print $line;
next;
}
my $is_diff_line;
if ($line =~ /\[\-.*\-\]/ || $line =~ /\{\+.*?\+\}/) {
my $copy = $line;
$copy =~ s/\[\-(.*?)\-\]\{\+.*?\+\}/\1/g;
$copy =~ s/\[\-(.*?)\-\] ( )?/ \1 /g;
$copy =~ s/\{\+.*?\+\} ?//g;
push(@minus, $copy);
$copy = $line;
$copy =~ s/\[\-.*?\-\]//g;
$copy =~ s/\{\+(.*?)\+\}/\1/g;
push(@plus, $copy);
$is_diff_line = 1;
}
unless ($is_diff_line) {
flush_diff();
print " $line" ;
}
}
flush_diff();
用法:
cat word-diff.txt | perl convert-word-diff.pl | git apply
希望我没有搞砸任何事情,而且您使用的是 Linux/Mac 并且拥有 Perl。 :-)
关于git-diff - 如何 git-apply git word diff,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54708202/