forked from robfig/glock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
143 lines (125 loc) · 3.48 KB
/
util.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
package main
import (
"fmt"
"go/build"
"io"
"os"
"path"
"path/filepath"
)
// managedRepo is the repo being managed by glock (where the GLOCKFILE resides)
// Like repoRoot, except managedRepo is allowed to be outside the GOPATH.
type managedRepo struct {
vcs *vcsCmd
dir string
}
// managedRepoRoot finds the VCS root for a given import path. Unlike
// dependency repos, the source repo is allowed to have its root outside of the
// GOPATH. This supports the use case of mounting your GOPATH within a
// repository, common in polyglot repos.
func managedRepoRoot(importPath string) (*managedRepo, error) {
var pkg, err = build.Import(importPath, "", build.FindOnly)
if err != nil {
return nil, err
}
var dir = pkg.Dir
for len(dir) > 1 {
vcs, err := lookVCS(dir)
if err == nil {
return &managedRepo{vcs, dir}, nil
}
dir = filepath.Dir(dir)
}
return nil, fmt.Errorf("no version control directory found for %q", importPath)
}
// glockRepoRootForImportPath wraps the vcs.go version. If the stock one
// doesn't work, it looks for .git, .hg directories up to the tree.
// This is done to support repos with non-go-get friendly names.
// Also, returns an error if it doesn't exist (e.g. it needs to be go gotten).
func glockRepoRootForImportPath(importPath string) (*repoRoot, error) {
var pkg, err = build.Import(importPath, "", build.FindOnly)
if err != nil {
return nil, err
}
rr, err := repoRootForImportPath(importPath)
if err == nil {
// it may not exist, even with err == nil
_, err = os.Stat(pkg.Dir)
if err != nil {
return nil, err
}
return rr, nil
}
var dir = pkg.ImportPath
for len(dir) > 1 {
rr, err := fastRepoRoot(dir)
if err == nil {
return rr, nil
}
dir = filepath.Dir(dir)
}
return nil, fmt.Errorf("no version control directory found for %q", importPath)
}
// fastRepoRoot just checks for existence of VCS dot directories to determine
// which VCS to use for the given import path.
// If none are found, an error is returned.
func fastRepoRoot(rootImportPath string) (*repoRoot, error) {
var pkg, err = build.Import(rootImportPath, "", build.FindOnly)
if err != nil {
return nil, err
}
vcs, err := lookVCS(pkg.Dir)
if err != nil {
return nil, err
}
return &repoRoot{
vcs: vcs,
repo: "",
root: rootImportPath,
}, nil
}
// lookVCS looks for known VCS dot directories in the given directory, and
// returns a vcs cmd if found, or an error if not (or if an error was encountered).
func lookVCS(dir string) (*vcsCmd, error) {
for _, vcsDir := range []string{".git", ".hg", ".bzr", ".svn"} {
_, err := os.Stat(filepath.Join(dir, vcsDir))
if os.IsNotExist(err) {
continue
}
if err != nil {
return nil, err
}
vcs := vcsByCmd(vcsDir[1:])
if vcs == nil {
return nil, fmt.Errorf("unknown version control system %q", vcsDir[1:])
}
return vcs, nil
}
return nil, fmt.Errorf("no repo found: %s", dir)
}
func gopath() string {
return filepath.SplitList(build.Default.GOPATH)[0]
}
func glockFilename(importPath string) string {
return path.Join(gopath(), "src", importPath, "GLOCKFILE")
}
func glockfileReader(importPath string, n bool) io.ReadCloser {
if n {
return os.Stdin
}
var glockfile, err = os.Open(glockFilename(importPath))
if err != nil {
perror(err)
}
return glockfile
}
func glockfileWriter(importPath string, n bool) io.WriteCloser {
if n {
return os.Stdout
}
var f, err = os.Create(glockFilename(importPath))
if err != nil {
perror(fmt.Errorf("error creating %s: %v", glockFilename, err))
}
return f
}