-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.go
More file actions
213 lines (198 loc) · 5 KB
/
commit.go
File metadata and controls
213 lines (198 loc) · 5 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
204
205
206
207
208
209
210
211
212
213
// Copyright 2014 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package ggit
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"strconv"
"strings"
"time"
)
func parseHashLine(line, leader string) (string, error) {
if len(line) != 42+len(leader) {
return "", fmt.Errorf("bad commit format: \"%s\"", line)
}
hash := line[len(leader)+1 : len(line)-1]
for _, c := range hash {
if c < 'a' && c > 'z' && c < '0' && c > '9' {
return "", fmt.Errorf("bad commit format hash: \"%s\"", line)
}
}
return hash, nil
}
// "whom" SP "name possibly with many spaces" SP "<" email ">" SP timestamp SP zone NL
func parsePersonLine(line, whom string) (name, email, zone string, t time.Time, err error) {
// work from the end since arbitrary spaces only appear in second entry
i := 0
for i = len(line) - 1; i >= 0; i-- {
if line[i] == ' ' {
zone = line[i+1 : len(line)-1]
break
}
}
if i <= 9 {
err = fmt.Errorf("bad person line %s", line)
return
}
lastSpace := i
for i--; i >= 0; i-- {
if line[i] == ' ' {
timeSec := int64(0)
timeSec, err = strconv.ParseInt(line[i+1:lastSpace], 10, 32)
if err != nil {
return
}
t = time.Unix(timeSec, 0)
break
}
}
if i <= 7 {
err = fmt.Errorf("bad person line %s", line)
return
}
lastSpace = i
for i--; i >= 0; i-- {
if line[i] == ' ' {
maybeEmail := line[i+1 : lastSpace]
if maybeEmail[0] != '<' || maybeEmail[len(maybeEmail)-1] != '>' {
err = fmt.Errorf("bad email %s", maybeEmail)
return
}
email = maybeEmail[1 : len(maybeEmail)-1]
break
}
}
if i <= 3 {
err = fmt.Errorf("bad person line %s", line)
return
}
lastSpace = i
for i = 0; i < len(whom); i++ {
if line[i] != whom[i] {
err = errors.New("bad person format")
return
}
}
name = line[i+1 : lastSpace]
return
}
type commit struct {
Hash, Tree string
Parent []string
author, authorEmail string
committer, committerEmail string
date time.Time
zone string
messageReader *bufio.Reader
zlibReader io.ReadCloser
messageStr *string // lazily populated from reader
}
// time.ANSIC with s/_2/2/
const timeFormat = "Mon Jan 2 15:04:05 2006"
func (c commit) String() string {
s := "commit " + c.Hash + "\n"
s += "Author: " + c.author + " <" + c.authorEmail + ">\n"
s += "Date: " + c.date.Format(timeFormat) + " " + c.zone + "\n\n"
lines := strings.Split(c.Message(), "\n")
for _, l := range lines {
s += " " + l + "\n"
}
return s
}
func parseKnownFields(c *commit, r io.Reader) error {
br := getBufioReader(r)
for {
line, err := br.ReadString('\n')
if err != nil {
return fmt.Errorf("ReadString err %v", err)
}
switch {
case line == "\n":
c.messageReader = br
return nil
case strings.HasPrefix(line, "tree "):
c.Tree, err = parseHashLine(line, "tree")
if err != nil {
return fmt.Errorf("hash %v", err)
}
case strings.HasPrefix(line, "parent "):
parent, err := parseHashLine(line, "parent")
if err != nil {
return fmt.Errorf("parent %v", err)
}
c.Parent = append(c.Parent, parent)
case strings.HasPrefix(line, "author "):
c.author, c.authorEmail, c.zone, c.date, err = parsePersonLine(line, "author")
if err != nil {
return fmt.Errorf("author %v", err)
}
case strings.HasPrefix(line, "committer "):
c.committer, c.committerEmail, _, _, err = parsePersonLine(line, "committer")
if err != nil {
return fmt.Errorf("committer %v", err)
}
default:
// unknown line, ignore for now
// fmt.Fprintf(os.Stderr, "unknown line \"%s\"\n", line)
}
}
}
func parseCommitObject(o Object) (commit, error) {
c := commit{}
err := parseKnownFields(&c, o.Reader)
if err != nil {
return commit{}, fmt.Errorf("parsing known fields %v", err)
}
c.zlibReader = o.zlibReader
return c, nil
}
func (c *commit) Close() {
if c.zlibReader != nil {
returnZlibReader(c.zlibReader)
c.zlibReader = nil
}
if c.messageReader != nil {
returnBufioReader(c.messageReader)
c.messageReader = nil
}
}
func (c *commit) Message() string {
if c.messageStr == nil {
b, err := ioutil.ReadAll(c.messageReader)
if err != nil {
panic(err)
}
c.Close()
s := string(b)
c.messageStr = &s
}
return *c.messageStr
}
func ReadCommit(committish string) (commit, error) {
hash := CommitishToHash(committish)
object, err := LookupObject(hash)
if err != nil {
return commit{}, fmt.Errorf("error parsing object %v", err)
}
if object.ObjectType != "commit" {
return commit{}, fmt.Errorf("object %s has bad type: %s", hash, object.ObjectType)
}
c, err := parseCommitObject(object)
if err != nil {
return commit{}, fmt.Errorf("error parsing commit %v", err)
}
c.Hash = hash
return c, nil
}
func showCommit(committish string) (string, error) {
c, err := ReadCommit(committish)
if err != nil {
return "", err
}
return c.String(), nil
}