summaryrefslogtreecommitdiff
path: root/internal/rtree/index.go
blob: b802ef8b29c5bdbae78817753632a0a832efeb33 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*******************************************************************************
*
* Copyright 2017 Stefan Majewsky <majewsky@gmx.net>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

package rtree

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"sort"
	"strings"

	"github.com/majewsky/gofu/internal/cli"

	yaml "gopkg.in/yaml.v2"
)

//Index represents the contents of the index file.
type Index struct {
	Repos []*Repo `yaml:"repos"`
}

//ReadIndex reads the index file.
func ReadIndex() (*Index, []error) {
	//read contents of index file
	buf, err := ioutil.ReadFile(IndexPath)
	if err != nil {
		if os.IsNotExist(err) {
			return &Index{Repos: nil}, nil
		}
		return nil, []error{err}
	}

	//deserialize YAML
	var index Index
	err = yaml.Unmarshal(buf, &index)
	if err != nil {
		return nil, []error{err}
	}

	//validate YAML
	var errs []error
	missing := func(key string, args ...interface{}) {
		errs = append(errs, fmt.Errorf("read %s: missing \"%s\"",
			IndexPath, fmt.Sprintf(key, args...),
		))
	}
	for idx, repo := range index.Repos {
		if repo.CheckoutPath == "" {
			missing("repos[%d].path", idx)
		}
		if len(repo.Remotes) == 0 {
			missing("repos[%d].remotes", idx)
		}
		for idx2, remote := range repo.Remotes {
			switch {
			case remote.Name == "":
				missing("repos[%d].remotes[%d].name", idx, idx2)
			case remote.URL == "":
				missing("repos[%d].remotes[%d].url", idx, idx2)
			}
		}
	}

	sort.Sort(reposByAbsPath(index.Repos))
	return &index, errs
}

type reposByAbsPath []*Repo

func (r reposByAbsPath) Len() int           { return len(r) }
func (r reposByAbsPath) Less(i, j int) bool { return r[i].AbsolutePath() < r[j].AbsolutePath() }
func (r reposByAbsPath) Swap(i, j int)      { r[i], r[j] = r[j], r[i] }

//Write writes the index file to disk.
func (i *Index) Write() error {
	sort.Sort(reposByAbsPath(i.Repos))
	buf, err := yaml.Marshal(i)
	if err != nil {
		return err
	}

	err = os.MkdirAll(filepath.Dir(IndexPath), 0755)
	if err != nil {
		return err
	}
	err = ioutil.WriteFile(IndexPath, buf, 0644)
	if err != nil {
		return err
	}

	//perform sanity check (TODO: do this instead when rebuilding the index)
	seen := make(map[string]bool)
	warned := make(map[string]bool)
	for _, repo := range i.Repos {
		if seen[repo.CheckoutPath] && !warned[repo.CheckoutPath] {
			cli.Interface.ShowWarning(
				fmt.Sprintf("repo %s appears multiple times in the index file!", repo.AbsolutePath()),
			)
			warned[repo.CheckoutPath] = true
		}
		seen[repo.CheckoutPath] = true
	}

	return nil
}

//Rebuild implements the `rtree index` subcommand.
func (i *Index) Rebuild() error {
	//check if existing index entries are still checked out
	var newRepos []*Repo
	for _, repo := range i.Repos {
		gitDirPath := filepath.Join(repo.AbsolutePath(), ".git")
		fi, err := os.Stat(gitDirPath)
		switch {
		case err == nil:
			// in a normal repo .git is a directory but when the repo is a submodule of another repo
			// and the .git dir is absorbed then it is a file which contains the path to the real .git directory
			if fi.IsDir() || fi.Mode().IsRegular() {
				//everything okay with this repo
				newRepos = append(newRepos, repo)
				continue
			}
			return fmt.Errorf("expected repository at %s, but is not a directory or file", gitDirPath)
		case !os.IsNotExist(err):
			return err
		}

		//repo has been deleted - ask what to do
		var remoteURLs []string
		for _, remote := range repo.Remotes {
			if remote.Name == "origin" {
				remoteURLs = []string{remote.URL.CompactURL()}
				break
			}
			remoteURLs = append(remoteURLs, remote.URL.CompactURL())
		}

		var selection string
		if len(remoteURLs) == 0 {
			selection, err = cli.Interface.Query(
				fmt.Sprintf("repository %s has been deleted; no remote to restore from", filepath.Join(RootPath, repo.CheckoutPath)),
				cli.Choice{Return: "d", Shortcut: 'd', Text: "delete from index"},
				cli.Choice{Return: "s", Shortcut: 's', Text: "skip"},
			)
		} else {
			selection, err = cli.Interface.Query(
				fmt.Sprintf("repository %s has been deleted", filepath.Join(RootPath, repo.CheckoutPath)),
				cli.Choice{Return: "r", Shortcut: 'r', Text: "restore from " + strings.Join(remoteURLs, " and ")},
				cli.Choice{Return: "d", Shortcut: 'd', Text: "delete from index"},
				cli.Choice{Return: "s", Shortcut: 's', Text: "skip"},
			)
		}
		if err != nil {
			return err
		}

		switch selection {
		case "r":
			err := repo.Checkout()
			if err != nil {
				return err
			}
			newRepos = append(newRepos, repo)
		case "d":
			continue
		case "s":
			newRepos = append(newRepos, repo)
		}
	}

	existingRepos := make(map[string]*Repo)
	for _, repo := range newRepos {
		existingRepos[repo.CheckoutPath] = repo
	}

	//index new repos
	err := ForeachPhysicalRepo(func(newRepo Repo) error {
		repo, exists := existingRepos[newRepo.CheckoutPath]
		if exists {
			//update the existing index entry with the new remotes
			repo.Remotes = newRepo.Remotes
		} else {
			newRepos = append(newRepos, &newRepo)
		}
		return nil
	})
	if err != nil {
		return err
	}

	i.Repos = newRepos
	return nil
}

//FindRepo locates the repo with the given remote if it exists on disk or (if
//allowClone is set) clones it and adds it to the index. This is the meat of
//`rtree get`, and is also used by `rtree drop`.
func (i *Index) FindRepo(rawRemoteURL string, allowClone bool) (*Repo, error) {
	//make sure that stdout is not used for prompts
	cli.Interface.StdoutProtected = true

	remoteURL := ParseRemoteURL(rawRemoteURL)
	basename := path.Base(remoteURL.CanonicalURL())

	//is this remote already checked out directly? also look for repos with the
	//same basename that could be forks
	var candidates []*Repo
	for _, repo := range i.Repos {
		isCandidate := false
		for _, remote := range repo.Remotes {
			if remoteURL == remote.URL {
				return repo, nil
			}
			if basename == path.Base(remote.URL.CanonicalURL()) {
				isCandidate = true
			}
		}
		if isCandidate {
			candidates = append(candidates, repo)
		}
	}

	//double-check if the repo is already checked out, but we didn't notice it yet
	newRepo, err := NewRepoFromRemoteURL(remoteURL)
	if err != nil {
		return nil, err
	}
	_, err = os.Stat(newRepo.AbsolutePath())
	switch {
	case err == nil:
		return nil, fmt.Errorf(
			"%s already exists (if there is a repo there, try `rtree index`)",
			newRepo.AbsolutePath(),
		)
	case !os.IsNotExist(err):
		return nil, err
	}

	if !allowClone {
		return nil, errors.New("no such remote in index (you can validate the index with `rtree index`)")
	}

	//if no fork candidates found, clone as new repo
	if len(candidates) == 0 {
		err := newRepo.Checkout()
		if err != nil {
			return nil, err
		}
		i.Repos = append(i.Repos, &newRepo)
		i.Write()
		return &newRepo, nil
	}

	//if we found fork candidates, ask the user to match the repo with a fork
	//candidate (or confirm that the repo shall be cloned fresh)
	if len(candidates) > 10 {
		candidates = candidates[:10]
	}
	choices := make([]cli.Choice, len(candidates)+1)
	for idx, repo := range candidates {
		choices[idx] = cli.Choice{Text: "add as remote to " + repo.AbsolutePath(), Return: repo.CheckoutPath}
	}
	choices[len(candidates)] = cli.Choice{
		Return:   "clone",
		Shortcut: 'n',
		Text:     "clone to " + newRepo.AbsolutePath(),
	}
	selection, err := cli.Interface.Query("Found possible fork candidates. What to do?", choices...)
	if err != nil {
		return nil, err
	}

	if selection == "clone" {
		err := newRepo.Checkout()
		if err != nil {
			return nil, err
		}
		i.Repos = append(i.Repos, &newRepo)
		i.Write()
		return &newRepo, nil
	}

	//find the repo selected by the user
	var target *Repo
	for _, repo := range candidates {
		if repo.CheckoutPath == selection {
			target = repo
			break
		}
	}

	//report the existing remotes, and ask for the name of the new remote
	prompt := "Existing remotes:\n"
	for _, remote := range target.Remotes {
		prompt += fmt.Sprintf("\t(%s) %s\n", remote.Name, remote.URL.CompactURL())
	}
	prompt += fmt.Sprintf("Enter remote name for %s:", remoteURL)
	remoteName, err := cli.Interface.ReadLine(prompt)
	if err != nil {
		return nil, err
	}

	err = cli.Interface.Run(cli.Command{
		Program: []string{"git", "remote", "add", remoteName, remoteURL.CompactURL()},
		WorkDir: target.AbsolutePath(),
	})
	if err != nil {
		return nil, err
	}

	err = cli.Interface.Run(cli.Command{
		Program: []string{"git", "remote", "update", remoteName},
		WorkDir: target.AbsolutePath(),
	})
	if err != nil {
		return nil, err
	}

	target.Remotes = append(target.Remotes, Remote{
		Name: remoteName,
		URL:  remoteURL,
	})
	i.Write()
	return target, nil
}

//ImportRepo moves the given repo into the rtree and adds it to the index.
func (i *Index) ImportRepo(dirPath string) error {
	//need to make dirPath absolute first
	dirPath, err := filepath.Abs(dirPath)
	if err != nil {
		return err
	}
	repo, err := NewRepoFromAbsolutePath(dirPath)
	if err != nil {
		return err
	}

	//repo must be outside $GOPATH/src
	if !strings.HasPrefix(repo.CheckoutPath, "../") {
		return fmt.Errorf("%s is already inside GOPATH", dirPath)
	}

	//select the remote which determines the checkout path
	choices := make([]cli.Choice, len(repo.Remotes))
	var checkoutPath string
	for idx, remote := range repo.Remotes {
		thisPath, err := remote.URL.CheckoutPath()
		if err != nil {
			return err
		}
		if remote.Name == "origin" {
			//prefer "origin" over everything else
			checkoutPath = thisPath
			break
		}
		choices[idx] = cli.Choice{Return: thisPath, Text: thisPath}
	}

	//cannot decide myself -> let the user select
	if checkoutPath == "" {
		if len(choices) == 0 {
			return errors.New("repo has no remotes")
		}

		question := fmt.Sprintf("Repo has multiple remotes. Where to put below %s?", RootPath)
		checkoutPath, err = cli.Interface.Query(question, choices...)
		if err != nil {
			return err
		}
	}

	//double-check that there is no such repo in the rtree yet
	for _, other := range i.Repos {
		if other.CheckoutPath == checkoutPath {
			return errors.New("will not overwrite existing checkout at " + other.AbsolutePath())
		}
	}

	//do the move
	err = repo.Move(checkoutPath, true)
	if err != nil {
		return err
	}
	i.Repos = append(i.Repos, &repo)
	return nil
}

//DropRepo deletes the given repo from the rtree and removes it from the index.
func (i *Index) DropRepo(repo *Repo) error {
	err := repo.Exec("git", "status")
	if err != nil {
		return err
	}
	ok, err := cli.Interface.Confirm(">> Drop this repo?")
	if !ok || err != nil {
		return err
	}

	err = os.RemoveAll(repo.AbsolutePath())
	if err != nil {
		return err
	}

	reposNew := make([]*Repo, 0, len(i.Repos)-1)
	for _, r := range i.Repos {
		if r.CheckoutPath != repo.CheckoutPath {
			reposNew = append(reposNew, r)
		}
	}
	i.Repos = reposNew
	return i.Write()
}