Skip to content

Commit ae2f255

Browse files
committed
Merge branch 'tg/git-remote'
The internal API to interact with "remote.*" configuration variables has been streamlined. * tg/git-remote: remote: use remote_is_configured() for add and rename remote: actually check if remote exits remote: simplify remote_is_configured() remote: use parse_config_key
2 parents 56f37fd + a31eeae commit ae2f255

File tree

5 files changed

+81
-65
lines changed

5 files changed

+81
-65
lines changed

builtin/fetch.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,10 +1022,9 @@ static int add_remote_or_group(const char *name, struct string_list *list)
10221022

10231023
git_config(get_remote_group, &g);
10241024
if (list->nr == prev_nr) {
1025-
struct remote *remote;
1026-
if (!remote_is_configured(name))
1025+
struct remote *remote = remote_get(name);
1026+
if (!remote_is_configured(remote))
10271027
return 0;
1028-
remote = remote_get(name);
10291028
string_list_append(list, remote->name);
10301029
}
10311030
return 1;

builtin/remote.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,7 @@ static int add(int argc, const char **argv)
186186
url = argv[1];
187187

188188
remote = remote_get(name);
189-
if (remote && (remote->url_nr > 1 ||
190-
(strcmp(name, remote->url[0]) &&
191-
strcmp(url, remote->url[0])) ||
192-
remote->fetch_refspec_nr))
189+
if (remote_is_configured(remote))
193190
die(_("remote %s already exists."), name);
194191

195192
strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
@@ -634,14 +631,14 @@ static int mv(int argc, const char **argv)
634631
rename.remote_branches = &remote_branches;
635632

636633
oldremote = remote_get(rename.old);
637-
if (!oldremote)
634+
if (!remote_is_configured(oldremote))
638635
die(_("No such remote: %s"), rename.old);
639636

640637
if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
641638
return migrate_file(oldremote);
642639

643640
newremote = remote_get(rename.new);
644-
if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
641+
if (remote_is_configured(newremote))
645642
die(_("remote %s already exists."), rename.new);
646643

647644
strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
@@ -773,7 +770,7 @@ static int rm(int argc, const char **argv)
773770
usage_with_options(builtin_remote_rm_usage, options);
774771

775772
remote = remote_get(argv[1]);
776-
if (!remote)
773+
if (!remote_is_configured(remote))
777774
die(_("No such remote: %s"), argv[1]);
778775

779776
known_remotes.to_delete = remote;
@@ -1441,9 +1438,9 @@ static int set_remote_branches(const char *remotename, const char **branches,
14411438

14421439
strbuf_addf(&key, "remote.%s.fetch", remotename);
14431440

1444-
if (!remote_is_configured(remotename))
1445-
die(_("No such remote '%s'"), remotename);
14461441
remote = remote_get(remotename);
1442+
if (!remote_is_configured(remote))
1443+
die(_("No such remote '%s'"), remotename);
14471444

14481445
if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) {
14491446
strbuf_release(&key);
@@ -1498,9 +1495,9 @@ static int get_url(int argc, const char **argv)
14981495

14991496
remotename = argv[0];
15001497

1501-
if (!remote_is_configured(remotename))
1502-
die(_("No such remote '%s'"), remotename);
15031498
remote = remote_get(remotename);
1499+
if (!remote_is_configured(remote))
1500+
die(_("No such remote '%s'"), remotename);
15041501

15051502
url_nr = 0;
15061503
if (push_mode) {
@@ -1566,9 +1563,9 @@ static int set_url(int argc, const char **argv)
15661563
if (delete_mode)
15671564
oldurl = newurl;
15681565

1569-
if (!remote_is_configured(remotename))
1570-
die(_("No such remote '%s'"), remotename);
15711566
remote = remote_get(remotename);
1567+
if (!remote_is_configured(remote))
1568+
die(_("No such remote '%s'"), remotename);
15721569

15731570
if (push_mode) {
15741571
strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);

remote.c

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -318,120 +318,115 @@ static void read_branches_file(struct remote *remote)
318318
static int handle_config(const char *key, const char *value, void *cb)
319319
{
320320
const char *name;
321+
int namelen;
321322
const char *subkey;
322323
struct remote *remote;
323324
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)
328327
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")) {
331330
return git_config_string(&branch->remote_name, key, value);
332-
} else if (!strcmp(subkey, ".pushremote")) {
331+
} else if (!strcmp(subkey, "pushremote")) {
333332
return git_config_string(&branch->pushremote_name, key, value);
334-
} else if (!strcmp(subkey, ".merge")) {
333+
} else if (!strcmp(subkey, "merge")) {
335334
if (!value)
336335
return config_error_nonbool(key);
337336
add_merge(branch, xstrdup(value));
338337
}
339338
return 0;
340339
}
341-
if (starts_with(key, "url.")) {
340+
if (parse_config_key(key, "url", &name, &namelen, &subkey) >= 0) {
342341
struct rewrite *rewrite;
343-
name = key + 4;
344-
subkey = strrchr(name, '.');
345-
if (!subkey)
342+
if (!name)
346343
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);
349346
if (!value)
350347
return config_error_nonbool(key);
351348
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);
354351
if (!value)
355352
return config_error_nonbool(key);
356353
add_instead_of(rewrite, xstrdup(value));
357354
}
358355
}
359356

360-
if (!starts_with(key, "remote."))
357+
if (parse_config_key(key, "remote", &name, &namelen, &subkey) < 0)
361358
return 0;
362-
name = key + 7;
363359

364360
/* Handle remote.* variables */
365-
if (!strcmp(name, "pushdefault"))
361+
if (!name && !strcmp(subkey, "pushdefault"))
366362
return git_config_string(&pushremote_name, key, value);
367363

364+
if (!name)
365+
return 0;
368366
/* Handle remote.<name>.* variables */
369367
if (*name == '/') {
370368
warning("Config remote shorthand cannot begin with '/': %s",
371369
name);
372370
return 0;
373371
}
374-
subkey = strrchr(name, '.');
375-
if (!subkey)
376-
return 0;
377-
remote = make_remote(name, subkey - name);
372+
remote = make_remote(name, namelen);
378373
remote->origin = REMOTE_CONFIG;
379-
if (!strcmp(subkey, ".mirror"))
374+
if (!strcmp(subkey, "mirror"))
380375
remote->mirror = git_config_bool(key, value);
381-
else if (!strcmp(subkey, ".skipdefaultupdate"))
376+
else if (!strcmp(subkey, "skipdefaultupdate"))
382377
remote->skip_default_update = git_config_bool(key, value);
383-
else if (!strcmp(subkey, ".skipfetchall"))
378+
else if (!strcmp(subkey, "skipfetchall"))
384379
remote->skip_default_update = git_config_bool(key, value);
385-
else if (!strcmp(subkey, ".prune"))
380+
else if (!strcmp(subkey, "prune"))
386381
remote->prune = git_config_bool(key, value);
387-
else if (!strcmp(subkey, ".url")) {
382+
else if (!strcmp(subkey, "url")) {
388383
const char *v;
389384
if (git_config_string(&v, key, value))
390385
return -1;
391386
add_url(remote, v);
392-
} else if (!strcmp(subkey, ".pushurl")) {
387+
} else if (!strcmp(subkey, "pushurl")) {
393388
const char *v;
394389
if (git_config_string(&v, key, value))
395390
return -1;
396391
add_pushurl(remote, v);
397-
} else if (!strcmp(subkey, ".push")) {
392+
} else if (!strcmp(subkey, "push")) {
398393
const char *v;
399394
if (git_config_string(&v, key, value))
400395
return -1;
401396
add_push_refspec(remote, v);
402-
} else if (!strcmp(subkey, ".fetch")) {
397+
} else if (!strcmp(subkey, "fetch")) {
403398
const char *v;
404399
if (git_config_string(&v, key, value))
405400
return -1;
406401
add_fetch_refspec(remote, v);
407-
} else if (!strcmp(subkey, ".receivepack")) {
402+
} else if (!strcmp(subkey, "receivepack")) {
408403
const char *v;
409404
if (git_config_string(&v, key, value))
410405
return -1;
411406
if (!remote->receivepack)
412407
remote->receivepack = v;
413408
else
414409
error("more than one receivepack given, using the first");
415-
} else if (!strcmp(subkey, ".uploadpack")) {
410+
} else if (!strcmp(subkey, "uploadpack")) {
416411
const char *v;
417412
if (git_config_string(&v, key, value))
418413
return -1;
419414
if (!remote->uploadpack)
420415
remote->uploadpack = v;
421416
else
422417
error("more than one uploadpack given, using the first");
423-
} else if (!strcmp(subkey, ".tagopt")) {
418+
} else if (!strcmp(subkey, "tagopt")) {
424419
if (!strcmp(value, "--no-tags"))
425420
remote->fetch_tags = -1;
426421
else if (!strcmp(value, "--tags"))
427422
remote->fetch_tags = 2;
428-
} else if (!strcmp(subkey, ".proxy")) {
423+
} else if (!strcmp(subkey, "proxy")) {
429424
return git_config_string((const char **)&remote->http_proxy,
430425
key, value);
431-
} else if (!strcmp(subkey, ".proxyauthmethod")) {
426+
} else if (!strcmp(subkey, "proxyauthmethod")) {
432427
return git_config_string((const char **)&remote->http_proxy_authmethod,
433428
key, value);
434-
} else if (!strcmp(subkey, ".vcs")) {
429+
} else if (!strcmp(subkey, "vcs")) {
435430
return git_config_string(&remote->foreign_vcs, key, value);
436431
}
437432
return 0;
@@ -718,18 +713,9 @@ struct remote *pushremote_get(const char *name)
718713
return remote_get_1(name, pushremote_for_branch);
719714
}
720715

721-
int remote_is_configured(const char *name)
716+
int remote_is_configured(struct remote *remote)
722717
{
723-
struct remotes_hash_key lookup;
724-
struct hashmap_entry lookup_entry;
725-
read_config();
726-
727-
init_remotes_hash();
728-
lookup.str = name;
729-
lookup.len = strlen(name);
730-
hashmap_entry_init(&lookup_entry, memhash(name, lookup.len));
731-
732-
return hashmap_get(&remotes_hash, &lookup_entry, &lookup) != NULL;
718+
return remote && remote->origin;
733719
}
734720

735721
int for_each_remote(each_remote_fn fn, void *priv)

remote.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "hashmap.h"
66

77
enum {
8+
REMOTE_UNCONFIGURED = 0,
89
REMOTE_CONFIG,
910
REMOTE_REMOTES,
1011
REMOTE_BRANCHES
@@ -59,7 +60,7 @@ struct remote {
5960

6061
struct remote *remote_get(const char *name);
6162
struct remote *pushremote_get(const char *name);
62-
int remote_is_configured(const char *name);
63+
int remote_is_configured(struct remote *remote);
6364

6465
typedef int each_remote_fn(struct remote *remote, void *priv);
6566
int for_each_remote(each_remote_fn fn, void *priv);

t/t5505-remote.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,39 @@ test_expect_success 'remove remote protects local branches' '
144144
)
145145
'
146146

147+
test_expect_success 'remove errors out early when deleting non-existent branch' '
148+
(
149+
cd test &&
150+
echo "fatal: No such remote: foo" >expect &&
151+
test_must_fail git remote rm foo 2>actual &&
152+
test_i18ncmp expect actual
153+
)
154+
'
155+
156+
test_expect_success 'rename errors out early when deleting non-existent branch' '
157+
(
158+
cd test &&
159+
echo "fatal: No such remote: foo" >expect &&
160+
test_must_fail git remote rename foo bar 2>actual &&
161+
test_i18ncmp expect actual
162+
)
163+
'
164+
165+
test_expect_success 'add existing foreign_vcs remote' '
166+
test_config remote.foo.vcs bar &&
167+
echo "fatal: remote foo already exists." >expect &&
168+
test_must_fail git remote add foo bar 2>actual &&
169+
test_i18ncmp expect actual
170+
'
171+
172+
test_expect_success 'add existing foreign_vcs remote' '
173+
test_config remote.foo.vcs bar &&
174+
test_config remote.bar.vcs bar &&
175+
echo "fatal: remote bar already exists." >expect &&
176+
test_must_fail git remote rename foo bar 2>actual &&
177+
test_i18ncmp expect actual
178+
'
179+
147180
cat >test/expect <<EOF
148181
* remote origin
149182
Fetch URL: $(pwd)/one

0 commit comments

Comments
 (0)