forked from x-motemen/ghq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getter.go
133 lines (119 loc) · 2.87 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
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"sync"
"github.com/x-motemen/ghq/logger"
)
var (
seen = make(map[string]bool)
mu = &sync.Mutex{}
)
func getRepoLock(localRepoRoot string) bool {
mu.Lock()
defer func() {
seen[localRepoRoot] = true
mu.Unlock()
}()
return !seen[localRepoRoot]
}
type getter struct {
update, shallow, silent, ssh, recursive bool
vcs, branch string
}
func (g *getter) get(argURL string) error {
u, err := newURL(argURL, g.ssh, false)
if err != nil {
return fmt.Errorf("Could not parse URL %q: %w", argURL, err)
}
remote, err := NewRemoteRepository(u)
if err != nil {
return err
}
return g.getRemoteRepository(remote)
}
// 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) error {
remoteURL := remote.URL()
local, err := LocalRepositoryFromURL(remoteURL)
if err != nil {
return err
}
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 err
}
}
switch {
case newPath:
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 err
}
}
if l := detectLocalRepoRoot(remoteURL.Path, repoURL.Path); l != "" {
localRepoRoot = filepath.Join(local.RootPath, remoteURL.Hostname(), l)
}
if getRepoLock(localRepoRoot) {
return vcs.Clone(&vcsGetOption{
url: repoURL,
dir: localRepoRoot,
shallow: g.shallow,
silent: g.silent,
branch: g.branch,
recursive: g.recursive,
})
}
return nil
case g.update:
logger.Log("update", fpath)
vcs, localRepoRoot := local.VCS()
if vcs == nil {
return fmt.Errorf("failed to detect VCS for %q", fpath)
}
if getRepoLock(localRepoRoot) {
return vcs.Update(&vcsGetOption{
dir: localRepoRoot,
silent: g.silent,
recursive: g.recursive,
})
}
return nil
}
logger.Log("exists", fpath)
return 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 ""
}