patch-id: use starts_with() and skip_prefix()

Get rid of magic numbers and avoid running over the end of a NUL
terminated string by using starts_with() and skip_prefix() instead
of memcmp().

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2016-05-28 18:20:23 +02:00 committed by Junio C Hamano
parent 765428699a
commit 2bb73ae803

View File

@ -81,16 +81,13 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result,
while (strbuf_getwholeline(line_buf, stdin, '\n') != EOF) {
char *line = line_buf->buf;
char *p = line;
const char *p = line;
int len;
if (!memcmp(line, "diff-tree ", 10))
p += 10;
else if (!memcmp(line, "commit ", 7))
p += 7;
else if (!memcmp(line, "From ", 5))
p += 5;
else if (!memcmp(line, "\\ ", 2) && 12 < strlen(line))
if (!skip_prefix(line, "diff-tree ", &p) &&
!skip_prefix(line, "commit ", &p) &&
!skip_prefix(line, "From ", &p) &&
starts_with(line, "\\ ") && 12 < strlen(line))
continue;
if (!get_sha1_hex(p, next_sha1)) {
@ -99,14 +96,14 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result,
}
/* Ignore commit comments */
if (!patchlen && memcmp(line, "diff ", 5))
if (!patchlen && !starts_with(line, "diff "))
continue;
/* Parsing diff header? */
if (before == -1) {
if (!memcmp(line, "index ", 6))
if (starts_with(line, "index "))
continue;
else if (!memcmp(line, "--- ", 4))
else if (starts_with(line, "--- "))
before = after = 1;
else if (!isalpha(line[0]))
break;
@ -114,14 +111,14 @@ static int get_one_patchid(unsigned char *next_sha1, unsigned char *result,
/* Looking for a valid hunk header? */
if (before == 0 && after == 0) {
if (!memcmp(line, "@@ -", 4)) {
if (starts_with(line, "@@ -")) {
/* 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))
if (!starts_with(line, "diff "))
break;
/* Else we're parsing another header. */