branch: support configuring --sort via .gitconfig

Add support for configuring default sort ordering for git branches. Command line option will override this configured value, using the exact same syntax. Signed-off-by: Samuel Maftoul <samuel.maftoul@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Samuel Maftoul committed Aug 16, 2018 at 11:35 UTC 560ae1c164ad040a389ccc47834dce8c15447294
4 files changed +64 -3
Documentation/config.txt
+6
@@ -1039,6 +1039,12 @@ branch.autoSetupRebase::
1039 branch to track another branch.
1040 This option defaults to never.
1041
1042 +branch.sort::
1043 + This variable controls the sort ordering of branches when displayed by
1044 + linkgit:git-branch[1]. Without the "--sort=<value>" option provided, the
1045 + value of this variable will be used as the default.
1046 + See linkgit:git-for-each-ref[1] field names for valid values.
1047 +
1048 branch.<name>.remote::
1049 When on branch <name>, it tells 'git fetch' and 'git push'
1050 which remote to fetch from/push to. The remote to push to
Documentation/git-branch.txt
+3 -2
@@ -268,10 +268,11 @@ start-point is either a local or remote-tracking branch.
268 order of the value. You may use the --sort=<key> option
269 multiple times, in which case the last key becomes the primary
270 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
273 full refname (including `refs/...` prefix). This lists
274 detached HEAD (if present) first, then local branches and
274 - finally remote-tracking branches.
275 + finally remote-tracking branches. See linkgit:git-config[1].
276
277
278 --points-at <object>::
builtin/branch.c
+9 -1
@@ -74,6 +74,14 @@ define_list_config_array(color_branch_slots);
74 static int git_branch_config(const char *var, const char *value, void *cb)
75 {
76 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 + }
85
86 if (starts_with(var, "column."))
87 return git_column_config(var, value, "branch", &colopts);
@@ -653,7 +661,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
661 if (argc == 2 && !strcmp(argv[1], "-h"))
662 usage_with_options(builtin_branch_usage, options);
663
656 - git_config(git_branch_config, NULL);
664 + git_config(git_branch_config, sorting_tail);
665
666 track = git_branch_track;
667
t/t3200-branch.sh
+46
@@ -1305,4 +1305,50 @@ test_expect_success 'tracking with unexpected .fetch refspec' '
1305 )
1306 '
1307
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 +
1354 test_done