gitea源码

tag.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package git
  4. import (
  5. "bytes"
  6. "sort"
  7. "code.gitea.io/gitea/modules/util"
  8. )
  9. // Tag represents a Git tag.
  10. type Tag struct {
  11. Name string
  12. ID ObjectID
  13. Object ObjectID // The id of this commit object
  14. Type string
  15. Tagger *Signature
  16. Message string
  17. Signature *CommitSignature
  18. }
  19. func parsePayloadSignature(data []byte, messageStart int) (payload, msg, sign string) {
  20. pos := messageStart
  21. signStart, signEnd := -1, -1
  22. for {
  23. eol := bytes.IndexByte(data[pos:], '\n')
  24. if eol < 0 {
  25. break
  26. }
  27. line := data[pos : pos+eol]
  28. signType, hasPrefix := bytes.CutPrefix(line, []byte("-----BEGIN "))
  29. signType, hasSuffix := bytes.CutSuffix(signType, []byte(" SIGNATURE-----"))
  30. if hasPrefix && hasSuffix {
  31. signEndBytes := append([]byte("\n-----END "), signType...)
  32. signEndBytes = append(signEndBytes, []byte(" SIGNATURE-----")...)
  33. signEnd = bytes.Index(data[pos:], signEndBytes)
  34. if signEnd != -1 {
  35. signStart = pos
  36. signEnd = pos + signEnd + len(signEndBytes)
  37. }
  38. }
  39. pos += eol + 1
  40. }
  41. if signStart != -1 && signEnd != -1 {
  42. msgEnd := max(messageStart, signStart-1)
  43. return string(data[:msgEnd]), string(data[messageStart:msgEnd]), string(data[signStart:signEnd])
  44. }
  45. return string(data), string(data[messageStart:]), ""
  46. }
  47. // Parse commit information from the (uncompressed) raw
  48. // data from the commit object.
  49. // \n\n separate headers from message
  50. func parseTagData(objectFormat ObjectFormat, data []byte) (*Tag, error) {
  51. tag := new(Tag)
  52. tag.ID = objectFormat.EmptyObjectID()
  53. tag.Object = objectFormat.EmptyObjectID()
  54. tag.Tagger = &Signature{}
  55. pos := 0
  56. for {
  57. eol := bytes.IndexByte(data[pos:], '\n')
  58. if eol == -1 {
  59. break // shouldn't happen, but could just tolerate it
  60. }
  61. if eol == 0 {
  62. pos++
  63. break // end of headers
  64. }
  65. line := data[pos : pos+eol]
  66. key, val, _ := bytes.Cut(line, []byte(" "))
  67. switch string(key) {
  68. case "object":
  69. id, err := NewIDFromString(string(val))
  70. if err != nil {
  71. return nil, err
  72. }
  73. tag.Object = id
  74. case "type":
  75. tag.Type = string(val) // A commit can have one or more parents
  76. case "tagger":
  77. tag.Tagger = parseSignatureFromCommitLine(util.UnsafeBytesToString(val))
  78. }
  79. pos += eol + 1
  80. }
  81. payload, msg, sign := parsePayloadSignature(data, pos)
  82. tag.Message = msg
  83. if len(sign) > 0 {
  84. tag.Signature = &CommitSignature{Signature: sign, Payload: payload}
  85. }
  86. return tag, nil
  87. }
  88. type tagSorter []*Tag
  89. func (ts tagSorter) Len() int {
  90. return len([]*Tag(ts))
  91. }
  92. func (ts tagSorter) Less(i, j int) bool {
  93. return []*Tag(ts)[i].Tagger.When.After([]*Tag(ts)[j].Tagger.When)
  94. }
  95. func (ts tagSorter) Swap(i, j int) {
  96. []*Tag(ts)[i], []*Tag(ts)[j] = []*Tag(ts)[j], []*Tag(ts)[i]
  97. }
  98. // sortTagsByTime
  99. func sortTagsByTime(tags []*Tag) {
  100. sorter := tagSorter(tags)
  101. sort.Sort(sorter)
  102. }