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.

tag.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package git
  5. import (
  6. "bytes"
  7. "sort"
  8. "strings"
  9. )
  10. // Tag represents a Git tag.
  11. type Tag struct {
  12. Name string
  13. ID SHA1
  14. repo *Repository
  15. Object SHA1 // The id of this commit object
  16. Type string
  17. Tagger *Signature
  18. Message string
  19. }
  20. // Commit return the commit of the tag reference
  21. func (tag *Tag) Commit() (*Commit, error) {
  22. return tag.repo.getCommit(tag.Object)
  23. }
  24. // Parse commit information from the (uncompressed) raw
  25. // data from the commit object.
  26. // \n\n separate headers from message
  27. func parseTagData(data []byte) (*Tag, error) {
  28. tag := new(Tag)
  29. // we now have the contents of the commit object. Let's investigate...
  30. nextline := 0
  31. l:
  32. for {
  33. eol := bytes.IndexByte(data[nextline:], '\n')
  34. switch {
  35. case eol > 0:
  36. line := data[nextline : nextline+eol]
  37. spacepos := bytes.IndexByte(line, ' ')
  38. reftype := line[:spacepos]
  39. switch string(reftype) {
  40. case "object":
  41. id, err := NewIDFromString(string(line[spacepos+1:]))
  42. if err != nil {
  43. return nil, err
  44. }
  45. tag.Object = id
  46. case "type":
  47. // A commit can have one or more parents
  48. tag.Type = string(line[spacepos+1:])
  49. case "tagger":
  50. sig, err := newSignatureFromCommitline(line[spacepos+1:])
  51. if err != nil {
  52. return nil, err
  53. }
  54. tag.Tagger = sig
  55. }
  56. nextline += eol + 1
  57. case eol == 0:
  58. tag.Message = strings.TrimRight(string(data[nextline+1:]), "\n")
  59. break l
  60. default:
  61. break l
  62. }
  63. }
  64. return tag, nil
  65. }
  66. type tagSorter []*Tag
  67. func (ts tagSorter) Len() int {
  68. return len([]*Tag(ts))
  69. }
  70. func (ts tagSorter) Less(i, j int) bool {
  71. return []*Tag(ts)[i].Tagger.When.After([]*Tag(ts)[j].Tagger.When)
  72. }
  73. func (ts tagSorter) Swap(i, j int) {
  74. []*Tag(ts)[i], []*Tag(ts)[j] = []*Tag(ts)[j], []*Tag(ts)[i]
  75. }
  76. // sortTagsByTime
  77. func sortTagsByTime(tags []*Tag) {
  78. sorter := tagSorter(tags)
  79. sort.Sort(sorter)
  80. }