]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'sm/branch-sort-config'
authorJunio C Hamano <gitster@pobox.com>
Mon, 27 Aug 2018 21:33:42 +0000 (14:33 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 Aug 2018 21:33:42 +0000 (14:33 -0700)
"git branch --list" learned to take the default sort order from the
'branch.sort' configuration variable, just like "git tag --list"
pays attention to 'tag.sort'.

* sm/branch-sort-config:
  branch: support configuring --sort via .gitconfig

Documentation/config.txt
Documentation/git-branch.txt
builtin/branch.c
t/t3200-branch.sh

index 136a345dd290ccccadef47aacaa6b0a6edeba732..9f2deae463a8b6cecd5a5588b4f1f15eb4240b6f 100644 (file)
@@ -1054,6 +1054,12 @@ branch.autoSetupRebase::
        branch to track another branch.
        This option defaults to never.
 
+branch.sort::
+       This variable controls the sort ordering of branches when displayed by
+       linkgit:git-branch[1]. Without the "--sort=<value>" option provided, the
+       value of this variable will be used as the default.
+       See linkgit:git-for-each-ref[1] field names for valid values.
+
 branch.<name>.remote::
        When on branch <name>, it tells 'git fetch' and 'git push'
        which remote to fetch from/push to.  The remote to push to
index 1072ca0eb61e45f5049fedfd24a7512ff81ee957..9767b2b483dbe634db1a700e4b3529593f8189ec 100644 (file)
@@ -268,10 +268,11 @@ start-point is either a local or remote-tracking branch.
        order of the value. You may use the --sort=<key> option
        multiple times, in which case the last key becomes the primary
        key. The keys supported are the same as those in `git
-       for-each-ref`. Sort order defaults to sorting based on the
+       for-each-ref`. Sort order defaults to the value configured for the
+       `branch.sort` variable if exists, or to sorting based on the
        full refname (including `refs/...` prefix). This lists
        detached HEAD (if present) first, then local branches and
-       finally remote-tracking branches.
+       finally remote-tracking branches. See linkgit:git-config[1].
 
 
 --points-at <object>::
index 4fc55c3508c2fff3e92e6235ed35ba582caf4b15..bbd006aab4b4798b7e86530fe3562c753e879bc1 100644 (file)
@@ -74,6 +74,14 @@ define_list_config_array(color_branch_slots);
 static int git_branch_config(const char *var, const char *value, void *cb)
 {
        const char *slot_name;
+       struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
+
+       if (!strcmp(var, "branch.sort")) {
+               if (!value)
+                       return config_error_nonbool(var);
+               parse_ref_sorting(sorting_tail, value);
+               return 0;
+       }
 
        if (starts_with(var, "column."))
                return git_column_config(var, value, "branch", &colopts);
@@ -653,7 +661,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
        if (argc == 2 && !strcmp(argv[1], "-h"))
                usage_with_options(builtin_branch_usage, options);
 
-       git_config(git_branch_config, NULL);
+       git_config(git_branch_config, sorting_tail);
 
        track = git_branch_track;
 
index dbca665da41908e7e3014564dc8ddb901030715b..93f21ab078080d00100cd63f6b5ee5cdfd636d29 100755 (executable)
@@ -1305,4 +1305,50 @@ test_expect_success 'tracking with unexpected .fetch refspec' '
        )
 '
 
+test_expect_success 'configured committerdate sort' '
+       git init sort &&
+       (
+               cd sort &&
+               git config branch.sort committerdate &&
+               test_commit initial &&
+               git checkout -b a &&
+               test_commit a &&
+               git checkout -b c &&
+               test_commit c &&
+               git checkout -b b &&
+               test_commit b &&
+               git branch >actual &&
+               cat >expect <<-\EOF &&
+                 master
+                 a
+                 c
+               * b
+               EOF
+               test_cmp expect actual
+       )
+'
+
+test_expect_success 'option override configured sort' '
+       (
+               cd sort &&
+               git config branch.sort committerdate &&
+               git branch --sort=refname >actual &&
+               cat >expect <<-\EOF &&
+                 a
+               * b
+                 c
+                 master
+               EOF
+               test_cmp expect actual
+       )
+'
+
+test_expect_success 'invalid sort parameter in configuration' '
+       (
+               cd sort &&
+               git config branch.sort "v:notvalid" &&
+               test_must_fail git branch
+       )
+'
+
 test_done