tag.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 "bytes"
  6. // Tag represents a Git tag.
  7. type Tag struct {
  8. Name string
  9. ID sha1
  10. repo *Repository
  11. Object sha1 // The id of this commit object
  12. Type string
  13. Tagger *Signature
  14. Message string
  15. }
  16. func (tag *Tag) Commit() (*Commit, error) {
  17. return tag.repo.getCommit(tag.Object)
  18. }
  19. // Parse commit information from the (uncompressed) raw
  20. // data from the commit object.
  21. // \n\n separate headers from message
  22. func parseTagData(data []byte) (*Tag, error) {
  23. tag := new(Tag)
  24. // we now have the contents of the commit object. Let's investigate...
  25. nextline := 0
  26. l:
  27. for {
  28. eol := bytes.IndexByte(data[nextline:], '\n')
  29. switch {
  30. case eol > 0:
  31. line := data[nextline : nextline+eol]
  32. spacepos := bytes.IndexByte(line, ' ')
  33. reftype := line[:spacepos]
  34. switch string(reftype) {
  35. case "object":
  36. id, err := NewIDFromString(string(line[spacepos+1:]))
  37. if err != nil {
  38. return nil, err
  39. }
  40. tag.Object = id
  41. case "type":
  42. // A commit can have one or more parents
  43. tag.Type = string(line[spacepos+1:])
  44. case "tagger":
  45. sig, err := newSignatureFromCommitline(line[spacepos+1:])
  46. if err != nil {
  47. return nil, err
  48. }
  49. tag.Tagger = sig
  50. }
  51. nextline += eol + 1
  52. case eol == 0:
  53. tag.Message = string(data[nextline+1:])
  54. break l
  55. default:
  56. break l
  57. }
  58. }
  59. return tag, nil
  60. }