remote: use parse_config_key

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 aformentioned commit, but can be replaced by calls to parse_config_key(), to simplify the code and clarify the intentions. Do that. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Feb 16, 2016 at 10:47 UTC bc60f8a77c6e676225810f33117abcf9117b1c1e
1 file changed +32 -37
remote.c
+32 -37
@@ -318,93 +318,88 @@ static void read_branches_file(struct remote *remote)
318 static int handle_config(const char *key, const char *value, void *cb)
319 {
320 const char *name;
321 + int namelen;
322 const char *subkey;
323 struct remote *remote;
324 struct branch *branch;
324 - if (starts_with(key, "branch.")) {
325 - name = key + 7;
326 - subkey = strrchr(name, '.');
327 - if (!subkey)
325 + if (parse_config_key(key, "branch", &name, &namelen, &subkey) >= 0) {
326 + if (!name)
327 return 0;
329 - branch = make_branch(name, subkey - name);
330 - if (!strcmp(subkey, ".remote")) {
328 + branch = make_branch(name, namelen);
329 + if (!strcmp(subkey, "remote")) {
330 return git_config_string(&branch->remote_name, key, value);
332 - } else if (!strcmp(subkey, ".pushremote")) {
331 + } else if (!strcmp(subkey, "pushremote")) {
332 return git_config_string(&branch->pushremote_name, key, value);
334 - } else if (!strcmp(subkey, ".merge")) {
333 + } else if (!strcmp(subkey, "merge")) {
334 if (!value)
335 return config_error_nonbool(key);
336 add_merge(branch, xstrdup(value));
337 }
338 return 0;
339 }
341 - if (starts_with(key, "url.")) {
340 + if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) {
341 struct rewrite *rewrite;
343 - name = key + 4;
344 - subkey = strrchr(name, '.');
345 - if (!subkey)
342 + if (!name)
343 return 0;
347 - if (!strcmp(subkey, ".insteadof")) {
348 - rewrite = make_rewrite(&rewrites, name, subkey - name);
344 + if (!strcmp(subkey, "insteadof")) {
345 + rewrite = make_rewrite(&rewrites, name, namelen);
346 if (!value)
347 return config_error_nonbool(key);
348 add_instead_of(rewrite, xstrdup(value));
352 - } else if (!strcmp(subkey, ".pushinsteadof")) {
353 - rewrite = make_rewrite(&rewrites_push, name, subkey - name);
349 + } else if (!strcmp(subkey, "pushinsteadof")) {
350 + rewrite = make_rewrite(&rewrites_push, name, namelen);
351 if (!value)
352 return config_error_nonbool(key);
353 add_instead_of(rewrite, xstrdup(value));
354 }
355 }
356
360 - if (!starts_with(key, "remote."))
357 + if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0)
358 return 0;
362 - name = key + 7;
359
360 /* Handle remote.* variables */
365 - if (!strcmp(name, "pushdefault"))
361 + if (!name && !strcmp(subkey, "pushdefault"))
362 return git_config_string(&pushremote_name, key, value);
363
364 + if (!name)
365 + return 0;
366 /* Handle remote.<name>.* variables */
367 if (*name == '/') {
368 warning("Config remote shorthand cannot begin with '/': %s",
369 name);
370 return 0;
371 }
374 - subkey = strrchr(name, '.');
375 - if (!subkey)
376 - return 0;
377 - remote = make_remote(name, subkey - name);
372 + remote = make_remote(name, namelen);
373 remote->origin = REMOTE_CONFIG;
379 - if (!strcmp(subkey, ".mirror"))
374 + if (!strcmp(subkey, "mirror"))
375 remote->mirror = git_config_bool(key, value);
381 - else if (!strcmp(subkey, ".skipdefaultupdate"))
376 + else if (!strcmp(subkey, "skipdefaultupdate"))
377 remote->skip_default_update = git_config_bool(key, value);
383 - else if (!strcmp(subkey, ".skipfetchall"))
378 + else if (!strcmp(subkey, "skipfetchall"))
379 remote->skip_default_update = git_config_bool(key, value);
385 - else if (!strcmp(subkey, ".prune"))
380 + else if (!strcmp(subkey, "prune"))
381 remote->prune = git_config_bool(key, value);
387 - else if (!strcmp(subkey, ".url")) {
382 + else if (!strcmp(subkey, "url")) {
383 const char *v;
384 if (git_config_string(&v, key, value))
385 return -1;
386 add_url(remote, v);
392 - } else if (!strcmp(subkey, ".pushurl")) {
387 + } else if (!strcmp(subkey, "pushurl")) {
388 const char *v;
389 if (git_config_string(&v, key, value))
390 return -1;
391 add_pushurl(remote, v);
397 - } else if (!strcmp(subkey, ".push")) {
392 + } else if (!strcmp(subkey, "push")) {
393 const char *v;
394 if (git_config_string(&v, key, value))
395 return -1;
396 add_push_refspec(remote, v);
402 - } else if (!strcmp(subkey, ".fetch")) {
397 + } else if (!strcmp(subkey, "fetch")) {
398 const char *v;
399 if (git_config_string(&v, key, value))
400 return -1;
401 add_fetch_refspec(remote, v);
407 - } else if (!strcmp(subkey, ".receivepack")) {
402 + } else if (!strcmp(subkey, "receivepack")) {
403 const char *v;
404 if (git_config_string(&v, key, value))
405 return -1;
@@ -412,7 +407,7 @@ static int handle_config(const char *key, const char *value, void *cb)
407 remote->receivepack = v;
408 else
409 error("more than one receivepack given, using the first");
415 - } else if (!strcmp(subkey, ".uploadpack")) {
410 + } else if (!strcmp(subkey, "uploadpack")) {
411 const char *v;
412 if (git_config_string(&v, key, value))
413 return -1;
@@ -420,18 +415,18 @@ static int handle_config(const char *key, const char *value, void *cb)
415 remote->uploadpack = v;
416 else
417 error("more than one uploadpack given, using the first");
423 - } else if (!strcmp(subkey, ".tagopt")) {
418 + } else if (!strcmp(subkey, "tagopt")) {
419 if (!strcmp(value, "--no-tags"))
420 remote->fetch_tags = -1;
421 else if (!strcmp(value, "--tags"))
422 remote->fetch_tags = 2;
428 - } else if (!strcmp(subkey, ".proxy")) {
423 + } else if (!strcmp(subkey, "proxy")) {
424 return git_config_string((const char **)&remote->http_proxy,
425 key, value);
431 - } else if (!strcmp(subkey, ".proxyauthmethod")) {
426 + } else if (!strcmp(subkey, "proxyauthmethod")) {
427 return git_config_string((const char **)&remote->http_proxy_authmethod,
428 key, value);
434 - } else if (!strcmp(subkey, ".vcs")) {
429 + } else if (!strcmp(subkey, "vcs")) {
430 return git_config_string(&remote->foreign_vcs, key, value);
431 }
432 return 0;