remote: simplify remote_is_configured()
The remote_is_configured() function allows checking whether a remote exists or not. The function however only works if remote_get() wasn't called before calling it. In addition, it only checks the configuration for remotes, but not remotes or branches files. Make use of the origin member of struct remote instead, which indicates where the remote comes from. It will be set to some value if the remote is configured in any file in the repository, but is initialized to 0 if the remote is only created in make_remote(). 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
674468b3642abfff7c61d5ff95fffc43b87f70b7
4 files changed
+12
-21
builtin/fetch.c
+2
-3
@@ -1016,10 +1016,9 @@ static int add_remote_or_group(const char *name, struct string_list *list)
1016
1017
git_config(get_remote_group, &g);
1018
if (list->nr == prev_nr) {
1019
- struct remote *remote;
1020
- if (!remote_is_configured(name))
1019
+ struct remote *remote = remote_get(name);
1020
+ if (!remote_is_configured(remote))
1021
return 0;
1022
- remote = remote_get(name);
1022
string_list_append(list, remote->name);
1023
}
1024
return 1;
builtin/remote.c
+6
-6
@@ -1441,9 +1441,9 @@ static int set_remote_branches(const char *remotename, const char **branches,
1441
1442
strbuf_addf(&key, "remote.%s.fetch", remotename);
1443
1444
- if (!remote_is_configured(remotename))
1445
- die(_("No such remote '%s'"), remotename);
1444
remote = remote_get(remotename);
1445
+ if (!remote_is_configured(remote))
1446
+ die(_("No such remote '%s'"), remotename);
1447
1448
if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) {
1449
strbuf_release(&key);
@@ -1498,9 +1498,9 @@ static int get_url(int argc, const char **argv)
1498
1499
remotename = argv[0];
1500
1501
- if (!remote_is_configured(remotename))
1502
- die(_("No such remote '%s'"), remotename);
1501
remote = remote_get(remotename);
1502
+ if (!remote_is_configured(remote))
1503
+ die(_("No such remote '%s'"), remotename);
1504
1505
url_nr = 0;
1506
if (push_mode) {
@@ -1566,9 +1566,9 @@ static int set_url(int argc, const char **argv)
1566
if (delete_mode)
1567
oldurl = newurl;
1568
1569
- if (!remote_is_configured(remotename))
1570
- die(_("No such remote '%s'"), remotename);
1569
remote = remote_get(remotename);
1570
+ if (!remote_is_configured(remote))
1571
+ die(_("No such remote '%s'"), remotename);
1572
1573
if (push_mode) {
1574
strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
remote.c
+2
-11
@@ -713,18 +713,9 @@ struct remote *pushremote_get(const char *name)
713
return remote_get_1(name, pushremote_for_branch);
714
}
715
716
-int remote_is_configured(const char *name)
716
+int remote_is_configured(struct remote *remote)
717
{
718
- struct remotes_hash_key lookup;
719
- struct hashmap_entry lookup_entry;
720
- read_config();
721
-
722
- init_remotes_hash();
723
- lookup.str = name;
724
- lookup.len = strlen(name);
725
- hashmap_entry_init(&lookup_entry, memhash(name, lookup.len));
726
-
727
- return hashmap_get(&remotes_hash, &lookup_entry, &lookup) != NULL;
718
+ return remote && remote->origin;
719
}
720
721
int for_each_remote(each_remote_fn fn, void *priv)
remote.h
+2
-1
@@ -5,6 +5,7 @@
5
#include "hashmap.h"
6
7
enum {
8
+ REMOTE_UNCONFIGURED = 0,
9
REMOTE_CONFIG,
10
REMOTE_REMOTES,
11
REMOTE_BRANCHES
@@ -59,7 +60,7 @@ struct remote {
60
61
struct remote *remote_get(const char *name);
62
struct remote *pushremote_get(const char *name);
62
-int remote_is_configured(const char *name);
63
+int remote_is_configured(struct remote *remote);
64
65
typedef int each_remote_fn(struct remote *remote, void *priv);
66
int for_each_remote(each_remote_fn fn, void *priv);