6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-09 00:29:17 +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

66
path/bundle.go Normal file
View File

@@ -0,0 +1,66 @@
package path
import (
"bufio"
"git-get/git"
"os"
"strings"
"github.com/pkg/errors"
)
var (
ErrInvalidNumberOfElements = errors.New("More than two space-separated 2 elements on the line")
)
// ParseBundleFile opens a given gitgetfile and parses its content into a slice of CloneOpts.
func ParseBundleFile(path string) ([]*git.CloneOpts, error) {
file, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "Failed opening gitgetfile %s", path)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var opts []*git.CloneOpts
var line int
for scanner.Scan() {
line++
opt, err := parseLine(scanner.Text())
if err != nil {
return nil, errors.Wrapf(err, "Failed parsing line %d", line)
}
opts = append(opts, opt)
}
return opts, nil
}
// parseLine splits a gitgetfile line into space-separated segments.
// First part is the URL to clone. Second, optional, is the branch (or tag) to checkout after cloning
func parseLine(line string) (*git.CloneOpts, error) {
parts := strings.Split(line, " ")
if len(parts) > 2 {
return nil, ErrInvalidNumberOfElements
}
url, err := ParseURL(parts[0])
if err != nil {
return nil, err
}
branch := ""
if len(parts) == 2 {
branch = parts[1]
}
return &git.CloneOpts{
URL: url,
Branch: branch,
// When cloning a bundle we ignore errors about already cloned repos
IgnoreExisting: true,
}, nil
}

55
path/bundle_test.go Normal file
View File

@@ -0,0 +1,55 @@
package path
import (
"testing"
)
func TestParsingRefs(t *testing.T) {
var tests = []struct {
line string
wantBranch string
wantErr error
}{
{
line: "https://github.com/grdl/git-get",
wantBranch: "",
wantErr: nil,
},
{
line: "https://github.com/grdl/git-get master",
wantBranch: "master",
wantErr: nil,
},
{
line: "https://github.com/grdl/git-get refs/tags/v1.0.0",
wantBranch: "refs/tags/v1.0.0",
wantErr: nil,
},
{
line: "https://github.com/grdl/git-get master branch",
wantBranch: "",
wantErr: ErrInvalidNumberOfElements,
},
{
line: "https://github.com",
wantBranch: "",
wantErr: ErrEmptyURLPath,
},
}
for i, test := range tests {
got, err := parseLine(test.line)
if err != nil && test.wantErr == nil {
t.Fatalf("Test case %d should not return an error", i)
}
if err != nil && test.wantErr != nil {
continue
}
if got.Branch != test.wantBranch {
t.Errorf("Failed test case %d, got: %s; wantBranch: %s", i, got.Branch, test.wantBranch)
}
}
}

View File

@@ -11,6 +11,10 @@ import (
"github.com/spf13/viper"
)
var (
ErrEmptyURLPath = errors.New("Parsed URL path is empty")
)
// scpSyntax matches the SCP-like addresses used by the ssh protocol (eg, [user@]host.xz:path/to/repo.git/).
// See: https://golang.org/src/cmd/go/internal/get/vcs.go
var scpSyntax = regexp.MustCompile(`^([a-zA-Z0-9_]+)@([a-zA-Z0-9._-]+):(.*)$`)
@@ -28,12 +32,12 @@ func ParseURL(rawURL string) (url *urlpkg.URL, err error) {
} else {
url, err = urlpkg.Parse(rawURL)
if err != nil {
return nil, errors.Wrap(err, "Failed parsing Path")
return nil, errors.Wrap(err, "Failed parsing URL")
}
}
if url.Host == "" && url.Path == "" {
return nil, errors.New("Parsed Path is empty")
return nil, ErrEmptyURLPath
}
if url.Scheme == "git+ssh" {

View File

@@ -61,7 +61,7 @@ func TestURLParse(t *testing.T) {
got := URLToPath(url)
if got != test.want {
t.Errorf("Wrong result of parsing Path: %s, got: %s; want: %s", test.in, got, test.want)
t.Errorf("Wrong result of parsing Path: %s, got: %s; wantBranch: %s", test.in, got, test.want)
}
}
}
@@ -79,7 +79,7 @@ func TestInvalidURLParse(t *testing.T) {
for _, in := range invalidURLs {
got, err := ParseURL(in)
if err == nil {
t.Errorf("Wrong result of parsing invalid Path: %s, got: %s, want: error", in, got)
t.Errorf("Wrong result of parsing invalid Path: %s, got: %s, wantBranch: error", in, got)
}
}
}