-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
203 lines (186 loc) · 4.61 KB
/
git.go
File metadata and controls
203 lines (186 loc) · 4.61 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"bytes"
"context"
"fmt"
"os/exec"
"strings"
"time"
)
// Commit represents a single git commit.
type Commit struct {
Hash string
Parents []string
AuthorName string
AuthorEmail string
AuthorDate time.Time
Subject string
Decorations []Decoration
}
// ShortHash returns the first 7 characters of the hash.
func (c *Commit) ShortHash() string {
if len(c.Hash) > 7 {
return c.Hash[:7]
}
return c.Hash
}
// RelativeDate returns a human-readable relative date.
func (c *Commit) RelativeDate() string {
d := time.Since(c.AuthorDate)
switch {
case d < time.Minute:
return "just now"
case d < time.Hour:
m := int(d.Minutes())
if m == 1 {
return "1 minute ago"
}
return fmt.Sprintf("%d minutes ago", m)
case d < 24*time.Hour:
h := int(d.Hours())
if h == 1 {
return "1 hour ago"
}
return fmt.Sprintf("%d hours ago", h)
case d < 30*24*time.Hour:
days := int(d.Hours() / 24)
if days == 1 {
return "1 day ago"
}
return fmt.Sprintf("%d days ago", days)
case d < 365*24*time.Hour:
months := int(d.Hours() / 24 / 30)
if months <= 1 {
return "1 month ago"
}
return fmt.Sprintf("%d months ago", months)
default:
years := int(d.Hours() / 24 / 365)
if years == 1 {
return "1 year ago"
}
return fmt.Sprintf("%d years ago", years)
}
}
// DecorationKind indicates the type of a decoration.
type DecorationKind int
const (
DecorationBranch DecorationKind = iota
DecorationTag
DecorationHead
)
// Decoration represents a branch or tag label on a commit.
type Decoration struct {
Name string
Kind DecorationKind
}
// gitOutput runs a git command in dir and returns its output.
func gitOutput(ctx context.Context, dir string, args ...string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("git %s: %w", args[0], err)
}
return out, nil
}
const commitFormat = "--format=%H%x00%P%x00%an%x00%ae%x00%aI%x00%s%x00%D"
// LoadCommits loads all commits from the given repository directory.
func LoadCommits(dir string) ([]Commit, error) {
out, err := gitOutput(context.Background(), dir, "log", "--all", commitFormat)
if err != nil {
return nil, err
}
return parseCommits(out)
}
// SearchCommits searches for commits matching the query.
// The query is matched against subject (--grep), author (--author), and hash prefix.
// The context can be used to cancel the git process.
func SearchCommits(ctx context.Context, dir, query string) ([]Commit, error) {
out, err := gitOutput(ctx, dir, "log", "--all",
"--grep="+query, "--author="+query, commitFormat)
if err != nil {
return nil, err
}
return parseCommits(out)
}
// LoadDiff returns the full commit info and diff for a single commit.
// The context can be used to cancel the git process.
func LoadDiff(ctx context.Context, dir, hash string) (string, error) {
out, err := gitOutput(ctx, dir, "show", "--format=fuller", "--root", hash)
if err != nil {
return "", err
}
return string(out), nil
}
func parseCommits(data []byte) ([]Commit, error) {
var commits []Commit
for _, line := range bytes.Split(data, []byte("\n")) {
if len(line) == 0 {
continue
}
parts := bytes.SplitN(line, []byte{0}, 7)
if len(parts) < 7 {
continue
}
t, err := time.Parse(time.RFC3339, string(parts[4]))
if err != nil {
t = time.Time{}
}
var parents []string
if len(parts[1]) > 0 {
parents = strings.Split(string(parts[1]), " ")
}
c := Commit{
Hash: string(parts[0]),
Parents: parents,
AuthorName: string(parts[2]),
AuthorEmail: string(parts[3]),
AuthorDate: t,
Subject: string(parts[5]),
Decorations: parseDecorations(string(parts[6])),
}
commits = append(commits, c)
}
return commits, nil
}
func parseDecorations(raw string) []Decoration {
if raw == "" {
return nil
}
var decs []Decoration
for _, part := range strings.Split(raw, ", ") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
switch {
case strings.HasPrefix(part, "HEAD -> "):
decs = append(decs, Decoration{
Name: strings.TrimPrefix(part, "HEAD -> "),
Kind: DecorationHead,
})
case strings.HasPrefix(part, "tag: "):
decs = append(decs, Decoration{
Name: strings.TrimPrefix(part, "tag: "),
Kind: DecorationTag,
})
case part == "HEAD":
decs = append(decs, Decoration{
Name: "HEAD",
Kind: DecorationHead,
})
default:
// Strip remote prefix for display.
name := part
if strings.HasPrefix(name, "origin/") {
name = part
}
decs = append(decs, Decoration{
Name: name,
Kind: DecorationBranch,
})
}
}
return decs
}