How to git-apply a git word diff

前端 未结 1 1283
不知归路
不知归路 2020-12-20 18:06

I needed to edit a messy commit commit that only changed a word in a few subsequent rows, keeping some of those changes and removing others. The changes were easy to see in

相关标签:
1条回答
  • 2020-12-20 18:17

    I couldn't find a working solution, so I've put together a script that converts word-diff into a regular diff that can be applied:

    #!/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();
    

    Usage:

    cat word-diff.txt | perl convert-word-diff.pl | git apply
    

    Hopefully I didn't mess up anything and you are on Linux/Mac and have Perl. :-)

    0 讨论(0)
提交回复
热议问题