mirror of
https://github.com/x-motemen/ghq.git
synced 2026-09-10 07:26:27 -04:00
Merge pull request #238 from motemen/adjsut-root
adjust ghq.root detection
This commit is contained in:
commit
d0126f4ecd
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@ import (
|
|||
"github.com/saracen/walker"
|
||||
)
|
||||
|
||||
const envGhqRoot = "GHQ_ROOT"
|
||||
|
||||
// LocalRepository represents local repository
|
||||
type LocalRepository struct {
|
||||
FullPath string
|
||||
|
|
@ -111,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
|
||||
|
|
@ -323,12 +329,14 @@ 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
|
||||
}
|
||||
|
||||
envRoot := os.Getenv("GHQ_ROOT")
|
||||
envRoot := os.Getenv(envGhqRoot)
|
||||
if envRoot != "" {
|
||||
_localRepositoryRoots = filepath.SplitList(envRoot)
|
||||
} else {
|
||||
|
|
@ -347,7 +355,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
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Reference in a new issue