Skip to content

Commit d89db6f

Browse files
committed
Merge branch 'sm/branch-sort-config'
"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
2 parents 9b73732 + 560ae1c commit d89db6f

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,12 @@ branch.autoSetupRebase::
10541054
branch to track another branch.
10551055
This option defaults to never.
10561056

1057+
branch.sort::
1058+
This variable controls the sort ordering of branches when displayed by
1059+
linkgit:git-branch[1]. Without the "--sort=<value>" option provided, the
1060+
value of this variable will be used as the default.
1061+
See linkgit:git-for-each-ref[1] field names for valid values.
1062+
10571063
branch.<name>.remote::
10581064
When on branch <name>, it tells 'git fetch' and 'git push'
10591065
which remote to fetch from/push to. The remote to push to

Documentation/git-branch.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,11 @@ start-point is either a local or remote-tracking branch.
268268
order of the value. You may use the --sort=<key> option
269269
multiple times, in which case the last key becomes the primary
270270
key. The keys supported are the same as those in `git
271-
for-each-ref`. Sort order defaults to sorting based on the
271+
for-each-ref`. Sort order defaults to the value configured for the
272+
`branch.sort` variable if exists, or to sorting based on the
272273
full refname (including `refs/...` prefix). This lists
273274
detached HEAD (if present) first, then local branches and
274-
finally remote-tracking branches.
275+
finally remote-tracking branches. See linkgit:git-config[1].
275276

276277

277278
--points-at <object>::

builtin/branch.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ define_list_config_array(color_branch_slots);
7474
static int git_branch_config(const char *var, const char *value, void *cb)
7575
{
7676
const char *slot_name;
77+
struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
78+
79+
if (!strcmp(var, "branch.sort")) {
80+
if (!value)
81+
return config_error_nonbool(var);
82+
parse_ref_sorting(sorting_tail, value);
83+
return 0;
84+
}
7785

7886
if (starts_with(var, "column."))
7987
return git_column_config(var, value, "branch", &colopts);
@@ -653,7 +661,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
653661
if (argc == 2 && !strcmp(argv[1], "-h"))
654662
usage_with_options(builtin_branch_usage, options);
655663

656-
git_config(git_branch_config, NULL);
664+
git_config(git_branch_config, sorting_tail);
657665

658666
track = git_branch_track;
659667

t/t3200-branch.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,4 +1305,50 @@ test_expect_success 'tracking with unexpected .fetch refspec' '
13051305
)
13061306
'
13071307

1308+
test_expect_success 'configured committerdate sort' '
1309+
git init sort &&
1310+
(
1311+
cd sort &&
1312+
git config branch.sort committerdate &&
1313+
test_commit initial &&
1314+
git checkout -b a &&
1315+
test_commit a &&
1316+
git checkout -b c &&
1317+
test_commit c &&
1318+
git checkout -b b &&
1319+
test_commit b &&
1320+
git branch >actual &&
1321+
cat >expect <<-\EOF &&
1322+
master
1323+
a
1324+
c
1325+
* b
1326+
EOF
1327+
test_cmp expect actual
1328+
)
1329+
'
1330+
1331+
test_expect_success 'option override configured sort' '
1332+
(
1333+
cd sort &&
1334+
git config branch.sort committerdate &&
1335+
git branch --sort=refname >actual &&
1336+
cat >expect <<-\EOF &&
1337+
a
1338+
* b
1339+
c
1340+
master
1341+
EOF
1342+
test_cmp expect actual
1343+
)
1344+
'
1345+
1346+
test_expect_success 'invalid sort parameter in configuration' '
1347+
(
1348+
cd sort &&
1349+
git config branch.sort "v:notvalid" &&
1350+
test_must_fail git branch
1351+
)
1352+
'
1353+
13081354
test_done

0 commit comments

Comments
 (0)