diff options
author | Paolo Bonzini <bonzini@gnu.org> | 2010-04-19 10:46:14 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2010-04-19 13:01:49 -0700 |
commit | 580fb25b7a941267aad747afdc74b9b4a42e154a (patch) | |
tree | 1a84ed3b7b0dcf78191474bda6e31a675ed82523 /builtin/patch-id.c | |
parent | 9ae144fbf2648a52da1cc5269918931b328180f9 (diff) | |
download | git-580fb25b7a941267aad747afdc74b9b4a42e154a.tar.gz |
patch-id: Add support for mbox format
I have an alias that takes two arguments and compares their patch IDs.
I would like to use to make sure I've tested exactly what I submit
(patch by patch), like
git patch-cmp origin/master.. file-being-sent
However, I cannot do that because git patch-id is fooled by the "-- "
trailer that git format-patch puts, or likely by the MIME boundary.
This patch adds hunk parsing logic to git patch-id in order to detect an
out of place "-" line and split the patch when it comes. In addition,
commit ids in the "From " lines are considered and printed in the output.
Signed-off-by: Paolo Bonzini <bonzini@gnu.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/patch-id.c')
-rw-r--r-- | builtin/patch-id.c | 68 |
1 files changed, 62 insertions, 6 deletions
diff --git a/builtin/patch-id.c b/builtin/patch-id.c index 973d830ecf..512530022e 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -28,10 +28,40 @@ static int remove_space(char *line) return dst - line; } +static int scan_hunk_header(const char *p, int *p_before, int *p_after) +{ + static const char digits[] = "0123456789"; + const char *q, *r; + int n; + + q = p + 4; + n = strspn(q, digits); + if (q[n] == ',') { + q += n + 1; + n = strspn(q, digits); + } + if (n == 0 || q[n] != ' ' || q[n+1] != '+') + return 0; + + r = q + n + 2; + n = strspn(r, digits); + if (r[n] == ',') { + r += n + 1; + n = strspn(r, digits); + } + if (n == 0) + return 0; + + *p_before = atoi(q); + *p_after = atoi(r); + return 1; +} + int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx) { static char line[1000]; int patchlen = 0, found_next = 0; + int before = -1, after = -1; while (fgets(line, sizeof(line), stdin) != NULL) { char *p = line; @@ -41,6 +71,8 @@ int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx) p += 10; else if (!memcmp(line, "commit ", 7)) p += 7; + else if (!memcmp(line, "From ", 5)) + p += 5; if (!get_sha1_hex(p, next_sha1)) { found_next = 1; @@ -51,13 +83,37 @@ int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx) if (!patchlen && memcmp(line, "diff ", 5)) continue; - /* Ignore git-diff index header */ - if (!memcmp(line, "index ", 6)) - continue; + /* Parsing diff header? */ + if (before == -1) { + if (!memcmp(line, "index ", 6)) + continue; + else if (!memcmp(line, "--- ", 4)) + before = after = 1; + else if (!isalpha(line[0])) + break; + } - /* Ignore line numbers when computing the SHA1 of the patch */ - if (!memcmp(line, "@@ -", 4)) - continue; + /* Looking for a valid hunk header? */ + if (before == 0 && after == 0) { + if (!memcmp(line, "@@ -", 4)) { + /* Parse next hunk, but ignore line numbers. */ + scan_hunk_header(line, &before, &after); + continue; + } + + /* Split at the end of the patch. */ + if (memcmp(line, "diff ", 5)) + break; + + /* Else we're parsing another header. */ + before = after = -1; + } + + /* If we get here, we're inside a hunk. */ + if (line[0] == '-' || line[0] == ' ') + before--; + if (line[0] == '+' || line[0] == ' ') + after--; /* Compute the sha without whitespace */ len = remove_space(line); |