gitea源码

signature.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "strconv"
  7. "strings"
  8. "time"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. // Helper to get a signature from the commit line, which looks like:
  12. //
  13. // full name <user@example.com> 1378823654 +0200
  14. //
  15. // Haven't found the official reference for the standard format yet.
  16. // This function never fails, if the "line" can't be parsed, it returns a default Signature with "zero" time.
  17. func parseSignatureFromCommitLine(line string) *Signature {
  18. sig := &Signature{}
  19. s1, sx, ok1 := strings.Cut(line, " <")
  20. s2, s3, ok2 := strings.Cut(sx, "> ")
  21. if !ok1 || !ok2 {
  22. sig.Name = line
  23. return sig
  24. }
  25. sig.Name, sig.Email = s1, s2
  26. if strings.Count(s3, " ") == 1 {
  27. ts, tz, _ := strings.Cut(s3, " ")
  28. seconds, _ := strconv.ParseInt(ts, 10, 64)
  29. if tzTime, err := time.Parse("-0700", tz); err == nil {
  30. sig.When = time.Unix(seconds, 0).In(tzTime.Location())
  31. }
  32. } else {
  33. // the old gitea code tried to parse the date in a few different formats, but it's not clear why.
  34. // according to public document, only the standard format "timestamp timezone" could be found, so drop other formats.
  35. log.Error("suspicious commit line format: %q", line)
  36. for _, fmt := range []string{ /*"Mon Jan _2 15:04:05 2006 -0700"*/ } {
  37. if t, err := time.Parse(fmt, s3); err == nil {
  38. sig.When = t
  39. break
  40. }
  41. }
  42. }
  43. return sig
  44. }