Skip to content

Commit afb0008

Browse files
committed
Fix recountdiff bug handling git diffs (#49)
Assisted-by: Cursor
1 parent 00e5ffa commit afb0008

4 files changed

Lines changed: 69 additions & 5 deletions

File tree

‎Makefile.am‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ TESTS = tests/newline1/run-test \
228228
tests/recount5/run-test \
229229
tests/recount6/run-test \
230230
tests/recount7/run-test \
231+
tests/recount8/run-test \
231232
tests/unwrapdiff1/run-test \
232233
tests/overstrip/run-test \
233234
tests/context1/run-test \

‎NEWS‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ Patchutils news
2222
processing patches with multiple files, breaking the patch format.
2323
Addresses GitHub issue #48.
2424

25+
Fixed recountdiff corruption of new file headers when git signature
26+
lines are present. Previously, recountdiff would incorrectly change
27+
"@@ -0,0 +1,N @@" to "@@ -1 +1,N @@" for new files in patches containing
28+
git signature lines (e.g., "-- \n2.50.1"), causing patch application
29+
to fail. Addresses GitHub issue #49.
30+
2531
0.4.2 (stable)
2632

2733
Build system improvements: only run xmlto once during documentation

‎scripts/recountdiff.in‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ while ($i < $#line) {
7777
my $spit = 0;
7878
if (/^\s/ or /^$/) { $orig++; $new++; }
7979
elsif (/^\+/) { $new++; }
80+
elsif (/^-- $/) {
81+
# Git signature line - end of patch content
82+
$spit = 1;
83+
}
8084
elsif (/^-/) {
8185
# Look out for new file changes.
8286
if (/^--- /) {
@@ -125,17 +129,33 @@ foreach (@off_lines) {
125129
next;
126130
}
127131
$line[$_] =~ /^@@ -(\d+),?(\d+)? \+(\d+),?(\d+)? @@(.*)$/;
128-
($ooff,$oc,$nc,$misc) = ($1,$2,$4,$5);
129-
$oc = 1 unless defined($oc);
132+
($ooff,$oc,$noff_orig,$nc,$misc) = ($1,$2,$3,$4,$5);
133+
134+
# Check for new file before applying defaults
135+
$is_new_file = ($ooff == 0 && defined($oc) && $oc == 0);
136+
137+
# For new files, preserve oc=0; otherwise apply default
138+
if (!$is_new_file) {
139+
$oc = 1 unless defined($oc);
140+
}
130141
$nc = 1 unless defined($nc);
131-
$noff = $ooff + $offset;
132-
$noff++ if ($oc == 0);
133-
$noff-- if ($nc == 0);
142+
143+
# For new files (ooff=0, original oc=0), preserve the original new offset
144+
if ($is_new_file) {
145+
$noff = $noff_orig + $offset;
146+
} else {
147+
$noff = $ooff + $offset;
148+
# Don't adjust offset for new files (when ooff is 0 and oc is 0)
149+
$noff++ if ($oc == 0 && $ooff != 0);
150+
$noff-- if ($nc == 0);
151+
}
134152
$line[$_] = "@@ -$ooff";
135153
$line[$_] .= ",$oc" if ($oc != 1);
136154
$line[$_] .= " +$noff";
137155
$line[$_] .= ",$nc" if ($nc != 1);
138156
$line[$_] .= " @@" . "$misc\n";
157+
158+
139159
$offset += $nc - $oc;
140160
}
141161

‎tests/recount8/run-test‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/sh
2+
3+
# This is a recountdiff(1) testcase.
4+
# Test: Fix bug with new files when git signature lines are present (issue #49).
5+
6+
. ${top_srcdir-.}/tests/common.sh
7+
8+
cat <<"EOF" > patch
9+
diff --git a/test_file b/test_file
10+
new file mode 100644
11+
index 0000000..83db48f
12+
--- /dev/null
13+
+++ b/test_file
14+
@@ -0,0 +1,3 @@
15+
+line1
16+
+line2
17+
+line3
18+
--
19+
2.50.1
20+
21+
EOF
22+
23+
${RECOUNTDIFF} patch > patch-recounted || exit 1
24+
cat <<"EOF" | cmp - patch-recounted || exit 1
25+
diff --git a/test_file b/test_file
26+
new file mode 100644
27+
index 0000000..83db48f
28+
--- /dev/null
29+
+++ b/test_file
30+
@@ -0,0 +1,3 @@
31+
+line1
32+
+line2
33+
+line3
34+
--
35+
2.50.1
36+
37+
EOF

0 commit comments

Comments
 (0)