Give error when no remote is configured
[gitweb.git] / remote.c
index 570e11286ea295e825a23b1d5ed40c9fbd02be57..199830ea93f6f87cd3764afe1880e968209439c7 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -4,6 +4,7 @@
 #include "commit.h"
 #include "diff.h"
 #include "revision.h"
+#include "dir.h"
 
 static struct refspec s_tag_refspec = {
        0,
@@ -37,6 +38,7 @@ static int branches_nr;
 
 static struct branch *current_branch;
 static const char *default_remote_name;
+static int explicit_default_remote_name;
 
 static struct rewrite **rewrite;
 static int rewrite_alloc;
@@ -103,6 +105,16 @@ static void add_url_alias(struct remote *remote, const char *url)
        add_url(remote, alias_url(url));
 }
 
+static struct remote *get_remote_by_name(const char *name)
+{
+       int i;
+       for (i = 0; i < remotes_nr; i++) {
+               if (!strcmp(name, remotes[i]->name))
+                       return remotes[i];
+       }
+       return NULL;
+}
+
 static struct remote *make_remote(const char *name, int len)
 {
        struct remote *ret;
@@ -329,8 +341,10 @@ static int handle_config(const char *key, const char *value, void *cb)
                        if (!value)
                                return config_error_nonbool(key);
                        branch->remote_name = xstrdup(value);
-                       if (branch == current_branch)
+                       if (branch == current_branch) {
                                default_remote_name = branch->remote_name;
+                               explicit_default_remote_name = 1;
+                       }
                } else if (!strcmp(subkey, ".merge")) {
                        if (!value)
                                return config_error_nonbool(key);
@@ -634,10 +648,7 @@ static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
 
 static int valid_remote_nick(const char *name)
 {
-       if (!name[0] || /* not empty */
-           (name[0] == '.' && /* not "." */
-            (!name[1] || /* not ".." */
-             (name[1] == '.' && !name[2]))))
+       if (!name[0] || is_dot_or_dotdot(name))
                return 0;
        return !strchr(name, '/'); /* no slash */
 }
@@ -645,11 +656,22 @@ static int valid_remote_nick(const char *name)
 struct remote *remote_get(const char *name)
 {
        struct remote *ret;
+       int name_given = 0;
 
        read_config();
-       if (!name)
+       if (name)
+               name_given = 1;
+       else {
                name = default_remote_name;
-       ret = make_remote(name, 0);
+               name_given = explicit_default_remote_name;
+       }
+       if (name_given)
+               ret = make_remote(name, 0);
+       else {
+               ret = get_remote_by_name(name);
+               if (!ret)
+                       return NULL;
+       }
        if (valid_remote_nick(name)) {
                if (!ret->url)
                        read_remotes_file(ret);