kakoune

Fork of kakoune, an acceptable text editor
git clone git://git.laack.co/kakoune.git
Log | Files | Refs

diff-parse.pl (3297B)


      1 #!/usr/bin/env perl
      2 
      3 use warnings;
      4 
      5 sub quote {
      6     my $token = shift;
      7     $token =~ s/'/''/g;
      8     return "'$token'";
      9 }
     10 sub fail {
     11     my $reason = shift;
     12     print quote("diff-parse.pl: $reason");
     13     exit 1;
     14 }
     15 
     16 my $begin;
     17 my $end;
     18 
     19 while (defined $ARGV[0]) {
     20     if ($ARGV[0] eq "--") {
     21         shift;
     22         last;
     23     }
     24     if ($ARGV[0] =~ m{^(BEGIN|END)$}) {
     25         if (not defined $ARGV[1]) {
     26             fail "missing argument to $ARGV[0]";
     27         }
     28         if ($ARGV[0] eq "BEGIN") {
     29             $begin = $ARGV[1];
     30         } else {
     31             $end = $ARGV[1];
     32         }
     33         shift, shift;
     34         next;
     35     }
     36     fail "unknown argument: $ARGV[0]";
     37 }
     38 
     39 # Inputs
     40 our $directory = $ENV{PWD};
     41 our $strip;
     42 our $in_file;
     43 our $in_file_line;
     44 our $version = "+";
     45 
     46 eval $begin if defined $begin;
     47 
     48 $in_file = "$directory/$in_file" if defined $in_file && $in_file ne "";
     49 
     50 # Outputs
     51 our $diff_line = 0;
     52 our $commit;
     53 our $file;
     54 our $file_line;
     55 our $other_file;
     56 our $other_file_line;
     57 our $diff_line_text;
     58 
     59 my $other_version;
     60 if ($version eq "+") {
     61     $other_version = "-";
     62 } else {
     63     $other_version = "+";
     64 }
     65 my $is_recursive_diff = 0;
     66 my $state = "header";
     67 my $fallback_file;
     68 
     69 sub strip {
     70     my $is_recursive_diff = shift;
     71     my $f = shift;
     72 
     73     my $effective_strip;
     74     if (defined $strip) {
     75         $effective_strip = $strip;
     76     } else {
     77         # A "diff -r" or "git diff" adds "diff" lines to
     78         # the output.  If no such line is present, we have
     79         # a plain diff between files (not directories), so
     80         # there should be no need to strip the directory.
     81         $effective_strip = $is_recursive_diff ? 1 : 0;
     82     }
     83 
     84     if ($f !~ m{^/}) {
     85         $f =~ s,^([^/]+/+){$effective_strip},, or fail "directory prefix underflow";
     86         $f = "$directory/$f";
     87     }
     88     return $f;
     89 }
     90 
     91 while (<STDIN>) {
     92     $diff_line++;
     93     s/^(> )*//g;
     94     $diff_line_text = $_;
     95     if (m{^commit (\w+)}) {
     96         $commit = $1;
     97         next;
     98     }
     99     if (m{^diff\b}) {
    100         $state = "header";
    101         $is_recursive_diff = 1;
    102         if (m{^diff -\S* (\S+) (\S+)$}) {
    103             $fallback_file = strip $is_recursive_diff, ($version eq "+" ? $2 : $1);
    104         }
    105         next;
    106     }
    107     if ($state eq "header") {
    108         if (m{^[$version]{3} ([^\t\n]+)}) {
    109             $file = strip $is_recursive_diff, $1;
    110             next;
    111         }
    112         if (m{^[$other_version]{3} ([^\t\n]+)}) {
    113             $other_file = strip $is_recursive_diff, $1;
    114             next;
    115         }
    116     }
    117     if (m{^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@}) {
    118         $state = "contents";
    119         $file_line = ($version eq "+" ? $2 : $1) - 1;
    120         $other_file_line = ($version eq "+" ? $1 : $2) - 1;
    121     } else {
    122         my $iscontext = m{^[ ]};
    123         if (m{^[ $version]}) {
    124            $file_line++ if defined $file_line;
    125         }
    126         if (m{^[ $other_version]}) {
    127            $other_file_line++ if defined $other_file_line;
    128         }
    129     }
    130     if (defined $in_file and defined $file and ($in_file eq "" or $file eq $in_file)) {
    131         if (defined $in_file_line and defined $file_line and $file_line >= $in_file_line) {
    132             last;
    133         }
    134     }
    135 }
    136 if (not defined $file) {
    137     $file = ($fallback_file or $other_file);
    138 }
    139 if (not defined $file) {
    140     fail "missing diff header";
    141 }
    142 
    143 eval $end if defined $end;