mirror of
https://github.com/grdl/git-get.git
synced 2026-02-05 00:54:41 +00:00
* Fix typo in readme * Reimplement all git methods without go-git * Rename repo pkg to git, add gitconfig methods * Improve tests for configuration reading * Rename package file to io and move RepoFinder there * Refactor printers - Remove smart printer - Decouple printers from git repos with interfaces - Update printer functions - Remove unnecessary flags - Add better remote URL detection * Update readme and go.mod * Add author to git commit in tests Otherwise tests will fail in CI. * Install git before running tests and don't use cgo * Add better error message, revert installing git * Ensure commit message is in quotes * Set up git config before running tests
77 lines
1.2 KiB
Go
77 lines
1.2 KiB
Go
package pkg
|
|
|
|
import (
|
|
"fmt"
|
|
"git-get/pkg/git"
|
|
"path"
|
|
)
|
|
|
|
// GetCfg provides configuration for the Get command.
|
|
type GetCfg struct {
|
|
Branch string
|
|
DefHost string
|
|
Dump string
|
|
Root string
|
|
URL string
|
|
}
|
|
|
|
// Get executes the "git get" command.
|
|
func Get(c *GetCfg) error {
|
|
if c.URL == "" && c.Dump == "" {
|
|
return fmt.Errorf("missing <REPO> argument or --dump flag")
|
|
}
|
|
|
|
if c.URL != "" {
|
|
return cloneSingleRepo(c)
|
|
}
|
|
|
|
if c.Dump != "" {
|
|
return cloneDumpFile(c)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func cloneSingleRepo(c *GetCfg) error {
|
|
url, err := ParseURL(c.URL, c.DefHost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cloneOpts := &git.CloneOpts{
|
|
URL: url,
|
|
Path: path.Join(c.Root, URLToPath(url)),
|
|
Branch: c.Branch,
|
|
}
|
|
|
|
_, err = git.Clone(cloneOpts)
|
|
|
|
return err
|
|
}
|
|
|
|
func cloneDumpFile(c *GetCfg) error {
|
|
parsedLines, err := parseDumpFile(c.Dump)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, line := range parsedLines {
|
|
url, err := ParseURL(line.rawurl, c.DefHost)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cloneOpts := &git.CloneOpts{
|
|
URL: url,
|
|
Path: path.Join(c.Root, URLToPath(url)),
|
|
Branch: line.branch,
|
|
IgnoreExisting: true,
|
|
}
|
|
|
|
_, err = git.Clone(cloneOpts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|