Code

Avoid segfault with 'git branch' when the HEAD is detached
[git.git] / builtin-branch.c
index 02fa38fd3b13b5c17aa23edbfd79f91a052adf76..6106a1abd5d5a001a44d06a66bc0ea2e47f5247d 100644 (file)
@@ -181,7 +181,8 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
 
 struct ref_item {
        char *name;
-       unsigned int kind;
+       char *dest;
+       unsigned int kind, len;
        struct commit *commit;
 };
 
@@ -193,19 +194,18 @@ struct ref_list {
        int kinds;
 };
 
-static int has_commit(struct commit *commit, struct commit_list *with_commit)
+static char *resolve_symref(const char *src, const char *prefix)
 {
-       if (!with_commit)
-               return 1;
-       while (with_commit) {
-               struct commit *other;
-
-               other = with_commit->item;
-               with_commit = with_commit->next;
-               if (in_merge_bases(other, &commit, 1))
-                       return 1;
-       }
-       return 0;
+       unsigned char sha1[20];
+       int flag;
+       const char *dst, *cp;
+
+       dst = resolve_ref(src, sha1, 0, &flag);
+       if (!(dst && (flag & REF_ISSYMREF)))
+               return NULL;
+       if (prefix && (cp = skip_prefix(dst, prefix)))
+               dst = cp;
+       return xstrdup(dst);
 }
 
 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
@@ -213,17 +213,28 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
        struct ref_list *ref_list = (struct ref_list*)(cb_data);
        struct ref_item *newitem;
        struct commit *commit;
-       int kind;
-       int len;
+       int kind, i;
+       const char *prefix, *orig_refname = refname;
+
+       static struct {
+               int kind;
+               const char *prefix;
+               int pfxlen;
+       } ref_kind[] = {
+               { REF_LOCAL_BRANCH, "refs/heads/", 11 },
+               { REF_REMOTE_BRANCH, "refs/remotes/", 13 },
+       };
 
        /* Detect kind */
-       if (!prefixcmp(refname, "refs/heads/")) {
-               kind = REF_LOCAL_BRANCH;
-               refname += 11;
-       } else if (!prefixcmp(refname, "refs/remotes/")) {
-               kind = REF_REMOTE_BRANCH;
-               refname += 13;
-       } else
+       for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
+               prefix = ref_kind[i].prefix;
+               if (strncmp(refname, prefix, ref_kind[i].pfxlen))
+                       continue;
+               kind = ref_kind[i].kind;
+               refname += ref_kind[i].pfxlen;
+               break;
+       }
+       if (ARRAY_SIZE(ref_kind) <= i)
                return 0;
 
        commit = lookup_commit_reference_gently(sha1, 1);
@@ -231,7 +242,7 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
                return error("branch '%s' does not point at a commit", refname);
 
        /* Filter with with_commit if specified */
-       if (!has_commit(commit, ref_list->with_commit))
+       if (!is_descendant_of(commit, ref_list->with_commit))
                return 0;
 
        /* Don't add types the caller doesn't want */
@@ -254,9 +265,14 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
        newitem->name = xstrdup(refname);
        newitem->kind = kind;
        newitem->commit = commit;
-       len = strlen(newitem->name);
-       if (len > ref_list->maxwidth)
-               ref_list->maxwidth = len;
+       newitem->len = strlen(refname);
+       newitem->dest = resolve_symref(orig_refname, prefix);
+       /* adjust for "remotes/" */
+       if (newitem->kind == REF_REMOTE_BRANCH &&
+           ref_list->kinds != REF_REMOTE_BRANCH)
+               newitem->len += 8;
+       if (newitem->len > ref_list->maxwidth)
+               ref_list->maxwidth = newitem->len;
 
        return 0;
 }
@@ -265,8 +281,10 @@ static void free_ref_list(struct ref_list *ref_list)
 {
        int i;
 
-       for (i = 0; i < ref_list->index; i++)
+       for (i = 0; i < ref_list->index; i++) {
                free(ref_list->list[i].name);
+               free(ref_list->list[i].dest);
+       }
        free(ref_list->list);
 }
 
@@ -307,11 +325,12 @@ static int matches_merge_filter(struct commit *commit)
 }
 
 static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
-                          int abbrev, int current)
+                          int abbrev, int current, char *prefix)
 {
        char c;
        int color;
        struct commit *commit = item->commit;
+       struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
 
        if (!matches_merge_filter(commit))
                return;
@@ -334,7 +353,18 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
                color = COLOR_BRANCH_CURRENT;
        }
 
-       if (verbose) {
+       strbuf_addf(&name, "%s%s", prefix, item->name);
+       if (verbose)
+               strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
+                           maxwidth, name.buf,
+                           branch_get_color(COLOR_BRANCH_RESET));
+       else
+               strbuf_addf(&out, "%c %s%s%s", c, branch_get_color(color),
+                           name.buf, branch_get_color(COLOR_BRANCH_RESET));
+
+       if (item->dest)
+               strbuf_addf(&out, " -> %s", item->dest);
+       else if (verbose) {
                struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
                const char *sub = " **** invalid ref ****";
 
@@ -348,28 +378,25 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
                if (item->kind == REF_LOCAL_BRANCH)
                        fill_tracking_info(&stat, item->name);
 
-               printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color),
-                      maxwidth, item->name,
-                      branch_get_color(COLOR_BRANCH_RESET),
-                      find_unique_abbrev(item->commit->object.sha1, abbrev),
-                      stat.buf, sub);
+               strbuf_addf(&out, " %s %s%s",
+                       find_unique_abbrev(item->commit->object.sha1, abbrev),
+                       stat.buf, sub);
                strbuf_release(&stat);
                strbuf_release(&subject);
-       } else {
-               printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
-                      branch_get_color(COLOR_BRANCH_RESET));
        }
+       printf("%s\n", out.buf);
+       strbuf_release(&name);
+       strbuf_release(&out);
 }
 
 static int calc_maxwidth(struct ref_list *refs)
 {
-       int i, l, w = 0;
+       int i, w = 0;
        for (i = 0; i < refs->index; i++) {
                if (!matches_merge_filter(refs->list[i].commit))
                        continue;
-               l = strlen(refs->list[i].name);
-               if (l > w)
-                       w = l;
+               if (refs->list[i].len > w)
+                       w = refs->list[i].len;
        }
        return w;
 }
@@ -401,14 +428,17 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
        qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
 
        detached = (detached && (kinds & REF_LOCAL_BRANCH));
-       if (detached && head_commit && has_commit(head_commit, with_commit)) {
+       if (detached && head_commit &&
+           is_descendant_of(head_commit, with_commit)) {
                struct ref_item item;
                item.name = xstrdup("(no branch)");
+               item.len = strlen(item.name);
                item.kind = REF_LOCAL_BRANCH;
+               item.dest = NULL;
                item.commit = head_commit;
                if (strlen(item.name) > ref_list.maxwidth)
                        ref_list.maxwidth = strlen(item.name);
-               print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1);
+               print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1, "");
                free(item.name);
        }
 
@@ -416,8 +446,11 @@ static void print_ref_list(int kinds, int detached, int verbose, int abbrev, str
                int current = !detached &&
                        (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
                        !strcmp(ref_list.list[i].name, head);
+               char *prefix = (kinds != REF_REMOTE_BRANCH &&
+                               ref_list.list[i].kind == REF_REMOTE_BRANCH)
+                               ? "remotes/" : "";
                print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
-                              abbrev, current);
+                              abbrev, current, prefix);
        }
 
        free_ref_list(&ref_list);
@@ -466,22 +499,6 @@ static void rename_branch(const char *oldname, const char *newname, int force)
        strbuf_release(&newsection);
 }
 
-static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset)
-{
-       unsigned char sha1[20];
-       struct commit *commit;
-
-       if (!arg)
-               return -1;
-       if (get_sha1(arg, sha1))
-               die("malformed object name %s", arg);
-       commit = lookup_commit_reference(sha1);
-       if (!commit)
-               die("no such commit %s", arg);
-       commit_list_insert(commit, opt->value);
-       return 0;
-}
-
 static int opt_parse_merge_filter(const struct option *opt, const char *arg, int unset)
 {
        merge_filter = ((opt->long_name[0] == 'n')
@@ -517,13 +534,13 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
                        OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
                        "print only branches that contain the commit",
                        PARSE_OPT_LASTARG_DEFAULT,
-                       opt_parse_with_commit, (intptr_t)"HEAD",
+                       parse_opt_with_commit, (intptr_t)"HEAD",
                },
                {
                        OPTION_CALLBACK, 0, "with", &with_commit, "commit",
                        "print only branches that contain the commit",
                        PARSE_OPT_HIDDEN | PARSE_OPT_LASTARG_DEFAULT,
-                       opt_parse_with_commit, (intptr_t) "HEAD",
+                       parse_opt_with_commit, (intptr_t) "HEAD",
                },
                OPT__ABBREV(&abbrev),