summaryrefslogtreecommitdiff
path: root/pkg/app/quick.go
blob: 488e1d65249bbdcc425e34a10d15448cf5810f82 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package app

import (
	"fmt"
	"sync"
	"time"

	"github.com/isacikgoz/gitbatch/pkg/git"
)

func quick(directories []string, depth int, mode string) {
	var wg sync.WaitGroup
	start := time.Now()
	for _, dir := range directories {
		wg.Add(1)
		go func(d string, mode string) {
			defer wg.Done()
			err := operate(d, mode)
			if err != nil {
				fmt.Printf("%s: %s\n", d, err.Error())
			} else {
				fmt.Printf("%s: successful\n", d)
			}
		}(dir, mode)
	}
	wg.Wait()
	elapsed := time.Since(start)
	fmt.Printf("%d repositories finished in: %s\n", len(directories), elapsed)
}

func operate(directory, mode string) error {
	r, err := git.FastInitializeRepo(directory)
	if err != nil {
		return err
	}
	switch mode {
	case "fetch":
		return git.Fetch(r, git.FetchOptions{
			RemoteName: "origin",
		})
	case "pull":
		return git.Pull(r, git.PullOptions{
			RemoteName: "origin",
		})
	}
	return nil
}