Skip to content

Commit

Permalink
remove logger.PanicIf
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Apr 27, 2019
1 parent 69ba6c1 commit 0d1c965
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 63 deletions.
44 changes: 29 additions & 15 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,11 @@ func doGet(c *cli.Context) error {
path := filepath.Clean(filepath.Join(wd, filepath.Join(parts...)))

var repoPath string
for _, r := range localRepositoryRoots() {
roots, err := localRepositoryRoots()
if err != nil {
return err
}
for _, r := range roots {
p := strings.TrimPrefix(path, r+string(filepath.Separator))
if p != path && (repoPath == "" || len(p) < len(repoPath)) {
repoPath = p
Expand Down Expand Up @@ -198,12 +202,15 @@ func doGet(c *cli.Context) error {
// If isShallow is true, does shallow cloning. (no effect if already cloned or the VCS is Mercurial and git-svn)
func getRemoteRepository(remote RemoteRepository, doUpdate bool, isShallow bool, vcsBackend string) error {
remoteURL := remote.URL()
local := LocalRepositoryFromURL(remoteURL)
local, err := LocalRepositoryFromURL(remoteURL)
if err != nil {
return err
}

path := local.FullPath
newPath := false

_, err := os.Stat(path)
_, err = os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
newPath = true
Expand Down Expand Up @@ -270,14 +277,14 @@ func doList(c *cli.Context) error {
}

repos := []*LocalRepository{}

walkLocalRepositories(func(repo *LocalRepository) {
if err := walkLocalRepositories(func(repo *LocalRepository) {
if filterFn(repo) == false {
return
}

repos = append(repos, repo)
})
}); err != nil {
return err
}

if printUniquePaths {
subpathCount := map[string]int{} // Count duplicated subpaths (ex. foo/dotfiles and bar/dotfiles)
Expand Down Expand Up @@ -327,18 +334,21 @@ func doLook(c *cli.Context) error {
}

reposFound := []*LocalRepository{}
walkLocalRepositories(func(repo *LocalRepository) {
if err := walkLocalRepositories(func(repo *LocalRepository) {
if repo.Matches(name) {
reposFound = append(reposFound, repo)
}
})
}); err != nil {
return err
}

if len(reposFound) == 0 {
url, err := NewURL(name)

if err == nil {
repo := LocalRepositoryFromURL(url)
_, err := os.Stat(repo.FullPath)
if url, err := NewURL(name); err == nil {
repo, err := LocalRepositoryFromURL(url)
if err != nil {
return err
}
_, err = os.Stat(repo.FullPath)

// if the directory exists
if err == nil {
Expand Down Expand Up @@ -472,7 +482,11 @@ func doImport(c *cli.Context) error {
func doRoot(c *cli.Context) error {
all := c.Bool("all")
if all {
for _, root := range localRepositoryRoots() {
roots, err := localRepositoryRoots()
if err != nil {
return err
}
for _, root := range roots {
fmt.Println(root)
}
} else {
Expand Down
93 changes: 59 additions & 34 deletions local_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"path"
"path/filepath"
"strings"

"github.com/motemen/ghq/logger"
)

type LocalRepository struct {
Expand All @@ -20,7 +18,11 @@ type LocalRepository struct {
func LocalRepositoryFromFullPath(fullPath string) (*LocalRepository, error) {
var relPath string

for _, root := range localRepositoryRoots() {
roots, err := localRepositoryRoots()
if err != nil {
return nil, err
}
for _, root := range roots {
if !strings.HasPrefix(fullPath, root) {
continue
}
Expand All @@ -38,10 +40,13 @@ func LocalRepositoryFromFullPath(fullPath string) (*LocalRepository, error) {

pathParts := strings.Split(relPath, string(filepath.Separator))

return &LocalRepository{fullPath, filepath.ToSlash(relPath), pathParts}, nil
return &LocalRepository{
FullPath: fullPath,
RelPath: filepath.ToSlash(relPath),
PathParts: pathParts}, nil
}

func LocalRepositoryFromURL(remoteURL *url.URL) *LocalRepository {
func LocalRepositoryFromURL(remoteURL *url.URL) (*LocalRepository, error) {
pathParts := append(
[]string{remoteURL.Host}, strings.Split(remoteURL.Path, "/")...,
)
Expand All @@ -50,22 +55,28 @@ func LocalRepositoryFromURL(remoteURL *url.URL) *LocalRepository {
var localRepository *LocalRepository

// Find existing local repository first
walkLocalRepositories(func(repo *LocalRepository) {
if err := walkLocalRepositories(func(repo *LocalRepository) {
if repo.RelPath == relPath {
localRepository = repo
}
})
}); err != nil {
return nil, err
}

if localRepository != nil {
return localRepository
return localRepository, nil
}

prim, err := primaryLocalRepositoryRoot()
if err != nil {
return nil, err
}
// No local repository found, returning new one
return &LocalRepository{
path.Join(primaryLocalRepositoryRoot(), relPath),
relPath,
pathParts,
}
FullPath: path.Join(prim, relPath),
RelPath: relPath,
PathParts: pathParts,
}, nil
}

// Subpaths returns lists of tail parts of relative path from the root directory (shortest first)
Expand All @@ -85,7 +96,11 @@ func (repo *LocalRepository) NonHostPath() string {
}

func (repo *LocalRepository) IsUnderPrimaryRoot() bool {
return strings.HasPrefix(repo.FullPath, primaryLocalRepositoryRoot())
prim, err := primaryLocalRepositoryRoot()
if err != nil {
return false
}
return strings.HasPrefix(repo.FullPath, prim)
}

// Matches checks if any subpath of the local repository equals the query.
Expand Down Expand Up @@ -150,9 +165,13 @@ func (repo *LocalRepository) VCS() *VCSBackend {

var vcsDirs = []string{".git", ".svn", ".hg", "_darcs", ".fslckout", "_FOSSIL_", "CVS"}

func walkLocalRepositories(callback func(*LocalRepository)) {
for _, root := range localRepositoryRoots() {
filepath.Walk(root, func(path string, fileInfo os.FileInfo, err error) error {
func walkLocalRepositories(callback func(*LocalRepository)) error {
roots, err := localRepositoryRoots()
if err != nil {
return err
}
for _, root := range roots {
if err := filepath.Walk(root, func(path string, fileInfo os.FileInfo, err error) error {
if err != nil || fileInfo == nil {
return nil
}
Expand Down Expand Up @@ -194,8 +213,11 @@ func walkLocalRepositories(callback func(*LocalRepository)) {
}
callback(repo)
return filepath.SkipDir
})
}); err != nil {
return err
}
}
return nil
}

var _localRepositoryRoots []string
Expand All @@ -206,49 +228,52 @@ 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`.
//
// TODO: More fancy default directory path?
func localRepositoryRoots() []string {
func localRepositoryRoots() ([]string, error) {
if len(_localRepositoryRoots) != 0 {
return _localRepositoryRoots
return _localRepositoryRoots, nil
}

envRoot := os.Getenv("GHQ_ROOT")
if envRoot != "" {
_localRepositoryRoots = filepath.SplitList(envRoot)
} else {
var err error
_localRepositoryRoots, err = GitConfigAll("ghq.root")
if err != nil {
logger.Log("error", err.Error())
os.Exit(1)
if _localRepositoryRoots, err = GitConfigAll("ghq.root"); err != nil {
return nil, err
}
}

if len(_localRepositoryRoots) == 0 {
homeDir, err := os.UserHomeDir()
logger.PanicIf(err)

if err != nil {
return nil, err
}
_localRepositoryRoots = []string{filepath.Join(homeDir, ".ghq")}
}

for i, v := range _localRepositoryRoots {
path := filepath.Clean(v)
if _, err := os.Stat(path); err == nil {
path, err = filepath.EvalSymlinks(path)
logger.PanicIf(err)
if path, err = filepath.EvalSymlinks(path); err != nil {
return nil, err
}
}
if !filepath.IsAbs(path) {
var err error
path, err = filepath.Abs(path)
logger.PanicIf(err)
if path, err = filepath.Abs(path); err != nil {
return nil, err
}
}
_localRepositoryRoots[i] = path
}

return _localRepositoryRoots
return _localRepositoryRoots, nil
}

func primaryLocalRepositoryRoot() string {
return localRepositoryRoots()[0]
func primaryLocalRepositoryRoot() (string, error) {
roots, err := localRepositoryRoots()
if err != nil {
return "", err
}
return roots[0], nil
}
17 changes: 9 additions & 8 deletions local_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,36 @@ func TestNewLocalRepository(t *testing.T) {
Expect(r.Subpaths()).To(Equal([]string{"ghq", "motemen/ghq", "scm/motemen/ghq", "stash.com/scm/motemen/ghq"}))

githubURL, _ := url.Parse("ssh://[email protected]/motemen/ghq.git")
r = LocalRepositoryFromURL(githubURL)
r, err = LocalRepositoryFromURL(githubURL)
Expect(err).To(BeNil())
Expect(r.FullPath).To(Equal("/repos/github.com/motemen/ghq"))

stashURL, _ := url.Parse("ssh://[email protected]/scm/motemen/ghq")
r = LocalRepositoryFromURL(stashURL)
r, _ = LocalRepositoryFromURL(stashURL)
Expect(r.FullPath).To(Equal("/repos/stash.com/scm/motemen/ghq"))

svnSourceforgeURL, _ := url.Parse("http://svn.code.sf.net/p/ghq/code/trunk")
r = LocalRepositoryFromURL(svnSourceforgeURL)
r, _ = LocalRepositoryFromURL(svnSourceforgeURL)
Expect(r.FullPath).To(Equal("/repos/svn.code.sf.net/p/ghq/code/trunk"))

gitSourceforgeURL, _ := url.Parse("http://git.code.sf.net/p/ghq/code")
r = LocalRepositoryFromURL(gitSourceforgeURL)
r, _ = LocalRepositoryFromURL(gitSourceforgeURL)
Expect(r.FullPath).To(Equal("/repos/git.code.sf.net/p/ghq/code"))

svnSourceforgeJpURL, _ := url.Parse("http://scm.sourceforge.jp/svnroot/ghq/")
r = LocalRepositoryFromURL(svnSourceforgeJpURL)
r, _ = LocalRepositoryFromURL(svnSourceforgeJpURL)
Expect(r.FullPath).To(Equal("/repos/scm.sourceforge.jp/svnroot/ghq"))

gitSourceforgeJpURL, _ := url.Parse("http://scm.sourceforge.jp/gitroot/ghq/ghq.git")
r = LocalRepositoryFromURL(gitSourceforgeJpURL)
r, _ = LocalRepositoryFromURL(gitSourceforgeJpURL)
Expect(r.FullPath).To(Equal("/repos/scm.sourceforge.jp/gitroot/ghq/ghq"))

svnAssemblaURL, _ := url.Parse("https://subversion.assembla.com/svn/ghq/")
r = LocalRepositoryFromURL(svnAssemblaURL)
r, _ = LocalRepositoryFromURL(svnAssemblaURL)
Expect(r.FullPath).To(Equal("/repos/subversion.assembla.com/svn/ghq"))

gitAssemblaURL, _ := url.Parse("https://git.assembla.com/ghq.git")
r = LocalRepositoryFromURL(gitAssemblaURL)
r, _ = LocalRepositoryFromURL(gitAssemblaURL)
Expect(r.FullPath).To(Equal("/repos/git.assembla.com/ghq"))
}

Expand Down
6 changes: 0 additions & 6 deletions logger/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,3 @@ func init() {
func Log(prefix, message string) {
logger.Log(prefix, message)
}

func PanicIf(err error) {
if err != nil {
panic(err)
}
}

0 comments on commit 0d1c965

Please sign in to comment.