Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate "sort -V" on Mac OSX

I have written a bash script that I need to work identically on linux and mac osx that relies on the sort command. I am piping the output of git tag -l to sort to get a list of all the version tags in the correct semantic order. GNU offers -V which makes this automagic but mac osx does not support this argument so i need to figure out how to accomplish this sort order without it.

6.3.1.1
6.3.1.10
6.3.1.11
6.3.1.2
6.3.1.3
...

needs to be sorted as

6.3.1.1
6.3.1.2
6.3.1.3
...
6.3.1.10
6.3.1.11
like image 780
sphoid Avatar asked Jan 28 '14 00:01

sphoid


1 Answers

You can use additional features of git tag to get a list of tags matching a pattern and sorted properly for version tag ordering (typically no leading zeros):

$ git tag --sort v:refname
v0.0.0
v0.0.1
v0.0.2
v0.0.3
v0.0.4
v0.0.5
v0.0.6
v0.0.7
v0.0.8
v0.0.9
v0.0.10
v0.0.11
v0.0.12

From $ man git-tag:

   --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. When this option is not given, the sort order
       defaults to the value configured for the tag.sort variable
       if it exists, or lexicographic order otherwise. See 
       git config(1).
like image 55
tbc Avatar answered Sep 16 '22 14:09

tbc