This repository has been archived by the owner on Sep 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvguess.go
163 lines (144 loc) · 4.71 KB
/
vguess.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package guessica
// This file is extracted from the "getver" project, for automatically finding the newest
// version number for a given PKGBUILD file, by examining the corresponding web page.
// It has also been modified to fetch the latest git commit for the latest git version tag.
// This code is not particularly pretty and probably needs a good refactoring or two.
import (
"bytes"
"errors"
"os/exec"
"strings"
)
// GuessSourceString is the function that is meant to be used from this source file
// It takes the contents of a PKGBUILD file and returns a new "source=" string.
// The new version number is guessed after looking online for a newer source.
// The git commit is included in the "source=" string, if possible.
// Returns the new pkgver and the new source.
// commonGitServer is often set to ie. "github.com"
func GuessSourceString(pkgbuildContents, commonGitServer string) (string, string, error) {
lines := strings.Split(pkgbuildContents, "\n")
var rawURL, rawSource string
inSource := false
for _, line := range lines {
// First remove trailing comments
if strings.Contains(line, " #") {
parts := strings.SplitN(line, " #", 2)
line = parts[0]
}
// Then check if we're in the source=() definition
if inSource && len(strings.TrimSpace(line)) != 0 && !strings.Contains(line, "=") {
rawSource += line
continue
} else {
inSource = false
}
// Save url, pkgver, pkgrel and source
if strings.HasPrefix(line, "url=") {
rawURL = line[4:]
} else if strings.HasPrefix(line, "source=") {
rawSource = line[7:]
inSource = true
}
}
url := unquote(strings.TrimSpace(rawURL))
if len(url) == 0 {
return "", "", errors.New("found no URL definition")
}
if strings.Contains(url, commonGitServer+"/") && !strings.Contains(url, "/releases/") {
if strings.HasSuffix(url, "/") {
url += "releases/latest"
} else {
url += "/releases/latest"
}
}
var (
foundURL bool
newVer string
err error
)
// Should the source array URL be used instead of the "url=" field?
if !strings.Contains(url, commonGitServer) && strings.Contains(rawSource, commonGitServer) {
// Use the url from the source instead of the url field
for _, sourceURL := range linkFinder.FindAllString(rawSource, -1) {
if strings.Contains(sourceURL, "#") {
sourceURL = strings.SplitN(sourceURL, "#", 2)[0]
}
sourceURL = strings.TrimSuffix(sourceURL, ".git")
getverURL := sourceURL
if strings.HasSuffix(getverURL, "/") {
getverURL += "releases/latest"
} else {
getverURL += "/releases/latest"
}
newVer, err = getver(getverURL)
if err == nil {
// ok
foundURL = true
url = sourceURL
break
}
}
}
if !foundURL {
newVer, err = getver(url)
if err != nil {
return "", "", errors.New("could not guess a version number by visiting " + url)
}
}
shortURL := url
if strings.HasPrefix(url, "http://") {
shortURL = url[7:]
} else if strings.HasPrefix(url, "https://") {
shortURL = url[8:]
}
shortURL = strings.TrimSuffix(shortURL, "releases/latest")
gotCommit := ""
// git ls-remote https://github.com/xyproto/o 2.9.2
tag := newVer
cmd := exec.Command("git", "ls-remote", "-t", "https://"+shortURL, tag)
data, err := cmd.CombinedOutput()
if err != nil || len(bytes.TrimSpace(data)) == 0 {
// Add a "v" in front of the tag
cmd = exec.Command("git", "ls-remote", "-t", "https://"+shortURL, "v"+tag)
data, err = cmd.CombinedOutput()
if err != nil || len(bytes.TrimSpace(data)) == 0 {
return "", "", errors.New("got no git commit hash from tag " + tag + " or tag v" + tag + " at " + shortURL)
}
gotCommit = strings.TrimSpace(string(data))
tag = "v" + newVer
} else {
gotCommit = strings.TrimSpace(string(data))
}
if len(gotCommit) == 0 {
return "", "", errors.New("got no git commit for tag " + tag + " or tag v" + tag)
}
fields := strings.Fields(gotCommit)
if len(fields) > 0 {
gotCommit = fields[0]
}
//fmt.Println("got commit: " + gotCommit)
source := rawSource
newSource := ""
if len(gotCommit) != 0 && strings.Contains(source, "#commit=") {
pos := strings.Index(source, "#commit=")
if pos == -1 {
return "", "", errors.New("found no #commit= in source")
}
pos += len("#commit=")
if pos+len(gotCommit) < len(source) {
// replace the existing commit hash, which is assumed to be as long as the new one
newSource = source[:pos] + gotCommit + source[pos+len(gotCommit):]
} else {
// the existing commit has was too short, just replace the rest of the line
newSource = source[:pos] + gotCommit + "\")"
}
}
// add a tag commit
if strings.HasSuffix(newSource, ")") {
newSource += " # tag: " + tag
}
if len(newVer) == 0 {
return "", "", errors.New("found no new version number")
}
return newVer, "source=" + newSource, nil
}