aboutsummaryrefslogtreecommitdiff
path: root/pkg/rtree/index.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2017-05-05 19:56:30 +0200
committerStefan Majewsky <majewsky@gmx.net>2017-05-05 19:56:30 +0200
commite1022c29c33363bd3c2471b7cef2d37dbd704dca (patch)
tree0dcdb756d10d0e53c4c5589fa12e5f547123b589 /pkg/rtree/index.go
parentcdf41693a87559dee892c042591a409200637b9a (diff)
downloadgofu-e1022c29c33363bd3c2471b7cef2d37dbd704dca.tar.gz
implement rtree import
Diffstat (limited to 'pkg/rtree/index.go')
-rw-r--r--pkg/rtree/index.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/pkg/rtree/index.go b/pkg/rtree/index.go
index aa2b2a3..2edc452 100644
--- a/pkg/rtree/index.go
+++ b/pkg/rtree/index.go
@@ -298,3 +298,52 @@ func (i *Index) InteractiveFindRepo(remoteURL string, allowClone bool) *Repo {
i.Write()
return target
}
+
+//InteractiveImportRepo moves the given repo into the rtree and adds it to the index.
+func (i *Index) InteractiveImportRepo(dirPath string) {
+ //need to make dirPath absolute first
+ dirPath, err := filepath.Abs(dirPath)
+ util.FatalIfError(err)
+ repo, err := NewRepoFromAbsolutePath(dirPath)
+ util.FatalIfError(err)
+
+ //repo must be outside $GOPATH/src
+ if !strings.HasPrefix(repo.CheckoutPath, "../") {
+ util.FatalIfError(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 := CheckoutPathForRemoteURL(ExpandRemoteURL(remote.URL))
+ if remote.Name == "origin" {
+ //prefer "origin" over everything else
+ checkoutPath = thisPath
+ break
+ }
+ choices[idx] = cli.Choice{Text: thisPath}
+ }
+
+ //cannot decide myself -> let the user select
+ if checkoutPath == "" {
+ if len(choices) == 0 {
+ util.FatalIfError(errors.New("repo has no remotes"))
+ }
+
+ question := fmt.Sprintf("Repo has multiple remotes. Where to put below %s?", RootPath)
+ choice, _ := cli.Query(question, choices...)
+ checkoutPath = choice.Text
+ }
+
+ //double-check that there is no such repo in the rtree yet
+ for _, other := range i.Repos {
+ if other.CheckoutPath == checkoutPath {
+ util.FatalIfError(errors.New("will not overwrite existing checkout at " + other.AbsolutePath()))
+ }
+ }
+
+ //do the move
+ util.FatalIfError(repo.Move(checkoutPath, true))
+ i.Repos = append(i.Repos, &repo)
+}