summaryrefslogtreecommitdiff
path: root/rill.go
blob: 5106a4493d739dd34005f1984294b72dc4b18dac (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package gitpb

// runs git, parses output
// types faster than you can

import (
	"errors"
	sync "sync"

	"github.com/destel/rill"
	"github.com/go-cmd/cmd"
	"go.wit.com/log"
)

var ErrorMissingGitConfig error = errors.New("missing .git/config")
var ErrorGitPullOnLocal error = errors.New("git pull on local only branch")
var ErrorGitPullOnDirty error = errors.New("cannot git pull on dirty repo")

func (repo *Repo) GitPull() (*cmd.Status, error) {
	/*
		currentName := repo.GetCurrentBranchName()
		if repo.IsOnlyLocalTag(currentName) {
			var result cmd.Status
			result.Exit = 21
			result.Error = ErrorGitPullOnLocal
			// log.Info("git pull skipped on local only branch", repo.GetGoPath())
			return result
		}
	*/
	var cmd []string
	cmd = append(cmd, "git", "pull")
	return nil, repo.RunVerbose(cmd)
}

// rill is awesome. long live rill
// attempt scan with rill
func (all *Repos) RillGitPull(part1 int, part2 int) map[*Repo]*cmd.Status {
	var lock sync.Mutex
	var allerr map[*Repo]*cmd.Status
	allerr = make(map[*Repo]*cmd.Status)

	// Convert a slice of user IDs into a channel
	ids := rill.FromSlice(all.Repos, nil)

	var counter int
	// Read users from the API.
	// Concurrency = 20
	dirs := rill.Map(ids, part1, func(id *Repo) (*Repo, error) {
		return id, nil
	})

	rill.ForEach(dirs, part2, func(repo *Repo) error {
		counter += 1
		if repo.CheckDirty() {
			// log.Info("git pull skipped on dirty repo", repo.GoPath)
			result := new(cmd.Status)
			result.Error = ErrorGitPullOnDirty
			lock.Lock()
			defer lock.Unlock()
			allerr[repo] = result
		} else {
			// todo: sort out what the hell is wrong with my code
			// something seems to be trampling things
			/*
				var dur time.Duration
				dur = time.Duration((1+rand.Intn(50))*20) * time.Millisecond
				time.Sleep(dur)
			*/
			var result *cmd.Status
			result, _ = repo.GitPull()
			log.Info("git pull", repo.GetGoPath())
			// log.Info(strings.Join(result.Stdout, "\n"))
			lock.Lock()
			defer lock.Unlock()
			allerr[repo] = result

		}
		return nil
	})

	//	for r, err := range allerr {
	//		log.Info("git pull error:", r.GoPath, err)
	//	}

	return allerr
}

func (repo *Repo) GitPullRealtime() cmd.Status {
	currentName := repo.GetCurrentBranchName()
	if repo.IsOnlyLocalTag(currentName) {
		var result cmd.Status
		result.Exit = 21
		result.Error = ErrorGitPullOnLocal
		// log.Info("git pull skipped on local only branch", repo.GoPath)
		return result
	}
	var cmd []string
	cmd = append(cmd, "git", "pull")
	r := repo.RunRealtime(cmd)
	return r
}