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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. "strconv"
  11. "time"
  12. "github.com/go-git/go-git/v5/plumbing/object"
  13. )
  14. // Signature represents the Author or Committer information.
  15. type Signature = object.Signature
  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. if len(line) > emailEnd+2 {
  31. firstChar := line[emailEnd+2]
  32. if firstChar >= 48 && firstChar <= 57 {
  33. timestop := bytes.IndexByte(line[emailEnd+2:], ' ')
  34. timestring := string(line[emailEnd+2 : emailEnd+2+timestop])
  35. seconds, _ := strconv.ParseInt(timestring, 10, 64)
  36. sig.When = time.Unix(seconds, 0)
  37. } else {
  38. sig.When, err = time.Parse(GitTimeLayout, string(line[emailEnd+2:]))
  39. if err != nil {
  40. return nil, err
  41. }
  42. }
  43. } else {
  44. // Fall back to unix 0 time
  45. sig.When = time.Unix(0, 0)
  46. }
  47. return sig, nil
  48. }