Re: [PATCH 1/4] remote: use skip_prefix
- From
Jeff King <peff@peff.net>
- Date
- Feb 15, 2016, 18:18 UTC
- Message-ID
- <20160215181837.GE26443@sigill.intra.peff.net>
- In-Reply-To
- <1455558150-30267-2-git-send-email-t.gummerer@gmail.com>
On Mon, Feb 15, 2016 at 06:42:27PM +0100, Thomas Gummerer wrote:
Show 9 quoted lines
> 95b567c7 ("use skip_prefix to avoid repeating strings") transformed
> calls using starts_with() and then skipping the length of the prefix to
> skip_prefix() calls. In remote.c there are a few calls like:
>
> if (starts_with(foo, "bar"))
> foo += 3
>
> These calls weren't touched by the commit mentioned above, but can
> benefit from the same treatment to avoid magic numbers.This is definitely an improvement, but I think we can actually go a step further here, and use parse_config_key. Like:
diff --git a/remote.c b/remote.c index 21e4ec3..8d2c3ca 100644 --- a/remote.c +++ b/remote.c @@ -318,15 +318,14 @@ static void read_branches_file(struct remote *remote) static int handle_config(const char *key, const char *value, void *cb) { const char *name; + int namelen; const char *subkey; struct remote *remote; struct branch *branch; - if (starts_with(key, "branch.")) { - name = key + 7; - subkey = strrchr(name, '.'); - if (!subkey) + if (starts_with(key, "branch", &name, &namelen, &subkey)) { + if (!name) return 0; - branch = make_branch(name, subkey - name); + branch = make_branch(name, namelen); if (!strcmp(subkey, ".remote")) { return git_config_string(&branch->remote_name, key, value); } else if (!strcmp(subkey, ".pushremote")) { and so on. The difference in lines of code isn't that great, but I think it makes the resulting code more obvious to read. -Peff