From be0b3f822b6375028b25f62894056e63cb7c3ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 30 Aug 2014 11:46:54 +0200 Subject: connect: simplify check_ref() using skip_prefix() and starts_with() Both callers of check_ref() pass in NUL-terminated strings for name. Remove the len parameter and then use skip_prefix() and starts_with() instead of memcmp() to check if it starts with certain strings. This gets rid of several magic string length constants and a strlen() call. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/connect.c b/connect.c index 5047402..34193e5 100644 --- a/connect.c +++ b/connect.c @@ -13,28 +13,24 @@ static char *server_capabilities; static const char *parse_feature_value(const char *, const char *, int *); -static int check_ref(const char *name, int len, unsigned int flags) +static int check_ref(const char *name, unsigned int flags) { if (!flags) return 1; - if (len < 5 || memcmp(name, "refs/", 5)) + if (!skip_prefix(name, "refs/", &name)) return 0; - /* Skip the "refs/" part */ - name += 5; - len -= 5; - /* REF_NORMAL means that we don't want the magic fake tag refs */ if ((flags & REF_NORMAL) && check_refname_format(name, 0)) return 0; /* REF_HEADS means that we want regular branch heads */ - if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6)) + if ((flags & REF_HEADS) && starts_with(name, "heads/")) return 1; /* REF_TAGS means that we want tags */ - if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5)) + if ((flags & REF_TAGS) && starts_with(name, "tags/")) return 1; /* All type bits clear means that we are ok with anything */ @@ -43,7 +39,7 @@ static int check_ref(const char *name, int len, unsigned int flags) int check_ref_type(const struct ref *ref, int flags) { - return check_ref(ref->name, strlen(ref->name), flags); + return check_ref(ref->name, flags); } static void die_initial_contact(int got_at_least_one_head) @@ -167,7 +163,7 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len, continue; } - if (!check_ref(name, name_len, flags)) + if (!check_ref(name, flags)) continue; ref = alloc_ref(buffer + 41); hashcpy(ref->old_sha1, old_sha1); -- cgit v0.10.2-6-g49f6 From d773144417b1a87b4529144ab0530ecfea103754 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 30 Aug 2014 11:47:19 +0200 Subject: pack-write: simplify index_pack_lockfile using skip_prefix() and xstrfmt() Get rid of magic string length constants by using skip_prefix() instead of memcmp() and use xstrfmt() for building a string instead of a PATH_MAX-sized buffer, snprintf() and xstrdup(). Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/pack-write.c b/pack-write.c index 9ccf804..33293ce 100644 --- a/pack-write.c +++ b/pack-write.c @@ -288,13 +288,12 @@ char *index_pack_lockfile(int ip_out) * case, we need it to remove the corresponding .keep file * later on. If we don't get that then tough luck with it. */ - if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n' && - memcmp(packname, "keep\t", 5) == 0) { - char path[PATH_MAX]; + if (read_in_full(ip_out, packname, 46) == 46 && packname[45] == '\n') { + const char *name; packname[45] = 0; - snprintf(path, sizeof(path), "%s/pack/pack-%s.keep", - get_object_directory(), packname + 5); - return xstrdup(path); + if (skip_prefix(packname, "keep\t", &name)) + return xstrfmt("%s/pack/pack-%s.keep", + get_object_directory(), name); } return NULL; } -- cgit v0.10.2-6-g49f6