generated from fallion/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.go
More file actions
83 lines (70 loc) · 1.9 KB
/
commit.go
File metadata and controls
83 lines (70 loc) · 1.9 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
package git
import (
"strconv"
"strings"
"time"
)
// Commit finds a commit based on commit hash and returns the Commit object
func (g *Git) Commit(hash Hash) (*Commit, error) {
hashStr := hash.String()
// Get commit message - use %B to get full body, then normalize
// go-git's Message field returns the raw commit message which always ends with \n
fullMessage, err := g.runGitCommand("log", "-1", "--format=%B", hashStr)
if err != nil {
return nil, err
}
// Normalize: remove trailing newlines and add exactly one
// This matches go-git's behavior where Message always ends with \n
message := strings.TrimRight(fullMessage, "\n")
if message != "" {
message += "\n"
}
// Get author info
authorName, err := g.runGitCommand("log", "-1", "--format=%an", hashStr)
if err != nil {
return nil, err
}
authorEmail, err := g.runGitCommand("log", "-1", "--format=%ae", hashStr)
if err != nil {
return nil, err
}
authorDateStr, err := g.runGitCommand("log", "-1", "--format=%at", hashStr)
if err != nil {
return nil, err
}
authorTimestamp, err := strconv.ParseInt(authorDateStr, 10, 64)
if err != nil {
return nil, err
}
// Get committer info
committerName, err := g.runGitCommand("log", "-1", "--format=%cn", hashStr)
if err != nil {
return nil, err
}
committerEmail, err := g.runGitCommand("log", "-1", "--format=%ce", hashStr)
if err != nil {
return nil, err
}
committerDateStr, err := g.runGitCommand("log", "-1", "--format=%ct", hashStr)
if err != nil {
return nil, err
}
committerTimestamp, err := strconv.ParseInt(committerDateStr, 10, 64)
if err != nil {
return nil, err
}
return &Commit{
Hash: hash,
Message: message,
Author: Signature{
Name: authorName,
Email: authorEmail,
When: time.Unix(authorTimestamp, 0),
},
Committer: Signature{
Name: committerName,
Email: committerEmail,
When: time.Unix(committerTimestamp, 0),
},
}, nil
}