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.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. package git
  6. import (
  7. "bytes"
  8. "strconv"
  9. "time"
  10. "gopkg.in/src-d/go-git.v4/plumbing/object"
  11. )
  12. // Signature represents the Author or Committer information.
  13. type Signature = object.Signature
  14. const (
  15. // GitTimeLayout is the (default) time layout used by git.
  16. GitTimeLayout = "Mon Jan _2 15:04:05 2006 -0700"
  17. )
  18. // Helper to get a signature from the commit line, which looks like these:
  19. // author Patrick Gundlach <gundlach@speedata.de> 1378823654 +0200
  20. // author Patrick Gundlach <gundlach@speedata.de> Thu, 07 Apr 2005 22:13:13 +0200
  21. // but without the "author " at the beginning (this method should)
  22. // be used for author and committer.
  23. //
  24. // FIXME: include timezone for timestamp!
  25. func newSignatureFromCommitline(line []byte) (_ *Signature, err error) {
  26. sig := new(Signature)
  27. emailStart := bytes.IndexByte(line, '<')
  28. sig.Name = string(line[:emailStart-1])
  29. emailEnd := bytes.IndexByte(line, '>')
  30. sig.Email = string(line[emailStart+1 : emailEnd])
  31. // Check date format.
  32. if len(line) > emailEnd+2 {
  33. firstChar := line[emailEnd+2]
  34. if firstChar >= 48 && firstChar <= 57 {
  35. timestop := bytes.IndexByte(line[emailEnd+2:], ' ')
  36. timestring := string(line[emailEnd+2 : emailEnd+2+timestop])
  37. seconds, _ := strconv.ParseInt(timestring, 10, 64)
  38. sig.When = time.Unix(seconds, 0)
  39. } else {
  40. sig.When, err = time.Parse(GitTimeLayout, string(line[emailEnd+2:]))
  41. if err != nil {
  42. return nil, err
  43. }
  44. }
  45. } else {
  46. // Fall back to unix 0 time
  47. sig.When = time.Unix(0, 0)
  48. }
  49. return sig, nil
  50. }