Merge branch 'kn/ref-filter-branch-list'
authorJunio C Hamano <gitster@pobox.com>
Tue, 14 Mar 2017 22:23:20 +0000 (15:23 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 14 Mar 2017 22:23:20 +0000 (15:23 -0700)
"git branch --list" takes the "--abbrev" and "--no-abbrev" options
to control the output of the object name in its "-v"(erbose)
output, but a recent update started ignoring them; this fixes it
before the breakage reaches to any released version.

* kn/ref-filter-branch-list:
branch: honor --abbrev/--no-abbrev in --list mode

1  2 
builtin/branch.c
t/t3200-branch.sh
diff --combined builtin/branch.c
index 291fe90de3f2f76db3add485e7dcf368b9ed35f4,64d3e7d7c9b4bc4c7c2ff8765ddcbabc14875c7b..5d44e36cba0e030cb77461784a65f77c44cb67cd
@@@ -191,20 -191,17 +191,20 @@@ static int delete_branches(int argc, co
        int ret = 0;
        int remote_branch = 0;
        struct strbuf bname = STRBUF_INIT;
 +      unsigned allowed_interpret;
  
        switch (kinds) {
        case FILTER_REFS_REMOTES:
                fmt = "refs/remotes/%s";
                /* For subsequent UI messages */
                remote_branch = 1;
 +              allowed_interpret = INTERPRET_BRANCH_REMOTE;
  
                force = 1;
                break;
        case FILTER_REFS_BRANCHES:
                fmt = "refs/heads/%s";
 +              allowed_interpret = INTERPRET_BRANCH_LOCAL;
                break;
        default:
                die(_("cannot use -a with -d"));
                char *target = NULL;
                int flags = 0;
  
 -              strbuf_branchname(&bname, argv[i]);
 +              strbuf_branchname(&bname, argv[i], allowed_interpret);
                free(name);
                name = mkpathdup(fmt, bname.buf);
  
                        goto next;
                }
  
 -              if (delete_ref(name, is_null_sha1(sha1) ? NULL : sha1,
 +              if (delete_ref(NULL, name, is_null_sha1(sha1) ? NULL : sha1,
                               REF_NODEREF)) {
                        error(remote_branch
                              ? _("Error deleting remote-tracking branch '%s'")
@@@ -338,9 -335,18 +338,18 @@@ static char *build_format(struct ref_fi
                    branch_get_color(BRANCH_COLOR_CURRENT));
  
        if (filter->verbose) {
+               struct strbuf obname = STRBUF_INIT;
+               if (filter->abbrev < 0)
+                       strbuf_addf(&obname, "%%(objectname:short)");
+               else if (!filter->abbrev)
+                       strbuf_addf(&obname, "%%(objectname)");
+               else
+                       strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev);
                strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth);
                strbuf_addf(&local, "%s", branch_get_color(BRANCH_COLOR_RESET));
-               strbuf_addf(&local, " %%(objectname:short=7) ");
+               strbuf_addf(&local, " %s ", obname.buf);
  
                if (filter->verbose > 1)
                        strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
                else
                        strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
  
-               strbuf_addf(&remote, "%s%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s%%(if)%%(symref)%%(then) -> %%(symref:short)"
-                           "%%(else) %%(objectname:short=7) %%(contents:subject)%%(end)",
+               strbuf_addf(&remote, "%s%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s"
+                           "%%(if)%%(symref)%%(then) -> %%(symref:short)"
+                           "%%(else) %s %%(contents:subject)%%(end)",
                            branch_get_color(BRANCH_COLOR_REMOTE), maxwidth, quote_literal_for_format(remote_prefix),
-                           branch_get_color(BRANCH_COLOR_RESET));
+                           branch_get_color(BRANCH_COLOR_RESET), obname.buf);
+               strbuf_release(&obname);
        } else {
                strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
                            branch_get_color(BRANCH_COLOR_RESET));
@@@ -474,15 -482,14 +485,15 @@@ static void rename_branch(const char *o
  
        if (rename_ref(oldref.buf, newref.buf, logmsg.buf))
                die(_("Branch rename failed"));
 -      strbuf_release(&logmsg);
  
        if (recovery)
                warning(_("Renamed a misnamed branch '%s' away"), oldref.buf + 11);
  
 -      if (replace_each_worktree_head_symref(oldref.buf, newref.buf))
 +      if (replace_each_worktree_head_symref(oldref.buf, newref.buf, logmsg.buf))
                die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
  
 +      strbuf_release(&logmsg);
 +
        strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
        strbuf_release(&oldref);
        strbuf_addf(&newsection, "branch.%s", newref.buf + 11);
diff --combined t/t3200-branch.sh
index e36ed3b4e18e22986233f72e016b491c801117db,39bd5ac8fa442ba53bb54fba5296719ce6b7dbc8..9f353c0efce82e4492b0f3ce3acce1756827462e
@@@ -139,12 -139,6 +139,12 @@@ test_expect_success 'git branch -M baz 
        test $(git rev-parse --abbrev-ref HEAD) = bam
  '
  
 +test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
 +      msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
 +      grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
 +      grep "^0\{40\}.*$msg$" .git/logs/HEAD
 +'
 +
  test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
        git checkout master &&
        git worktree add -b baz bazdir &&
@@@ -213,6 -207,31 +213,31 @@@ test_expect_success 'git branch --list 
        test_path_is_missing .git/refs/heads/t
  '
  
+ test_expect_success 'git branch --list -v with --abbrev' '
+       test_when_finished "git branch -D t" &&
+       git branch t &&
+       git branch -v --list t >actual.default &&
+       git branch -v --list --abbrev t >actual.abbrev &&
+       test_cmp actual.default actual.abbrev &&
+       git branch -v --list --no-abbrev t >actual.noabbrev &&
+       git branch -v --list --abbrev=0 t >actual.0abbrev &&
+       test_cmp actual.noabbrev actual.0abbrev &&
+       git branch -v --list --abbrev=36 t >actual.36abbrev &&
+       # how many hexdigits are used?
+       read name objdefault rest <actual.abbrev &&
+       read name obj36 rest <actual.36abbrev &&
+       objfull=$(git rev-parse --verify t) &&
+       # are we really getting abbreviations?
+       test "$obj36" != "$objdefault" &&
+       expr "$obj36" : "$objdefault" >/dev/null &&
+       test "$objfull" != "$obj36" &&
+       expr "$objfull" : "$obj36" >/dev/null
+ '
  test_expect_success 'git branch --column' '
        COLUMNS=81 git branch --column=column >actual &&
        cat >expected <<\EOF &&