Code

mergetool--lib: specialize diff options for emerge and ecmerge
[git.git] / builtin-branch.c
index 07a440eebacc1aa4e944874704bedfed7efb00e8..91098ca9b106239916af000cb54a4bf09629e6b6 100644 (file)
@@ -121,11 +121,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
                        die("Couldn't look up commit object for HEAD");
        }
        for (i = 0; i < argc; i++, strbuf_release(&bname)) {
-               int len = strlen(argv[i]);
-
-               if (interpret_nth_last_branch(argv[i], &bname) != len)
-                       strbuf_add(&bname, argv[i], len);
-
+               strbuf_branchname(&bname, argv[i]);
                if (kinds == REF_LOCAL_BRANCH && !strcmp(head, bname.buf)) {
                        error("Cannot delete the branch '%s' "
                              "which you are currently on.", bname.buf);
@@ -305,19 +301,30 @@ static int ref_cmp(const void *r1, const void *r2)
        return strcmp(c1->name, c2->name);
 }
 
-static void fill_tracking_info(struct strbuf *stat, const char *branch_name)
+static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
+               int show_upstream_ref)
 {
        int ours, theirs;
        struct branch *branch = branch_get(branch_name);
 
-       if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs))
+       if (!stat_tracking_info(branch, &ours, &theirs)) {
+               if (branch && branch->merge && branch->merge[0]->dst &&
+                   show_upstream_ref)
+                       strbuf_addf(stat, "[%s] ",
+                           shorten_unambiguous_ref(branch->merge[0]->dst, 0));
                return;
+       }
+
+       strbuf_addch(stat, '[');
+       if (show_upstream_ref)
+               strbuf_addf(stat, "%s: ",
+                       shorten_unambiguous_ref(branch->merge[0]->dst, 0));
        if (!ours)
-               strbuf_addf(stat, "[behind %d] ", theirs);
+               strbuf_addf(stat, "behind %d] ", theirs);
        else if (!theirs)
-               strbuf_addf(stat, "[ahead %d] ", ours);
+               strbuf_addf(stat, "ahead %d] ", ours);
        else
-               strbuf_addf(stat, "[ahead %d, behind %d] ", ours, theirs);
+               strbuf_addf(stat, "ahead %d, behind %d] ", ours, theirs);
 }
 
 static int matches_merge_filter(struct commit *commit)
@@ -383,7 +390,7 @@ 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);
+                       fill_tracking_info(&stat, item->name, verbose > 1);
 
                strbuf_addf(&out, " %s %s%s",
                        find_unique_abbrev(item->commit->object.sha1, abbrev),
@@ -468,22 +475,27 @@ static void rename_branch(const char *oldname, const char *newname, int force)
        struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
        unsigned char sha1[20];
        struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
+       int recovery = 0;
 
        if (!oldname)
                die("cannot rename the current branch while not on any.");
 
-       strbuf_addf(&oldref, "refs/heads/%s", oldname);
-
-       if (check_ref_format(oldref.buf))
-               die("Invalid branch name: %s", oldref.buf);
-
-       strbuf_addf(&newref, "refs/heads/%s", newname);
+       if (strbuf_check_branch_ref(&oldref, oldname)) {
+               /*
+                * Bad name --- this could be an attempt to rename a
+                * ref that we used to allow to be created by accident.
+                */
+               if (resolve_ref(oldref.buf, sha1, 1, NULL))
+                       recovery = 1;
+               else
+                       die("Invalid branch name: '%s'", oldname);
+       }
 
-       if (check_ref_format(newref.buf))
-               die("Invalid branch name: %s", newref.buf);
+       if (strbuf_check_branch_ref(&newref, newname))
+               die("Invalid branch name: '%s'", newname);
 
        if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
-               die("A branch named '%s' already exists.", newname);
+               die("A branch named '%s' already exists.", newref.buf + 11);
 
        strbuf_addf(&logmsg, "Branch: renamed %s to %s",
                 oldref.buf, newref.buf);
@@ -492,6 +504,9 @@ static void rename_branch(const char *oldname, const char *newname, int force)
                die("Branch rename failed");
        strbuf_release(&logmsg);
 
+       if (recovery)
+               warning("Renamed a misnamed branch '%s' away", oldref.buf + 11);
+
        /* no need to pass logmsg here as HEAD didn't really move */
        if (!strcmp(oldname, head) && create_symref("HEAD", newref.buf, NULL))
                die("Branch renamed to %s, but HEAD is not updated!", newname);