-
Notifications
You must be signed in to change notification settings - Fork 0
/
repos.go
168 lines (141 loc) · 3.24 KB
/
repos.go
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package zcmd
import (
"context"
"errors"
"os"
"path/filepath"
"github.com/cybozu-go/well"
log "github.com/sirupsen/logrus"
)
const (
cmdGit = "/usr/bin/git"
)
// Updater is client for repos update
type Updater struct {
root string
repositories []string
}
// NewUpdater returns Updater with given root directory
func NewUpdater(root string) (*Updater, error) {
if len(root) == 0 {
return nil, errors.New("no root directory specified")
}
return &Updater{
root: root,
}, nil
}
// FindRepositories traverses given directory and return git repositories
func (u *Updater) FindRepositories() error {
return filepath.Walk(u.root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
return err
}
if info.Name() != ".git" {
// Skip
return nil
}
gitPath := filepath.Dir(path)
log.WithFields(log.Fields{
"path": gitPath,
}).Info("found git repository")
u.repositories = append(u.repositories, gitPath)
return nil
})
}
// Update fetches, checkouts and pulls git repositories
func (u *Updater) Update(ctx context.Context, jobs int) error {
jobChan := make(chan struct{}, jobs)
for i := 0; i < jobs; i++ {
jobChan <- struct{}{}
}
env := well.NewEnvironment(ctx)
for _, r := range u.repositories {
ri := r
env.Go(func(ctx context.Context) error {
<-jobChan
defer func() { jobChan <- struct{}{} }()
err := clean(ctx, ri)
if err != nil {
log.WithFields(log.Fields{
"path": ri,
"error": err,
}).Error("git clean")
return err
}
log.WithFields(log.Fields{
"path": ri,
}).Info("git clean")
err = checkoutMaster(ctx, ri)
if err != nil {
log.WithFields(log.Fields{
"path": ri,
"error": err,
}).Error("git checkout master")
return err
}
log.WithFields(log.Fields{
"path": ri,
}).Info("git checkout master")
err = pull(ctx, ri)
if err != nil {
log.WithFields(log.Fields{
"path": ri,
"error": err,
}).Error("git pull")
return err
}
log.WithFields(log.Fields{
"path": ri,
}).Info("git pull")
err = status(ctx, ri)
if err != nil {
log.WithFields(log.Fields{
"path": ri,
"error": err,
}).Error("git status")
return err
}
log.WithFields(log.Fields{
"path": ri,
}).Info("git status")
return nil
})
}
env.Stop()
return env.Wait()
}
func clean(ctx context.Context, path string) error {
args := []string{"clean", "-xdf"}
cmd := well.CommandContext(ctx, cmdGit, args...)
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func checkoutMaster(ctx context.Context, path string) error {
args := []string{"checkout", "master"}
cmd := well.CommandContext(ctx, cmdGit, args...)
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func pull(ctx context.Context, path string) error {
args := []string{"pull"}
cmd := well.CommandContext(ctx, cmdGit, args...)
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func status(ctx context.Context, path string) error {
args := []string{"status"}
cmd := well.CommandContext(ctx, cmdGit, args...)
cmd.Dir = path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}