Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get lock using repoPath to avoid duplicate get on import #163

Merged
merged 1 commit into from
May 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,26 @@ import (
"os"
"path/filepath"
"strings"
"sync"

"github.com/motemen/ghq/logger"
"golang.org/x/xerrors"
)

var (
seen = make(map[string]bool)
mu = &sync.Mutex{}
)

func getRepoLock(repoPath string) bool {
mu.Lock()
defer func() {
seen[repoPath] = true
mu.Unlock()
}()
return !seen[repoPath]
}

type getter struct {
update, shallow, silent, ssh bool
vcs string
Expand Down Expand Up @@ -106,21 +121,22 @@ func (g *getter) getRemoteRepository(remote RemoteRepository) error {
}
repoPath = strings.TrimSuffix(filepath.Join(root, repoURL.Host, repoURL.Path), ".git")
}
err := vcs.Clone(repoURL, repoPath, g.shallow, g.silent)
if err != nil {
return err
if getRepoLock(repoPath) {
return vcs.Clone(repoURL, repoPath, g.shallow, g.silent)
}
} else {
if g.update {
logger.Log("update", path)
vcs, repoPath := local.VCS()
if vcs == nil {
return fmt.Errorf("failed to detect VCS for %q", path)
}
vcs.Update(repoPath, g.silent)
} else {
logger.Log("exists", path)
return nil
}
if g.update {
logger.Log("update", path)
vcs, repoPath := local.VCS()
if vcs == nil {
return fmt.Errorf("failed to detect VCS for %q", path)
}
if getRepoLock(repoPath) {
return vcs.Update(repoPath, g.silent)
}
return nil
}
logger.Log("exists", path)
return nil
}