This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
repo_config.go
204 lines (184 loc) · 5.4 KB
/
repo_config.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
package src
import (
"bytes"
"errors"
"fmt"
"log"
"path/filepath"
"code.google.com/p/rog-go/parallel"
"os"
"os/exec"
"strings"
"syscall"
"sourcegraph.com/sourcegraph/srclib/graph"
)
type Repo struct {
RootDir string // Root directory containing repository being analyzed
VCSType string // VCS type (git or hg)
CommitID string // CommitID of current working directory
CloneURL string // CloneURL of repo.
}
// URI returns the Repo's URI. It returns the empty string if the
// Repo's CloneURL is malformed or empty.
func (c *Repo) URI() string {
if uri, err := graph.TryMakeURI(c.CloneURL); err != nil {
return ""
} else {
return uri
}
}
func OpenRepo(dir string) (*Repo, error) {
if fi, err := os.Stat(dir); err != nil || !fi.Mode().IsDir() {
return nil, fmt.Errorf("not a directory: %q", dir)
}
rc := new(Repo)
// VCS and root directory
var err error
rc.RootDir, rc.VCSType, err = getRootDir(dir)
if err != nil || rc.RootDir == "" {
log.Printf("Failed to detect git/hg repository root dir for %q; continuing.", dir)
// Be permissive and return a repo even if there is no git/hg repository.
wd, err := os.Getwd()
if err != nil {
return nil, err
}
rc.RootDir = wd
// TODO: Ensure that builds without commit ids are successful.
rc.CommitID = "ffffffffffffffffffffffffffffffffffffffff"
rc.VCSType = ""
rc.CloneURL = ""
return rc, nil
}
par := parallel.NewRun(4)
par.Do(func() error {
// Current commit ID
var err error
rc.CommitID, err = resolveWorkingTreeRevision(rc.VCSType, rc.RootDir)
return err
})
par.Do(func() error {
// Get repo URI from clone URL.
rc.CloneURL = getVCSCloneURL(rc.VCSType, rc.RootDir)
return nil
})
return rc, par.Wait()
}
func resolveWorkingTreeRevision(vcsType string, dir string) (string, error) {
var cmd *exec.Cmd
switch vcsType {
case "git":
cmd = exec.Command("git", "rev-parse", "HEAD")
case "hg":
cmd = exec.Command("hg", "--config", "trusted.users=root", "identify", "--debug", "-i")
default:
return "", fmt.Errorf("unknown vcs type: %q", vcsType)
}
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("exec %v failed: %s. Output was:\n\n%s", cmd.Args, err, out)
}
// hg adds a "+" if the wd is dirty
return strings.TrimSuffix(string(bytes.TrimSpace(out)), "+"), nil
}
func getRootDir(dir string) (rootDir string, vcsType string, err error) {
dir, err = filepath.Abs(dir)
if err != nil {
return "", "", err
}
ancestors := ancestorDirsAndSelfExceptRoot(dir)
vcsTypes := []string{"git", "hg"}
for i := len(ancestors) - 1; i >= 0; i-- {
ancDir := ancestors[i]
for _, vt := range vcsTypes {
// Don't check that the FileInfo is a dir because git
// submodules have a .git file.
if _, err := os.Stat(filepath.Join(ancDir, "."+vt)); err == nil {
return ancDir, vt, nil
}
}
}
return "", "", nil
}
// ancestorDirsAndSelfExceptRoot returns a list of p's ancestor
// directories (including itself but excluding the root ("." or "/")).
func ancestorDirsAndSelfExceptRoot(p string) []string {
if p == "" {
return nil
}
if len(p) == 1 && (p[0] == '.' || p[0] == '/') {
return nil
}
var dirs []string
for i, c := range p {
if c == '/' {
dirs = append(dirs, p[:i])
}
}
dirs = append(dirs, p)
return dirs
}
// getVCSCloneURL gets the primary remote url. getVCSCloneURL returns
// the empty string if it fails or if there is no primary remote.
func getVCSCloneURL(vcsType string, repoDir string) string {
logWarning := func(err error) {
if GlobalOpt.Verbose {
log.Printf("Warning: getVCSCloneURL: %s", err)
}
}
run := func(args ...string) (string, error) {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = repoDir
out, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
cloneURL := strings.TrimSpace(string(out))
if vcsType == "git" {
cloneURL = strings.Replace(cloneURL, "git@github.com:", "git://github.com/", 1)
}
return cloneURL, nil
}
switch vcsType {
case "git":
// Try to get the "srclib" remote first.
url, err := run("git", "config", "remote.srclib.url")
if err == nil {
return url
}
url, err = run("git", "config", "remote.origin.url")
if code, _ := exitStatus(err); code == 1 {
// `git config --get` returns exit code 1 if the config key doesn't exist.
logWarning(errNoVCSCloneURL)
return ""
} else if err != nil {
logWarning(errNoVCSCloneURL)
return ""
}
return url
case "hg":
cloneURL, err := run("hg", "--config", "trusted.users=root", "paths", "default")
if err != nil {
logWarning(err)
return ""
}
return cloneURL
default:
logWarning(fmt.Errorf("unrecognized VCS %v", vcsType))
return ""
}
}
var errNoVCSCloneURL = errors.New("Could not determine remote clone URL for the current repository. For git repositories, srclib checks for remotes named 'srclib' or 'origin' (in that order). Run 'git remote add NAME URL' to add a remote, where NAME is either 'srclib' or 'origin' and URL is a git clone URL (e.g. https://example.com/repo.git).' to add a remote. For hg repositories, srclib checks the 'default' remote.")
func exitStatus(err error) (uint32, error) {
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
// There is no platform independent way to retrieve
// the exit code, but the following will work on Unix
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
return uint32(status.ExitStatus()), nil
}
}
return 0, err
}
return 0, nil
}