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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 27, 2017 at 02:17 UTC
7eb4b9d025e1ac2ed5a49530c614b7e69054625a
1 file changed
+6
-9
path.c
+6
-9
@@ -636,7 +636,8 @@ void strbuf_git_common_path(struct strbuf *sb,
636
int validate_headref(const char *path)
637
{
638
struct stat st;
639
- char *buf, buffer[256];
639
+ char buffer[256];
640
+ const char *refname;
641
unsigned char sha1[20];
642
int fd;
643
ssize_t len;
@@ -668,14 +669,10 @@ int validate_headref(const char *path)
669
/*
670
* Is it a symbolic ref?
671
*/
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/"))
676
return 0;
677
}
678