-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreset.go
42 lines (36 loc) · 999 Bytes
/
reset.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
package git
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/samber/lo"
"projectforge.dev/projectforge/app/project"
"projectforge.dev/projectforge/app/util"
)
func (s *Service) Reset(ctx context.Context, prj *project.Project, logger util.Logger) (*Result, error) {
x, err := gitReset(ctx, prj.Path, logger)
if err != nil {
return nil, errors.Wrap(err, "unable to reset git repo")
}
count := lo.CountBy(util.StringSplitLines(x), func(line string) bool {
return strings.HasPrefix(line, " ")
})
status := ok
fetched := "no changes"
if count > 0 {
status = fmt.Sprintf("[%s] reset", util.StringPlural(count, "file"))
fetched = status
}
return NewResult(prj, status, util.ValueMap{"updates": fetched}), nil
}
func gitReset(ctx context.Context, path string, logger util.Logger) (string, error) {
out, err := gitCmd(ctx, "reset --hard", path, logger)
if err != nil {
if isNoRepo(err) {
return "", nil
}
return "", err
}
return out, nil
}