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
204
205
206
207
|
package catfile
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
"github.com/golang/protobuf/ptypes/timestamp"
"gitlab.com/gitlab-org/gitaly/internal/git"
"gitlab.com/gitlab-org/gitaly/internal/git/repository"
"gitlab.com/gitlab-org/gitaly/internal/git/trailerparser"
"gitlab.com/gitlab-org/gitaly/internal/helper"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
)
// GetCommit looks up a commit by revision using an existing Batch instance.
func GetCommit(ctx context.Context, c Batch, revision git.Revision) (*gitalypb.GitCommit, error) {
obj, err := c.Commit(ctx, revision+"^{commit}")
if err != nil {
return nil, err
}
return parseRawCommit(obj.Reader, &obj.ObjectInfo)
}
// GetCommitWithTrailers looks up a commit by revision using an existing Batch instance, and
// includes Git trailers in the returned commit.
func GetCommitWithTrailers(ctx context.Context, gitCmdFactory git.CommandFactory, repo repository.GitRepo, c Batch, revision git.Revision) (*gitalypb.GitCommit, error) {
commit, err := GetCommit(ctx, c, revision)
if err != nil {
return nil, err
}
// We use the commit ID here instead of revision. This way we still get
// trailers if the revision is not a SHA but e.g. a tag name.
showCmd, err := gitCmdFactory.New(ctx, repo, git.SubCmd{
Name: "show",
Args: []string{commit.Id},
Flags: []git.Option{
git.Flag{Name: "--format=%(trailers:unfold,separator=%x00)"},
git.Flag{Name: "--no-patch"},
},
})
if err != nil {
return nil, fmt.Errorf("error when creating git show command: %w", err)
}
scanner := bufio.NewScanner(showCmd)
if scanner.Scan() {
if len(scanner.Text()) > 0 {
commit.Trailers = trailerparser.Parse([]byte(scanner.Text()))
}
if scanner.Scan() {
return nil, fmt.Errorf("git show produced more than one line of output, the second line is: %v", scanner.Text())
}
}
return commit, nil
}
// GetCommitMessage looks up a commit message and returns it in its entirety.
func GetCommitMessage(ctx context.Context, c Batch, repo repository.GitRepo, revision git.Revision) ([]byte, error) {
obj, err := c.Commit(ctx, revision+"^{commit}")
if err != nil {
return nil, err
}
_, body, err := splitRawCommit(obj.Reader)
if err != nil {
return nil, err
}
return body, nil
}
func parseRawCommit(r io.Reader, info *ObjectInfo) (*gitalypb.GitCommit, error) {
header, body, err := splitRawCommit(r)
if err != nil {
return nil, err
}
return buildCommit(header, body, info)
}
func splitRawCommit(r io.Reader) ([]byte, []byte, error) {
raw, err := ioutil.ReadAll(r)
if err != nil {
return nil, nil, err
}
split := bytes.SplitN(raw, []byte("\n\n"), 2)
header := split[0]
var body []byte
if len(split) == 2 {
body = split[1]
}
return header, body, nil
}
func buildCommit(header, body []byte, info *ObjectInfo) (*gitalypb.GitCommit, error) {
commit := &gitalypb.GitCommit{
Id: info.Oid.String(),
BodySize: int64(len(body)),
Body: body,
Subject: subjectFromBody(body),
}
if max := helper.MaxCommitOrTagMessageSize; len(body) > max {
commit.Body = commit.Body[:max]
}
scanner := bufio.NewScanner(bytes.NewReader(header))
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 || line[0] == ' ' {
continue
}
headerSplit := strings.SplitN(line, " ", 2)
if len(headerSplit) != 2 {
continue
}
switch headerSplit[0] {
case "parent":
commit.ParentIds = append(commit.ParentIds, headerSplit[1])
case "author":
commit.Author = parseCommitAuthor(headerSplit[1])
case "committer":
commit.Committer = parseCommitAuthor(headerSplit[1])
case "gpgsig":
commit.SignatureType = detectSignatureType(headerSplit[1])
case "tree":
commit.TreeId = headerSplit[1]
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return commit, nil
}
const maxUnixCommitDate = 1 << 53
func parseCommitAuthor(line string) *gitalypb.CommitAuthor {
author := &gitalypb.CommitAuthor{}
splitName := strings.SplitN(line, "<", 2)
author.Name = []byte(strings.TrimSuffix(splitName[0], " "))
if len(splitName) < 2 {
return author
}
line = splitName[1]
splitEmail := strings.SplitN(line, ">", 2)
if len(splitEmail) < 2 {
return author
}
author.Email = []byte(splitEmail[0])
secSplit := strings.Fields(splitEmail[1])
if len(secSplit) < 1 {
return author
}
sec, err := strconv.ParseInt(secSplit[0], 10, 64)
if err != nil || sec > maxUnixCommitDate || sec < 0 {
sec = git.FallbackTimeValue.Unix()
}
author.Date = ×tamp.Timestamp{Seconds: sec}
if len(secSplit) == 2 {
author.Timezone = []byte(secSplit[1])
}
return author
}
func subjectFromBody(body []byte) []byte {
return bytes.TrimRight(bytes.SplitN(body, []byte("\n"), 2)[0], "\r\n")
}
func detectSignatureType(line string) gitalypb.SignatureType {
switch strings.TrimSuffix(line, "\n") {
case "-----BEGIN SIGNED MESSAGE-----":
return gitalypb.SignatureType_X509
case "-----BEGIN PGP MESSAGE-----":
return gitalypb.SignatureType_PGP
case "-----BEGIN PGP SIGNATURE-----":
return gitalypb.SignatureType_PGP
default:
return gitalypb.SignatureType_NONE
}
}
|