-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (67 loc) · 1.57 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
type Commit struct {
Time time.Time
ID string
Author string
Message string
ChangedFiles []string
}
func checkIfError(e error) {
if e != nil {
fmt.Fprintf(os.Stderr, "%s\n", e)
os.Exit(1)
}
}
func main() {
path := "."
if len(os.Args) > 1 {
path = os.Args[1]
}
r, err := git.PlainOpen(path)
checkIfError(err)
ref, err := r.Head()
checkIfError(err)
history, err := r.Log(&git.LogOptions{From: ref.Hash()})
checkIfError(err)
commits := []Commit{}
err = history.ForEach(func(c *object.Commit) error {
commit := Commit{
ID: c.ID().String(),
Author: c.Author.Name,
Time: c.Author.When,
Message: strings.TrimRight(c.Message, "\n"),
}
if len(c.ParentHashes) > 0 {
if parent, err := r.CommitObject(c.ParentHashes[0]); err == nil {
if patch, err := parent.Patch(c); err == nil {
for _, p := range patch.FilePatches() {
if from, to := p.Files(); from != nil {
commit.ChangedFiles = append(commit.ChangedFiles, from.Path())
} else if to != from {
commit.ChangedFiles = append(commit.ChangedFiles, to.Path())
}
}
}
}
}
commits = append(commits, commit)
return nil
})
checkIfError(err)
sort.Slice(commits, func(i, j int) bool {
return commits[i].Time.Unix() > commits[j].Time.Unix()
})
if out, err := json.MarshalIndent(commits, "", " "); err == nil {
fmt.Println(string(out))
}
}