21
21
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-]+)*))?$` )
22
22
)
23
23
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.
24
29
// 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 ) {
26
31
var latestTag string
27
32
var commitsAhead int
28
33
@@ -31,7 +36,7 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
31
36
cmd .Dir = gitdir
32
37
if out , err := cmd .Output (); err == nil {
33
38
latestTag = strings .TrimSpace (string (out ))
34
- hasRelease = true
39
+ u . hasRelease = true
35
40
log .Printf ("Found latest tag %q" , latestTag )
36
41
37
42
if ! semverRegexp .MatchString (latestTag ) {
@@ -44,11 +49,11 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
44
49
cmd .Dir = gitdir
45
50
out , err := cmd .Output ()
46
51
if err != nil {
47
- return "" , true , false , err
52
+ return "" , err
48
53
}
49
54
commitsAhead , err = strconv .Atoi (strings .TrimSpace (string (out )))
50
55
if err != nil {
51
- return "" , true , false , err
56
+ return "" , err
52
57
}
53
58
54
59
if commitsAhead == 0 {
@@ -58,13 +63,15 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
58
63
log .Printf ("INFO: master is ahead of %q by %v commits" , latestTag , commitsAhead )
59
64
}
60
65
61
- version = strings . TrimPrefix ( latestTag , "v" )
62
- isRelease = true
66
+ u . commitIsh = latestTag
67
+ u . version = strings . TrimPrefix ( latestTag , "v" )
63
68
64
69
if forcePrerelease {
65
70
log .Printf ("INFO: Force packaging master (prerelease) as requested by user" )
71
+ // Fallthrough to package @master (prerelease)
66
72
} else {
67
- return version , hasRelease , isRelease , nil
73
+ u .isRelease = true
74
+ return u .version , nil
68
75
}
69
76
}
70
77
@@ -73,26 +80,26 @@ func pkgVersionFromGit(gitdir string, forcePrerelease bool) (version string, has
73
80
// 1.0~rc1 < 1.0 < 1.0+b1, as per
74
81
// https://www.debian.org/doc/manuals/maint-guide/first.en.html#namever
75
82
mainVer := "0.0~"
76
- if hasRelease {
77
- mainVer = version + "+"
83
+ if u . hasRelease {
84
+ mainVer = u . version + "+"
78
85
}
79
86
80
87
// Find committer date, UNIX timestamp
81
88
cmd = exec .Command ("git" , "log" , "--pretty=format:%ct" , "-n1" )
82
89
cmd .Dir = gitdir
83
90
lastCommitUnixBytes , err := cmd .Output ()
84
91
if err != nil {
85
- return "" , hasRelease , isRelease , err
92
+ return "" , err
86
93
}
87
94
lastCommitUnix , err := strconv .ParseInt (strings .TrimSpace (string (lastCommitUnixBytes )), 0 , 64 )
88
95
if err != nil {
89
- return "" , hasRelease , isRelease , err
96
+ return "" , err
90
97
}
91
98
92
- // This results in an output like 4 .10.2-232-g9f107c8
99
+ // This results in an output like "v4 .10.2-232-g9f107c8"
93
100
cmd = exec .Command ("git" , "describe" , "--long" , "--tags" )
94
101
cmd .Dir = gitdir
95
- lastCommitSha := ""
102
+ lastCommitHash := ""
96
103
describeBytes , err := cmd .Output ()
97
104
if err != nil {
98
105
// 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
101
108
cmd .Stderr = os .Stderr
102
109
revparseBytes , err := cmd .Output ()
103
110
if err != nil {
104
- return "" , hasRelease , isRelease , err
111
+ return "" , err
105
112
}
106
- lastCommitSha = strings .TrimSpace (string (revparseBytes ))
113
+ lastCommitHash = strings .TrimSpace (string (revparseBytes ))
114
+ u .commitIsh = lastCommitHash
107
115
} else {
108
116
submatches := describeRegexp .FindSubmatch (describeBytes )
109
117
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 ))
111
119
}
112
- lastCommitSha = string (submatches [1 ])
120
+ lastCommitHash = string (submatches [1 ])
121
+ u .commitIsh = strings .TrimSpace (string (describeBytes ))
113
122
}
114
- version = fmt .Sprintf ("%sgit%s.%s" ,
123
+ u . version = fmt .Sprintf ("%sgit%s.%s" ,
115
124
mainVer ,
116
125
time .Unix (lastCommitUnix , 0 ).UTC ().Format ("20060102" ),
117
- lastCommitSha )
118
- return version , hasRelease , isRelease , nil
126
+ lastCommitHash )
127
+ return u . version , nil
119
128
}
0 commit comments