You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

signature_nogogit.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. //go:build !gogit
  6. // +build !gogit
  7. package git
  8. import (
  9. "bytes"
  10. "fmt"
  11. "strconv"
  12. "time"
  13. )
  14. // Signature represents the Author or Committer information.
  15. type Signature struct {
  16. // Name represents a person name. It is an arbitrary string.
  17. Name string
  18. // Email is an email, but it cannot be assumed to be well-formed.
  19. Email string
  20. // When is the timestamp of the signature.
  21. When time.Time
  22. }
  23. func (s *Signature) String() string {
  24. return fmt.Sprintf("%s <%s>", s.Name, s.Email)
  25. }
  26. // Decode decodes a byte array representing a signature to signature
  27. func (s *Signature) Decode(b []byte) {
  28. sig, _ := newSignatureFromCommitline(b)
  29. s.Email = sig.Email
  30. s.Name = sig.Name
  31. s.When = sig.When
  32. }
  33. // Helper to get a signature from the commit line, which looks like these:
  34. // author Patrick Gundlach <gundlach@speedata.de> 1378823654 +0200
  35. // author Patrick Gundlach <gundlach@speedata.de> Thu, 07 Apr 2005 22:13:13 +0200
  36. // but without the "author " at the beginning (this method should)
  37. // be used for author and committer.
  38. func newSignatureFromCommitline(line []byte) (sig *Signature, err error) {
  39. sig = new(Signature)
  40. emailStart := bytes.LastIndexByte(line, '<')
  41. emailEnd := bytes.LastIndexByte(line, '>')
  42. if emailStart == -1 || emailEnd == -1 || emailEnd < emailStart {
  43. return
  44. }
  45. sig.Name = string(line[:emailStart-1])
  46. sig.Email = string(line[emailStart+1 : emailEnd])
  47. hasTime := emailEnd+2 < len(line)
  48. if !hasTime {
  49. return
  50. }
  51. // Check date format.
  52. firstChar := line[emailEnd+2]
  53. if firstChar >= 48 && firstChar <= 57 {
  54. idx := bytes.IndexByte(line[emailEnd+2:], ' ')
  55. if idx < 0 {
  56. return
  57. }
  58. timestring := string(line[emailEnd+2 : emailEnd+2+idx])
  59. seconds, _ := strconv.ParseInt(timestring, 10, 64)
  60. sig.When = time.Unix(seconds, 0)
  61. idx += emailEnd + 3
  62. if idx >= len(line) || idx+5 > len(line) {
  63. return
  64. }
  65. timezone := string(line[idx : idx+5])
  66. tzhours, err1 := strconv.ParseInt(timezone[0:3], 10, 64)
  67. tzmins, err2 := strconv.ParseInt(timezone[3:], 10, 64)
  68. if err1 != nil || err2 != nil {
  69. return
  70. }
  71. if tzhours < 0 {
  72. tzmins *= -1
  73. }
  74. tz := time.FixedZone("", int(tzhours*60*60+tzmins*60))
  75. sig.When = sig.When.In(tz)
  76. } else {
  77. sig.When, err = time.Parse(GitTimeLayout, string(line[emailEnd+2:]))
  78. if err != nil {
  79. return
  80. }
  81. }
  82. return
  83. }