Can I make git print x.y.z style tag names in a sensible order?

我的梦境 提交于 2019-11-30 20:07:32
Seth Robertson

Easier solution:

serv ~: echo -e "1.1.1\n1.3.2\n1.1.10\n1.1.2" | sort -V
1.1.1
1.1.2
1.1.10
1.3.2

Breakdown of the sort options being used here:

  • -V sort by version
serv ~: echo -e "1.1.1\n1.3.2\n1.1.10\n1.1.2" | sort -n -t. -k1,1 -k2,2 -k3,3
1.1.1
1.1.2
1.1.10
1.3.2

Breakdown of the sort options being used here:

  • -n - sort using numerical string order (thus 10 comes after 1)
  • -t. - use periods as field separators
  • -k1,1 define a sort key on the first field (and only the first field)
  • -k2,2 define a sort key on the second field (and only the second field)
  • -k3,3 define a sort key on the third field (and only the third field)

You will soon (with Git 1.9.x/2.0, Q2 2014) be able to use git only for getting the right sorted output:

See commit b6de0c6, from commit 9ef176b, authored by Nguyễn Thái Ngọc Duy (pclouds):

 --sort=<type>

Sort in a specific order.
Supported type is:

  • "refname" (lexicographic order),
  • "version:refname" or "v:refname" (tag names are treated as versions).

Prepend "-" to reverse sort order.


In your case:

 git tag -l --sort=version:refname 

A few test cases:

git tag foo1.3 &&
git tag foo1.6 &&
git tag foo1.10

Here is what you would get:

# lexical sort
git tag -l --sort=refname "foo*"
foo1.10
foo1.3
foo1.6

# version sort
git tag -l --sort=version:refname "foo*" >actual &&
foo1.3
foo1.6
foo1.10

# reverse version sort
git tag -l --sort=-version:refname "foo*" >actual &&
foo1.10
foo1.6
foo1.3

# reverse lexical sort
git tag -l --sort=-refname "foo*" >actual &&
foo1.6
foo1.3
foo1.10
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!