git-module

git clone git://git.lin.moe/fork/git-module.git

 1package git
 2
 3import (
 4	"bytes"
 5	"strconv"
 6	"time"
 7)
 8
 9// Signature represents a author or committer.
10type Signature struct {
11	// The name of the person.
12	Name string
13	// The email address.
14	Email string
15	// The time of the signature.
16	When time.Time
17}
18
19// parseSignature parses signature information from the (uncompressed) commit
20// line, which looks like the following but without the "author " at the
21// beginning:
22//
23//	author Patrick Gundlach <gundlach@speedata.de> 1378823654 +0200
24//	author Patrick Gundlach <gundlach@speedata.de> Thu Apr 07 22:13:13 2005 +0200
25//
26// This method should only be used for parsing author and committer.
27func parseSignature(line []byte) (*Signature, error) {
28	emailStart := bytes.IndexByte(line, '<')
29	emailEnd := bytes.IndexByte(line, '>')
30	sig := &Signature{
31		Name:  string(line[:emailStart-1]),
32		Email: string(line[emailStart+1 : emailEnd]),
33	}
34
35	// Check the date format
36	firstChar := line[emailEnd+2]
37	if firstChar >= 48 && firstChar <= 57 { // ASCII code for 0-9
38		timestop := bytes.IndexByte(line[emailEnd+2:], ' ')
39		timestamp := line[emailEnd+2 : emailEnd+2+timestop]
40		seconds, err := strconv.ParseInt(string(timestamp), 10, 64)
41		if err != nil {
42			return nil, err
43		}
44		sig.When = time.Unix(seconds, 0)
45		return sig, nil
46	}
47
48	var err error
49	sig.When, err = time.Parse("Mon Jan _2 15:04:05 2006 -0700", string(line[emailEnd+2:]))
50	if err != nil {
51		return nil, err
52	}
53	return sig, nil
54}