Skip to content

Commit 9ae144f

Browse files
bonzinigitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5469e2d commit 9ae144f

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

builtin/patch-id.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@ static int remove_space(char *line)
2828
return dst - line;
2929
}
3030

31-
static void generate_id_list(void)
31+
int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx)
3232
{
33-
static unsigned char sha1[20];
3433
static char line[1000];
35-
git_SHA_CTX ctx;
36-
int patchlen = 0;
34+
int patchlen = 0, found_next = 0;
3735

38-
git_SHA1_Init(&ctx);
3936
while (fgets(line, sizeof(line), stdin) != NULL) {
40-
unsigned char n[20];
4137
char *p = line;
4238
int len;
4339

@@ -46,11 +42,9 @@ static void generate_id_list(void)
4642
else if (!memcmp(line, "commit ", 7))
4743
p += 7;
4844

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;
5448
}
5549

5650
/* Ignore commit comments */
@@ -68,9 +62,28 @@ static void generate_id_list(void)
6862
/* Compute the sha without whitespace */
6963
len = remove_space(line);
7064
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);
7286
}
73-
flush_current_id(patchlen, sha1, &ctx);
7487
}
7588

7689
static const char patch_id_usage[] = "git patch-id < patch";

0 commit comments

Comments
 (0)