code.gitea.io/gitea@v1.19.3/modules/git/signature_nogogit.go (about) 1 // Copyright 2015 The Gogs Authors. All rights reserved. 2 // Copyright 2019 The Gitea Authors. All rights reserved. 3 // SPDX-License-Identifier: MIT 4 5 //go:build !gogit 6 7 package git 8 9 import ( 10 "bytes" 11 "fmt" 12 "strconv" 13 "strings" 14 "time" 15 ) 16 17 // Signature represents the Author or Committer information. 18 type 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 25 } 26 27 func (s *Signature) String() string { 28 return fmt.Sprintf("%s <%s>", s.Name, s.Email) 29 } 30 31 // Decode decodes a byte array representing a signature to signature 32 func (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 func newSignatureFromCommitline(line []byte) (sig *Signature, err error) { 47 sig = new(Signature) 48 emailStart := bytes.LastIndexByte(line, '<') 49 emailEnd := bytes.LastIndexByte(line, '>') 50 if emailStart == -1 || emailEnd == -1 || emailEnd < emailStart { 51 return 52 } 53 54 if emailStart > 0 { // Empty name has already occurred, even if it shouldn't 55 sig.Name = strings.TrimSpace(string(line[:emailStart-1])) 56 } 57 sig.Email = string(line[emailStart+1 : emailEnd]) 58 59 hasTime := emailEnd+2 < len(line) 60 if !hasTime { 61 return 62 } 63 64 // Check date format. 65 firstChar := line[emailEnd+2] 66 if firstChar >= 48 && firstChar <= 57 { 67 idx := bytes.IndexByte(line[emailEnd+2:], ' ') 68 if idx < 0 { 69 return 70 } 71 72 timestring := string(line[emailEnd+2 : emailEnd+2+idx]) 73 seconds, _ := strconv.ParseInt(timestring, 10, 64) 74 sig.When = time.Unix(seconds, 0) 75 76 idx += emailEnd + 3 77 if idx >= len(line) || idx+5 > len(line) { 78 return 79 } 80 81 timezone := string(line[idx : idx+5]) 82 tzhours, err1 := strconv.ParseInt(timezone[0:3], 10, 64) 83 tzmins, err2 := strconv.ParseInt(timezone[3:], 10, 64) 84 if err1 != nil || err2 != nil { 85 return 86 } 87 if tzhours < 0 { 88 tzmins *= -1 89 } 90 tz := time.FixedZone("", int(tzhours*60*60+tzmins*60)) 91 sig.When = sig.When.In(tz) 92 } else { 93 sig.When, err = time.Parse(GitTimeLayout, string(line[emailEnd+2:])) 94 if err != nil { 95 return 96 } 97 } 98 return sig, err 99 }