Skip to content

Commit

Permalink
Merge pull request #3 from Dalee/refs
Browse files Browse the repository at this point in the history
simple refs setting
  • Loading branch information
ns3777k authored Dec 4, 2018
2 parents aa87422 + 4ed1d76 commit d6a93b6
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type (
jsonListFile string
srcKey string
dstKey string
refs refs

repoList []repo
}
Expand All @@ -32,8 +33,19 @@ type (
src string
dst string
}

refs []string
)

func (r *refs) String() string {
return "refs"
}

func (r *refs) Set(value string) error {
*r = append(*r, value)
return nil
}

// git exec helper
func runGitCommand(dir string, args ...string) error {
cmd := exec.Command("git", args...)
Expand All @@ -47,17 +59,28 @@ func runGitCommand(dir string, args ...string) error {
}

// perform repository mirroring
func (r *repo) sync(cacheDir string, cleanupOnSuccess bool) error {
func (r *repo) sync(cacheDir string, cleanupOnSuccess bool, refs *refs) error {
var scenario [][]string

// check if repository exists
_, s := os.Stat(path.Join(r.dir, "HEAD"))
if os.IsNotExist(s) {
scenario = append(scenario, []string{cacheDir, "clone", "--mirror", r.src, r.dir})
if err := runGitCommand(cacheDir, "clone", "--mirror", r.src, r.dir); err != nil {
return err
}
} else {
scenario = append(scenario, []string{r.dir, "fetch", "-p", "origin"})
}

if len(*refs) > 0 {
runGitCommand(r.dir, "config", "--unset-all", "remote.origin.push")

for _, ref := range *refs {
refSpec := fmt.Sprintf("+refs/%s/*:refs/%s/*", ref, ref)
scenario = append(scenario, []string{r.dir, "config", "--add", "remote.origin.push", refSpec})
}
}

// add rest of commands..
scenario = append(scenario, [][]string{
{r.dir, "symbolic-ref", "HEAD", "refs/heads/master"},
Expand Down Expand Up @@ -93,6 +116,7 @@ func parseCommandLine() (mirror, error) {
flag.BoolVar(&cfg.cleanupCacheDir, "cleanCache", false, "Cache cleanup (automatic when cache directory is not provided)")
flag.IntVar(&cfg.concurrentJobs, "concurrency", 5, "Number of workers")
flag.BoolVar(&helpRequested, "help", false, "This help")
flag.Var(&cfg.refs, "ref", "Refs to mirror (default all)")
flag.Parse()

// if help requested or argument mismatch count, just exit with usage
Expand Down Expand Up @@ -188,7 +212,7 @@ func (c *mirror) process() (chan bool, chan string, chan error) {

chGuard <- true
started := time.Now()
if err := item.sync(c.cacheDir, c.cleanupCacheDir); err != nil {
if err := item.sync(c.cacheDir, c.cleanupCacheDir, &c.refs); err != nil {
chErr <- err
}

Expand Down

0 comments on commit d6a93b6

Please sign in to comment.