77package git
88
99import (
10- "bytes"
1110 "fmt"
12- "strconv"
13- "strings"
1411 "time"
12+
13+ "code.gitea.io/gitea/modules/util"
1514)
1615
17- // Signature represents the Author or Committer information.
16+ // Signature represents the Author, Committer or Tagger information.
1817type Signature struct {
19- // Name represents a person name. It is an arbitrary string.
20- Name string
21- // Email is an email, but it cannot be assumed to be well-formed.
22- Email string
23- // When is the timestamp of the signature.
24- When time.Time
18+ Name string // the committer name, it can be anything
19+ Email string // the committer email, it can be anything
20+ When time.Time // the timestamp of the signature
2521}
2622
2723func (s * Signature ) String () string {
@@ -30,71 +26,5 @@ func (s *Signature) String() string {
3026
3127// Decode decodes a byte array representing a signature to signature
3228func (s * Signature ) Decode (b []byte ) {
33- sig , _ := newSignatureFromCommitline (b )
34- s .Email = sig .Email
35- s .Name = sig .Name
36- s .When = sig .When
37- }
38-
39- // Helper to get a signature from the commit line, which looks like these:
40- //
41- // author Patrick Gundlach <gundlach@speedata.de> 1378823654 +0200
42- // author Patrick Gundlach <gundlach@speedata.de> Thu, 07 Apr 2005 22:13:13 +0200
43- //
44- // but without the "author " at the beginning (this method should)
45- // be used for author and committer.
46- // FIXME: there are a lot of "return sig, err" (but the err is also nil), that's the old behavior, to avoid breaking
47- func newSignatureFromCommitline (line []byte ) (sig * Signature , err error ) {
48- sig = new (Signature )
49- emailStart := bytes .LastIndexByte (line , '<' )
50- emailEnd := bytes .LastIndexByte (line , '>' )
51- if emailStart == - 1 || emailEnd == - 1 || emailEnd < emailStart {
52- return sig , err
53- }
54-
55- if emailStart > 0 { // Empty name has already occurred, even if it shouldn't
56- sig .Name = strings .TrimSpace (string (line [:emailStart - 1 ]))
57- }
58- sig .Email = string (line [emailStart + 1 : emailEnd ])
59-
60- hasTime := emailEnd + 2 < len (line )
61- if ! hasTime {
62- return sig , err
63- }
64-
65- // Check date format.
66- firstChar := line [emailEnd + 2 ]
67- if firstChar >= 48 && firstChar <= 57 {
68- idx := bytes .IndexByte (line [emailEnd + 2 :], ' ' )
69- if idx < 0 {
70- return sig , err
71- }
72-
73- timestring := string (line [emailEnd + 2 : emailEnd + 2 + idx ])
74- seconds , _ := strconv .ParseInt (timestring , 10 , 64 )
75- sig .When = time .Unix (seconds , 0 )
76-
77- idx += emailEnd + 3
78- if idx >= len (line ) || idx + 5 > len (line ) {
79- return sig , err
80- }
81-
82- timezone := string (line [idx : idx + 5 ])
83- tzhours , err1 := strconv .ParseInt (timezone [0 :3 ], 10 , 64 )
84- tzmins , err2 := strconv .ParseInt (timezone [3 :], 10 , 64 )
85- if err1 != nil || err2 != nil {
86- return sig , err
87- }
88- if tzhours < 0 {
89- tzmins *= - 1
90- }
91- tz := time .FixedZone ("" , int (tzhours * 60 * 60 + tzmins * 60 ))
92- sig .When = sig .When .In (tz )
93- } else {
94- sig .When , err = time .Parse (GitTimeLayout , string (line [emailEnd + 2 :]))
95- if err != nil {
96- return sig , err
97- }
98- }
99- return sig , err
29+ * s = * parseSignatureFromCommitLine (util .UnsafeBytesToString (b ))
10030}
0 commit comments