Skip to content

Commit 59aebbd

Browse files
committed
use parse_hunk_header from git
1 parent 48bc5e2 commit 59aebbd

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

libs/header_clean/header_clean.pl

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,21 @@
5959
########################################
6060
# Check for "@@ -3,41 +3,63 @@" syntax #
6161
########################################
62-
} elsif ($change_hunk_indicators && $line =~ /^${ansi_sequence_regex}@@@* (.+?) @@@*(.*)/) {
63-
my $file_str = $4;
62+
} elsif ($change_hunk_indicators && $line =~ /^${ansi_sequence_regex}(@@@* .+? @@@*)(.*)/) {
63+
64+
my $hunk_header = $4;
6465
my $remain = $5;
6566

6667
if ($1) {
6768
print $1; # Print out whatever color we're using
6869
}
6970

70-
my ($start_line) = $file_str =~ m/\+(\d+)/;
71-
$start_line = abs($start_line + 0);
72-
71+
my ($orig_offset, $orig_count, $new_offset, $new_count) = parse_hunk_header($hunk_header);
7372
$last_file_seen = basename($last_file_seen);
7473

7574
# Plus three line for context
76-
print "@ $last_file_seen:" . ($start_line + 3) . " \@${remain}\n";
77-
#print $line;
75+
print "@ $last_file_seen:" . ($new_offset + 3) . " \@${remain}\n";
76+
7877
###################################
7978
# Remove any new file permissions #
8079
###################################
@@ -105,6 +104,16 @@
105104
}
106105
}
107106

107+
# Courtesy of github.com/git/git/blob/ab5d01a/git-add--interactive.perl#L798-L805
108+
sub parse_hunk_header {
109+
my ($line) = @_;
110+
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
111+
$line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
112+
$o_cnt = 1 unless defined $o_cnt;
113+
$n_cnt = 1 unless defined $n_cnt;
114+
return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
115+
}
116+
108117
sub strip_empty_first_line {
109118
my $foo = shift(); # Array passed in by reference
110119

0 commit comments

Comments
 (0)