如何对git word差异进行git-apply

Ana*_*ory 5 git-diff word-diff git-apply

我需要编辑一个凌乱的commit提交,该提交只会在随后的几行中更改一个单词,保留其中一些更改,而删除其他更改。这些更改很容易在中看到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
Run Code Online (Sandbox Code Playgroud)

我想用作的补丁git apply

但是,vanilla git apply words.diff失败,出现了一个错误fatal: corrupt patch at line 6-正常的diff文件将以不受影响的行中的空格开头-并且我看不到任何可能使它git apply的联机帮助页接受word-diff文件的内容。

我如何说服git apply将这种格式的文件作为补丁?或者如何轻松地将此文件转换为有效补丁?

eye*_*van 7

我找不到可行的解决方案,因此我编写了一个脚本,可将 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();
Run Code Online (Sandbox Code Playgroud)

用法:

cat word-diff.txt | perl convert-word-diff.pl | git apply
Run Code Online (Sandbox Code Playgroud)

希望我没有搞砸任何事情,并且您使用的是 Linux/Mac 并且拥有 Perl。:-)