git-tar-tree: no more void pointer arithmetic
[git.git] / builtin-push.c
index a0c1caa..66b9407 100644 (file)
@@ -72,15 +72,22 @@ static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
 {
        int n = 0;
        FILE *f = fopen(git_path("remotes/%s", repo), "r");
+       int has_explicit_refspec = refspec_nr || all || tags;
 
        if (!f)
                return -1;
        while (fgets(buffer, BUF_SIZE, f)) {
+               int is_refspec;
                char *s, *p;
 
-               if (strncmp("URL: ", buffer, 5))
+               if (!strncmp("URL: ", buffer, 5)) {
+                       is_refspec = 0;
+                       s = buffer + 5;
+               } else if (!strncmp("Push: ", buffer, 6)) {
+                       is_refspec = 1;
+                       s = buffer + 6;
+               } else
                        continue;
-               s = buffer + 5;
 
                /* Remove whitespace at the head.. */
                while (isspace(*s))
@@ -93,9 +100,14 @@ static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
                while (isspace(p[-1]))
                        *--p = 0;
 
-               uri[n++] = strdup(s);
-               if (n == MAX_URI)
-                       break;
+               if (!is_refspec) {
+                       if (n < MAX_URI)
+                               uri[n++] = strdup(s);
+                       else
+                               error("more than %d URL's specified, ignoreing the rest", MAX_URI);
+               }
+               else if (is_refspec && !has_explicit_refspec)
+                       add_refspec(strdup(s));
        }
        fclose(f);
        if (!n)
@@ -103,6 +115,41 @@ static int get_remotes_uri(const char *repo, const char *uri[MAX_URI])
        return n;
 }
 
+static const char **config_uri;
+static const char *config_repo;
+static int config_repo_len;
+static int config_current_uri;
+static int config_get_refspecs;
+
+static int get_remote_config(const char* key, const char* value)
+{
+       if (!strncmp(key, "remote.", 7) &&
+           !strncmp(key + 7, config_repo, config_repo_len)) {
+               if (!strcmp(key + 7 + config_repo_len, ".url")) {
+                       if (config_current_uri < MAX_URI)
+                               config_uri[config_current_uri++] = strdup(value);
+                       else
+                               error("more than %d URL's specified, ignoring the rest", MAX_URI);
+               }
+               else if (config_get_refspecs &&
+                        !strcmp(key + 7 + config_repo_len, ".push"))
+                       add_refspec(strdup(value));
+       }
+       return 0;
+}
+
+static int get_config_remotes_uri(const char *repo, const char *uri[MAX_URI])
+{
+       config_repo_len = strlen(repo);
+       config_repo = repo;
+       config_current_uri = 0;
+       config_uri = uri;
+       config_get_refspecs = !(refspec_nr || all || tags);
+
+       git_config(get_remote_config);
+       return config_current_uri;
+}
+
 static int get_branches_uri(const char *repo, const char *uri[MAX_URI])
 {
        const char *slash = strchr(repo, '/');
@@ -135,7 +182,13 @@ static int get_branches_uri(const char *repo, const char *uri[MAX_URI])
        return 1;
 }
 
-static int get_uri(const char *repo, const char *uri[MAX_URI])
+/*
+ * Read remotes and branches file, fill the push target URI
+ * list.  If there is no command line refspecs, read Push: lines
+ * to set up the *refspec list as well.
+ * return the number of push target URIs
+ */
+static int read_config(const char *repo, const char *uri[MAX_URI])
 {
        int n;
 
@@ -144,6 +197,10 @@ static int get_uri(const char *repo, const char *uri[MAX_URI])
                if (n > 0)
                        return n;
 
+               n = get_config_remotes_uri(repo, uri);
+               if (n > 0)
+                       return n;
+
                n = get_branches_uri(repo, uri);
                if (n > 0)
                        return n;
@@ -156,12 +213,12 @@ static int get_uri(const char *repo, const char *uri[MAX_URI])
 static int do_push(const char *repo)
 {
        const char *uri[MAX_URI];
-       int i, n = get_uri(repo, uri);
-       int remote;
+       int i, n;
+       int common_argc;
        const char **argv;
        int argc;
 
-       n = get_uri(repo, uri);
+       n = read_config(repo, uri);
        if (n <= 0)
                die("bad repository '%s'", repo);
 
@@ -174,23 +231,25 @@ static int do_push(const char *repo)
                argv[argc++] = "--force";
        if (execute)
                argv[argc++] = execute;
-       if (thin)
-               argv[argc++] = "--thin";
-       remote = argc;
-       argv[argc++] = "dummy-remote";
-       while (refspec_nr--)
-               argv[argc++] = *refspec++;
-       argv[argc] = NULL;
+       common_argc = argc;
 
        for (i = 0; i < n; i++) {
                int error;
+               int dest_argc = common_argc;
+               int dest_refspec_nr = refspec_nr;
+               const char **dest_refspec = refspec;
                const char *dest = uri[i];
                const char *sender = "git-send-pack";
                if (!strncmp(dest, "http://", 7) ||
                    !strncmp(dest, "https://", 8))
                        sender = "git-http-push";
+               else if (thin)
+                       argv[dest_argc++] = "--thin";
                argv[0] = sender;
-               argv[remote] = dest;
+               argv[dest_argc++] = dest;
+               while (dest_refspec_nr--)
+                       argv[dest_argc++] = *dest_refspec++;
+               argv[dest_argc] = NULL;
                error = run_command_v(argc, argv);
                if (!error)
                        continue;