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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
// SPDX-FileCopyrightText: 2017 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: GPL-3.0-only
package rtree
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"git.xyrillian.de/gofu/internal/cli"
)
// Repo describes the entry for a repository in the index file.
type Repo struct {
//CheckoutPath shall be relative to the RootPath.
CheckoutPath string `json:"path"`
//Remotes maps remote names (as noted in the .git/config of the repo) to
//remote URLs (as they appear in the .git/config of the repo, i.e. possibly
//abbreviated).
Remotes map[string]Remote `json:"remotes"`
}
// Remote describes a remote that is configured in a Repo.
type Remote struct {
URLs []RemoteURL `json:"urls"`
}
// AbsolutePath returns the absolute CheckoutPath of this repo.
func (r Repo) AbsolutePath() string {
return filepath.Join(RootPath, r.CheckoutPath)
}
// GitDirPath returns the path of the .git directory of this repo.
func (r Repo) GitDirPath() string {
return filepath.Join(r.AbsolutePath(), ".git")
}
// CompactURLs returns all URLs for this remote in their compact form.
func (r Remote) CompactURLs() []string {
result := make([]string, len(r.URLs))
for idx, url := range r.URLs {
result[idx] = url.CompactURL()
}
return result
}
// NewRepoFromAbsolutePath initializes a Repo instance by scanning the existing
// checkout at the given path.
func NewRepoFromAbsolutePath(path string) (repo Repo, err error) {
repo.CheckoutPath, err = filepath.Rel(RootPath, path)
if err != nil {
return
}
//list remotes
out, err := cli.Interface.CaptureStdout(cli.Command{
Program: []string{"git", "config", "-l"},
WorkDir: path,
})
if err != nil {
return
}
repo.Remotes = make(map[string]Remote)
for line := range strings.SplitSeq(out, "\n") {
match := remoteConfigRx.FindStringSubmatch(line)
if match == nil {
continue
}
name, url := match[1], ParseRemoteURL(match[2])
if remote, ok := repo.Remotes[name]; ok {
remote.URLs = append(remote.URLs, url)
repo.Remotes[name] = remote
} else {
repo.Remotes[name] = Remote{URLs: []RemoteURL{url}}
}
}
return
}
// NewRepoFromRemoteURL initializes a Repo instance for checking out a remote
// for the first time. The checkout does not happen until Checkout() is called.
func NewRepoFromRemoteURL(remoteURL RemoteURL) (Repo, error) {
checkoutPath, err := remoteURL.CheckoutPath()
return Repo{
CheckoutPath: checkoutPath,
Remotes: map[string]Remote{
"origin": {
URLs: []RemoteURL{remoteURL},
},
},
}, err
}
var remoteConfigRx = regexp.MustCompile(`remote\.([^=]+)\.url=(.+)`)
// ForeachPhysicalRepo walks over the repository tree, executing the action
// function once for every repo encountered (but *not* for repos contained
// within other repos, e.g. submodules).
func ForeachPhysicalRepo(action func(repo Repo) error) error {
return filepath.Walk(RootPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
//look for repos, i.e. directories containing a .git directory
if !info.IsDir() {
return nil
}
_, err = os.Stat(filepath.Join(path, ".git"))
if err != nil {
return nil
}
//appears to be a repo
repo, err := NewRepoFromAbsolutePath(path)
if err == nil {
err = action(repo)
}
if err != nil {
return err
}
//do not traverse further down into submodules etc.
return filepath.SkipDir
})
}
// Checkout creates the repo in the given path with the given remotes. The
// working copy will only be initialized if there is an "origin" remote.
func (r Repo) Checkout() error {
//check if we have an "origin" remote to clone from
var originURL RemoteURL
for remoteName, remote := range r.Remotes {
if remoteName == "origin" {
originURL = remote.URLs[0]
break
}
}
if originURL == "" {
err := cli.Interface.Run(cli.Command{
Program: []string{"git", "init", r.AbsolutePath()},
})
if err != nil {
return err
}
cli.Interface.ShowWarning(`will not checkout anything since there is no remote named "origin"`)
} else {
err := cli.Interface.Run(cli.Command{
Program: []string{"git", "clone", originURL.CanonicalURL(), r.AbsolutePath()},
})
if err != nil {
return err
}
}
remotesAdded := false
for remoteName, remote := range r.Remotes {
for idx, url := range remote.URLs {
if idx == 0 && remoteName != "origin" {
err := cli.Interface.Run(cli.Command{
Program: []string{"git", "remote", "add", remoteName, url.CanonicalURL()},
WorkDir: r.AbsolutePath(),
})
if err != nil {
return err
}
remotesAdded = true
} else if idx > 0 {
err := cli.Interface.Run(cli.Command{
Program: []string{"git", "remote", "set-url", "--add", remoteName, url.CanonicalURL()},
WorkDir: r.AbsolutePath(),
})
if err != nil {
return err
}
remotesAdded = true
}
}
}
if remotesAdded {
return cli.Interface.Run(cli.Command{
Program: []string{"git", "remote", "update"},
WorkDir: r.AbsolutePath(),
})
}
return nil
}
// Exec implements the meat of the `rtree exec` command. It returns
// true iff the command exited successfully.
func (r Repo) Exec(cmdline ...string) error {
cli.Interface.ShowProgress(r.AbsolutePath())
return cli.Interface.Run(cli.Command{
Program: cmdline,
WorkDir: r.AbsolutePath(),
})
}
// Move sets the CheckoutPath to the given value and moves the existing repo
// from the old to the new checkoutPath. If makeSymlink is given, a symlink will
// be created from the old to the new location.
func (r *Repo) Move(checkoutPath string, makeSymlink bool) error {
sourcePath := filepath.Join(RootPath, r.CheckoutPath)
targetPath := filepath.Join(RootPath, checkoutPath)
//ensure that target does not exist
_, err := os.Lstat(targetPath)
if err == nil {
return fmt.Errorf("cannot move %s to %s: target exists in filesystem", sourcePath, targetPath)
}
if !os.IsNotExist(err) {
return err
}
//prepare directory to move repo into
err = os.MkdirAll(filepath.Dir(targetPath), 0755)
if err != nil {
return err
}
//move directory
err = os.Rename(sourcePath, targetPath)
if err != nil {
return err
}
r.CheckoutPath = checkoutPath
//if requested, make compatibility symlink
if makeSymlink {
return os.Symlink(targetPath, sourcePath)
}
return nil
}
// ReformatRemoteURLs rewrites the remote URLs in this repo's .git/config into
// their canonical forms.
func (r Repo) ReformatRemoteURLs() error {
// NOTE: This is a bit convoluted because the specific case of updating URLs
// for a remote with multiple URLs requires multiple steps. First, we clear
// out all non-primary URLs, and then re-add them after updating the primary URL.
actualRepo, err := NewRepoFromAbsolutePath(r.AbsolutePath())
if err != nil {
return err
}
for remoteName, remote := range r.Remotes {
actualRemote := actualRepo.Remotes[remoteName]
if len(actualRemote.URLs) > 1 {
for _, url := range actualRemote.URLs[1:] {
err := cli.Interface.Run(cli.Command{
Program: []string{"git", "remote", "set-url", "--delete", remoteName, url.CanonicalURL()},
WorkDir: r.AbsolutePath(),
})
if err != nil {
return err
}
}
}
err := cli.Interface.Run(cli.Command{
Program: []string{"git", "remote", "set-url", remoteName, remote.URLs[0].CanonicalURL()},
WorkDir: r.AbsolutePath(),
})
if err != nil {
return err
}
if len(remote.URLs) > 1 {
for _, url := range remote.URLs[1:] {
err := cli.Interface.Run(cli.Command{
Program: []string{"git", "remote", "set-url", "--add", remoteName, url.CanonicalURL()},
WorkDir: r.AbsolutePath(),
})
if err != nil {
return err
}
}
}
}
return nil
}
|