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_gogit.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. //go:build gogit
  5. package git
  6. import (
  7. "bytes"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/go-git/go-git/v5/plumbing/object"
  12. )
  13. // Signature represents the Author or Committer information.
  14. type Signature = object.Signature
  15. // Helper to get a signature from the commit line, which looks like these:
  16. //
  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. //
  20. // but without the "author " at the beginning (this method should)
  21. // be used for author and committer.
  22. //
  23. // FIXME: include timezone for timestamp!
  24. func newSignatureFromCommitline(line []byte) (_ *Signature, err error) {
  25. sig := new(Signature)
  26. emailStart := bytes.IndexByte(line, '<')
  27. if emailStart > 0 { // Empty name has already occurred, even if it shouldn't
  28. sig.Name = strings.TrimSpace(string(line[:emailStart-1]))
  29. }
  30. emailEnd := bytes.IndexByte(line, '>')
  31. sig.Email = string(line[emailStart+1 : emailEnd])
  32. // Check date format.
  33. if len(line) > emailEnd+2 {
  34. firstChar := line[emailEnd+2]
  35. if firstChar >= 48 && firstChar <= 57 {
  36. timestop := bytes.IndexByte(line[emailEnd+2:], ' ')
  37. timestring := string(line[emailEnd+2 : emailEnd+2+timestop])
  38. seconds, _ := strconv.ParseInt(timestring, 10, 64)
  39. sig.When = time.Unix(seconds, 0)
  40. } else {
  41. sig.When, err = time.Parse(GitTimeLayout, string(line[emailEnd+2:]))
  42. if err != nil {
  43. return nil, err
  44. }
  45. }
  46. } else {
  47. // Fall back to unix 0 time
  48. sig.When = time.Unix(0, 0)
  49. }
  50. return sig, nil
  51. }