summaryrefslogtreecommitdiff
path: root/http-push.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2014-06-18 15:47:50 -0400
committerJunio C Hamano <gitster@pobox.com>2014-06-20 10:44:45 -0700
commitae021d87911da4328157273df24779892cb51277 (patch)
treeaede96cb37d98c5675cd3f31322d4fb50f14d6e9 /http-push.c
parent21a2d4ada52132e6b0b67f8e28aa4bcda416f7f2 (diff)
downloadgit-ae021d87911da4328157273df24779892cb51277.tar.gz
use skip_prefix to avoid magic numbers
It's a common idiom to match a prefix and then skip past it with a magic number, like: if (starts_with(foo, "bar")) foo += 3; This is easy to get wrong, since you have to count the prefix string yourself, and there's no compiler check if the string changes. We can use skip_prefix to avoid the magic numbers here. Note that some of these conversions could be much shorter. For example: if (starts_with(arg, "--foo=")) { bar = arg + 6; continue; } could become: if (skip_prefix(arg, "--foo=", &bar)) continue; However, I have left it as: if (skip_prefix(arg, "--foo=", &v)) { bar = v; continue; } to visually match nearby cases which need to actually process the string. Like: if (skip_prefix(arg, "--foo=", &v)) { bar = atoi(v); continue; } Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'http-push.c')
-rw-r--r--http-push.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/http-push.c b/http-push.c
index de00d1693a..26dfa6724e 100644
--- a/http-push.c
+++ b/http-push.c
@@ -770,9 +770,9 @@ static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
lock->owner = xmalloc(strlen(ctx->cdata) + 1);
strcpy(lock->owner, ctx->cdata);
} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
- if (starts_with(ctx->cdata, "Second-"))
- lock->timeout =
- strtol(ctx->cdata + 7, NULL, 10);
+ const char *arg;
+ if (skip_prefix(ctx->cdata, "Second-", &arg))
+ lock->timeout = strtol(arg, NULL, 10);
} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
lock->token = xmalloc(strlen(ctx->cdata) + 1);
strcpy(lock->token, ctx->cdata);
@@ -1561,6 +1561,7 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
{
char *url;
struct strbuf buffer = STRBUF_INIT;
+ const char *name;
url = xmalloc(strlen(repo->url) + strlen(path) + 1);
sprintf(url, "%s%s", repo->url, path);
@@ -1578,8 +1579,8 @@ static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
return;
/* If it's a symref, set the refname; otherwise try for a sha1 */
- if (starts_with((char *)buffer.buf, "ref: ")) {
- *symref = xmemdupz((char *)buffer.buf + 5, buffer.len - 6);
+ if (skip_prefix(buffer.buf, "ref: ", &name)) {
+ *symref = xmemdupz(name, buffer.len - (name - buffer.buf));
} else {
get_sha1_hex(buffer.buf, sha1);
}