Skip to contentSkip to conversation

branch: support configuring --sort via .gitconfig

PATCH In review [email protected] 3 participants 6 messages Aug 15, 2018 – Aug 16, 2018 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 branch: support configuring --sort via .gitconfig

From: Samuel Maftoul <[email protected]>

Add support for configuring default sort ordering for git branches. Command line option will override this configured value, using the exact same syntax.

---
 Documentation/config.txt     |  5 +++++
 Documentation/git-branch.txt |  4 ++++
 builtin/branch.c             | 10 +++++++++-
 t/t3200-branch.sh            | 46 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 64 insertions(+), 1 deletion(-)
Documentation/config.txt +5 -0
index 63365dcf3..82e306d20 100644
@@ -1034,6 +1034,11 @@ 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.
+
 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
Documentation/git-branch.txt +4 -0
index 1072ca0eb..9212a8d5d 100644
@@ -272,6 +272,10 @@ start-point is either a local or remote-tracking branch.
 	full refname (including `refs/...` prefix). This lists
 	detached HEAD (if present) first, then local branches and
 	finally remote-tracking branches.
+	The keys supported are the same as those in `git for-each-ref`.
+	Sort order defaults to the value configured for the `tag.sort`
+	variable if it exists, or lexicographic order otherwise. See
+	linkgit:git-config[1].
 
 
 --points-at <object>::
builtin/branch.c +9 -1
index 4fc55c350..bbd006aab 100644
@@ -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;
 
t/t3200-branch.sh +46 -0

50 lines not on this page · raw message

Signature · 1 line
2.14.3 (Apple Git-98)
#1 lore raw
Quoted 3 lines · On Wed, Aug 15, 2018 at 7:16 AM <[email protected]> wrote:

Add support for configuring default sort ordering for git branches. Command line option will override this configured value, using the exact same syntax.

Your Signed-off-by: is missing. See Documentation/SubmittingPatches.

Documentation/config.txt branch.autoSetupRebase:: · line 1037

Quoted 6 lines
Documentation/config.txt +4 -0
@@ -1034,6 +1034,11 @@ branch.autoSetupRebase::
+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.

I realize that you copied this description from 'tag.sort', thus inherited its existing weakness, but as a reader of this, the first question which popped into my head was "what are the possible <value>s? This description gives no clues and leaves the reader hanging. Better would be either to list the values or point the reader (possibly with a linkgit:) at documentation which does list them.

Documentation/git-branch.txt start-point is either a local or remote-tracking branch. · line 276

Quoted 7 lines
Documentation/git-branch.txt +2 -0
@@ -272,6 +272,10 @@ start-point is either a local or remote-tracking branch.
        full refname (including `refs/...` prefix). This lists
        detached HEAD (if present) first, then local branches and
        finally remote-tracking branches.
+       The keys supported are the same as those in `git for-each-ref`.
+       Sort order defaults to the value configured for the `tag.sort`

Did you mean s/tag/branch/?

Documentation/git-branch.txt start-point is either a local or remote-tracking branch. · line 278

Quoted 2 lines
Documentation/git-branch.txt +2 -0
+       variable if it exists, or lexicographic order otherwise. See
+       linkgit:git-config[1].

Except for the "See linkgit:git-config[1]", isn't this new text mostly duplicating what this item already says? When I look at Documentation/git-branch.txt, I see:

Sort based on the key given. Prefix - to sort in descending 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 full refname (including refs/... prefix). This lists detached HEAD (if present) first, then local branches and finally remote-tracking branches.

I added ** to highlight the existing text which this duplicates.

Quoted 5 lines
t/t3200-branch.sh +3 -0
@@ -1305,4 +1305,50 @@ test_expect_success 'tracking with unexpected .fetch refspec' '
+test_expect_success 'configured committerdate sort' '
+       git init sort &&
+       (

t/t3200-branch.sh test_expect_success 'tracking with unexpected .fetch refspec' ' · line 1317

Quoted 10 lines
t/t3200-branch.sh +10 -0
+               cd sort &&
+               git config branch.sort committerdate &&
+               [...]
+       )
+'
+
+test_expect_success 'option override configured sort' '
+       (
+               cd sort &&
+               git branch --sort=refname >actual &&

I would trust this test more if it explicitly configured "branch.sort" rather than inheriting the value from test(s) above it. That way you wouldn't have to worry about someone later inserting a test above this one which changes or removes the value.

Quoted 8 lines
t/t3200-branch.sh +8 -0
+               cat >expect <<-\EOF &&
+                 a
+               * b
+                 c
+                 master
+               EOF
+               test_cmp expect actual
+       )

t/t3200-branch.sh test_expect_success 'tracking with unexpected .fetch refspec' ' · line 1352

Quoted 10 lines
t/t3200-branch.sh +10 -0
+'
+
+test_expect_success 'invalid sort parameter in configuration' '
+       (
+               cd sort &&
+               git config branch.sort "v:notvalid" &&
+               test_must_fail git branch
+
+       )
+'

Style: Lose the unnecessary blank line.

Thanks.

#2 lore raw

PATCH 1/1 branch: support configuring --sort via .gitconfig

From: Samuel Maftoul <[email protected]>

Add support for configuring default sort ordering for git branches. Command line option will override this configured value, using the exact same syntax.

---
 Documentation/config.txt     |  6 +++++
 Documentation/git-branch.txt |  3 +++
 builtin/branch.c             | 10 +++++++-
 t/t3200-branch.sh            | 47 ++++++++++++++++++++++++++++++++++++
 4 files changed, 65 insertions(+), 1 deletion(-)
Documentation/config.txt +6 -0
index 63365dcf3..1236d1ec9 100644
@@ -1034,6 +1034,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
Documentation/git-branch.txt +3 -0
index 1072ca0eb..1be009a35 100644
@@ -272,6 +272,9 @@ start-point is either a local or remote-tracking branch.
 	full refname (including `refs/...` prefix). This lists
 	detached HEAD (if present) first, then local branches and
 	finally remote-tracking branches.
+	Sort order defaults to the value configured for the `branch.sort`
+	variable if it exists, or lexicographic order otherwise. See
+	linkgit:git-config[1].
 
 
 --points-at <object>::
builtin/branch.c +9 -1
index 4fc55c350..bbd006aab 100644
@@ -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;
 
t/t3200-branch.sh +47 -0

51 lines not on this page · raw message

Signature · 1 line
2.18.0
#3 lore raw

Documentation/config.txt branch.autoSetupRebase:: · line 1038

Quoted 13 lines · On Thu, Aug 16, 2018 at 4:06 AM <[email protected]> wrote:

Add support for configuring default sort ordering for git branches. Command line option will override this configured value, using the exact same syntax.

---

Documentation/config.txt +5 -0
@@ -1034,6 +1034,12 @@ branch.autoSetupRebase::
+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.

Thanks for adding some information about what values are valid for this config variable.

Documentation/git-branch.txt start-point is either a local or remote-tracking branch. · line 277

Quoted 8 lines
Documentation/git-branch.txt +3 -0
@@ -272,6 +272,9 @@ start-point is either a local or remote-tracking branch.
        full refname (including `refs/...` prefix). This lists
        detached HEAD (if present) first, then local branches and
        finally remote-tracking branches.
+       Sort order defaults to the value configured for the `branch.sort`
+       variable if it exists, or lexicographic order otherwise. See
+       linkgit:git-config[1].

This change still has problems pointed out by my earlier review[1]. The existing text in git-branch.txt says:

Sort order defaults to sorting based on the
full refname...

Which is both redundant with the new text you add and seems to say something rather different.

t/t3200-branch.sh test_expect_success 'tracking with unexpected .fetch refspec' ' · line 1312

Quoted 10 lines
t/t3200-branch.sh +8 -0
@@ -1305,4 +1305,51 @@ test_expect_success 'tracking with unexpected .fetch refspec' '
+test_expect_success 'invalid sort parameter in configuration' '
+       (
+               cd sort &&
+               git config branch.sort "v:notvalid" &&
+               test_must_fail git branch
+
+       )
+'

See my earlier review[1] regarding the unnecessary blank line in this new test.

[1]: https://public-inbox.org/git/CAPig+cSUy7rFwhmJ1SFHsAjPkoWparfY6wAjV=6kJyUL3SLQUw@mail.gmail.com/

#4 lore raw

PATCH 1/1 branch: support configuring --sort via .gitconfig

From: Samuel Maftoul <[email protected]>

Add support for configuring default sort ordering for git branches. Command line option will override this configured value, using the exact same syntax.

---
 Documentation/config.txt     |  6 +++++
 Documentation/git-branch.txt |  5 ++--
 builtin/branch.c             | 10 +++++++-
 t/t3200-branch.sh            | 46 ++++++++++++++++++++++++++++++++++++
 4 files changed, 64 insertions(+), 3 deletions(-)
Documentation/config.txt +6 -0
index fd8d27e76..7f7a50123 100644
@@ -1039,6 +1039,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
Documentation/git-branch.txt +3 -2
index 1072ca0eb..9767b2b48 100644
@@ -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>::
builtin/branch.c +9 -1
index 4fc55c350..bbd006aab 100644
@@ -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;
 
t/t3200-branch.sh +46 -0

50 lines not on this page · raw message

Signature · 1 line
2.18.0
#5 lore raw
Quoted 5 lines · [email protected] writes:

From: Samuel Maftoul <[email protected]>

Add support for configuring default sort ordering for git branches. Command line option will override this configured value, using the exact same syntax.

Using the exact same syntax as ...?

Quoted 13 lines
---
 Documentation/config.txt     |  6 +++++
 Documentation/git-branch.txt |  5 ++--
 builtin/branch.c             | 10 +++++++-
 t/t3200-branch.sh            | 46 ++++++++++++++++++++++++++++++++++++
 4 files changed, 64 insertions(+), 3 deletions(-)
Documentation/config.txt +0 -0
index fd8d27e76..7f7a50123 100644

Documentation/config.txt branch.autoSetupRebase:: · line 1046

Quoted 8 lines
Documentation/config.txt +5 -0
@@ -1039,6 +1039,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.

OK, the answer to the above question is "same syntax as used for the value of the branch.sort configuration variable".

Quoted 5 lines
Documentation/git-branch.txt +0 -0
index 1072ca0eb..9767b2b48 100644

Documentation/git-branch.txt start-point is either a local or remote-tracking branch. · line 275

Quoted 10 lines
Documentation/git-branch.txt +3 -2
@@ -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].

OK.

Quoted 6 lines
builtin/branch.c +0 -0
index 4fc55c350..bbd006aab 100644
@@ -74,6 +74,14 @@ define_list_config_array(color_branch_slots);
 static int git_branch_config(const char *var, const char *value, void *cb)

builtin/branch.c define_list_config_array(color_branch_slots); · line 84

Quoted 10 lines
builtin/branch.c +8 -0
 {
 	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;
+	}

Hmph. It is unfortunate that "don't feed me NULL" check is not in parse_ref_sorting() but is in parse_opt_ref_sorting(). But that is a separate low-hanging fruit. The code we see here is correct.

Will queue. Thanks.