6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-05 20:37:55 +00:00

Add a --bundle flag accepting a bundle file of repos to clone

Also refactor CloneRepo to accept CloneOpts as agruments - makes it cleaner and easier to test.
This commit is contained in:
Grzegorz Dlugoszewski
2020-06-12 17:01:35 +02:00
parent f9f2553231
commit 823a522a97
8 changed files with 190 additions and 26 deletions

View File

@@ -26,17 +26,26 @@ type Repo struct {
Status *RepoStatus
}
func CloneRepo(url *url.URL, path string, branch string, quiet bool) (*Repo, error) {
// CloneOpts specify details about repository to clone.
type CloneOpts struct {
URL *url.URL
Path string // TODO: should Path be a part of clone opts?
Branch string
Quiet bool
IgnoreExisting bool // TODO: implement!
}
func CloneRepo(opts *CloneOpts) (*Repo, error) {
var progress io.Writer
if !quiet {
if !opts.Quiet {
progress = os.Stdout
fmt.Printf("Cloning into '%s'...\n", path)
fmt.Printf("Cloning into '%s'...\n", opts.Path)
}
// TODO: can this be cleaner?
var auth transport.AuthMethod
var err error
if url.Scheme == "ssh" {
if opts.URL.Scheme == "ssh" {
if auth, err = sshKeyAuth(); err != nil {
return nil, err
}
@@ -44,13 +53,13 @@ func CloneRepo(url *url.URL, path string, branch string, quiet bool) (*Repo, err
// If branch name is actually a tag (ie. is prefixed with refs/tags) - check out that tag.
// Otherwise, assume it's a branch name and check it out.
refName := plumbing.ReferenceName(branch)
refName := plumbing.ReferenceName(opts.Branch)
if !refName.IsTag() {
refName = plumbing.NewBranchReferenceName(branch)
refName = plumbing.NewBranchReferenceName(opts.Branch)
}
opts := &git.CloneOptions{
URL: url.String(),
gitOpts := &git.CloneOptions{
URL: opts.URL.String(),
Auth: auth,
RemoteName: git.DefaultRemoteName,
ReferenceName: refName,
@@ -62,12 +71,12 @@ func CloneRepo(url *url.URL, path string, branch string, quiet bool) (*Repo, err
Tags: git.AllTags,
}
repo, err := git.PlainClone(path, false, opts)
repo, err := git.PlainClone(opts.Path, false, gitOpts)
if err != nil {
return nil, errors.Wrap(err, "Failed cloning repo")
}
return NewRepo(repo, path), nil
return NewRepo(repo, opts.Path), nil
}
func OpenRepo(repoPath string) (*Repo, error) {

View File

@@ -247,7 +247,14 @@ func (r *Repo) clone(t *testing.T, branch string) *Repo {
repoURL, err := url.Parse("file://" + r.Path)
checkFatal(t, err)
repo, err := CloneRepo(repoURL, dir, branch, true)
cloneOpts := &CloneOpts{
URL: repoURL,
Path: dir,
Branch: branch,
Quiet: true,
}
repo, err := CloneRepo(cloneOpts)
checkFatal(t, err)
return repo