summaryrefslogtreecommitdiff
path: root/builtin
diff options
context:
space:
mode:
authorJerry Zhang <jerry@skydio.com>2022-02-02 04:19:45 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-02-02 19:24:23 (GMT)
commit757e75c81e4332e363b90a0534a657b1bb6546fa (patch)
tree56335565b56b3ecd5c8b688d61765de86f61894c /builtin
parent56fa5ac39a8601d965127abebbc533f29eaef751 (diff)
downloadgit-757e75c81e4332e363b90a0534a657b1bb6546fa.zip
git-757e75c81e4332e363b90a0534a657b1bb6546fa.tar.gz
git-757e75c81e4332e363b90a0534a657b1bb6546fa.tar.bz2
patch-id: fix scan_hunk_header on diffs with 1 line of before/after
Normally diffs will contain a hunk header of the format "@@ -2,2 +2,15 @@ code". However when there is only 1 line of change, the unified diff format allows for the second comma separated value to be omitted in either before or after line counts. This can produce hunk headers that look like "@@ -2 +2,18 @@ code" or "@@ -2,2 +2 @@ code". As a result, scan_hunk_header mistakenly returns the line number as line count, which then results in unpredictable parsing errors with the rest of the patch, including giving multiple lines of output for a single commit. Fix by explicitly setting line count to 1 when there is no comma, and add a test. apply.c contains this same logic except it is correct. A worthwhile future project might be to unify these two diff parsers so they both benefit from fixes. Signed-off-by: Jerry Zhang <jerry@skydio.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin')
-rw-r--r--builtin/patch-id.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/builtin/patch-id.c b/builtin/patch-id.c
index 822ffff..881fcf3 100644
--- a/builtin/patch-id.c
+++ b/builtin/patch-id.c
@@ -32,8 +32,12 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
n = strspn(q, digits);
if (q[n] == ',') {
q += n + 1;
+ *p_before = atoi(q);
n = strspn(q, digits);
+ } else {
+ *p_before = 1;
}
+
if (n == 0 || q[n] != ' ' || q[n+1] != '+')
return 0;
@@ -41,13 +45,14 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
n = strspn(r, digits);
if (r[n] == ',') {
r += n + 1;
+ *p_after = atoi(r);
n = strspn(r, digits);
+ } else {
+ *p_after = 1;
}
if (n == 0)
return 0;
- *p_before = atoi(q);
- *p_after = atoi(r);
return 1;
}