]> asedeno.scripts.mit.edu Git - git.git/blobdiff - remote.c
Remove dead code: show_log() sep argument and diff_options.msg_sep
[git.git] / remote.c
index 40ed24633f4a63ef9decc1575c4863d1b894ae37..2d9af4023eba6f8b2fe528ccbf03569fcaa265ee 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -232,7 +232,7 @@ static void read_branches_file(struct remote *remote)
 {
        const char *slash = strchr(remote->name, '/');
        char *frag;
-       char *branch;
+       struct strbuf branch;
        int n = slash ? slash - remote->name : 1000;
        FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
        char *s, *p;
@@ -258,17 +258,33 @@ static void read_branches_file(struct remote *remote)
        strcpy(p, s);
        if (slash)
                strcat(p, slash);
+
+       /*
+        * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when
+        * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from
+        * the partial URL obtained from the branches file plus
+        * "/netdev-2.6" and does not store it in any tracking ref.
+        * #branch specifier in the file is ignored.
+        *
+        * Otherwise, the branches file would have URL and optionally
+        * #branch specified.  The "master" (or specified) branch is
+        * fetched and stored in the local branch of the same name.
+        */
+       strbuf_init(&branch, 0);
        frag = strchr(p, '#');
        if (frag) {
                *(frag++) = '\0';
-               branch = xmalloc(strlen(frag) + 12);
-               strcpy(branch, "refs/heads/");
-               strcat(branch, frag);
+               strbuf_addf(&branch, "refs/heads/%s", frag);
+       } else
+               strbuf_addstr(&branch, "refs/heads/master");
+       if (!slash) {
+               strbuf_addf(&branch, ":refs/heads/%s", remote->name);
        } else {
-               branch = "refs/heads/master";
+               strbuf_reset(&branch);
+               strbuf_addstr(&branch, "HEAD:");
        }
        add_url_alias(remote, p);
-       add_fetch_refspec(remote, branch);
+       add_fetch_refspec(remote, strbuf_detach(&branch, 0));
        remote->fetch_tags = 1; /* always auto-follow */
 }
 
@@ -299,7 +315,7 @@ static int handle_config(const char *key, const char *value)
        }
        if (!prefixcmp(key, "url.")) {
                struct rewrite *rewrite;
-               name = key + 5;
+               name = key + 4;
                subkey = strrchr(name, '.');
                if (!subkey)
                        return 0;
@@ -393,7 +409,7 @@ static void read_config(void)
        alias_all_urls();
 }
 
-static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch)
+static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify)
 {
        int i;
        int st;
@@ -417,17 +433,21 @@ static struct refspec *parse_refspec_internal(int nr_refspec, const char **refsp
                        rhs++;
                        rlen = strlen(rhs);
                        is_glob = (2 <= rlen && !strcmp(rhs + rlen - 2, "/*"));
-                       rs[i].dst = xstrndup(rhs, rlen - is_glob * 2);
+                       if (is_glob)
+                               rlen -= 2;
+                       rs[i].dst = xstrndup(rhs, rlen);
                }
 
                llen = (rhs ? (rhs - lhs - 1) : strlen(lhs));
-               if (is_glob != (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)))
-                       goto invalid;
-
-               if (is_glob) {
+               if (2 <= llen && !memcmp(lhs + llen - 2, "/*", 2)) {
+                       if ((rhs && !is_glob) || (!rhs && fetch))
+                               goto invalid;
+                       is_glob = 1;
                        llen -= 2;
-                       rlen -= 2;
+               } else if (rhs && is_glob) {
+                       goto invalid;
                }
+
                rs[i].pattern = is_glob;
                rs[i].src = xstrndup(lhs, llen);
 
@@ -446,7 +466,7 @@ static struct refspec *parse_refspec_internal(int nr_refspec, const char **refsp
                        }
                        /*
                         * RHS
-                        * - missing is allowed.
+                        * - missing is ok, and is same as empty.
                         * - empty is ok; it means not to store.
                         * - otherwise it must be a valid looking ref.
                         */
@@ -499,17 +519,32 @@ static struct refspec *parse_refspec_internal(int nr_refspec, const char **refsp
        return rs;
 
  invalid:
+       if (verify) {
+               free(rs);
+               return NULL;
+       }
        die("Invalid refspec '%s'", refspec[i]);
 }
 
+int valid_fetch_refspec(const char *fetch_refspec_str)
+{
+       const char *fetch_refspec[] = { fetch_refspec_str };
+       struct refspec *refspec;
+
+       refspec = parse_refspec_internal(1, fetch_refspec, 1, 1);
+       if (refspec)
+               free(refspec);
+       return !!refspec;
+}
+
 struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
 {
-       return parse_refspec_internal(nr_refspec, refspec, 1);
+       return parse_refspec_internal(nr_refspec, refspec, 1, 0);
 }
 
 struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
 {
-       return parse_refspec_internal(nr_refspec, refspec, 0);
+       return parse_refspec_internal(nr_refspec, refspec, 0, 0);
 }
 
 static int valid_remote_nick(const char *name)
@@ -777,6 +812,26 @@ static struct ref *make_linked_ref(const char *name, struct ref ***tail)
        return ret;
 }
 
+static char *guess_ref(const char *name, struct ref *peer)
+{
+       struct strbuf buf = STRBUF_INIT;
+       unsigned char sha1[20];
+
+       const char *r = resolve_ref(peer->name, sha1, 1, NULL);
+       if (!r)
+               return NULL;
+
+       if (!prefixcmp(r, "refs/heads/"))
+               strbuf_addstr(&buf, "refs/heads/");
+       else if (!prefixcmp(r, "refs/tags/"))
+               strbuf_addstr(&buf, "refs/tags/");
+       else
+               return NULL;
+
+       strbuf_addstr(&buf, name);
+       return strbuf_detach(&buf, NULL);
+}
+
 static int match_explicit(struct ref *src, struct ref *dst,
                          struct ref ***dst_tail,
                          struct refspec *rs,
@@ -785,6 +840,7 @@ static int match_explicit(struct ref *src, struct ref *dst,
        struct ref *matched_src, *matched_dst;
 
        const char *dst_value = rs->dst;
+       char *dst_guess;
 
        if (rs->pattern)
                return errs;
@@ -831,10 +887,15 @@ static int match_explicit(struct ref *src, struct ref *dst,
        case 0:
                if (!memcmp(dst_value, "refs/", 5))
                        matched_dst = make_linked_ref(dst_value, dst_tail);
+               else if((dst_guess = guess_ref(dst_value, matched_src)))
+                       matched_dst = make_linked_ref(dst_guess, dst_tail);
                else
-                       error("dst refspec %s does not match any "
-                             "existing ref on the remote and does "
-                             "not start with refs/.", dst_value);
+                       error("unable to push to unqualified destination: %s\n"
+                             "The destination refspec neither matches an "
+                             "existing ref on the remote nor\n"
+                             "begins with refs/, and we are unable to "
+                             "guess a prefix based on the source ref.",
+                             dst_value);
                break;
        default:
                matched_dst = NULL;