From ed2b9374599cc00467b1c27121318748cc0d3f49 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Sat, 28 Mar 2020 13:53:10 +0100 Subject: rename pkg/ to internal/ --- internal/rtree/get_test.go | 140 ++++++++++++++ internal/rtree/index.go | 431 ++++++++++++++++++++++++++++++++++++++++++ internal/rtree/init.go | 99 ++++++++++ internal/rtree/main.go | 171 +++++++++++++++++ internal/rtree/remote.go | 75 ++++++++ internal/rtree/repo.go | 225 ++++++++++++++++++++++ internal/rtree/shared_test.go | 204 ++++++++++++++++++++ 7 files changed, 1345 insertions(+) create mode 100644 internal/rtree/get_test.go create mode 100644 internal/rtree/index.go create mode 100644 internal/rtree/init.go create mode 100644 internal/rtree/main.go create mode 100644 internal/rtree/remote.go create mode 100644 internal/rtree/repo.go create mode 100644 internal/rtree/shared_test.go (limited to 'internal/rtree') diff --git a/internal/rtree/get_test.go b/internal/rtree/get_test.go new file mode 100644 index 0000000..bdd0f2e --- /dev/null +++ b/internal/rtree/get_test.go @@ -0,0 +1,140 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky +* +* 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 . +* +*******************************************************************************/ + +package rtree + +import ( + "fmt" + "path/filepath" + "testing" +) + +var testIndexWithTwoRepos = Index{ + Repos: []*Repo{ + { + CheckoutPath: "github.com/foo/bar", + Remotes: []Remote{ + {Name: "origin", URL: "https://github.com/foo/bar"}, + }, + }, + { + CheckoutPath: "github.com/git/git", + Remotes: []Remote{ + {Name: "origin", URL: "gh:git/git"}, + }, + }, + }, +} + +func TestGetExistingRepo(t *testing.T) { + Test{ + Args: []string{"get", "gh:git/git"}, + Index: testIndexWithTwoRepos, + ExpectOutput: filepath.Join(RootPath, "/github.com/git/git") + "\n", + }.Run(t) +} + +func TestGetExistingRepoWithoutShortcut(t *testing.T) { + Test{ + Args: []string{"get", "https://github.com/git/git"}, + Index: testIndexWithTwoRepos, + ExpectOutput: filepath.Join(RootPath, "/github.com/git/git") + "\n", + }.Run(t) +} + +func TestGetNewRepo(t *testing.T) { + target := filepath.Join(RootPath, "/github.com/another/repo") + + Test{ + Args: []string{"get", "gh:another/repo"}, + Index: testIndexWithTwoRepos, + ExpectOutput: target + "\n", + ExpectExecution: Recorded("git clone gh:another/repo " + target), + ExpectIndex: &Index{ + Repos: []*Repo{ + { + CheckoutPath: "github.com/another/repo", + Remotes: []Remote{ + {Name: "origin", URL: "gh:another/repo"}, + }, + }, + testIndexWithTwoRepos.Repos[0], + testIndexWithTwoRepos.Repos[1], + }, + }, + }.Run(t) +} + +func TestGetNewForkAsRemote(t *testing.T) { + target := filepath.Join(RootPath, "/github.com/git/git") + Test{ + Args: []string{"get", "https://example.com/git"}, + Index: testIndexWithTwoRepos, + Input: fmt.Sprintf("add as remote to %s\nmyfork\n", target), + ExpectOutput: target + "\n", + ExpectError: fmt.Sprintf( + "Found possible fork candidates. What to do? -> add as remote to %s\n"+ + "Existing remotes:\n\t(origin) gh:git/git\n"+ + "Enter remote name for https://example.com/git: myfork\n", + target, + ), + ExpectExecution: Recorded( + "@"+target+" git remote add myfork https://example.com/git", + "@"+target+" git remote update myfork", + ), + ExpectIndex: &Index{ + Repos: []*Repo{ + testIndexWithTwoRepos.Repos[0], + { + CheckoutPath: "github.com/git/git", + Remotes: []Remote{ + {Name: "origin", URL: "gh:git/git"}, + {Name: "myfork", URL: "https://example.com/git"}, + }, + }, + }, + }, + }.Run(t) +} + +func TestGetNewForkAsSeparate(t *testing.T) { + target := filepath.Join(RootPath, "/example.com/git") + Test{ + Args: []string{"get", "https://example.com/git"}, + Index: testIndexWithTwoRepos, + Input: fmt.Sprintf("clone to %s\n", target), + ExpectOutput: target + "\n", + ExpectError: fmt.Sprintf( + "Found possible fork candidates. What to do? -> clone to %s\n", + target, + ), + ExpectExecution: Recorded("git clone https://example.com/git " + target), + ExpectIndex: &Index{ + Repos: []*Repo{ + { + CheckoutPath: "example.com/git", + Remotes: []Remote{ + {Name: "origin", URL: "https://example.com/git"}, + }, + }, + testIndexWithTwoRepos.Repos[0], + testIndexWithTwoRepos.Repos[1], + }, + }, + }.Run(t) +} diff --git a/internal/rtree/index.go b/internal/rtree/index.go new file mode 100644 index 0000000..4ca7a41 --- /dev/null +++ b/internal/rtree/index.go @@ -0,0 +1,431 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky +* +* 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 . +* +*******************************************************************************/ + +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: + if fi.IsDir() { + //everything okay with this repo + newRepos = append(newRepos, repo) + continue + } + return fmt.Errorf("expected repository at %s, but is not a directory", 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} + break + } + remoteURLs = append(remoteURLs, remote.URL) + } + + 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(remoteURL string, allowClone bool) (*Repo, error) { + //make sure that stdout is not used for prompts + cli.Interface.StdoutProtected = true + + expandedRemoteURL := ExpandRemoteURL(remoteURL) + basename := path.Base(expandedRemoteURL) + + //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 { + otherExpandedRemoteURL := ExpandRemoteURL(remote.URL) + if expandedRemoteURL == otherExpandedRemoteURL { + return repo, nil + } + if basename == path.Base(otherExpandedRemoteURL) { + 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) + } + 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}, + 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 := CheckoutPathForRemoteURL(ExpandRemoteURL(remote.URL)) + 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() +} diff --git a/internal/rtree/init.go b/internal/rtree/init.go new file mode 100644 index 0000000..569f042 --- /dev/null +++ b/internal/rtree/init.go @@ -0,0 +1,99 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky +* +* 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 . +* +*******************************************************************************/ + +package rtree + +import ( + "os" + "path/filepath" + "regexp" + "strings" + + "github.com/majewsky/gofu/internal/cli" +) + +//RemoteAlias describes an alias that can be used in a Git remote URL (as +//defined by the "url..insteadOf" directive in man:git-config(1)). +type RemoteAlias struct { + Alias string + Replacement string +} + +//IndexPath is where the index file is stored. +var IndexPath string + +//RootPath is the directory below which all repositories are located. Its value +//is $GOPATH/src to match the repository layout created by `go get`. +var RootPath string + +//RemoteAliases is the list of remote aliases that is used by ExpandRemoteURL(). +var RemoteAliases []*RemoteAlias + +//Init initializes the global variables of this package to their standard +//values, unless they are already populated. Unit tests shall set IndexPath, +//RootPath etc. before calling Exec(), such that this function becomes a no-op +//when called by Exec(). +// +//Returns false if initialization failed. +func Init() bool { + ok := true //until shown otherwise + + if IndexPath == "" { + homeDir := os.Getenv("HOME") + if homeDir == "" { + cli.Interface.ShowError("$HOME is not set (rtree needs the HOME variable to locate its index file)") + ok = false //but keep going to report all errors at once + } else { + IndexPath = filepath.Join(homeDir, ".rtree/index.yaml") + } + } + + if RootPath == "" { + gopath := os.Getenv("GOPATH") + if gopath == "" { + cli.Interface.ShowError("$GOPATH is not set (rtree needs the GOPATH variable to know where to look for and place repos)") + ok = false //but keep going to report all errors at once + } else { + RootPath = filepath.Join(gopath, "src") + } + } + + if RemoteAliases == nil { + out, err := cli.Interface.CaptureStdout(cli.Command{ + Program: []string{"git", "config", "--global", "-l"}, + }) + if err != nil { + cli.Interface.ShowError(err.Error()) + ok = false //but keep going to report all errors at once + } + + rx := regexp.MustCompile(`^url\.([^=]+)\.insteadof=(.+)$`) + for _, line := range strings.Split(out, "\n") { + match := rx.FindStringSubmatch(line) + if match == nil { + continue + } + RemoteAliases = append(RemoteAliases, &RemoteAlias{ + Alias: match[2], + Replacement: match[1], + }) + } + } + + return ok +} diff --git a/internal/rtree/main.go b/internal/rtree/main.go new file mode 100644 index 0000000..d1692cd --- /dev/null +++ b/internal/rtree/main.go @@ -0,0 +1,171 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky +* +* 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 . +* +*******************************************************************************/ + +package rtree + +import ( + "strings" + + "github.com/majewsky/gofu/internal/cli" +) + +//Exec executes the rtree applet and returns an exit code (0 for success, >0 +//for error). The argument is os.Args minus the leading "rtree" or "gofu +//rtree". All side-effects (reading from stdin, writing to stdout/stderr, +//executing other programs) pass through cli.Interface and can be intercepted +//there for the purpose of testing. +func Exec(args []string) int { + if !Init() { + return 1 + } + + if len(args) == 0 { + return usage() + } + + index, errs := ReadIndex() + if len(errs) > 0 { + for _, err := range errs { + cli.Interface.ShowError(err.Error()) + } + return 1 + } + + var err error + switch args[0] { + case "get": + if len(args) != 2 { + return usage() + } + err = commandGet(index, args[1]) + case "drop": + if len(args) != 2 { + return usage() + } + err = commandDrop(index, args[1]) + case "index": + if len(args) != 1 { + return usage() + } + err = commandIndex(index) + case "repos": + if len(args) != 1 { + return usage() + } + commandRepos(index) + case "remotes": + if len(args) != 1 { + return usage() + } + commandRemotes(index) + case "import": + if len(args) != 2 { + return usage() + } + err = commandImport(index, args[1]) + case "each": + if len(args) < 2 { + return usage() + } + return commandEach(index, args[1:]) + default: + return usage() + } + + if err == nil { + return 0 + } + cli.Interface.ShowError(err.Error()) + return 1 +} + +var usageStr = strings.TrimSpace(` +Usage: + rtree [get|drop] + rtree [index|repos|remotes] + rtree import + rtree each +`) + +func usage() int { + cli.Interface.ShowUsage(usageStr) + return 1 +} + +func commandGet(index *Index, url string) error { + repo, err := index.FindRepo(url, true) + if err != nil { + return err + } + cli.Interface.ShowResult(repo.AbsolutePath()) + return nil +} + +func commandDrop(index *Index, url string) error { + repo, err := index.FindRepo(url, false) + if err != nil { + return err + } + return index.DropRepo(repo) +} + +func commandIndex(index *Index) error { + err := index.Rebuild() + if err != nil { + return err + } + return index.Write() +} + +func commandRepos(index *Index) { + var items []string + for _, repo := range index.Repos { + items = append(items, repo.CheckoutPath) + } + cli.Interface.ShowResultsSorted(items) +} + +func commandRemotes(index *Index) { + var items []string + for _, repo := range index.Repos { + for _, remote := range repo.Remotes { + items = append(items, remote.URL) + } + } + cli.Interface.ShowResultsSorted(items) +} + +func commandEach(index *Index, cmdline []string) (exitCode int) { + exitCode = 0 + for _, repo := range index.Repos { + err := repo.Exec(cmdline...) + if err != nil { + cli.Interface.ShowError(err.Error()) + exitCode = 1 + } + } + return +} + +func commandImport(index *Index, dirPath string) error { + err := index.ImportRepo(dirPath) + if err != nil { + return err + } + return index.Write() +} diff --git a/internal/rtree/remote.go b/internal/rtree/remote.go new file mode 100644 index 0000000..0c4e880 --- /dev/null +++ b/internal/rtree/remote.go @@ -0,0 +1,75 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky +* +* 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 . +* +*******************************************************************************/ + +package rtree + +import ( + "net/url" + "path/filepath" + "regexp" + "strings" +) + +//ExpandRemoteURL derive the canonical URL for a given remote by substituting +//aliases defined in the system-wide and user-global Git config. For example, +//with +// +// $ cat /etc/gitconfig +// [url "git://github.com/"] +// insteadOf = gh: +// +//and the input "gh:foo/bar", this function returns "git://github.com/foo/bar". +func ExpandRemoteURL(remoteURL string) string { + var best *RemoteAlias + for _, current := range RemoteAliases { + if strings.HasPrefix(remoteURL, current.Alias) { + if best == nil || len(best.Alias) < len(current.Alias) { + best = current + } + } + } + if best == nil { + return remoteURL + } + return best.Replacement + strings.TrimPrefix(remoteURL, best.Alias) +} + +//This regex recognizes the scp-like syntax for git remotes +//(i.e. "[user@]example.org:path/to/repo") as specified by the "GIT URLS" +//section of man:git-clone(1). +var scpSyntaxRx = regexp.MustCompile(`^(?:[^/@:]+@)?([^/:]+\.[^/:]+):(.+)$`) + +//CheckoutPathForRemoteURL derives the checkout path for a remote URL that has +//already been expanded with ExpandRemoteURL() if necessary. +// +// "https://example.org/foo/bar" -> "example.org/foo/bar" +// "git@example.org:foo/bar" -> "example.org/foo/bar" +// +func CheckoutPathForRemoteURL(remoteURL string) (string, error) { + match := scpSyntaxRx.FindStringSubmatch(remoteURL) + if match != nil { + //match[1] is the hostname, match[2] is the path to the repo + return filepath.Join(match[1], match[2]), nil + } + + u, err := url.Parse(remoteURL) + if err != nil { + return "", err + } + return filepath.Join(u.Hostname(), u.Path), nil +} diff --git a/internal/rtree/repo.go b/internal/rtree/repo.go new file mode 100644 index 0000000..4af51c1 --- /dev/null +++ b/internal/rtree/repo.go @@ -0,0 +1,225 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky +* +* 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 . +* +*******************************************************************************/ + +package rtree + +import ( + "fmt" + "os" + "path/filepath" + "regexp" + "strings" + + "github.com/majewsky/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 `yaml:"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 []Remote `yaml:"remotes"` +} + +//Remote describes a remote that is configured in a Repo. +type Remote struct { + Name string `yaml:"name"` + URL string `yaml:"url"` +} + +//AbsolutePath returns the absolute CheckoutPath of this repo. +func (r Repo) AbsolutePath() string { + return filepath.Join(RootPath, r.CheckoutPath) +} + +//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 + } + + for _, line := range strings.Split(out, "\n") { + match := remoteConfigRx.FindStringSubmatch(line) + if match == nil { + continue + } + repo.Remotes = append(repo.Remotes, Remote{ + Name: match[1], + URL: match[2], + }) + } + 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 string) (Repo, error) { + checkoutPath, err := CheckoutPathForRemoteURL(ExpandRemoteURL(remoteURL)) + return Repo{ + CheckoutPath: checkoutPath, + Remotes: []Remote{ + { + Name: "origin", + URL: 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 string + for _, remote := range r.Remotes { + if remote.Name == "origin" { + originURL = remote.URL + 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, r.AbsolutePath()}, + }) + if err != nil { + return err + } + } + + remotesAdded := false + for _, remote := range r.Remotes { + if remote.Name != "origin" { + err := cli.Interface.Run(cli.Command{ + Program: []string{"git", "remote", "add", remote.Name, remote.URL}, + 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 +} diff --git a/internal/rtree/shared_test.go b/internal/rtree/shared_test.go new file mode 100644 index 0000000..6720b1c --- /dev/null +++ b/internal/rtree/shared_test.go @@ -0,0 +1,204 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky +* +* 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 . +* +*******************************************************************************/ + +package rtree + +import ( + "bytes" + "errors" + "fmt" + "io" + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/majewsky/gofu/internal/cli" + yaml "gopkg.in/yaml.v2" +) + +//Path to a directory where tests can put their index files. +var indexTmpDir = filepath.Join(os.TempDir(), fmt.Sprintf("rtree-test-%d", os.Getpid())) + +func TestMain(m *testing.M) { + //make sure that test does not accidentally access user's actual rtree or index + os.Setenv("HOME", "") + os.Setenv("GOPATH", "") + //setup test configuration + RootPath = "/unittest/gopath/src" + RemoteAliases = []*RemoteAlias{ + {Alias: "gh:", Replacement: "https://github.com/"}, + {Alias: "my/", Replacement: "git@git.example.com:"}, + } + + exitCode := m.Run() + + //shared teardown + os.RemoveAll(indexTmpDir) + + os.Exit(exitCode) +} + +//Test describes a call to Main(), the environment that's given to it, and the +//assertions that are checked after the call returns. +type Test struct { + Args []string + Input string + Index Index + ExpectFailure bool + ExpectOutput string + ExpectError string + ExpectIndex *Index //if nil, .Index will be used instead + ExpectExecution []RecordedCommand +} + +func (test Test) Run(t *testing.T) { + //write index file, if any + IndexPath = filepath.Join(indexTmpDir, t.Name()+".yaml") + if test.Index.Repos != nil { + err := test.Index.Write() + if err != nil { + t.Fatalf("%s: cannot write index to %s: %s", t.Name(), IndexPath, err.Error()) + } + } + + //setup cli.Interface for test + var stdout bytes.Buffer + var stderr bytes.Buffer + cs := CommandSimulator{Cmd: test.ExpectExecution} + cli.SetupInterface(bytes.NewReader([]byte(test.Input)), &stdout, &stderr, cs.Next) + + //check exit code + exitCode := Exec(test.Args) + switch { + case exitCode == 0 && test.ExpectFailure: + t.Errorf("%s: expected failure, but returned success", t.Name()) + case exitCode != 0 && !test.ExpectFailure: + t.Errorf("%s: expected success, but returned failure", t.Name()) + } + + //check output + output := string(stdout.Bytes()) + if output != test.ExpectOutput { + t.Errorf("%s: expected stdout %#v, but got %#v", t.Name(), test.ExpectOutput, output) + } + output = string(stderr.Bytes()) + if output != test.ExpectError { + t.Errorf("%s: expected stderr %#v, but got %#v", t.Name(), test.ExpectError, output) + } + + //check index + idx := &test.Index + if test.ExpectIndex != nil { + idx = test.ExpectIndex + } + expectedIdxStr, err := yaml.Marshal(idx) + if err != nil { + t.Fatal(err.Error()) + } + actualIdxStr, err := ioutil.ReadFile(IndexPath) + if err != nil { + t.Fatalf("%s: could not read index from %s: %s", t.Name(), IndexPath, err.Error()) + } + if string(expectedIdxStr) != string(actualIdxStr) { + t.Errorf("%s: index does not match expectation after test; diff follows", t.Name()) + cmd := exec.Command("diff", "-u", "-", IndexPath) + cmd.Stdin = bytes.NewReader([]byte(expectedIdxStr)) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + if err != nil { + t.Fatal(err.Error()) + } + } +} + +type RecordedCommand struct { + Cmd cli.Command + Stdout string + Stderr string + Fails bool +} + +//Recorded is a shortcut function for initializing a []RecordedCommand. It +//splits each line on whitespace to obtain the command line of that command, +//and recognizes a leading "@/some/path" to set the workdir. +// +//This function can only be used for RecordedCommands without output that do not fail. +func Recorded(lines ...string) (cs []RecordedCommand) { + cs = make([]RecordedCommand, len(lines)) + for idx, line := range lines { + cmdline := strings.Fields(line) + if strings.HasPrefix(cmdline[0], "@") { + cs[idx].Cmd.WorkDir = strings.TrimPrefix(cmdline[0], "@") + cmdline = cmdline[1:] + } + cs[idx].Cmd.Program = cmdline + } + return +} + +//CommandSimulator implements the cli.CommandRunner interface (via its Next +//method). When a cli.Command is given to Next(), it is matched with the next +//command in the .Cmd list, and the result from that RecordedCommand is +//returned. If the given Command is different from the one expected (or if the +//.Cmd list has been exhausted), an error is returned. +type CommandSimulator struct { + Cmd []RecordedCommand + idx int +} + +func (s *CommandSimulator) Next(c cli.Command, stdin io.Reader, stdout, stderr io.Writer) error { + //take next RecordedCommand from list + if s.idx >= len(s.Cmd) { + return errors.New("got command to execute, but recorded commands have been exhausted") + } + sc := s.Cmd[s.idx] + s.idx++ + + //check if the given Command matches the expectation + if !areStringListsEqual(sc.Cmd.Program, c.Program) { + return fmt.Errorf("expected command %#v, but got %#v", + strings.Join(sc.Cmd.Program, " "), strings.Join(c.Program, " "), + ) + } + if sc.Cmd.WorkDir != c.WorkDir { + return fmt.Errorf("expected command workdir %s, but got %s", sc.Cmd.WorkDir, c.WorkDir) + } + + stdout.Write([]byte(sc.Stdout)) + stderr.Write([]byte(sc.Stderr)) + if sc.Fails { + return fmt.Errorf("command %#v has failed", strings.Join(c.Program, " ")) + } + return nil +} + +func areStringListsEqual(a []string, b []string) bool { + if len(a) != len(b) { + return false + } + for idx := range a { + if a[idx] != b[idx] { + return false + } + } + return true +} -- cgit v1.3.1