Compare commits

...

2 Commits

2 changed files with 169 additions and 137 deletions

View File

@ -168,6 +168,7 @@ If the URL scheme starts with `git+`, the source will be downloaded as a git rep
- `~branch`: Specify which branch of the repo to check out.
- `~commit`: Specify which commit of the repo to check out.
- `~depth`: Specify what depth should be used when cloning the repo. Must be an integer.
- `~name`: Specify the name of the directory into which the git repo should be cloned.
Examples:

View File

@ -23,6 +23,7 @@ import (
"context"
"crypto/sha256"
"errors"
"hash"
"io"
"net/http"
"net/url"
@ -67,6 +68,22 @@ func Get(ctx context.Context, opts GetOptions) error {
}
query := src.Query()
if strings.HasPrefix(src.Scheme, "git+") {
err = getGit(ctx, src, query, opts)
if err != nil {
return err
}
} else {
err = getFile(ctx, src, query, opts)
if err != nil {
return err
}
}
return nil
}
func getGit(ctx context.Context, src *url.URL, query url.Values, opts GetOptions) (err error) {
tag := query.Get("~tag")
query.Del("~tag")
@ -79,6 +96,9 @@ func Get(ctx context.Context, opts GetOptions) error {
depthStr := query.Get("~depth")
query.Del("~depth")
name := query.Get("~name")
query.Del("~name")
var refName plumbing.ReferenceName
if tag != "" {
refName = plumbing.NewTagReferenceName(tag)
@ -86,12 +106,13 @@ func Get(ctx context.Context, opts GetOptions) error {
refName = plumbing.NewBranchReferenceName(branch)
}
if strings.HasPrefix(src.Scheme, "git+") {
src.Scheme = strings.TrimPrefix(src.Scheme, "git+")
src.RawQuery = query.Encode()
name := path.Base(src.Path)
if name == "" {
name = path.Base(src.Path)
name = strings.TrimSuffix(name, ".git")
}
dstDir := opts.Destination
if opts.EncloseGit {
@ -132,7 +153,9 @@ func Get(ctx context.Context, opts GetOptions) error {
}
return w.Checkout(checkoutOpts)
} else {
}
func getFile(ctx context.Context, src *url.URL, query url.Values, opts GetOptions) error {
name := query.Get("~name")
query.Del("~name")
@ -183,6 +206,16 @@ func Get(ctx context.Context, opts GetOptions) error {
} else if err != nil {
return err
} else {
err = extractFile(ctx, input, hash, format, name, opts)
if err != nil {
return err
}
}
return nil
}
func extractFile(ctx context.Context, input io.Reader, hash hash.Hash, format archiver.Format, name string, opts GetOptions) (err error) {
r := io.TeeReader(input, hash)
fname := format.Name()
@ -249,8 +282,6 @@ func Get(ctx context.Context, opts GetOptions) error {
return ErrChecksumMismatch
}
}
}
}
return nil
}