-
Notifications
You must be signed in to change notification settings - Fork 4
/
fetch.go
49 lines (43 loc) · 1.03 KB
/
fetch.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
package git
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
"projectforge.dev/projectforge/app/project"
"projectforge.dev/projectforge/app/util"
)
func (s *Service) Fetch(ctx context.Context, prj *project.Project, logger util.Logger) (*Result, error) {
x, err := gitFetch(ctx, prj.Path, true, logger)
if err != nil {
return nil, errors.Wrap(err, "unable to fetch")
}
count := 0
lines := strings.Split(x, "\n")
for _, line := range lines {
if strings.HasPrefix(line, " ") {
count++
}
}
status := ok
fetched := noUpdates
if count > 0 {
status = fmt.Sprintf("[%d] %s fetched", count, util.StringPluralMaybe("update", count))
fetched = status
}
return NewResult(prj, status, util.ValueMap{"updates": fetched}), nil
}
func gitFetch(ctx context.Context, path string, dryRun bool, logger util.Logger) (string, error) {
cmd := "fetch"
if dryRun {
cmd += " --dry-run"
}
out, err := gitCmd(ctx, cmd, path, logger)
if err != nil {
if isNoRepo(err) {
return "", nil
}
return "", err
}
return out, nil
}