git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [feature request] Is it possible to have git tag can be sorted and filtered by semver?

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Jul 22, 2024, 21:30 UTC
Message-ID
<Zp7Pg7ZOlBZcfJei@tapette.crustytoothpaste.net>
In-Reply-To
<CADeMgjBeyWkE3mp+-x57NSsyNLQf3cTta+Xm5uyVOQE-rpT6yg@mail.gmail.com>
On 2024-07-22 at 16:58:11, Thaina Yu wrote:
Show 13 quoted lines
> Currently there is `ls-remote` and `tag` that has option to
> --sort=version:refname which is really useful when we have tag as
> version format and let it sort as numeric version
> 
> But it could be better if we can also sort and filter the version in
> the range of semver format. So we can use git system as dependency
> management system natively
> 
> So I would like to propose a new sort option semver:refname
> 
> `git ls-remote --sort=semver:refname myRepoUrl refs/tags/v{^0.*.*}`
> where brace {} can be used to define semver versioning ranges for the
> patterns in addition to glob filter

Assuming we add such a feature, how does sorting by SemVer differ from the current version sorting? That is, where is the current version sorting deficient for SemVer? Also, what do you want to happen when a tag doesn't meet SemVer requirements (note that the "v" prefix is not allowed in SemVer, although it's customary in tags)?

As for the special range syntax, I think the typical suggestion is to filter the output of ls-remote or for-each-ref by piping it to a suitable program. Perl or Ruby are common choices here, and both could easily parse SemVer tags. For example:

  git for-each-ref --sort=v:refname refs/tags/ |
  ruby -ne 'if %r[\trefs/tags/v(\d+)\.(\d+)\.(\d+)$]; ver = Regexp.last_match[1..3].map(&:to_i); puts $_ if [[2, 6, 3], ver, [2, 15, 2]].sort[1] == ver; end'
Git is intentionally designed to support this kind of shell scripting.
-- 
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA
Previous: Thaina YuNext: rsbecker@nexbridge.com
Message 2 of 6 in “[feature request] Is it possible to have git tag can be sorted and filtered by semver?”
  1. Thaina YuJul 22, 2024
  2. brian m. carlsonJul 22, 2024
  3. rsbecker@nexbridge.comJul 22, 2024
  4. Junio C HamanoJul 22, 2024
  5. brian m. carlsonJul 23, 2024
  6. Thaina YuJul 23, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.