summaryrefslogtreecommitdiff
path: root/rill.go
blob: facc074c4fbedbb4bec9979298d6b069dcaf2297 (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
package gitpb

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

import (
	"errors"
	"sort"
	"strings"
	"time"

	"go.wit.com/log"
)

func (all *GitTags) SortByAge() *GitTagIterator {
	packs := all.selectAllGitTag()

	sort.Sort(GitTagAge(packs))

	iterator := NewGitTagIterator(packs)
	return iterator
}

type GitTagAge []*GitTag

func (a GitTagAge) Len() int { return len(a) }

// sorts in ? order
func (a GitTagAge) Less(i, j int) bool {
	if time.Since(a[i].Authordate.AsTime()) < time.Since(a[j].Authordate.AsTime()) {
		return true
	}
	return false
}
func (a GitTagAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// rill is awesome. long live rill
// attempt scan with rill
func (all *Repos) RillGitPull() error {
	loop := all.SortByGoPath()
	for loop.Scan() {
		t := loop.Next()
		log.Info("repo", t.GoPath)
	}
	/*
		packs := all.SortByGoPath()
		// Convert a slice of user IDs into a channel
		ids := rill.FromSlice(packs, nil)

		// Read users from the API.
		// Concurrency = 20
		dirs := rill.Map(ids, 20, func(id string) (*Repo, error) {
			return packs[id], nil
		})

		// Activate users.
		// Concurrency = 10
		err := rill.ForEach(dirs, 10, func(repo *Repo) error {
			// could do something here
			// fmt.Printf("Repo found : %s\n", repo.GoPath)
			// repo.Run([]string{"git", "pull"})
			return nil
		})
	*/
	return nil
}

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

// checks to see if you can do a 'git pull'
func (repo *Repo) IsOnlyLocalTag(currentName string) bool {
	log.Warn("IsOnlyLocalTag(currentName string) not re-implemented yet")
	return false
}

func (repo *Repo) GitPull() (string, error) {
	currentName := repo.GetCurrentBranchName()
	if repo.IsOnlyLocalTag(currentName) {
		return "", ErrorGitPullOnLocal
	}
	var cmd []string
	cmd = append(cmd, "git", "pull")
	r := repo.Run(cmd)
	output := strings.Join(r.Stdout, "\n")
	if r.Error != nil {
		output = "git error_,,,_a_,,,_b_,,,c"
	}
	if r.Error == nil {
		log.Log(GITPBWARN, "git pull ran", repo.GetGoPath())
		log.Log(GITPBWARN, "git pull output", output)
	} else {
		log.Log(GITPBWARN, "git pull error", repo.GetGoPath(), r.Error)
	}
	return output, r.Error
}