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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Aug 30, 2014 at 11:46 UTC
be0b3f822b6375028b25f62894056e63cb7c3ce4
1 file changed
+6
-10
connect.c
+6
-10
@@ -13,28 +13,24 @@
13
static char *server_capabilities;
14
static const char *parse_feature_value(const char *, const char *, int *);
15
16
-static int check_ref(const char *name, int len, unsigned int flags)
16
+static int check_ref(const char *name, unsigned int flags)
17
{
18
if (!flags)
19
return 1;
20
21
- if (len < 5 || memcmp(name, "refs/", 5))
21
+ if (!skip_prefix(name, "refs/", &name))
22
return 0;
23
24
- /* Skip the "refs/" part */
25
- name += 5;
26
- len -= 5;
27
-
24
/* REF_NORMAL means that we don't want the magic fake tag refs */
25
if ((flags & REF_NORMAL) && check_refname_format(name, 0))
26
return 0;
27
28
/* REF_HEADS means that we want regular branch heads */
33
- if ((flags & REF_HEADS) && !memcmp(name, "heads/", 6))
29
+ if ((flags & REF_HEADS) && starts_with(name, "heads/"))
30
return 1;
31
32
/* REF_TAGS means that we want tags */
37
- if ((flags & REF_TAGS) && !memcmp(name, "tags/", 5))
33
+ if ((flags & REF_TAGS) && starts_with(name, "tags/"))
34
return 1;
35
36
/* 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)
39
40
int check_ref_type(const struct ref *ref, int flags)
41
{
46
- return check_ref(ref->name, strlen(ref->name), flags);
42
+ return check_ref(ref->name, flags);
43
}
44
45
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,
163
continue;
164
}
165
170
- if (!check_ref(name, name_len, flags))
166
+ if (!check_ref(name, flags))
167
continue;
168
ref = alloc_ref(buffer + 41);
169
hashcpy(ref->old_sha1, old_sha1);