From c89c976ecd0bf10e668f015e62778c55f2861597 Mon Sep 17 00:00:00 2001 From: Songmu Date: Thu, 26 Dec 2019 00:52:42 +0900 Subject: [PATCH 1/4] if GHQ_ROOT env is specified, don't urlMatchLocalRepositoryRoots --- local_repository.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/local_repository.go b/local_repository.go index d3eab8a..fc4d735 100644 --- a/local_repository.go +++ b/local_repository.go @@ -347,7 +347,7 @@ func localRepositoryRoots(all bool) ([]string, error) { _localRepositoryRoots = []string{filepath.Join(homeDir, ".ghq")} } - if all { + if all && envRoot == "" { roots, err := urlMatchLocalRepositoryRoots() if err != nil { return nil, err From 38562bbe5cbfbbc8883c43c77b950db04329659c Mon Sep 17 00:00:00 2001 From: Songmu Date: Thu, 26 Dec 2019 00:58:38 +0900 Subject: [PATCH 2/4] define envGhqRoot --- cmd_list_test.go | 10 +++++----- cmd_root_test.go | 15 +++++++-------- local_repository.go | 4 +++- local_repository_test.go | 8 ++++---- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/cmd_list_test.go b/cmd_list_test.go index 5a67c39..41185f1 100644 --- a/cmd_list_test.go +++ b/cmd_list_test.go @@ -175,7 +175,7 @@ func TestDoList_query(t *testing.T) { func TestDoList_unique(t *testing.T) { defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) - defer func(orig string) { os.Setenv("GHQ_ROOT", orig) }(os.Getenv("GHQ_ROOT")) + defer func(orig string) { os.Setenv(envGhqRoot, orig) }(os.Getenv(envGhqRoot)) tmp1 := newTempDir(t) defer os.RemoveAll(tmp1) @@ -184,7 +184,7 @@ func TestDoList_unique(t *testing.T) { _localRepositoryRoots = nil rootPaths := []string{tmp1, tmp2} - os.Setenv("GHQ_ROOT", strings.Join(rootPaths, string(os.PathListSeparator))) + os.Setenv(envGhqRoot, strings.Join(rootPaths, string(os.PathListSeparator))) for _, rootPath := range rootPaths { os.MkdirAll(filepath.Join(rootPath, "github.com/motemen/ghq/.git"), 0755) } @@ -198,7 +198,7 @@ func TestDoList_unique(t *testing.T) { func TestDoList_unknownRoot(t *testing.T) { defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) - defer tmpEnv("GHQ_ROOT", "/path/to/unknown-ghq")() + defer tmpEnv(envGhqRoot, "/path/to/unknown-ghq")() _localRepositoryRoots = nil err := newApp().Run([]string{"ghq", "list"}) @@ -217,7 +217,7 @@ func TestDoList_notPermittedRoot(t *testing.T) { os.Chmod(dir, 0755) os.RemoveAll(dir) }(tmpdir) - defer tmpEnv("GHQ_ROOT", tmpdir)() + defer tmpEnv(envGhqRoot, tmpdir)() _localRepositoryRoots = nil os.Chmod(tmpdir, 0000) @@ -240,7 +240,7 @@ func TestDoList_withSystemHiddenDir(t *testing.T) { os.Chmod(systemHidden, 0755) os.RemoveAll(dir) }(tmpdir) - defer tmpEnv("GHQ_ROOT", tmpdir)() + defer tmpEnv(envGhqRoot, tmpdir)() _localRepositoryRoots = nil diff --git a/cmd_root_test.go b/cmd_root_test.go index e3f5179..3705d22 100644 --- a/cmd_root_test.go +++ b/cmd_root_test.go @@ -35,7 +35,6 @@ func samePaths(lhs, rhs string) bool { } func TestDoRoot(t *testing.T) { - ghqrootEnv := "GHQ_ROOT" testCases := []struct { name string setup func() func() @@ -43,23 +42,23 @@ func TestDoRoot(t *testing.T) { }{{ name: "env", setup: func() func() { - orig := os.Getenv(ghqrootEnv) - os.Setenv(ghqrootEnv, "/path/to/ghqroot1"+string(os.PathListSeparator)+"/path/to/ghqroot2") - return func() { os.Setenv(ghqrootEnv, orig) } + orig := os.Getenv(envGhqRoot) + os.Setenv(envGhqRoot, "/path/to/ghqroot1"+string(os.PathListSeparator)+"/path/to/ghqroot2") + return func() { os.Setenv(envGhqRoot, orig) } }, expect: "/path/to/ghqroot1\n", allExpect: "/path/to/ghqroot1\n/path/to/ghqroot2\n", }, { name: "gitconfig", setup: func() func() { - orig := os.Getenv(ghqrootEnv) - os.Setenv(ghqrootEnv, "") + orig := os.Getenv(envGhqRoot) + os.Setenv(envGhqRoot, "") teardown := gitconfig.WithConfig(t, `[ghq] root = /path/to/ghqroot11 root = /path/to/ghqroot12 `) return func() { - os.Setenv(ghqrootEnv, orig) + os.Setenv(envGhqRoot, orig) teardown() } }, @@ -76,7 +75,7 @@ func TestDoRoot(t *testing.T) { } f.Close() - restore1 := tmpEnv(ghqrootEnv, "") + restore1 := tmpEnv(envGhqRoot, "") restore2 := tmpEnv("GIT_CONFIG", fpath) restore3 := tmpEnv("HOME", "/path/to/ghqhome") diff --git a/local_repository.go b/local_repository.go index fc4d735..93ad75a 100644 --- a/local_repository.go +++ b/local_repository.go @@ -14,6 +14,8 @@ import ( "github.com/saracen/walker" ) +const envGhqRoot = "GHQ_ROOT" + // LocalRepository represents local repository type LocalRepository struct { FullPath string @@ -328,7 +330,7 @@ func localRepositoryRoots(all bool) ([]string, error) { return _localRepositoryRoots, nil } - envRoot := os.Getenv("GHQ_ROOT") + envRoot := os.Getenv(envGhqRoot) if envRoot != "" { _localRepositoryRoots = filepath.SplitList(envRoot) } else { diff --git a/local_repository_test.go b/local_repository_test.go index 89b0f68..71275db 100644 --- a/local_repository_test.go +++ b/local_repository_test.go @@ -123,7 +123,7 @@ func TestNewLocalRepository(t *testing.T) { func TestLocalRepositoryRoots(t *testing.T) { defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) - defer func(orig string) { os.Setenv("GHQ_ROOT", orig) }(os.Getenv("GHQ_ROOT")) + defer func(orig string) { os.Setenv(envGhqRoot, orig) }(os.Getenv(envGhqRoot)) wd, err := os.Getwd() if err != nil { @@ -147,7 +147,7 @@ func TestLocalRepositoryRoots(t *testing.T) { for _, tc := range testCases { t.Run(tc.root, func(t *testing.T) { _localRepositoryRoots = nil - os.Setenv("GHQ_ROOT", tc.root) + os.Setenv(envGhqRoot, tc.root) got, err := localRepositoryRoots(true) if err != nil { t.Errorf("error should be nil, but: %s", err) @@ -297,11 +297,11 @@ func TestFindVCSBackend(t *testing.T) { func TestLocalRepository_VCS(t *testing.T) { defer func(orig []string) { _localRepositoryRoots = orig }(_localRepositoryRoots) - defer func(orig string) { os.Setenv("GHQ_ROOT", orig) }(os.Getenv("GHQ_ROOT")) + defer func(orig string) { os.Setenv(envGhqRoot, orig) }(os.Getenv(envGhqRoot)) _localRepositoryRoots = nil tmpdir := newTempDir(t) - os.Setenv("GHQ_ROOT", tmpdir) + os.Setenv(envGhqRoot, tmpdir) pkg := filepath.Join(tmpdir, "github.com", "motemen", "ghq") subpkg := filepath.Join(pkg, "logger") From cca354673382c500b6525b468d2175b791910d9b Mon Sep 17 00:00:00 2001 From: Songmu Date: Thu, 26 Dec 2019 01:01:43 +0900 Subject: [PATCH 3/4] GHQ_ROOT env is top priority to getRoot --- local_repository.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/local_repository.go b/local_repository.go index 93ad75a..6b83647 100644 --- a/local_repository.go +++ b/local_repository.go @@ -113,6 +113,10 @@ func LocalRepositoryFromURL(remoteURL *url.URL) (*LocalRepository, error) { } func getRoot(u string) (string, error) { + prim := os.Getenv(envGhqRoot) + if prim != "" { + return prim, nil + } prim, err := gitconfig.Do("--path", "--get-urlmatch", "ghq.root", u) if err != nil && !gitconfig.IsNotFound(err) { return "", err From f7da670e73fcead4910e4646ba081bff714412e5 Mon Sep 17 00:00:00 2001 From: Songmu Date: Thu, 26 Dec 2019 01:13:50 +0900 Subject: [PATCH 4/4] add comment --- local_repository.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/local_repository.go b/local_repository.go index 6b83647..53c478a 100644 --- a/local_repository.go +++ b/local_repository.go @@ -329,6 +329,8 @@ var _localRepositoryRoots []string // - If GHQ_ROOT environment variable is nonempty, use it as the only root dir. // - Otherwise, use the result of `git config --get-all ghq.root` as the dirs. // - Otherwise, fallback to the default root, `~/.ghq`. +// - When GHQ_ROOT is empty, specific root dirs are added from the result of +// `git config --path --get-regexp '^ghq\..+\.root$` func localRepositoryRoots(all bool) ([]string, error) { if len(_localRepositoryRoots) != 0 { return _localRepositoryRoots, nil