Merge branch 'pb/patch-id-plus'

* pb/patch-id-plus:
  patch-id: Add support for mbox format
  patch-id: extract parsing one diff out of generate_id_list
This commit is contained in:
Junio C Hamano 2010-05-21 04:02:22 -07:00
commit 4d54494816
2 changed files with 117 additions and 20 deletions

View file

@ -28,16 +28,42 @@ static int remove_space(char *line)
return dst - line;
}
static void generate_id_list(void)
static int scan_hunk_header(const char *p, int *p_before, int *p_after)
{
static unsigned char sha1[20];
static char line[1000];
git_SHA_CTX ctx;
int patchlen = 0;
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;
git_SHA1_Init(&ctx);
while (fgets(line, sizeof(line), stdin) != NULL) {
unsigned char n[20];
char *p = line;
int len;
@ -45,32 +71,75 @@ static void generate_id_list(void)
p += 10;
else if (!memcmp(line, "commit ", 7))
p += 7;
else if (!memcmp(line, "From ", 5))
p += 5;
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 */
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);
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";

View file

@ -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