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

梦想与她 提交于 2019-12-30 06:22:13

问题


Consider this list of version numbers:

0.3.0
0.3.1
...
0.3.8
0.3.9
0.3.10
0.3.11

git tag would print them in the following order:

0.3.0
0.3.1
0.3.10
0.3.11
0.3.2
...

I there any way to make git tag print them in 'numeric' order as opposed to alphabetical order? Or a workaround - perhaps a program I can pipe the output through to order them how I want?


回答1:


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



回答2:


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)



回答3:


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


来源:https://stackoverflow.com/questions/6091306/can-i-make-git-print-x-y-z-style-tag-names-in-a-sensible-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!