This repository has been archived by the owner on May 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
versions.go
133 lines (106 loc) · 2.89 KB
/
versions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"os"
"os/exec"
"sort"
"strings"
version "github.com/hashicorp/go-version"
"github.com/juju/errors"
)
func getLatestVersionMatchingPattern(repo string, versionPattern string) (string, error) {
repoPath, err := getPackageRootDir(repo)
if err != nil {
return "", errors.Trace(err)
}
if exists, _ := pathExists(repoPath); !exists {
return versionPattern, nil
}
wd, err := os.Getwd()
if err != nil {
return "", errors.Trace(err)
}
err = os.Chdir(repoPath)
if err != nil {
return "", errors.Trace(err)
}
defer func() {
_ = os.Chdir(wd)
}()
var repoType string
if exists, _ := pathExists(".git"); exists {
repoType = "git"
} else if exists, _ := pathExists(".hg"); exists {
repoType = "hg"
} else if exists, _ := pathExists(".bzr"); exists {
repoType = "bzr"
} else {
return versionPattern, nil
}
if versionPattern == "" {
if repoType == "git" {
return "master", nil
} else if repoType == "hg" {
return "tip", nil
} else if repoType == "bzr" {
return "", nil
}
}
if repoType == "hg" || repoType == "bzr" {
return versionPattern, nil
}
// first, try feeding it through git to see if it's a valid rev
gitResolveCommand := []string{"git", "rev-parse", "-q", "--verify", versionPattern}
output, err := exec.Command(gitResolveCommand[0], gitResolveCommand[1:]...).Output()
if err != nil {
if _, ok := err.(*exec.ExitError); !ok {
return "", errors.Trace(err)
}
}
gitResolvedString := strings.TrimSpace(string(output))
if gitResolvedString != "" {
return gitResolvedString, nil
}
// second, try parsing it
tagListB, err := exec.Command("git", "tag").Output()
if err != nil {
return "", errors.Trace(err)
}
versionToTag := make(map[*version.Version]string)
tagList := strings.Split(strings.TrimSpace(string(tagListB)), "\n")
processedTagList := make([]*version.Version, len(tagList))
for i, tag := range tagList {
stringVersion := tag
if strings.HasPrefix(tag, "v") {
stringVersion = strings.Replace(tag, "v", "", 1)
}
v, err := version.NewVersion(stringVersion)
if err != nil {
continue
}
processedTagList[i] = v
versionToTag[v] = tag
}
sort.Sort(version.Collection(processedTagList))
constraints, err := version.NewConstraint(versionPattern)
if err != nil {
return "", errors.Trace(err)
}
var resultVersion string
for i := len(processedTagList) - 1; i >= 0; i-- {
ver := processedTagList[i]
if constraints.Check(ver) {
resultVersion = versionToTag[ver]
}
}
if resultVersion == "" {
return "", fmt.Errorf("unable to find a version matching constraint %s for package %s", versionPattern, repo)
}
gitResolveCommand = []string{"git", "rev-parse", "-q", "--verify", resultVersion}
output, err = exec.Command(gitResolveCommand[0], gitResolveCommand[1:]...).Output()
if err != nil {
return "", errors.Trace(err)
} else {
return strings.TrimSpace(string(output)), nil
}
}