Skip to content

Commit d077db1

Browse files
committed
Merge branch 'jz/patch-id-hunk-header-parsing-fix'
Unlike "git apply", "git patch-id" did not handle patches with hunks that has only 1 line in either preimage or postimage, which has been corrected. * jz/patch-id-hunk-header-parsing-fix: patch-id: fix scan_hunk_header on diffs with 1 line of before/after patch-id: fix antipatterns in tests
2 parents 75ff34b + 757e75c commit d077db1

File tree

2 files changed

+68
-36
lines changed

2 files changed

+68
-36
lines changed

builtin/patch-id.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,27 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
3232
n = strspn(q, digits);
3333
if (q[n] == ',') {
3434
q += n + 1;
35+
*p_before = atoi(q);
3536
n = strspn(q, digits);
37+
} else {
38+
*p_before = 1;
3639
}
40+
3741
if (n == 0 || q[n] != ' ' || q[n+1] != '+')
3842
return 0;
3943

4044
r = q + n + 2;
4145
n = strspn(r, digits);
4246
if (r[n] == ',') {
4347
r += n + 1;
48+
*p_after = atoi(r);
4449
n = strspn(r, digits);
50+
} else {
51+
*p_after = 1;
4552
}
4653
if (n == 0)
4754
return 0;
4855

49-
*p_before = atoi(q);
50-
*p_after = atoi(r);
5156
return 1;
5257
}
5358

t/t4204-patch-id.sh

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ calc_patch_id () {
3838
shift
3939
git patch-id "$@" >patch-id.output &&
4040
sed "s/ .*//" patch-id.output >patch-id_"$patch_name" &&
41-
test_line_count -gt 0 patch-id_"$patch_name"
41+
test_line_count -eq 1 patch-id_"$patch_name"
4242
}
4343

4444
get_top_diff () {
@@ -166,40 +166,67 @@ test_expect_success 'patch-id respects config from subdir' '
166166
)
167167
'
168168

169-
cat >nonl <<\EOF
170-
diff --git i/a w/a
171-
index e69de29..2e65efe 100644
172-
--- i/a
173-
+++ w/a
174-
@@ -0,0 +1 @@
175-
+a
176-
\ No newline at end of file
177-
diff --git i/b w/b
178-
index e69de29..6178079 100644
179-
--- i/b
180-
+++ w/b
181-
@@ -0,0 +1 @@
182-
+b
183-
EOF
184-
185-
cat >withnl <<\EOF
186-
diff --git i/a w/a
187-
index e69de29..7898192 100644
188-
--- i/a
189-
+++ w/a
190-
@@ -0,0 +1 @@
191-
+a
192-
diff --git i/b w/b
193-
index e69de29..6178079 100644
194-
--- i/b
195-
+++ w/b
196-
@@ -0,0 +1 @@
197-
+b
198-
EOF
199-
200169
test_expect_success 'patch-id handles no-nl-at-eof markers' '
201-
cat nonl | calc_patch_id nonl &&
202-
cat withnl | calc_patch_id withnl &&
170+
cat >nonl <<-\EOF &&
171+
diff --git i/a w/a
172+
index e69de29..2e65efe 100644
173+
--- i/a
174+
+++ w/a
175+
@@ -0,0 +1 @@
176+
+a
177+
\ No newline at end of file
178+
diff --git i/b w/b
179+
index e69de29..6178079 100644
180+
--- i/b
181+
+++ w/b
182+
@@ -0,0 +1 @@
183+
+b
184+
EOF
185+
cat >withnl <<-\EOF &&
186+
diff --git i/a w/a
187+
index e69de29..7898192 100644
188+
--- i/a
189+
+++ w/a
190+
@@ -0,0 +1 @@
191+
+a
192+
diff --git i/b w/b
193+
index e69de29..6178079 100644
194+
--- i/b
195+
+++ w/b
196+
@@ -0,0 +1 @@
197+
+b
198+
EOF
199+
calc_patch_id nonl <nonl &&
200+
calc_patch_id withnl <withnl &&
203201
test_cmp patch-id_nonl patch-id_withnl
204202
'
203+
204+
test_expect_success 'patch-id handles diffs with one line of before/after' '
205+
cat >diffu1 <<-\EOF &&
206+
diff --git a/bar b/bar
207+
index bdaf90f..31051f6 100644
208+
--- a/bar
209+
+++ b/bar
210+
@@ -2 +2,2 @@
211+
b
212+
+c
213+
diff --git a/car b/car
214+
index 00750ed..2ae5e34 100644
215+
--- a/car
216+
+++ b/car
217+
@@ -1 +1,2 @@
218+
3
219+
+d
220+
diff --git a/foo b/foo
221+
index e439850..7146eb8 100644
222+
--- a/foo
223+
+++ b/foo
224+
@@ -2 +2,2 @@
225+
a
226+
+e
227+
EOF
228+
calc_patch_id diffu1 <diffu1 &&
229+
test_config patchid.stable true &&
230+
calc_patch_id diffu1stable <diffu1
231+
'
205232
test_done

0 commit comments

Comments
 (0)