Skip to content

Commit

Permalink
patch-id.c: use strbuf instead of a fixed buffer
Browse files Browse the repository at this point in the history
get_one_patchid() uses a rather dumb heuristic to determine if the
passed buffer is part of the next commit. Whenever the first 40 bytes
are a valid hexadecimal sha1 representation, get_one_patchid() returns
next_sha1.

Once the current line is longer than the fixed buffer, this will break
(provided the additional bytes make a valid hexadecimal sha1). As a result
patch-id returns incorrect results. Instead, use strbuf and read one line
at a time.

Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Michael Schubert <mschub@elegosoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Michael Schubert authored and Junio C Hamano committed Sep 22, 2011
1 parent f696543 commit b9ab810
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions builtin/patch-id.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
return 1;
}

static int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx)
static int get_one_patchid(unsigned char *next_sha1, git_SHA_CTX *ctx, struct strbuf *line_buf)
{
static char line[1000];
int patchlen = 0, found_next = 0;
int before = -1, after = -1;

while (fgets(line, sizeof(line), stdin) != NULL) {
while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
char *line = line_buf->buf;
char *p = line;
int len;

Expand Down Expand Up @@ -133,14 +133,16 @@ static void generate_id_list(void)
unsigned char sha1[20], n[20];
git_SHA_CTX ctx;
int patchlen;
struct strbuf line_buf = STRBUF_INIT;

git_SHA1_Init(&ctx);
hashclr(sha1);
while (!feof(stdin)) {
patchlen = get_one_patchid(n, &ctx);
patchlen = get_one_patchid(n, &ctx, &line_buf);
flush_current_id(patchlen, sha1, &ctx);
hashcpy(sha1, n);
}
strbuf_release(&line_buf);
}

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

0 comments on commit b9ab810

Please sign in to comment.