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.

48 lines
1.4 KiB

  1. // Copyright 2015 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. // Signature represents the Author or 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. emailEnd := bytes.IndexByte(line, '>')
  28. sig.Email = string(line[emailStart+1 : emailEnd])
  29. // Check date format.
  30. firstChar := line[emailEnd+2]
  31. if firstChar >= 48 && firstChar <= 57 {
  32. timestop := bytes.IndexByte(line[emailEnd+2:], ' ')
  33. timestring := string(line[emailEnd+2 : emailEnd+2+timestop])
  34. seconds, _ := strconv.ParseInt(timestring, 10, 64)
  35. sig.When = time.Unix(seconds, 0)
  36. } else {
  37. sig.When, err = time.Parse("Mon Jan _2 15:04:05 2006 -0700", string(line[emailEnd+2:]))
  38. if err != nil {
  39. return nil, err
  40. }
  41. }
  42. return sig, nil
  43. }