/
gotag.go
251 lines (219 loc) · 5.9 KB
/
gotag.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2011, Bryan Matsuo. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
/* Filename: gotag.go
* Author: Bryan Matsuo <bryan.matsuo@gmail.com>
* Created: Sat Nov 5 19:46:28 PDT 2011
* Description: Main source file in gotag
*/
import (
"github.com/bmatsuo/go-script/script"
"path/filepath"
"template"
"runtime"
"strconv"
"strings"
"bytes"
"log"
"fmt"
"os"
"io"
)
type tabbedwriter struct {
io.Writer
written bool
}
func Tabbed(wr io.Writer) *tabbedwriter {
return &tabbedwriter{Writer:wr}
}
func (tw *tabbedwriter) Write(p []byte) (int, error) {
if !tw.written {
tw.written = true
n, err := tw.Writer.Write([]byte{'\t'})
if n != 1 || err != nil {
return n, err
}
}
// TODO replace with an efficient loop that doesn't allocate an array.
q := bytes.Replace(p, []byte{'\n'}, []byte("\n\t"), -1)
n, err := tw.Writer.Write(q)
if n != len(q) {
r := bytes.Replace(q[:n], []byte("\n\t"), []byte{'\n'}, -1)
return len(r), err
}
return len(p), err
}
var tfuncs = template.FuncMap{
"quote": func(x interface{}) (string, error) {
switch x.(type) {
case string:
return script.ShellQuote(x.(string)), nil
}
return "", fmt.Errorf("argument %#v is not a string", x)
},
}
var cmdtemplates = `
{{/* Outputs a shell command given a list of strings (executable + args) */}}
{{define "cmd"}}{{if ""}}
{{end}}{{with $cmd := .}}{{range $i, $arg := $cmd}}{{if ""}}
{{end}}{{if $i}} {{end}}{{quote $arg}}{{end}}{{end}}{{end}}
{{/* Outputs a list of comands .cmds. If .dir is set, the working directory is set with cd*/}}
{{define "script"}}{{if ""}}
{{end}}{{if .dir}}cd {{quote .dir}}
{{end}}{{if ""}}
{{end}}{{range $i, $cmd := .cmds}}{{if $i}}
{{end}}{{if ""}}
{{end}}{{template "cmd" $cmd}}{{end}}{{end}}
`
var templates = template.SetMust(new(template.Set).Funcs(tfuncs).Parse(cmdtemplates))
func PackagePath(importpath string) string {
return filepath.Join(runtime.GOROOT(), "src", "pkg", importpath)
}
func ReadablePackagePath(importpath string) string {
return filepath.Join("$GOROOT", "src", "pkg", importpath)
}
type ShellCmd []string
func CmdTemplateScript(sh script.Scriptor, dir string, cmds ...ShellCmd) script.Script {
if sh == nil {
panic("nil scriptor")
}
var d string
if dir != "." {
d = dir
}
buff := new(bytes.Buffer)
err := templates.Template("script").Execute(buff, map[string]interface{}{"dir": d, "cmds": cmds})
if err != nil {
log.Println(err)
}
s := sh.NewScript(string(buff.Bytes()))
s.SetStdout(Tabbed(os.Stdout))
s.SetStderr(Tabbed(os.Stderr))
return s
}
var archlinker = map[string]string{
"amd64": "6l",
"386": "8l",
"arm": "5l",
}
func Must(err error) {
if err != nil {
log.Fatal(err)
}
}
var GoLinker = archlinker[runtime.GOARCH]
func GetGoVersion() (version string, revision int, err error) {
if GoLinker == "" {
panic(fmt.Errorf("unknown architechture %s", runtime.GOARCH))
}
var p []byte
p, _, err = script.Output(script.Bash.NewScript(fmt.Sprintf("%s -V", GoLinker)))
if err != nil {
return
}
pieces := strings.Fields(string(p))
if len(pieces) < 2 {
err = fmt.Errorf("Didn't understand Go version %s", string(p))
}
version = pieces[len(pieces)-2]
revision, err = strconv.Atoi(pieces[len(pieces)-1])
return
}
func GoRepositoryTag(version string) string { return "go." + version }
func MakeTag(root string, force bool, verbose bool) {
gover, gorev, err := GetGoVersion()
Must(err)
gotag := GoRepositoryTag(gover)
if verbose {
log.Printf(" Linker: %s", GoLinker)
log.Printf(" Version: %s", gover)
log.Printf("Revision: %d", gorev)
log.Printf(" Tag: %s", gotag)
}
fmt.Print("Verifying project build integrity.\n")
var project GoProject
project, err = NewProject(root)
Must(err)
Must(BuildAndClean(project))
var git Repository
git, err = NewGitRepo(root)
Must(err)
if opt.Commit == "" { // It's OK to tag past commits if the HEAD is dirty.
clean, err := git.IsClean()
Must(err)
if !clean {
fmt.Fprint(os.Stderr, "The repository has uncommited changes.\n")
fmt.Fprint(os.Stderr, "Commit the changes and run Gotag again.\n")
os.Exit(1)
}
}
if opt.Fetch {
fmt.Print("Fetching remote tags\n")
Must(git.TagsFetch())
}
var tags []string
tags, err = git.Tags()
Must(err)
// Look for a tag named for the current version.
hasCurrentTag := false
for i := range tags {
hasCurrentTag = gotag == tags[i]
if hasCurrentTag {
break
}
}
// If found a try to delete it.
if hasCurrentTag {
fmt.Printf("Found tag %s\n", gotag)
if force {
Must(git.TagDelete(gotag))
} else {
fmt.Fprintf(os.Stderr, "use -f flag to update %s\n", gotag)
os.Exit(1)
}
}
// Create the new tag.
annotation := fmt.Sprintf("Latest build for Go version %s", gover)
if opt.Commit != "" {
fmt.Fprintf(os.Stderr, "Creating tag %s %#v (%s)\n", gotag, annotation, opt.Commit)
Must(git.TagNew(gotag, annotation, opt.Commit))
} else {
fmt.Fprintf(os.Stderr, "Creating tag %s %#v\n", gotag, annotation)
Must(git.TagNew(gotag, annotation))
}
if opt.Push {
fmt.Fprint(os.Stderr, "Pushing tags to remote repository\n")
Must(git.TagsPush())
}
}
func UpdateTags(root, install string, verbose bool) {
var git Repository
var err error
git, err = NewGitRepo(root)
Must(err)
fmt.Fprintf(os.Stderr, "Updating tags in %s\n", ReadablePackagePath(install))
Must(git.TagsFetch())
if install != "" {
fmt.Fprintf(os.Stderr, "Goinstalling %s\n", install)
goinstall := ShellCmd{"goinstall", "-u", install}
if verbose {
goinstall = append(goinstall[:2], "-v", install)
}
_, err := CmdTemplateScript(script.Bash, ".", goinstall).Execute()
Must(err)
}
}
var opt options
func main() {
opt = parseFlags()
if opt.Refresh {
if opt.Install {
UpdateTags(opt.Root, opt.ImportPath, opt.Verbose)
} else {
UpdateTags(opt.Root, "", opt.Verbose)
}
} else {
MakeTag(opt.Root, opt.Force, opt.Verbose)
}
}