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

[refactor] define type vcsGetOption struct #216

Merged
merged 3 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ func withFakeGitBackend(t *testing.T, block func(*testing.T, string, *_cloneArgs

var originalGitBackend = GitBackend
tmpBackend := &VCSBackend{
Clone: func(remote *url.URL, local string, shallow, silent bool, branch string) error {
Clone: func(vg *vcsGetOption) error {
cloneArgs = _cloneArgs{
remote: remote,
local: filepath.FromSlash(local),
shallow: shallow,
branch: branch,
remote: vg.url,
local: filepath.FromSlash(vg.dir),
shallow: vg.shallow,
branch: vg.branch,
}
return nil
},
Update: func(local string, silent bool) error {
Update: func(vg *vcsGetOption) error {
updateArgs = _updateArgs{
local: local,
local: vg.dir,
}
return nil
},
Expand Down
13 changes: 11 additions & 2 deletions getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ func (g *getter) getRemoteRepository(remote RemoteRepository) error {
}

if getRepoLock(localRepoRoot) {
return vcs.Clone(repoURL, localRepoRoot, g.shallow, g.silent, g.branch)
return vcs.Clone(&vcsGetOption{
url: repoURL,
dir: localRepoRoot,
shallow: g.shallow,
silent: g.silent,
branch: g.branch,
})
}
return nil
}
Expand All @@ -146,7 +152,10 @@ func (g *getter) getRemoteRepository(remote RemoteRepository) error {
return fmt.Errorf("failed to detect VCS for %q", fpath)
}
if getRepoLock(localRepoRoot) {
return vcs.Update(localRepoRoot, g.silent)
return vcs.Update(&vcsGetOption{
dir: localRepoRoot,
silent: g.silent,
})
}
return nil
}
Expand Down
127 changes: 68 additions & 59 deletions vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,43 @@ func runInDir(silent bool) func(dir, command string, args ...string) error {
// A VCSBackend represents a VCS backend.
type VCSBackend struct {
// Clones a remote repository to local path.
Clone func(*url.URL, string, bool, bool, string) error
Clone func(*vcsGetOption) error
// Updates a cloned local repository.
Update func(string, bool) error
Update func(*vcsGetOption) error
// Returns VCS specific files
Contents func() []string
}

type vcsGetOption struct {
url *url.URL
dir string
recursive, shallow, silent bool
branch string
}

// GitBackend is the VCSBackend of git
var GitBackend = &VCSBackend{
// support submodules?
Clone: func(remote *url.URL, local string, shallow, silent bool, branch string) error {
dir, _ := filepath.Split(local)
Clone: func(vg *vcsGetOption) error {
dir, _ := filepath.Split(vg.dir)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}

args := []string{"clone"}
if shallow {
if vg.shallow {
args = append(args, "--depth", "1")
}
if branch != "" {
args = append(args, "--branch", branch, "--single-branch")
if vg.branch != "" {
args = append(args, "--branch", vg.branch, "--single-branch")
}
args = append(args, remote.String(), local)
args = append(args, vg.url.String(), vg.dir)

return run(silent)("git", args...)
return run(vg.silent)("git", args...)
},
Update: func(local string, silent bool) error {
return runInDir(silent)(local, "git", "pull", "--ff-only")
Update: func(vg *vcsGetOption) error {
return runInDir(vg.silent)(vg.dir, "git", "pull", "--ff-only")
},
Contents: func() []string {
return []string{".git"}
Expand All @@ -64,28 +71,29 @@ var GitBackend = &VCSBackend{

// SubversionBackend is the VCSBackend for subversion
var SubversionBackend = &VCSBackend{
Clone: func(remote *url.URL, local string, shallow, silent bool, branch string) error {
dir, _ := filepath.Split(local)
Clone: func(vg *vcsGetOption) error {
dir, _ := filepath.Split(vg.dir)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}

args := []string{"checkout"}
if shallow {
if vg.shallow {
args = append(args, "--depth", "1")
}
if branch != "" {
copied := *remote
remote := vg.url
if vg.branch != "" {
copied := *vg.url
remote = &copied
remote.Path += "/branches/" + url.PathEscape(branch)
remote.Path += "/branches/" + url.PathEscape(vg.branch)
}
args = append(args, remote.String(), local)
args = append(args, remote.String(), vg.dir)

return run(silent)("svn", args...)
return run(vg.silent)("svn", args...)
},
Update: func(local string, silent bool) error {
return runInDir(silent)(local, "svn", "update")
Update: func(vg *vcsGetOption) error {
return runInDir(vg.silent)(vg.dir, "svn", "update")
},
Contents: func() []string {
return []string{".svn"}
Expand All @@ -95,22 +103,23 @@ var SubversionBackend = &VCSBackend{
// GitsvnBackend is the VCSBackend for git-svn
var GitsvnBackend = &VCSBackend{
// git-svn seems not supporting shallow clone currently.
Clone: func(remote *url.URL, local string, ignoredShallow, silent bool, branch string) error {
dir, _ := filepath.Split(local)
Clone: func(vg *vcsGetOption) error {
dir, _ := filepath.Split(vg.dir)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
if branch != "" {
remote := vg.url
if vg.branch != "" {
copied := *remote
remote = &copied
remote.Path += "/branches/" + url.PathEscape(branch)
remote.Path += "/branches/" + url.PathEscape(vg.branch)
}

return run(silent)("git", "svn", "clone", remote.String(), local)
return run(vg.silent)("git", "svn", "clone", remote.String(), vg.dir)
},
Update: func(local string, silent bool) error {
return runInDir(silent)(local, "git", "svn", "rebase")
Update: func(vg *vcsGetOption) error {
return runInDir(vg.silent)(vg.dir, "git", "svn", "rebase")
},
Contents: func() []string {
return []string{".git/svn"}
Expand All @@ -120,22 +129,22 @@ var GitsvnBackend = &VCSBackend{
// MercurialBackend is the VCSBackend for mercurial
var MercurialBackend = &VCSBackend{
// Mercurial seems not supporting shallow clone currently.
Clone: func(remote *url.URL, local string, ignoredShallow, silent bool, branch string) error {
dir, _ := filepath.Split(local)
Clone: func(vg *vcsGetOption) error {
dir, _ := filepath.Split(vg.dir)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
args := []string{"clone"}
if branch != "" {
args = append(args, "--branch", branch)
if vg.branch != "" {
args = append(args, "--branch", vg.branch)
}
args = append(args, remote.String(), local)
args = append(args, vg.url.String(), vg.dir)

return run(silent)("hg", args...)
return run(vg.silent)("hg", args...)
},
Update: func(local string, silent bool) error {
return runInDir(silent)(local, "hg", "pull", "--update")
Update: func(vg *vcsGetOption) error {
return runInDir(vg.silent)(vg.dir, "hg", "pull", "--update")
},
Contents: func() []string {
return []string{".hg"}
Expand All @@ -144,38 +153,38 @@ var MercurialBackend = &VCSBackend{

// DarcsBackend is the VCSBackend for darcs
var DarcsBackend = &VCSBackend{
Clone: func(remote *url.URL, local string, shallow, silent bool, branch string) error {
if branch != "" {
Clone: func(vg *vcsGetOption) error {
if vg.branch != "" {
return errors.New("Darcs does not support branch")
}

dir, _ := filepath.Split(local)
dir, _ := filepath.Split(vg.dir)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}

args := []string{"get"}
if shallow {
if vg.shallow {
args = append(args, "--lazy")
}
args = append(args, remote.String(), local)
args = append(args, vg.url.String(), vg.dir)

return run(silent)("darcs", args...)
return run(vg.silent)("darcs", args...)
},
Update: func(local string, silent bool) error {
return runInDir(silent)(local, "darcs", "pull")
Update: func(vg *vcsGetOption) error {
return runInDir(vg.silent)(vg.dir, "darcs", "pull")
},
Contents: func() []string {
return []string{"_darcs"}
},
}

var cvsDummyBackend = &VCSBackend{
Clone: func(remote *url.URL, local string, ignoredShallow, silent bool, branch string) error {
Clone: func(vg *vcsGetOption) error {
return errors.New("CVS clone is not supported")
},
Update: func(local string, silent bool) error {
Update: func(vg *vcsGetOption) error {
return errors.New("CVS update is not supported")
},
Contents: func() []string {
Expand All @@ -187,21 +196,21 @@ const fossilRepoName = ".fossil" // same as Go

// FossilBackend is the VCSBackend for fossil
var FossilBackend = &VCSBackend{
Clone: func(remote *url.URL, local string, shallow, silent bool, branch string) error {
if branch != "" {
Clone: func(vg *vcsGetOption) error {
if vg.branch != "" {
return errors.New("Fossil does not support cloning specific branch")
}
if err := os.MkdirAll(local, 0755); err != nil {
if err := os.MkdirAll(vg.dir, 0755); err != nil {
return err
}

if err := run(silent)("fossil", "clone", remote.String(), filepath.Join(local, fossilRepoName)); err != nil {
if err := run(vg.silent)("fossil", "clone", vg.url.String(), filepath.Join(vg.dir, fossilRepoName)); err != nil {
return err
}
return runInDir(silent)(local, "fossil", "open", fossilRepoName)
return runInDir(vg.silent)(vg.dir, "fossil", "open", fossilRepoName)
},
Update: func(local string, silent bool) error {
return runInDir(silent)(local, "fossil", "update")
Update: func(vg *vcsGetOption) error {
return runInDir(vg.silent)(vg.dir, "fossil", "update")
},
Contents: func() []string {
return []string{".fslckout", "_FOSSIL_"}
Expand All @@ -211,20 +220,20 @@ var FossilBackend = &VCSBackend{
// BazaarBackend is the VCSBackend for bazaar
var BazaarBackend = &VCSBackend{
// bazaar seems not supporting shallow clone currently.
Clone: func(remote *url.URL, local string, ignoredShallow, silent bool, branch string) error {
if branch != "" {
Clone: func(vg *vcsGetOption) error {
if vg.branch != "" {
return errors.New("--branch option is unavailable for Bazaar since branch is included in remote URL")
}
dir, _ := filepath.Split(local)
dir, _ := filepath.Split(vg.dir)
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
return run(silent)("bzr", "branch", remote.String(), local)
return run(vg.silent)("bzr", "branch", vg.url.String(), vg.dir)
},
Update: func(local string, silent bool) error {
Update: func(vg *vcsGetOption) error {
// Without --overwrite bzr will not pull tags that changed.
return runInDir(silent)(local, "bzr", "pull", "--overwrite")
return runInDir(vg.silent)(vg.dir, "bzr", "pull", "--overwrite")
},
Contents: func() []string {
return []string{".bzr"}
Expand Down
Loading