mirror of
https://github.com/grdl/git-get.git
synced 2026-02-04 14:31:49 +00:00
Path is for URLs, filepath is for os paths. This solves the problem with wrong repo root on windows.
83 lines
1.4 KiB
Go
83 lines
1.4 KiB
Go
package pkg
|
|
|
|
import (
|
|
"fmt"
|
|
"git-get/pkg/git"
|
|
"git-get/pkg/io"
|
|
"path/filepath"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
opts := &git.CloneOpts{
|
|
URL: url,
|
|
Path: filepath.Join(c.Root, URLToPath(url)),
|
|
Branch: c.Branch,
|
|
}
|
|
|
|
_, err = git.Clone(opts)
|
|
|
|
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
|
|
}
|
|
|
|
opts := &git.CloneOpts{
|
|
URL: url,
|
|
Path: filepath.Join(c.Root, URLToPath(url)),
|
|
Branch: line.branch,
|
|
}
|
|
|
|
// If target path already exists, skip cloning this repo
|
|
if exists, _ := io.Exists(opts.Path); exists {
|
|
continue
|
|
}
|
|
|
|
fmt.Printf("Cloning %s...\n", opts.URL.String())
|
|
_, err = git.Clone(opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|