From 9ae144fbf2648a52da1cc5269918931b328180f9 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 19 Apr 2010 10:46:13 +0200 Subject: [PATCH 1/2] patch-id: extract parsing one diff out of generate_id_list This simplifies a bit the next patch, since it will have more than one condition to exit the loop. Signed-off-by: Paolo Bonzini Signed-off-by: Junio C Hamano --- builtin/patch-id.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/builtin/patch-id.c b/builtin/patch-id.c index af0911e4bd..973d830ecf 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -28,16 +28,12 @@ static int remove_space(char *line) return dst - line; } -static void generate_id_list(void) +int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx) { - static unsigned char sha1[20]; static char line[1000]; - git_SHA_CTX ctx; - int patchlen = 0; + int patchlen = 0, found_next = 0; - git_SHA1_Init(&ctx); while (fgets(line, sizeof(line), stdin) != NULL) { - unsigned char n[20]; char *p = line; int len; @@ -46,11 +42,9 @@ static void generate_id_list(void) else if (!memcmp(line, "commit ", 7)) p += 7; - if (!get_sha1_hex(p, n)) { - flush_current_id(patchlen, sha1, &ctx); - hashcpy(sha1, n); - patchlen = 0; - continue; + if (!get_sha1_hex(p, next_sha1)) { + found_next = 1; + break; } /* Ignore commit comments */ @@ -68,9 +62,28 @@ static void generate_id_list(void) /* Compute the sha without whitespace */ len = remove_space(line); patchlen += len; - git_SHA1_Update(&ctx, line, len); + git_SHA1_Update(ctx, line, len); + } + + if (!found_next) + hashclr(next_sha1); + + return patchlen; +} + +static void generate_id_list(void) +{ + unsigned char sha1[20], n[20]; + git_SHA_CTX ctx; + int patchlen; + + git_SHA1_Init(&ctx); + hashclr(sha1); + while (!feof(stdin)) { + patchlen = get_one_patchid(n, &ctx); + flush_current_id(patchlen, sha1, &ctx); + hashcpy(sha1, n); } - flush_current_id(patchlen, sha1, &ctx); } static const char patch_id_usage[] = "git patch-id < patch"; From 580fb25b7a941267aad747afdc74b9b4a42e154a Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Mon, 19 Apr 2010 10:46:14 +0200 Subject: [PATCH 2/2] 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 Signed-off-by: Junio C Hamano --- builtin/patch-id.c | 68 +++++++++++++++++++++++++++++++++++++++++---- t/t4204-patch-id.sh | 28 +++++++++++++++++++ 2 files changed, 90 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); diff --git a/t/t4204-patch-id.sh b/t/t4204-patch-id.sh index 04f7bae850..68e2652814 100755 --- a/t/t4204-patch-id.sh +++ b/t/t4204-patch-id.sh @@ -18,6 +18,11 @@ test_expect_success 'patch-id output is well-formed' ' grep "^[a-f0-9]\{40\} $(git rev-parse HEAD)$" output ' +calc_patch_id () { + git patch-id | + sed "s# .*##" > patch-id_"$1" +} + get_patch_id () { git log -p -1 "$1" | git patch-id | sed "s# .*##" > patch-id_"$1" @@ -35,4 +40,27 @@ test_expect_success 'patch-id detects inequality' ' ! test_cmp patch-id_master patch-id_notsame ' +test_expect_success 'patch-id supports git-format-patch output' ' + get_patch_id master && + git checkout same && + git format-patch -1 --stdout | calc_patch_id same && + test_cmp patch-id_master patch-id_same && + set `git format-patch -1 --stdout | git patch-id` && + test "$2" = `git rev-parse HEAD` +' + +test_expect_success 'whitespace is irrelevant in footer' ' + get_patch_id master && + git checkout same && + git format-patch -1 --stdout | sed "s/ \$//" | calc_patch_id same && + test_cmp patch-id_master patch-id_same +' + +test_expect_success 'patch-id supports git-format-patch MIME output' ' + get_patch_id master && + git checkout same && + git format-patch -1 --attach --stdout | calc_patch_id same && + test_cmp patch-id_master patch-id_same +' + test_done