Skip to content

Commit 674468b

Browse files
tgummerergitster
authored andcommitted
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>
1 parent bc60f8a commit 674468b

File tree

4 files changed

+12
-21
lines changed

4 files changed

+12
-21
lines changed

builtin/fetch.c

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

10171017
git_config(get_remote_group, &g);
10181018
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))
10211021
return 0;
1022-
remote = remote_get(name);
10231022
string_list_append(list, remote->name);
10241023
}
10251024
return 1;

builtin/remote.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,9 +1441,9 @@ static int set_remote_branches(const char *remotename, const char **branches,
14411441

14421442
strbuf_addf(&key, "remote.%s.fetch", remotename);
14431443

1444-
if (!remote_is_configured(remotename))
1445-
die(_("No such remote '%s'"), remotename);
14461444
remote = remote_get(remotename);
1445+
if (!remote_is_configured(remote))
1446+
die(_("No such remote '%s'"), remotename);
14471447

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

14991499
remotename = argv[0];
15001500

1501-
if (!remote_is_configured(remotename))
1502-
die(_("No such remote '%s'"), remotename);
15031501
remote = remote_get(remotename);
1502+
if (!remote_is_configured(remote))
1503+
die(_("No such remote '%s'"), remotename);
15041504

15051505
url_nr = 0;
15061506
if (push_mode) {
@@ -1566,9 +1566,9 @@ static int set_url(int argc, const char **argv)
15661566
if (delete_mode)
15671567
oldurl = newurl;
15681568

1569-
if (!remote_is_configured(remotename))
1570-
die(_("No such remote '%s'"), remotename);
15711569
remote = remote_get(remotename);
1570+
if (!remote_is_configured(remote))
1571+
die(_("No such remote '%s'"), remotename);
15721572

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

remote.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -713,18 +713,9 @@ struct remote *pushremote_get(const char *name)
713713
return remote_get_1(name, pushremote_for_branch);
714714
}
715715

716-
int remote_is_configured(const char *name)
716+
int remote_is_configured(struct remote *remote)
717717
{
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;
728719
}
729720

730721
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);

0 commit comments

Comments
 (0)