Debian Codegraph @

cmd/go-contrib-init/contrib.go

raw symbols SCIP
1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // The go-contrib-init command helps new Go contributors get their development
6 // environment set up for the Go contribution process.
7 //
8 // It aims to be a complement or alternative to https://golang.org/doc/contribute.html.
9 package main
10
11 import (
12 "bytes"
13 "flag"
14 "fmt"
15 "go/build"
16 "log"
17 "os"
18 "os/exec"
19 "path/filepath"
20 "regexp"
21 "runtime"
22 "strings"
23 )
24
25 var (
26 repo = flag.String("repo", detectrepo(), "Which go repo you want to contribute to. Use \"go\" for the core, or e.g. \"net\" for golang.org/x/net/*")
27 dry = flag.Bool("dry-run", false, "Fail with problems instead of trying to fix things.")
28 )
29
30 func main() {
31 log.SetFlags(0)
32 flag.Parse()
33
34 checkCLA()
35 checkGoroot()
36 checkWorkingDir()
37 checkGitOrigin()
38 checkGitCodeReview()
39 fmt.Print("All good. Happy hacking!\n" +
40 "Remember to squash your revised commits and preserve the magic Change-Id lines.\n" +
41 "Next steps: https://golang.org/doc/contribute.html#commit_changes\n")
42 }
43
44 func detectrepo() string {
45 wd, err := os.Getwd()
46 if err != nil {
47 return "go"
48 }
49
50 for _, path := range filepath.SplitList(build.Default.GOPATH) {
51 rightdir := filepath.Join(path, "src", "golang.org", "x") + string(os.PathSeparator)
52 if strings.HasPrefix(wd, rightdir) {
53 tail := wd[len(rightdir):]
54 end := strings.Index(tail, string(os.PathSeparator))
55 if end > 0 {
56 repo := tail[:end]
57 return repo
58 }
59 }
60 }
61
62 return "go"
63 }
64
65 var googleSourceRx = regexp.MustCompile(`(?m)^(go|go-review)?\.googlesource.com\b`)
66
67 func checkCLA() {
68 slurp, err := os.ReadFile(cookiesFile())
69 if err != nil && !os.IsNotExist(err) {
70 log.Fatal(err)
71 }
72 if googleSourceRx.Match(slurp) {
73 // Probably good.
74 return
75 }
76 log.Fatal("Your .gitcookies file isn't configured.\n" +
77 "Next steps:\n" +
78 " * Submit a CLA (https://golang.org/doc/contribute.html#cla) if not done\n" +
79 " * Go to https://go.googlesource.com/ and click \"Generate Password\" at the top,\n" +
80 " then follow instructions.\n" +
81 " * Run go-contrib-init again.\n")
82 }
83
84 func expandUser(s string) string {
85 env := "HOME"
86 if runtime.GOOS == "windows" {
87 env = "USERPROFILE"
88 } else if runtime.GOOS == "plan9" {
89 env = "home"
90 }
91 home := os.Getenv(env)
92 if home == "" {
93 return s
94 }
95
96 if len(s) >= 2 && s[0] == '~' && os.IsPathSeparator(s[1]) {
97 if runtime.GOOS == "windows" {
98 s = filepath.ToSlash(filepath.Join(home, s[2:]))
99 } else {
100 s = filepath.Join(home, s[2:])
101 }
102 }
103 return os.Expand(s, func(env string) string {
104 if env == "HOME" {
105 return home
106 }
107 return os.Getenv(env)
108 })
109 }
110
111 func cookiesFile() string {
112 out, _ := exec.Command("git", "config", "http.cookiefile").Output()
113 if s := strings.TrimSpace(string(out)); s != "" {
114 if strings.HasPrefix(s, "~") {
115 s = expandUser(s)
116 }
117 return s
118 }
119 if runtime.GOOS == "windows" {
120 return filepath.Join(os.Getenv("USERPROFILE"), ".gitcookies")
121 }
122 return filepath.Join(os.Getenv("HOME"), ".gitcookies")
123 }
124
125 func checkGoroot() {
126 v := os.Getenv("GOROOT")
127 if v == "" {
128 return
129 }
130 if *repo == "go" {
131 if strings.HasPrefix(v, "/usr/") {
132 log.Fatalf("Your GOROOT environment variable is set to %q\n"+
133 "This is almost certainly not what you want. Either unset\n"+
134 "your GOROOT or set it to the path of your development version\n"+
135 "of Go.", v)
136 }
137 slurp, err := os.ReadFile(filepath.Join(v, "VERSION"))
138 if err == nil {
139 slurp = bytes.TrimSpace(slurp)
140 log.Fatalf("Your GOROOT environment variable is set to %q\n"+
141 "But that path is to a binary release of Go, with VERSION file %q.\n"+
142 "You should hack on Go in a fresh checkout of Go. Fix or unset your GOROOT.\n",
143 v, slurp)
144 }
145 }
146 }
147
148 func checkWorkingDir() {
149 wd, err := os.Getwd()
150 if err != nil {
151 log.Fatal(err)
152 }
153 if *repo == "go" {
154 if inGoPath(wd) {
155 log.Fatalf(`You can't work on Go from within your GOPATH. Please checkout Go outside of your GOPATH
156
157 Current directory: %s
158 GOPATH: %s
159 `, wd, os.Getenv("GOPATH"))
160 }
161 return
162 }
163 }
164
165 func inGoPath(wd string) bool {
166 if os.Getenv("GOPATH") == "" {
167 return false
168 }
169
170 for _, path := range filepath.SplitList(os.Getenv("GOPATH")) {
171 if strings.HasPrefix(wd, filepath.Join(path, "src")) {
172 return true
173 }
174 }
175
176 return false
177 }
178
179 // mostly check that they didn't clone from github
180 func checkGitOrigin() {
181 if _, err := exec.LookPath("git"); err != nil {
182 log.Fatalf("You don't appear to have git installed. Do that.")
183 }
184 wantRemote := "https://go.googlesource.com/" + *repo
185 remotes, err := exec.Command("git", "remote", "-v").Output()
186 if err != nil {
187 msg := cmdErr(err)
188 if strings.Contains(msg, "Not a git repository") {
189 log.Fatalf("Your current directory is not in a git checkout of %s", wantRemote)
190 }
191 log.Fatalf("Error running git remote -v: %v", msg)
192 }
193 matches := 0
194 for line := range strings.SplitSeq(string(remotes), "\n") {
195 line = strings.TrimSpace(line)
196 if !strings.HasPrefix(line, "origin") {
197 continue
198 }
199 if !strings.Contains(line, wantRemote) {
200 curRemote := strings.Fields(strings.TrimPrefix(line, "origin"))[0]
201 // TODO: if not in dryRun mode, just fix it?
202 log.Fatalf("Current directory's git was cloned from %q; origin should be %q", curRemote, wantRemote)
203 }
204 matches++
205 }
206 if matches == 0 {
207 log.Fatalf("git remote -v output didn't contain expected %q. Got:\n%s", wantRemote, remotes)
208 }
209 }
210
211 func cmdErr(err error) string {
212 if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
213 return fmt.Sprintf("%s: %s", err, ee.Stderr)
214 }
215 return fmt.Sprint(err)
216 }
217
218 func checkGitCodeReview() {
219 if _, err := exec.LookPath("git-codereview"); err != nil {
220 if *dry {
221 log.Fatalf("You don't appear to have git-codereview tool. While this is technically optional,\n" +
222 "almost all Go contributors use it. Our documentation and this tool assume it is used.\n" +
223 "To install it, run:\n\n\t$ go get golang.org/x/review/git-codereview\n\n(Then run go-contrib-init again)")
224 }
225 err := exec.Command("go", "get", "golang.org/x/review/git-codereview").Run()
226 if err != nil {
227 log.Fatalf("Error running go get golang.org/x/review/git-codereview: %v", cmdErr(err))
228 }
229 log.Printf("Installed git-codereview (ran `go get golang.org/x/review/git-codereview`)")
230 }
231 missing := false
232 for _, cmd := range []string{"change", "gofmt", "mail", "pending", "submit", "sync"} {
233 v, _ := exec.Command("git", "config", "alias."+cmd).Output()
234 if strings.Contains(string(v), "codereview") {
235 continue
236 }
237 if *dry {
238 log.Printf("Missing alias. Run:\n\t$ git config alias.%s \"codereview %s\"", cmd, cmd)
239 missing = true
240 } else {
241 err := exec.Command("git", "config", "alias."+cmd, "codereview "+cmd).Run()
242 if err != nil {
243 log.Fatalf("Error setting alias.%s: %v", cmd, cmdErr(err))
244 }
245 }
246 }
247 if missing {
248 log.Fatalf("Missing aliases. (While optional, this tool assumes you use them.)")
249 }
250 }