2121 semverRegexp = regexp .MustCompile (`^v(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` )
2222)
2323
24+ // pkgVersionFromGit determines the actual version to be packaged
25+ // from the git repository status and user preference.
26+ // Besides returning the Debian upstream version, the "upstream" struct
27+ // struct fields u.version, u.commitIsh, u.hasRelease and u.isRelease
28+ // are also set.
2429// TODO: also support other VCS
25- func pkgVersionFromGit (gitdir string , forcePrerelease bool ) (version string , hasRelease , isRelease bool , err error ) {
30+ func pkgVersionFromGit (gitdir string , u * upstream , forcePrerelease bool ) (string , error ) {
2631 var latestTag string
2732 var commitsAhead int
2833
@@ -31,7 +36,7 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
3136 cmd .Dir = gitdir
3237 if out , err := cmd .Output (); err == nil {
3338 latestTag = strings .TrimSpace (string (out ))
34- hasRelease = true
39+ u . hasRelease = true
3540 log .Printf ("Found latest tag %q" , latestTag )
3641
3742 if ! semverRegexp .MatchString (latestTag ) {
@@ -44,11 +49,11 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
4449 cmd .Dir = gitdir
4550 out , err := cmd .Output ()
4651 if err != nil {
47- return "" , true , false , err
52+ return "" , err
4853 }
4954 commitsAhead , err = strconv .Atoi (strings .TrimSpace (string (out )))
5055 if err != nil {
51- return "" , true , false , err
56+ return "" , err
5257 }
5358
5459 if commitsAhead == 0 {
@@ -58,13 +63,15 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
5863 log .Printf ("INFO: master is ahead of %q by %v commits" , latestTag , commitsAhead )
5964 }
6065
61- version = strings . TrimPrefix ( latestTag , "v" )
62- isRelease = true
66+ u . commitIsh = latestTag
67+ u . version = strings . TrimPrefix ( latestTag , "v" )
6368
6469 if forcePrerelease {
6570 log .Printf ("INFO: Force packaging master (prerelease) as requested by user" )
71+ // Fallthrough to package @master (prerelease)
6672 } else {
67- return version , hasRelease , isRelease , nil
73+ u .isRelease = true
74+ return u .version , nil
6875 }
6976 }
7077
@@ -73,26 +80,26 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
7380 // 1.0~rc1 < 1.0 < 1.0+b1, as per
7481 // https://www.debian.org/doc/manuals/maint-guide/first.en.html#namever
7582 mainVer := "0.0~"
76- if hasRelease {
77- mainVer = version + "+"
83+ if u . hasRelease {
84+ mainVer = u . version + "+"
7885 }
7986
8087 // Find committer date, UNIX timestamp
8188 cmd = exec .Command ("git" , "log" , "--pretty=format:%ct" , "-n1" )
8289 cmd .Dir = gitdir
8390 lastCommitUnixBytes , err := cmd .Output ()
8491 if err != nil {
85- return "" , hasRelease , isRelease , err
92+ return "" , err
8693 }
8794 lastCommitUnix , err := strconv .ParseInt (strings .TrimSpace (string (lastCommitUnixBytes )), 0 , 64 )
8895 if err != nil {
89- return "" , hasRelease , isRelease , err
96+ return "" , err
9097 }
9198
92- // This results in an output like 4 .10.2-232-g9f107c8
99+ // This results in an output like "v4 .10.2-232-g9f107c8"
93100 cmd = exec .Command ("git" , "describe" , "--long" , "--tags" )
94101 cmd .Dir = gitdir
95- lastCommitSha := ""
102+ lastCommitHash := ""
96103 describeBytes , err := cmd .Output ()
97104 if err != nil {
98105 // In case there are no tags at all, we just use the sha of the current commit
@@ -101,19 +108,21 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
101108 cmd .Stderr = os .Stderr
102109 revparseBytes , err := cmd .Output ()
103110 if err != nil {
104- return "" , hasRelease , isRelease , err
111+ return "" , err
105112 }
106- lastCommitSha = strings .TrimSpace (string (revparseBytes ))
113+ lastCommitHash = strings .TrimSpace (string (revparseBytes ))
114+ u .commitIsh = lastCommitHash
107115 } else {
108116 submatches := describeRegexp .FindSubmatch (describeBytes )
109117 if submatches == nil {
110- return "" , hasRelease , isRelease , fmt .Errorf ("git describe output %q does not match expected format" , string (describeBytes ))
118+ return "" , fmt .Errorf ("git describe output %q does not match expected format" , string (describeBytes ))
111119 }
112- lastCommitSha = string (submatches [1 ])
120+ lastCommitHash = string (submatches [1 ])
121+ u .commitIsh = strings .TrimSpace (string (describeBytes ))
113122 }
114- version = fmt .Sprintf ("%sgit%s.%s" ,
123+ u . version = fmt .Sprintf ("%sgit%s.%s" ,
115124 mainVer ,
116125 time .Unix (lastCommitUnix , 0 ).UTC ().Format ("20060102" ),
117- lastCommitSha )
118- return version , hasRelease , isRelease , nil
126+ lastCommitHash )
127+ return u . version , nil
119128}
0 commit comments