-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
getter.go
156 lines (141 loc) · 3.66 KB
/
getter.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
package main
import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"sync"
"github.com/x-motemen/ghq/logger"
)
var seen sync.Map
func getRepoLock(localRepoRoot string) bool {
_, loaded := seen.LoadOrStore(localRepoRoot, struct{}{})
return !loaded
}
type getInfo struct {
localRepository *LocalRepository
}
type getter struct {
update, shallow, silent, ssh, recursive, bare bool
vcs, branch string
}
func (g *getter) get(argURL string) (getInfo, error) {
u, err := newURL(argURL, g.ssh, false)
if err != nil {
return getInfo{}, fmt.Errorf("could not parse URL %q: %w", argURL, err)
}
branch := g.branch
if pos := strings.LastIndexByte(u.Path, '@'); pos >= 0 {
u.Path, branch = u.Path[:pos], u.Path[pos+1:]
}
remote, err := NewRemoteRepository(u)
if err != nil {
return getInfo{}, err
}
return g.getRemoteRepository(remote, branch)
}
// getRemoteRepository clones or updates a remote repository remote.
// If doUpdate is true, updates the locally cloned repository. Otherwise does nothing.
// If isShallow is true, does shallow cloning. (no effect if already cloned or the VCS is Mercurial and git-svn)
func (g *getter) getRemoteRepository(remote RemoteRepository, branch string) (getInfo, error) {
remoteURL := remote.URL()
local, err := LocalRepositoryFromURL(remoteURL, g.bare)
if err != nil {
return getInfo{}, err
}
info := getInfo{
localRepository: local,
}
var (
fpath = local.FullPath
newPath = false
)
_, err = os.Stat(fpath)
if err != nil {
if os.IsNotExist(err) {
newPath = true
err = nil
}
if err != nil {
return getInfo{}, err
}
}
switch {
case newPath:
if remoteURL.Scheme == "codecommit" {
logger.Log("clone", fmt.Sprintf("%s -> %s", remoteURL.Opaque, fpath))
} else {
logger.Log("clone", fmt.Sprintf("%s -> %s", remoteURL, fpath))
}
var (
localRepoRoot = fpath
repoURL = remoteURL
)
vcs, ok := vcsRegistry[g.vcs]
if !ok {
vcs, repoURL, err = remote.VCS()
if err != nil {
return getInfo{}, err
}
}
if l := detectLocalRepoRoot(remoteURL.Path, repoURL.Path); l != "" {
localRepoRoot = filepath.Join(local.RootPath, remoteURL.Hostname(), l)
}
if g.bare {
localRepoRoot = localRepoRoot + ".git"
}
if remoteURL.Scheme == "codecommit" {
repoURL, _ = url.Parse(remoteURL.Opaque)
}
if getRepoLock(localRepoRoot) {
return info,
vcs.Clone(&vcsGetOption{
url: repoURL,
dir: localRepoRoot,
shallow: g.shallow,
silent: g.silent,
branch: branch,
recursive: g.recursive,
bare: g.bare,
})
}
return info, nil
case g.update:
logger.Log("update", fpath)
vcs, localRepoRoot := local.VCS()
if vcs == nil {
return getInfo{}, fmt.Errorf("failed to detect VCS for %q", fpath)
}
repoURL := remoteURL
if remoteURL.Scheme == "codecommit" {
repoURL, _ = url.Parse(remoteURL.Opaque)
}
if getRepoLock(localRepoRoot) {
return info, vcs.Update(&vcsGetOption{
url: repoURL,
dir: localRepoRoot,
silent: g.silent,
recursive: g.recursive,
bare: g.bare,
})
}
return info, nil
}
logger.Log("exists", fpath)
return info, nil
}
func detectLocalRepoRoot(remotePath, repoPath string) string {
remotePath = strings.TrimSuffix(strings.TrimSuffix(remotePath, "/"), ".git")
repoPath = strings.TrimSuffix(strings.TrimSuffix(repoPath, "/"), ".git")
pathParts := strings.Split(repoPath, "/")
pathParts = pathParts[1:]
for i := 0; i < len(pathParts); i++ {
subPath := "/" + path.Join(pathParts[i:]...)
if subIdx := strings.Index(remotePath, subPath); subIdx >= 0 {
return remotePath[0:subIdx] + subPath
}
}
return ""
}