2025-03-24 15:00:34: merge [main:f6e45d7212331e8c861cded4b952eaec0b701cc1];
This commit is contained in:
1 parent
dbf061213d
commit
922641647a
39 files changed
+1262
-1418
No files matched your search
+55
-47
@@ -13,57 +13,65 @@ import (
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
semverRegexp = regexp.MustCompile("v([0-9]+)\\.([0-9]+)\\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?(?:\\+[0-9A-Za-z-]+)?$")
|
||||
)
|
||||
type GitGet struct {
|
||||
semverRegexp *regexp.Regexp
|
||||
}
|
||||
|
||||
func GitGet() *cli.Command {
|
||||
func NewGitGet() GitGet {
|
||||
return GitGet{
|
||||
semverRegexp: regexp.MustCompile(`v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$`),
|
||||
}
|
||||
}
|
||||
|
||||
func (c GitGet) Command() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "gitget",
|
||||
Usage: "gitget [repository] [file] (commit|tag)",
|
||||
Description: "clone and print specified file from specified repository and specified commit hash or tag",
|
||||
Action: func(context *cli.Context) error {
|
||||
storage := memory.NewStorage()
|
||||
repo := context.Args().Get(0)
|
||||
|
||||
file := context.Args().Get(1)
|
||||
commit := context.Args().Get(2)
|
||||
|
||||
repository, err := git.Clone(storage, memfs.New(), &git.CloneOptions{
|
||||
URL: repo,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "clone %s", repo)
|
||||
}
|
||||
tree, err := repository.Worktree()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get worktree")
|
||||
}
|
||||
|
||||
if commit != "" {
|
||||
checkoutOpts := git.CheckoutOptions{}
|
||||
if semverRegexp.MatchString(commit) {
|
||||
checkoutOpts.Branch = plumbing.NewTagReferenceName(commit)
|
||||
} else {
|
||||
checkoutOpts.Hash = plumbing.NewHash(commit)
|
||||
}
|
||||
err = tree.Checkout(&checkoutOpts)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "checkout: %s", commit)
|
||||
}
|
||||
}
|
||||
|
||||
f, err := tree.Filesystem.Open(file)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "open %s", file)
|
||||
}
|
||||
|
||||
_, err = io.Copy(os.Stdout, f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Action: c.action,
|
||||
}
|
||||
}
|
||||
|
||||
func (c GitGet) action(ctx *cli.Context) error {
|
||||
storage := memory.NewStorage()
|
||||
repo := ctx.Args().Get(0)
|
||||
|
||||
file := ctx.Args().Get(1)
|
||||
commit := ctx.Args().Get(2)
|
||||
|
||||
repository, err := git.Clone(storage, memfs.New(), &git.CloneOptions{
|
||||
URL: repo,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "clone %s", repo)
|
||||
}
|
||||
tree, err := repository.Worktree()
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get worktree")
|
||||
}
|
||||
|
||||
if commit != "" {
|
||||
checkoutOpts := git.CheckoutOptions{}
|
||||
if c.semverRegexp.MatchString(commit) {
|
||||
checkoutOpts.Branch = plumbing.NewTagReferenceName(commit)
|
||||
} else {
|
||||
checkoutOpts.Hash = plumbing.NewHash(commit)
|
||||
}
|
||||
err = tree.Checkout(&checkoutOpts)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "checkout: %s", commit)
|
||||
}
|
||||
}
|
||||
|
||||
f, err := tree.Filesystem.Open(file)
|
||||
if err != nil {
|
||||
return errors.WithMessagef(err, "open %s", file)
|
||||
}
|
||||
|
||||
_, err = io.Copy(os.Stdout, f)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user