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 <bonzini@gnu.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Paolo Bonzini committed
Apr 19, 2010 at 10:46 UTC
9ae144fbf2648a52da1cc5269918931b328180f9
1 file changed
+26
-13
builtin/patch-id.c
+26
-13
@@ -28,16 +28,12 @@ static int remove_space(char *line)
28
return dst - line;
29
}
30
31
-static void generate_id_list(void)
31
+int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx)
32
{
33
- static unsigned char sha1[20];
33
static char line[1000];
35
- git_SHA_CTX ctx;
36
- int patchlen = 0;
34
+ int patchlen = 0, found_next = 0;
35
38
- git_SHA1_Init(&ctx);
36
while (fgets(line, sizeof(line), stdin) != NULL) {
40
- unsigned char n[20];
37
char *p = line;
38
int len;
39
@@ -46,11 +42,9 @@ static void generate_id_list(void)
42
else if (!memcmp(line, "commit ", 7))
43
p += 7;
44
49
- if (!get_sha1_hex(p, n)) {
50
- flush_current_id(patchlen, sha1, &ctx);
51
- hashcpy(sha1, n);
52
- patchlen = 0;
53
- continue;
45
+ if (!get_sha1_hex(p, next_sha1)) {
46
+ found_next = 1;
47
+ break;
48
}
49
50
/* Ignore commit comments */
@@ -68,9 +62,28 @@ static void generate_id_list(void)
62
/* Compute the sha without whitespace */
63
len = remove_space(line);
64
patchlen += len;
71
- git_SHA1_Update(&ctx, line, len);
65
+ git_SHA1_Update(ctx, line, len);
66
+ }
67
+
68
+ if (!found_next)
69
+ hashclr(next_sha1);
70
+
71
+ return patchlen;
72
+}
73
+
74
+static void generate_id_list(void)
75
+{
76
+ unsigned char sha1[20], n[20];
77
+ git_SHA_CTX ctx;
78
+ int patchlen;
79
+
80
+ git_SHA1_Init(&ctx);
81
+ hashclr(sha1);
82
+ while (!feof(stdin)) {
83
+ patchlen = get_one_patchid(n, &ctx);
84
+ flush_current_id(patchlen, sha1, &ctx);
85
+ hashcpy(sha1, n);
86
}
73
- flush_current_id(patchlen, sha1, &ctx);
87
}
88
89
static const char patch_id_usage[] = "git patch-id < patch";