| 1 |
package gitobj |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"encoding/hex" |
| 7 |
"fmt" |
| 8 |
"hash" |
| 9 |
"io" |
| 10 |
"strings" |
| 11 |
"time" |
| 12 |
) |
| 13 |
|
| 14 |
// Signature represents a commit signature, which can represent either |
| 15 |
// committership or authorship of the commit that this signature belongs to. It |
| 16 |
// specifies a name, email, and time that the signature was created. |
| 17 |
// |
| 18 |
// NOTE: this type is _not_ used by the `*Commit` instance, as it does not |
| 19 |
// preserve cruft bytes. It is kept as a convenience type to test with. |
| 20 |
type Signature struct { |
| 21 |
// Name is the first and last name of the individual holding this |
| 22 |
// signature. |
| 23 |
Name string |
| 24 |
// Email is the email address of the individual holding this signature. |
| 25 |
Email string |
| 26 |
// When is the instant in time when the signature was created. |
| 27 |
When time.Time |
| 28 |
} |
| 29 |
|
| 30 |
const ( |
| 31 |
formatTimeZoneOnly = "-0700" |
| 32 |
) |
| 33 |
|
| 34 |
// String implements the fmt.Stringer interface and formats a Signature as |
| 35 |
// expected in the Git commit internal object format. For instance: |
| 36 |
// |
| 37 |
// Taylor Blau <[email protected]> 1494258422 -0600 |
| 38 |
func (s *Signature) String() string { |
| 39 |
at := s.When.Unix() |
| 40 |
zone := s.When.Format(formatTimeZoneOnly) |
| 41 |
|
| 42 |
return fmt.Sprintf("%s <%s> %d %s", s.Name, s.Email, at, zone) |
| 43 |
} |
| 44 |
|
| 45 |
// ExtraHeader encapsulates a key-value pairing of header key to header value. |
| 46 |
// It is stored as a struct{string, string} in memory as opposed to a |
| 47 |
// map[string]string to maintain ordering in a byte-for-byte encode/decode round |
| 48 |
// trip. |
| 49 |
type ExtraHeader struct { |
| 50 |
// K is the header key, or the first run of bytes up until a ' ' (\x20) |
| 51 |
// character. |
| 52 |
K string |
| 53 |
// V is the header value, or the remaining run of bytes in the line, |
| 54 |
// stripping off the above "K" field as a prefix. |
| 55 |
V string |
| 56 |
} |
| 57 |
|
| 58 |
// Commit encapsulates a Git commit entry. |
| 59 |
type Commit struct { |
| 60 |
// Author is the Author this commit, or the original writer of the |
| 61 |
// contents. |
| 62 |
// |
| 63 |
// NOTE: this field is stored as a string to ensure any extra "cruft" |
| 64 |
// bytes are preserved through migration. |
| 65 |
Author string |
| 66 |
// Committer is the individual or entity that added this commit to the |
| 67 |
// history. |
| 68 |
// |
| 69 |
// NOTE: this field is stored as a string to ensure any extra "cruft" |
| 70 |
// bytes are preserved through migration. |
| 71 |
Committer string |
| 72 |
// ParentIDs are the IDs of all parents for which this commit is a |
| 73 |
// linear child. |
| 74 |
ParentIDs [][]byte |
| 75 |
// TreeID is the root Tree associated with this commit. |
| 76 |
TreeID []byte |
| 77 |
// ExtraHeaders stores headers not listed above, for instance |
| 78 |
// "encoding", "gpgsig", or "mergetag" (among others). |
| 79 |
ExtraHeaders []*ExtraHeader |
| 80 |
// Message is the commit message, including any signing information |
| 81 |
// associated with this commit. |
| 82 |
Message string |
| 83 |
} |
| 84 |
|
| 85 |
// Type implements Object.ObjectType by returning the correct object type for |
| 86 |
// Commits, CommitObjectType. |
| 87 |
func (c *Commit) Type() ObjectType { return CommitObjectType } |
| 88 |
|
| 89 |
// Decode implements Object.Decode and decodes the uncompressed commit being |
| 90 |
// read. It returns the number of uncompressed bytes being consumed off of the |
| 91 |
// stream, which should be strictly equal to the size given. |
| 92 |
// |
| 93 |
// If any error was encountered along the way, that will be returned, along with |
| 94 |
// the number of bytes read up to that point. |
| 95 |
func (c *Commit) Decode(hash hash.Hash, from io.Reader, size int64) (n int, err error) { |
| 96 |
var finishedHeaders bool |
| 97 |
var messageParts []string |
| 98 |
|
| 99 |
s := bufio.NewScanner(from) |
| 100 |
s.Buffer(nil, 10*1024*1024) |
| 101 |
for s.Scan() { |
| 102 |
text := s.Text() |
| 103 |
n = n + len(text+"\n") |
| 104 |
|
| 105 |
if len(s.Text()) == 0 && !finishedHeaders { |
| 106 |
finishedHeaders = true |
| 107 |
continue |
| 108 |
} |
| 109 |
|
| 110 |
if fields := strings.Split(text, " "); !finishedHeaders { |
| 111 |
if len(fields) == 0 { |
| 112 |
// Executing in this block means that we got a |
| 113 |
// whitespace-only line, while parsing a header. |
| 114 |
// |
| 115 |
// Append it to the last-parsed header, and |
| 116 |
// continue. |
| 117 |
c.ExtraHeaders[len(c.ExtraHeaders)-1].V += |
| 118 |
fmt.Sprintf("\n%s", text[1:]) |
| 119 |
continue |
| 120 |
} |
| 121 |
|
| 122 |
switch fields[0] { |
| 123 |
case "tree": |
| 124 |
id, err := hex.DecodeString(fields[1]) |
| 125 |
if err != nil { |
| 126 |
return n, fmt.Errorf("error parsing tree: %s", err) |
| 127 |
} |
| 128 |
c.TreeID = id |
| 129 |
case "parent": |
| 130 |
id, err := hex.DecodeString(fields[1]) |
| 131 |
if err != nil { |
| 132 |
return n, fmt.Errorf("error parsing parent: %s", err) |
| 133 |
} |
| 134 |
c.ParentIDs = append(c.ParentIDs, id) |
| 135 |
case "author": |
| 136 |
if len(text) >= 7 { |
| 137 |
c.Author = text[7:] |
| 138 |
} else { |
| 139 |
c.Author = "" |
| 140 |
} |
| 141 |
case "committer": |
| 142 |
if len(text) >= 10 { |
| 143 |
c.Committer = text[10:] |
| 144 |
} else { |
| 145 |
c.Committer = "" |
| 146 |
} |
| 147 |
default: |
| 148 |
if strings.HasPrefix(s.Text(), " ") { |
| 149 |
idx := len(c.ExtraHeaders) - 1 |
| 150 |
hdr := c.ExtraHeaders[idx] |
| 151 |
|
| 152 |
// Append the line of text (removing the |
| 153 |
// leading space) to the last header |
| 154 |
// that we parsed, adding a newline |
| 155 |
// between the two. |
| 156 |
hdr.V = strings.Join(append( |
| 157 |
[]string{hdr.V}, s.Text()[1:], |
| 158 |
), "\n") |
| 159 |
} else { |
| 160 |
c.ExtraHeaders = append(c.ExtraHeaders, &ExtraHeader{ |
| 161 |
K: fields[0], |
| 162 |
V: strings.Join(fields[1:], " "), |
| 163 |
}) |
| 164 |
} |
| 165 |
} |
| 166 |
} else { |
| 167 |
messageParts = append(messageParts, s.Text()) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
c.Message = strings.Join(messageParts, "\n") |
| 172 |
|
| 173 |
if err = s.Err(); err != nil { |
| 174 |
return n, fmt.Errorf("failed to parse commit buffer: %s", err) |
| 175 |
} |
| 176 |
return n, err |
| 177 |
} |
| 178 |
|
| 179 |
// Encode encodes the commit's contents to the given io.Writer, "w". If there was |
| 180 |
// any error copying the commit's contents, that error will be returned. |
| 181 |
// |
| 182 |
// Otherwise, the number of bytes written will be returned. |
| 183 |
func (c *Commit) Encode(to io.Writer) (n int, err error) { |
| 184 |
n, err = fmt.Fprintf(to, "tree %s\n", hex.EncodeToString(c.TreeID)) |
| 185 |
if err != nil { |
| 186 |
return n, err |
| 187 |
} |
| 188 |
|
| 189 |
for _, pid := range c.ParentIDs { |
| 190 |
n1, err := fmt.Fprintf(to, "parent %s\n", hex.EncodeToString(pid)) |
| 191 |
if err != nil { |
| 192 |
return n, err |
| 193 |
} |
| 194 |
|
| 195 |
n = n + n1 |
| 196 |
} |
| 197 |
|
| 198 |
n2, err := fmt.Fprintf(to, "author %s\ncommitter %s\n", c.Author, c.Committer) |
| 199 |
if err != nil { |
| 200 |
return n, err |
| 201 |
} |
| 202 |
|
| 203 |
n = n + n2 |
| 204 |
|
| 205 |
for _, hdr := range c.ExtraHeaders { |
| 206 |
n3, err := fmt.Fprintf(to, "%s %s\n", |
| 207 |
hdr.K, strings.Replace(hdr.V, "\n", "\n ", -1)) |
| 208 |
if err != nil { |
| 209 |
return n, err |
| 210 |
} |
| 211 |
|
| 212 |
n = n + n3 |
| 213 |
} |
| 214 |
|
| 215 |
// c.Message is built from messageParts in the Decode() function. |
| 216 |
// |
| 217 |
// Since each entry in messageParts _does not_ contain its trailing LF, |
| 218 |
// append an empty string to capture the final newline. |
| 219 |
n4, err := fmt.Fprintf(to, "\n%s\n", c.Message) |
| 220 |
if err != nil { |
| 221 |
return n, err |
| 222 |
} |
| 223 |
|
| 224 |
return n + n4, err |
| 225 |
} |
| 226 |
|
| 227 |
// Equal returns whether the receiving and given commits are equal, or in other |
| 228 |
// words, whether they are represented by the same SHA-1 when saved to the |
| 229 |
// object database. |
| 230 |
func (c *Commit) Equal(other *Commit) bool { |
| 231 |
if (c == nil) != (other == nil) { |
| 232 |
return false |
| 233 |
} |
| 234 |
|
| 235 |
if c != nil { |
| 236 |
if len(c.ParentIDs) != len(other.ParentIDs) { |
| 237 |
return false |
| 238 |
} |
| 239 |
for i := 0; i < len(c.ParentIDs); i++ { |
| 240 |
p1 := c.ParentIDs[i] |
| 241 |
p2 := other.ParentIDs[i] |
| 242 |
|
| 243 |
if !bytes.Equal(p1, p2) { |
| 244 |
return false |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if len(c.ExtraHeaders) != len(other.ExtraHeaders) { |
| 249 |
return false |
| 250 |
} |
| 251 |
for i := 0; i < len(c.ExtraHeaders); i++ { |
| 252 |
e1 := c.ExtraHeaders[i] |
| 253 |
e2 := other.ExtraHeaders[i] |
| 254 |
|
| 255 |
if e1.K != e2.K || e1.V != e2.V { |
| 256 |
return false |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
return c.Author == other.Author && |
| 261 |
c.Committer == other.Committer && |
| 262 |
c.Message == other.Message && |
| 263 |
bytes.Equal(c.TreeID, other.TreeID) |
| 264 |
} |
| 265 |
return true |
| 266 |
} |