signature.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "bytes"
  7. "strconv"
  8. "time"
  9. )
  10. // Author and Committer information
  11. type Signature struct {
  12. Email string
  13. Name string
  14. When time.Time
  15. }
  16. // Helper to get a signature from the commit line, which looks like these:
  17. // author Patrick Gundlach <gundlach@speedata.de> 1378823654 +0200
  18. // author Patrick Gundlach <gundlach@speedata.de> Thu, 07 Apr 2005 22:13:13 +0200
  19. // but without the "author " at the beginning (this method should)
  20. // be used for author and committer.
  21. //
  22. // FIXME: include timezone for timestamp!
  23. func newSignatureFromCommitline(line []byte) (_ *Signature, err error) {
  24. sig := new(Signature)
  25. emailstart := bytes.IndexByte(line, '<')
  26. sig.Name = string(line[:emailstart-1])
  27. emailstop := bytes.IndexByte(line, '>')
  28. sig.Email = string(line[emailstart+1 : emailstop])
  29. // Check date format.
  30. firstChar := line[emailstop+2]
  31. if firstChar >= 48 && firstChar <= 57 {
  32. timestop := bytes.IndexByte(line[emailstop+2:], ' ')
  33. timestring := string(line[emailstop+2 : emailstop+2+timestop])
  34. seconds, err := strconv.ParseInt(timestring, 10, 64)
  35. if err != nil {
  36. return nil, err
  37. }
  38. sig.When = time.Unix(seconds, 0)
  39. } else {
  40. sig.When, err = time.Parse("Mon Jan _2 15:04:05 2006 -0700", string(line[emailstop+2:]))
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. return sig, nil
  46. }