Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Thomas Gummerer authored and Junio C Hamano committed Feb 16, 2016
1 parent bc60f8a commit 674468b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
5 changes: 2 additions & 3 deletions builtin/fetch.c
Expand Up @@ -1016,10 +1016,9 @@ static int add_remote_or_group(const char *name, struct string_list *list)

git_config(get_remote_group, &g);
if (list->nr == prev_nr) {
struct remote *remote;
if (!remote_is_configured(name))
struct remote *remote = remote_get(name);
if (!remote_is_configured(remote))
return 0;
remote = remote_get(name);
string_list_append(list, remote->name);
}
return 1;
Expand Down
12 changes: 6 additions & 6 deletions builtin/remote.c
Expand Up @@ -1441,9 +1441,9 @@ static int set_remote_branches(const char *remotename, const char **branches,

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

if (!remote_is_configured(remotename))
die(_("No such remote '%s'"), remotename);
remote = remote_get(remotename);
if (!remote_is_configured(remote))
die(_("No such remote '%s'"), remotename);

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

remotename = argv[0];

if (!remote_is_configured(remotename))
die(_("No such remote '%s'"), remotename);
remote = remote_get(remotename);
if (!remote_is_configured(remote))
die(_("No such remote '%s'"), remotename);

url_nr = 0;
if (push_mode) {
Expand Down Expand Up @@ -1566,9 +1566,9 @@ static int set_url(int argc, const char **argv)
if (delete_mode)
oldurl = newurl;

if (!remote_is_configured(remotename))
die(_("No such remote '%s'"), remotename);
remote = remote_get(remotename);
if (!remote_is_configured(remote))
die(_("No such remote '%s'"), remotename);

if (push_mode) {
strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
Expand Down
13 changes: 2 additions & 11 deletions remote.c
Expand Up @@ -713,18 +713,9 @@ struct remote *pushremote_get(const char *name)
return remote_get_1(name, pushremote_for_branch);
}

int remote_is_configured(const char *name)
int remote_is_configured(struct remote *remote)
{
struct remotes_hash_key lookup;
struct hashmap_entry lookup_entry;
read_config();

init_remotes_hash();
lookup.str = name;
lookup.len = strlen(name);
hashmap_entry_init(&lookup_entry, memhash(name, lookup.len));

return hashmap_get(&remotes_hash, &lookup_entry, &lookup) != NULL;
return remote && remote->origin;
}

int for_each_remote(each_remote_fn fn, void *priv)
Expand Down
3 changes: 2 additions & 1 deletion remote.h
Expand Up @@ -5,6 +5,7 @@
#include "hashmap.h"

enum {
REMOTE_UNCONFIGURED = 0,
REMOTE_CONFIG,
REMOTE_REMOTES,
REMOTE_BRANCHES
Expand Down Expand Up @@ -59,7 +60,7 @@ struct remote {

struct remote *remote_get(const char *name);
struct remote *pushremote_get(const char *name);
int remote_is_configured(const char *name);
int remote_is_configured(struct remote *remote);

typedef int each_remote_fn(struct remote *remote, void *priv);
int for_each_remote(each_remote_fn fn, void *priv);
Expand Down

0 comments on commit 674468b

Please sign in to comment.