Skip to contentSkip to conversation

tag: support --sort=version

PATCH Superseded Nguyễn Thái Ngọc Duy 4 participants 25 messages Jun 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tag: support --sort=version

--sort=version sorts tags as versions. GNU extension's strverscmp is used and no real compat implementation is provided so this is Linux only.

---
I didn't know that coreutils' sort is simply a wrapper of strverscmp. With that GNU extension, implementing --sort is easy. Mac and Windows are welcome to reimplement strverscmp (of rip it off glibc).

 Documentation/git-tag.txt |  4 ++++
 builtin/tag.c             | 49 ++++++++++++++++++++++++++++++++++++++++++-----
 git-compat-util.h         |  7 +++++++
 3 files changed, 55 insertions(+), 5 deletions(-)
Documentation/git-tag.txt +4 -0
index 404257d..fc23dc0 100644
@@ -95,6 +95,10 @@ OPTIONS
 	using fnmatch(3)).  Multiple patterns may be given; if any of
 	them matches, the tag is shown.
 
+--sort=<type>::
+	Sort in a specific order. Supported type is "version". Prepend
+	"-" to revert sort order.
+
 --column[=<options>]::
 --no-column::
 	Display tag listing in columns. See configuration variable
builtin/tag.c +44 -5

94 lines not on this page · raw message

git-compat-util.h +7 -0
index cbd86c3..22089e9 100644
@@ -721,4 +721,11 @@ void warn_on_inaccessible(const char *path);
 /* Get the passwd entry for the UID of the current process. */
 struct passwd *xgetpwuid_self(void);
 
+#ifndef __GNU_LIBRARY__
+static inline int strverscmp(const char *s1, const char *s2)
+{
+	die("strverscmp() not supported");
+}
+#endif
+
 #endif
Signature · 1 line
1.9.0.40.gaa8c3ea
#1 lore raw
Quoted 2 lines · Nguyễn Thái Ngọc Duy

--sort=version sorts tags as versions. GNU extension's strverscmp is used and no real compat implementation is provided so this is Linux only.

Sounds like a good goal.

I wonder, if we were to merge the for-each-ref and tag implementations[1], how this would integrate with for-each-ref's sorting. It can sort on arbitrary fields like "--sort=-*authordate". I think given the syntax you provide, this would fall out naturally as just another key (albeit a slightly magical one, as it is building on the %(refname:short) field but using a different comparator).

Would we ever want to version-sort any other field? Perhaps %(content:subject) for a tag? I'm not sure what would be the most natural way to specify that. Maybe "--sort=version:content:subject", with just "--version" as a synonym for "version:refname:short".

We don't need to do any of that immediately. This is mostly just me thinking aloud, to make sure we do not paint ourselves into a corner compatibility-wise.

-Peff

[1] I have patches which I really need to polish and send out that combine the ref-selection code (so tag, branch, and for-each-ref all know "--merged", "--contains", etc). I'd really like to combine the sorting and formatting code, too, so everybody can use "--sort" and "--format" with all of the associated fields.

author
#2 lore raw
Quoted 14 lines · Jeff King
Quoted 2 lines · Nguyễn Thái Ngọc Duy

--sort=version sorts tags as versions. GNU extension's strverscmp is used and no real compat implementation is provided so this is Linux only.

Sounds like a good goal.

I wonder, if we were to merge the for-each-ref and tag implementations[1], how this would integrate with for-each-ref's sorting. It can sort on arbitrary fields like "--sort=-*authordate". I think given the syntax you provide, this would fall out naturally as just another key (albeit a slightly magical one, as it is building on the %(refname:short) field but using a different comparator).

Quoted 4 lines

Would we ever want to version-sort any other field? Perhaps %(content:subject) for a tag? I'm not sure what would be the most natural way to specify that. Maybe "--sort=version:content:subject", with just "--version" as a synonym for "version:refname:short".

If f-e-r and tag share code and syntax then we could use another letter for this sort type (like we use '-' to reverse sort order). I think that would make the implementation easier as well. So we could have --sort=.refname for version sorting refname, --sort=-.refname for reversed version sort, and sort=-refname for reverse alphabetical sort.

Quoted 3 lines

We don't need to do any of that immediately. This is mostly just me thinking aloud, to make sure we do not paint ourselves into a corner compatibility-wise.

Good thinking. If you plan to use the exact same sort syntax f-e-r uses now, pick a letter (the dot I used above is probably not the best), I'll rewrite this patch to use the same syntax.

Quoted 8 lines

-Peff

[1] I have patches which I really need to polish and send out that combine the ref-selection code (so tag, branch, and for-each-ref all know "--merged", "--contains", etc). I'd really like to combine the sorting and formatting code, too, so everybody can use "--sort" and "--format" with all of the associated fields.

Signature · 1 line
Duy
#3 lore raw
Quoted 7 lines · Duy Nguyen
Quoted 3 lines

We don't need to do any of that immediately. This is mostly just me thinking aloud, to make sure we do not paint ourselves into a corner compatibility-wise.

Good thinking. If you plan to use the exact same sort syntax f-e-r uses now, pick a letter (the dot I used above is probably not the best), I'll rewrite this patch to use the same syntax.

I think I actually prefer the full word "version", as you have already. It's clear what it means, and we can extend the syntax generally to:

--sort=[-][comparison:]field

like:

--sort=-version:subject

for descending version-sort by subject. And then as a special-case convenience, make "version" without a field default to "version:refname". There's no ambiguity because the set of comparison names and field-names is fixed, and we know there is no overlap.

If want to, we can _also_ give a one-letter abbreviation to the comparison field, like:

--sort=v:subject

but that is not necessary.

So I think your patch is fine as-is. It is perhaps a little funny to start with the special case and only implement the general case later, but:

1. We would want the special case eventually, because it is the most natural thing to type, and pretty clearly the most common use-case.

2. We may not ever even end up with the general case. This is just about making sure that if we _do_ add it, that it fits syntactically with "--sort=version".

-Peff

author
#4 lore raw
Quoted 2 lines · Jeff King

I think I actually prefer the full word "version", as you have already. It's clear what it means, and we can extend the syntax generally to:

Agreed. It's hard to find a letter that reminds you about "version".

Quoted 12 lines

--sort=[-][comparison:]field

like:

--sort=-version:subject

for descending version-sort by subject. And then as a special-case convenience, make "version" without a field default to "version:refname". There's no ambiguity because the set of comparison names and field-names is fixed, and we know there is no overlap.

Quoted 6 lines

If want to, we can _also_ give a one-letter abbreviation to the comparison field, like:

--sort=v:subject

but that is not necessary.

Why not reversed order? So its syntax could be

[ "-" ] FIELD [ ":" [ "version" | "v" ] ]

It fits better to current f-e-r syntax where modifiers are after the colon. And it avoids the possibility that someone adds field "version" and we can't tell what "version" is what.

Signature · 1 line
Duy
#5 lore raw
Quoted 9 lines · Duy Nguyen
Quoted 1 line

--sort=[-][comparison:]field

[...]
Why not reversed order? So its syntax could be

[ "-" ] FIELD [ ":" [ "version" | "v" ] ]

It fits better to current f-e-r syntax where modifiers are after the colon. And it avoids the possibility that someone adds field "version" and we can't tell what "version" is what.

I find my version a bit more obvious, for two reasons:

1. "version" here is not a modifier of the field name, it is a modifier of the sort. You cannot use it in non-sort contexts (like --format), and you cannot order it like other modifiers (you cannot say "refname:version:short", only "refname:short:version").

2. There are actually two sort-modifiers: "-" for ordering, and then a comparator. In your proposal, they are split, whereas in mine, they are next to each other.

-Peff

author
#6 lore raw
Quoted 12 lines · Jeff King
Quoted 9 lines · Duy Nguyen
Quoted 1 line

--sort=[-][comparison:]field

[...]
Why not reversed order? So its syntax could be

[ "-" ] FIELD [ ":" [ "version" | "v" ] ]

It fits better to current f-e-r syntax where modifiers are after the colon. And it avoids the possibility that someone adds field "version" and we can't tell what "version" is what.

Quoted 6 lines

I find my version a bit more obvious, for two reasons:

1. "version" here is not a modifier of the field name, it is a modifier of the sort. You cannot use it in non-sort contexts (like --format), and you cannot order it like other modifiers (you cannot say "refname:version:short", only "refname:short:version").

Or you can read it like "type cast this field as a version", where sorting is affected but formatting, not so much. So you can specify it with --format (but it's no-op, unless we find a fancy way to color versions). I don't see a problem with accepting both refname:version:short and refname:short:version in future for-each-ref either. It will be the first time we accept multiple modifiers though.

Quoted 3 lines

2. There are actually two sort-modifiers: "-" for ordering, and then a comparator. In your proposal, they are split, whereas in mine, they are next to each other.

Signature · 1 line
Duy
#7 lore raw
In reply to Duy Nguyen
Quoted 7 lines · Duy Nguyen

Why not reversed order? So its syntax could be

[ "-" ] FIELD [ ":" [ "version" | "v" ] ]

It fits better to current f-e-r syntax where modifiers are after the colon. And it avoids the possibility that someone adds field "version" and we can't tell what "version" is what.

I think that reads well.

#8 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 10 lines · Nguyễn Thái Ngọc Duy

--sort=version sorts tags as versions. GNU extension's strverscmp is used and no real compat implementation is provided so this is Linux only.

---

Documentation/git-tag.txt +0 -0
index 404257d..fc23dc0 100644

Documentation/git-tag.txt OPTIONS · line 100

Quoted 6 lines
Documentation/git-tag.txt +3 -0
@@ -95,6 +95,10 @@ OPTIONS
        using fnmatch(3)).  Multiple patterns may be given; if any of
        them matches, the tag is shown.
 
+--sort=<type>::
+       Sort in a specific order. Supported type is "version". Prepend
+       "-" to revert sort order.

s/revert/reverse/

Quoted 4 lines
Documentation/git-tag.txt +1 -0
+
 --column[=<options>]::
 --no-column::
        Display tag listing in columns. See configuration variable
#9 lore raw

PATCH v2 1/1 tag: support --sort=<spec>

In reply to Nguyễn Thái Ngọc Duy

--sort=refname:version (or --sort=refname:v for short) sorts tags as if they are versions. --sort=-refname reverses the order (with or without ":version"). This syntax is chosen to make it compatible with future extension in "for-each-ref --sort"

GNU extension strverscmp is used so this is Linux only. Mac and Windows will need to bundle a compat implementation (and long term we may want to use compat version only so we can make XXX-rc, XXX-pre... appear before XXX)

---
The new prereq GNULINUX is an ugly workaround until people provide strverscmp compat implementation. I hope that will happen soon as strverscmp.c does not look very complex.

 Documentation/git-tag.txt |  6 +++++
 builtin/tag.c             | 69 +++++++++++++++++++++++++++++++++++++++++++----
 git-compat-util.h         |  7 +++++
 t/t7004-tag.sh            | 43 +++++++++++++++++++++++++++++
 t/test-lib.sh             |  2 ++
 5 files changed, 122 insertions(+), 5 deletions(-)
Documentation/git-tag.txt +6 -0
index 404257d..d8633bb 100644
@@ -95,6 +95,12 @@ OPTIONS
 	using fnmatch(3)).  Multiple patterns may be given; if any of
 	them matches, the tag is shown.
 
+--sort=<type>::
+	Sort in a specific order. Supported type is "refname"
+	(lexical order), "refname:version" or "refname:v" (tag names
+	are treated as version strings). Prepend "-" to reverse sorting
+	order.
+
 --column[=<options>]::
 --no-column::
 	Display tag listing in columns. See configuration variable
builtin/tag.c +64 -5

123 lines not on this page · raw message

git-compat-util.h +7 -0
index cbd86c3..22089e9 100644
@@ -721,4 +721,11 @@ void warn_on_inaccessible(const char *path);
 /* Get the passwd entry for the UID of the current process. */
 struct passwd *xgetpwuid_self(void);
 
+#ifndef __GNU_LIBRARY__
+static inline int strverscmp(const char *s1, const char *s2)
+{
+	die("strverscmp() not supported");
+}
+#endif
+
 #endif
t/t7004-tag.sh +43 -0

47 lines not on this page · raw message

t/test-lib.sh +2 -0
index 1531c24..5e8c39a 100644
@@ -771,6 +771,8 @@ case $(uname -s) in
 	;;
 esac
 
+[ "$(uname -o)" = "GNU/Linux" ] && test_set_prereq GNULINUX
+
 ( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
 test -z "$NO_PERL" && test_set_prereq PERL
 test -z "$NO_PYTHON" && test_set_prereq PYTHON
Signature · 1 line
1.9.0.40.gaa8c3ea
#10 lore raw

Re: [PATCH v2] tag: support --sort=<spec>

Quoted 5 lines · Nguyễn Thái Ngọc Duy

---
The new prereq GNULINUX is an ugly workaround until people provide strverscmp compat implementation. I hope that will happen soon as strverscmp.c does not look very complex.

Should GNULINUX be called HAVE_STRVERSCMP in the Makefile?

Then this:

git-compat-util.h void warn_on_inaccessible(const char *path); · line 729

Quoted 12 lines
git-compat-util.h +6 -0
@@ -721,4 +721,11 @@ void warn_on_inaccessible(const char *path);
 /* Get the passwd entry for the UID of the current process. */
 struct passwd *xgetpwuid_self(void);
 
+#ifndef __GNU_LIBRARY__
+static inline int strverscmp(const char *s1, const char *s2)
+{
+	die("strverscmp() not supported");
+}
+#endif

becomes "#ifndef HAVE_STRVERSCMP", and this:

t/test-lib.sh case $(uname -s) in · line 775

Quoted 10 lines
t/test-lib.sh +2 -0
index 1531c24..5e8c39a 100644
@@ -771,6 +771,8 @@ case $(uname -s) in
 	;;
 esac
 
+[ "$(uname -o)" = "GNU/Linux" ] && test_set_prereq GNULINUX
+

can pick up the value from GIT-BUILD-OPTIONS as a prerequisite (see the way we handle NO_PERL for an example). Though if we can just grab the glibc version as a fallback, we can do away with that completely.

-Peff

#11 lore raw

Re: [PATCH v2] tag: support --sort=<spec>

Quoted 35 lines · Jeff King
Quoted 5 lines · Nguyễn Thái Ngọc Duy

---
The new prereq GNULINUX is an ugly workaround until people provide strverscmp compat implementation. I hope that will happen soon as strverscmp.c does not look very complex.

Should GNULINUX be called HAVE_STRVERSCMP in the Makefile?

Then this:

git-compat-util.h void warn_on_inaccessible(const char *path); · line 729

Quoted 12 lines
git-compat-util.h +6 -0
@@ -721,4 +721,11 @@ void warn_on_inaccessible(const char *path);
 /* Get the passwd entry for the UID of the current process. */
 struct passwd *xgetpwuid_self(void);
 
+#ifndef __GNU_LIBRARY__
+static inline int strverscmp(const char *s1, const char *s2)
+{
+	die("strverscmp() not supported");
+}
+#endif

becomes "#ifndef HAVE_STRVERSCMP", and this:

Quoted 8 lines
t/test-lib.sh +0 -0
index 1531c24..5e8c39a 100644
@@ -771,6 +771,8 @@ case $(uname -s) in
 	;;
 esac
 
Quoted 6 lines

t/test-lib.sh case $(uname -s) in · line 775

Quoted 2 lines
t/test-lib.sh +2 -0
+[ "$(uname -o)" = "GNU/Linux" ] && test_set_prereq GNULINUX
+

can pick up the value from GIT-BUILD-OPTIONS as a prerequisite (see the way we handle NO_PERL for an example). Though if we can just grab the glibc version as a fallback, we can do away with that completely.

;-) I like that.

author
#12 lore raw

Re: [PATCH v2] tag: support --sort=<spec>

Quoted 8 lines · Junio C Hamano
Quoted 8 lines
Quoted 8 lines
t/test-lib.sh +0 -0
index 1531c24..5e8c39a 100644
@@ -771,6 +771,8 @@ case $(uname -s) in
     ;;
 esac
 
Quoted 9 lines
Quoted 6 lines

t/test-lib.sh case $(uname -s) in · line 775

Quoted 2 lines
t/test-lib.sh +2 -0
+[ "$(uname -o)" = "GNU/Linux" ] && test_set_prereq GNULINUX
+

can pick up the value from GIT-BUILD-OPTIONS as a prerequisite (see the way we handle NO_PERL for an example). Though if we can just grab the glibc version as a fallback, we can do away with that completely.

;-) I like that.

Jeff, I'm still waiting if you agree to go with this syntax or put version before refname before rerolling (either with build time option like this, or implement the compat function myself).

Signature · 1 line
Duy
#13 lore raw

Re: [PATCH v2] tag: support --sort=<spec>

Quoted 7 lines · Duy Nguyen
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

+[ "$(uname -o)" = "GNU/Linux" ] && test_set_prereq GNULINUX +

can pick up the value from GIT-BUILD-OPTIONS as a prerequisite (see the way we handle NO_PERL for an example). Though if we can just grab the glibc version as a fallback, we can do away with that completely.

Quoted 6 lines
Quoted 2 lines

;-) I like that.

Jeff, I'm still waiting if you agree to go with this syntax or put version before refname before rerolling (either with build time option like this, or implement the compat function myself).

Sorry. I didn't respond because I was trying to think of something to say besides "no, I still like mine better". I admit that it's mostly a gut feeling, and I don't have any argument beyond what I've already made (your "it's like a typecast" does make some sense, but I think that is a convoluted way of thinking about it).

-Peff

#14 lore raw

PATCH v3 1/1 tag: support --sort=<spec>

In reply to Nguyễn Thái Ngọc Duy

--sort=version:refname (or --sort=v:refname for short) sorts tags as if they are versions. --sort=-refname reverses the order (with or without ":version").

versioncmp() is copied from string/strverscmp.c in glibc commit ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding style. The implementation is under LGPL-2.1 and according to [1] I can relicense it to GPLv2.

[1] http://www.gnu.org/licenses/gpl-faq.html#AllCompatibility

---
v3 tweaks the sorting syntax a bit (version:refname vs refname:version) and embed strverscmp() to git code (instead of relying on one from glibc).

 Documentation/git-tag.txt |  6 ++++
 Makefile                  |  1 +
 builtin/tag.c             | 68 ++++++++++++++++++++++++++++++++++---
 cache.h                   |  2 ++
 t/t7004-tag.sh            | 43 ++++++++++++++++++++++++
 versioncmp.c (new)        | 85 +++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 200 insertions(+), 5 deletions(-)
Documentation/git-tag.txt +6 -0
index 404257d..9b05931 100644
@@ -95,6 +95,12 @@ OPTIONS
 	using fnmatch(3)).  Multiple patterns may be given; if any of
 	them matches, the tag is shown.
 
+--sort=<type>::
+	Sort in a specific order. Supported type is "refname"
+	(lexicographic order), "version:refname" or "v:refname" (tags
+	name are treated as versions). Prepend "-" to reverse sort
+	order.
+
 --column[=<options>]::
 --no-column::
 	Display tag listing in columns. See configuration variable
Makefile +1 -0
index dddaf4f..16b00a5 100644
@@ -884,6 +884,7 @@ LIB_OBJS += userdiff.o
 LIB_OBJS += utf8.o
 LIB_OBJS += varint.o
 LIB_OBJS += version.o
+LIB_OBJS += versioncmp.o
 LIB_OBJS += walker.o
 LIB_OBJS += wildmatch.o
 LIB_OBJS += wrapper.o
builtin/tag.c +63 -5

122 lines not on this page · raw message

cache.h +2 -0
index dc040fb..082a783 100644
@@ -1367,4 +1367,6 @@ int stat_validity_check(struct stat_validity *sv, const char *path);
  */
 void stat_validity_update(struct stat_validity *sv, int fd);
 
+int versioncmp(const char *s1, const char *s2);
+
 #endif /* CACHE_H */
t/t7004-tag.sh +43 -0

47 lines not on this page · raw message

versioncmp.c new file +85 -0

85 lines not on this page · raw message

Signature · 1 line
1.9.0.40.gaa8c3ea
#15 lore raw

Re: [PATCH v3] tag: support --sort=<spec>

Quoted 4 lines · Nguyễn Thái Ngọc Duy

versioncmp() is copied from string/strverscmp.c in glibc commit ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding style. The implementation is under LGPL-2.1 and according to [1] I can relicense it to GPLv2.

I'd propose this trivial change squashed in to record the above in the file in question.

Thanks.

versioncmp.c +7 -0
index 18a9632..8f5a388 100644
@@ -1,6 +1,13 @@
 #include "git-compat-util.h"
 
 /*
+ * versioncmp(): copied from string/strverscmp.c in glibc commit
+ * ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding
+ * style. The implementation is under LGPL-2.1 and Git relicenses it
+ * to GPLv2.
+ */
+
+/*
  * states: S_N: normal, S_I: comparing integral part, S_F: comparing
  * fractionnal parts, S_Z: idem but with leading Zeroes only
  */
#16 lore raw

Re: [PATCH v3] tag: support --sort=<spec>

In reply to Nguyễn Thái Ngọc Duy
Quoted 4 lines · Nguyễn Thái Ngọc Duy

versioncmp() is copied from string/strverscmp.c in glibc commit ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding style. The implementation is under LGPL-2.1 and according to [1] I can relicense it to GPLv2.

Cool. I think doing this makes the most sense, as we do not have to worry about build-time config (and I do not see any particular reason why we would want to use the system strverscmp on glibc systems).

Quoted 7 lines
builtin/tag.c +7 -0
+static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
+{
+	int *sort = opt->value;
+	if (*arg == '-') {
+		*sort = REVERSE_SORT;
+		arg++;
+	} else

builtin/tag.c static int parse_opt_points_at(const struct option *opt __attribute__((unused)), · line 478

Quoted 10 lines
builtin/tag.c +10 -0
+		*sort = STRCMP_SORT;
+	if (starts_with(arg, "version:")) {
+		*sort |= VERCMP_SORT;
+		arg += 8;
+	} else if (starts_with(arg, "v:")) {
+		*sort |= VERCMP_SORT;
+		arg += 2;
+	}
+	if (strcmp(arg, "refname"))
+		die(_("unsupported sort specification %s"), arg);

I found this logic a bit weird, as STRCMP_SORT and VERCMP_SORT are not mutually exclusive flags, but REVERSE and STRCMP are. I would have thought REVERSE is the flag, and the other two are selections. Like:

  int flags = 0;

  if (*arg == '-') {
          flags |= REVERSE_SORT;
          arg++;
  }
  if (starts_with(arg, "version:")) {
          *sort = VERCMP_SORT;
          arg += 8;
  } else
          *sort = STRCMP_SORT;

  *sort |= flags;

I think they end up doing the same thing, but maybe I am missing something.

-Peff

author
#17 lore raw

Re: [PATCH v3] tag: support --sort=<spec>

Quoted 10 lines · Jeff King
Quoted 4 lines · Nguyễn Thái Ngọc Duy

versioncmp() is copied from string/strverscmp.c in glibc commit ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding style. The implementation is under LGPL-2.1 and according to [1] I can relicense it to GPLv2.

Cool. I think doing this makes the most sense, as we do not have to worry about build-time config (and I do not see any particular reason why we would want to use the system strverscmp on glibc systems).

Another reason I want to stay away from glibc is I want to fix the algorithm to sort YY-preXX and YY-rcXX before YY. There could be reasons that glibc might reject such a change even if it makes sense in our context. Even if we make it to newer glibc and fix compat version, people on older glibc will not receive the fix while people on compat do. Not so good.

Quoted 36 lines
Quoted 7 lines
builtin/tag.c +7 -0
+static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
+{
+     int *sort = opt->value;
+     if (*arg == '-') {
+             *sort = REVERSE_SORT;
+             arg++;
+     } else

builtin/tag.c static int parse_opt_points_at(const struct option *opt __attribute__((unused)), · line 478

Quoted 10 lines
builtin/tag.c +10 -0
+             *sort = STRCMP_SORT;
+     if (starts_with(arg, "version:")) {
+             *sort |= VERCMP_SORT;
+             arg += 8;
+     } else if (starts_with(arg, "v:")) {
+             *sort |= VERCMP_SORT;
+             arg += 2;
+     }
+     if (strcmp(arg, "refname"))
+             die(_("unsupported sort specification %s"), arg);

I found this logic a bit weird, as STRCMP_SORT and VERCMP_SORT are not mutually exclusive flags, but REVERSE and STRCMP are. I would have thought REVERSE is the flag, and the other two are selections. Like:

  int flags = 0;

  if (*arg == '-') {
          flags |= REVERSE_SORT;
          arg++;
  }
  if (starts_with(arg, "version:")) {
          *sort = VERCMP_SORT;
          arg += 8;
  } else
          *sort = STRCMP_SORT;

  *sort |= flags;
Quoted 2 lines

I think they end up doing the same thing, but maybe I am missing something.

No you're not. I did not make it absolute beautiful because I expected it to be deleted soon after you polish your f-e-r series. Will resend with this change shortly.

Signature · 1 line
Duy
#18 lore raw

Re: [PATCH v3] tag: support --sort=<spec>

Quoted 10 lines · Duy Nguyen
Quoted 3 lines

Cool. I think doing this makes the most sense, as we do not have to worry about build-time config (and I do not see any particular reason why we would want to use the system strverscmp on glibc systems).

Another reason I want to stay away from glibc is I want to fix the algorithm to sort YY-preXX and YY-rcXX before YY. There could be reasons that glibc might reject such a change even if it makes sense in our context. Even if we make it to newer glibc and fix compat version, people on older glibc will not receive the fix while people on compat do. Not so good.

Yeah, the handling of -rc has bugged me, too (in my personal alias, I just grep out the -rc before feeding the list to "sort -V" :) ).

I'd worry slightly, though, that there are other schemes where that behaves poorly. Should we optimize for git's version numbering, or for what most other projects want? There could even be room for two types of version-compare. But before thinking about that, I'd want to know why glibc behaves as it does.

-Peff

author
#19 lore raw

Re: [PATCH v3] tag: support --sort=<spec>

Quoted 2 lines · Jeff King

But before thinking about that, I'd want to know why glibc behaves as it does.

Pure guess. It may be because it targets more than software version. In strverscmp man page, the example is "jan1, jan10, jan2...". versionsort() in glibc might be the reason that strverscmp was added and it's used to compare/sort dir entries, so the target might be numbered log files.

Signature · 1 line
Duy
author
#20 lore raw

Re: [PATCH v3] tag: support --sort=<spec>

In reply to Jeff King
Quoted 5 lines · Jeff King

I'd worry slightly, though, that there are other schemes where that behaves poorly. Should we optimize for git's version numbering, or for what most other projects want? There could even be room for two types of version-compare. But before thinking about that, I'd want to know why glibc behaves as it does.

We don't have to force one version style for all projects. We could provide --sort="thisver:refname" vs. "thatver:refname", or put the "-pre" part in config file. The important thing is we can control the version algorithm.

Signature · 1 line
Duy
#21 lore raw

Re: [PATCH v3] tag: support --sort=<spec>

Quoted 11 lines · Duy Nguyen
Quoted 5 lines · Jeff King

I'd worry slightly, though, that there are other schemes where that behaves poorly. Should we optimize for git's version numbering, or for what most other projects want? There could even be room for two types of version-compare. But before thinking about that, I'd want to know why glibc behaves as it does.

We don't have to force one version style for all projects. We could provide --sort="thisver:refname" vs. "thatver:refname", or put the "-pre" part in config file. The important thing is we can control the version algorithm.

Right, exactly. It may make sense to just do it the way _we_ think is sensible for now, then, and we can add a glibc-compatible one later if somebody actually wants it.

-Peff

#22 lore raw

PATCH v4 1/1 tag: support --sort=<spec>

In reply to Nguyễn Thái Ngọc Duy

--sort=version:refname (or --sort=v:refname for short) sorts tags as if they are versions. --sort=-refname reverses the order (with or without ":version").

versioncmp() is copied from string/strverscmp.c in glibc commit ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding style. The implementation is under LGPL-2.1 and according to [1] I can relicense it to GPLv2.

[1] http://www.gnu.org/licenses/gpl-faq.html#AllCompatibility

---
- add the relicensing note in versioncmp.c
- reorder the logic in parse_opt_sort for better readability
- include cache.h instead of git-compat-util.h in versioncmp.c

 Documentation/git-tag.txt |  6 ++++
 Makefile                  |  1 +
 builtin/tag.c             | 71 +++++++++++++++++++++++++++++++++---
 cache.h                   |  2 ++
 t/t7004-tag.sh            | 43 ++++++++++++++++++++++
 versioncmp.c (new)        | 92 +++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 210 insertions(+), 5 deletions(-)
 create mode 100644 versioncmp.c
Documentation/git-tag.txt +6 -0
index 404257d..9b05931 100644
@@ -95,6 +95,12 @@ OPTIONS
 	using fnmatch(3)).  Multiple patterns may be given; if any of
 	them matches, the tag is shown.
 
+--sort=<type>::
+	Sort in a specific order. Supported type is "refname"
+	(lexicographic order), "version:refname" or "v:refname" (tags
+	name are treated as versions). Prepend "-" to reverse sort
+	order.
+
 --column[=<options>]::
 --no-column::
 	Display tag listing in columns. See configuration variable
Makefile +1 -0
index dddaf4f..16b00a5 100644
@@ -884,6 +884,7 @@ LIB_OBJS += userdiff.o
 LIB_OBJS += utf8.o
 LIB_OBJS += varint.o
 LIB_OBJS += version.o
+LIB_OBJS += versioncmp.o
 LIB_OBJS += walker.o
 LIB_OBJS += wildmatch.o
 LIB_OBJS += wrapper.o
builtin/tag.c +66 -5

125 lines not on this page · raw message

cache.h +2 -0
index dc040fb..082a783 100644
@@ -1367,4 +1367,6 @@ int stat_validity_check(struct stat_validity *sv, const char *path);
  */
 void stat_validity_update(struct stat_validity *sv, int fd);
 
+int versioncmp(const char *s1, const char *s2);
+
 #endif /* CACHE_H */
t/t7004-tag.sh +43 -0

47 lines not on this page · raw message

versioncmp.c new file +92 -0

92 lines not on this page · raw message

Signature · 1 line
1.9.0.40.gaa8c3ea
#23 lore raw

Re: [PATCH v4] tag: support --sort=<spec>

Quoted 8 lines · Nguyễn Thái Ngọc Duy

--sort=version:refname (or --sort=v:refname for short) sorts tags as if they are versions. --sort=-refname reverses the order (with or without ":version").

versioncmp() is copied from string/strverscmp.c in glibc commit ee9247c38a8def24a59eb5cfb7196a98bef8cfdc, reformatted to Git coding style. The implementation is under LGPL-2.1 and according to [1] I can relicense it to GPLv2.

This looks good to me. One minor typo:

Documentation/git-tag.txt OPTIONS · line 101

Quoted 12 lines
Documentation/git-tag.txt +4 -0
index 404257d..9b05931 100644
@@ -95,6 +95,12 @@ OPTIONS
 	using fnmatch(3)).  Multiple patterns may be given; if any of
 	them matches, the tag is shown.
 
+--sort=<type>::
+	Sort in a specific order. Supported type is "refname"
+	(lexicographic order), "version:refname" or "v:refname" (tags
+	name are treated as versions). Prepend "-" to reverse sort

s/tags name/tag names/

You had mentioned earlier tweaking the version comparison to handle things like -rc better. I think that can come on top of this initial patch, but we should probably figure out the final sort order before including this in a release.

-Peff

author
#24 lore raw

Re: [PATCH v4] tag: support --sort=<spec>

Quoted 4 lines · Jeff King

You had mentioned earlier tweaking the version comparison to handle things like -rc better. I think that can come on top of this initial patch, but we should probably figure out the final sort order before including this in a release.

Yeah I have been thinking about it too. That's why I did not send the reroll "shortly" as I said. So far I like the idea of using config file to specify -rc and the like. If XX is in the configured list and it's the last component in the version string, then reorder it. Seems to work well with different versioning schemes listed at
http://en.wikipedia.org/wiki/Software_versioning.

Signature · 1 line
Duy