Skip to content

Commit 7eb4b9d

Browse files
peffgitster
authored andcommitted
validate_headref: use skip_prefix for symref parsing
Since the previous commit guarantees that our symref buffer is NUL-terminated, we can just use skip_prefix() and friends to parse it. This is shorter and saves us having to deal with magic numbers and keeping the "len" counter up to date. While we're at it, let's name the rather obscure "buf" to "refname", since that is the thing we are parsing with it. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6e68c91 commit 7eb4b9d

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

path.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,8 @@ void strbuf_git_common_path(struct strbuf *sb,
636636
int validate_headref(const char *path)
637637
{
638638
struct stat st;
639-
char *buf, buffer[256];
639+
char buffer[256];
640+
const char *refname;
640641
unsigned char sha1[20];
641642
int fd;
642643
ssize_t len;
@@ -668,14 +669,10 @@ int validate_headref(const char *path)
668669
/*
669670
* Is it a symbolic ref?
670671
*/
671-
if (len < 4)
672-
return -1;
673-
if (!memcmp("ref:", buffer, 4)) {
674-
buf = buffer + 4;
675-
len -= 4;
676-
while (len && isspace(*buf))
677-
buf++, len--;
678-
if (len >= 5 && !memcmp("refs/", buf, 5))
672+
if (skip_prefix(buffer, "ref:", &refname)) {
673+
while (isspace(*refname))
674+
refname++;
675+
if (starts_with(refname, "refs/"))
679676
return 0;
680677
}
681678

0 commit comments

Comments
 (0)