-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhistory.go
83 lines (73 loc) · 1.98 KB
/
history.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
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) History(ctx context.Context, prj *project.Project, hist *HistoryResult, logger util.Logger) (*Result, error) {
err := gitHistory(ctx, prj.Path, hist, logger)
if err != nil {
return nil, errors.Wrap(err, "unable to retrieve history")
}
return NewResult(prj, ok, util.ValueMap{"history": hist}), nil
}
func gitHistory(ctx context.Context, path string, hist *HistoryResult, logger util.Logger) error {
// if hist.Commit != "" {
// curr := &HistoryEntry{SHA: hist.Commit}
// curr.Files = HistoryFiles{
// {Status: "OK", File: "foo.txt"},
// }
// hist.Entries = append(hist.Entries, curr)
// return nil
//}
format := "%H»¦«%an»¦«%ae»¦«%cd»¦«%B»¦¦¦«"
cmd := "log --pretty=format:" + format
if hist.Since != nil {
cmd += fmt.Sprintf(" --since %v", hist.Since)
}
if hist.Limit > 0 {
cmd += fmt.Sprintf(" --max-count %d", hist.Limit)
}
lo.ForEach(hist.Authors, func(author string, _ int) {
cmd += fmt.Sprintf(" --author %s", author)
})
if hist.Path != "" {
cmd += fmt.Sprintf(" -- %s", path)
}
out, err := gitCmd(ctx, cmd, path, logger)
if err != nil {
if isNoRepo(err) {
return nil
}
return err
}
// hist.Debug = out
res, err := ParseResultsDelimited(out)
if err != nil {
return err
}
hist.Entries = res
return nil
}
func ParseResultsDelimited(output string) (HistoryEntries, error) {
var commits HistoryEntries
lines := util.StringSplitAndTrim(output, "»¦¦¦«")
for _, line := range lines {
parts := strings.Split(line, "»¦«")
if len(parts) != 5 {
return nil, errors.Errorf("line [%s] only has [%d] parts", line, len(parts))
}
commits = append(commits, &HistoryEntry{
SHA: parts[0],
AuthorName: parts[1],
AuthorEmail: parts[2],
Message: parts[4],
Occurred: parts[3],
})
}
return commits, nil
}