|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + |
| 10 | + "gopkg.in/src-d/go-git.v3/core" |
| 11 | +) |
| 12 | + |
| 13 | +// Tag represents an annotated tag object. It points to a single git object of |
| 14 | +// any type, but tags typically are applied to commit or blob objects. It |
| 15 | +// provides a reference that associates the target with a tag name. It also |
| 16 | +// contains meta-information about the tag, including the tagger, tag date and |
| 17 | +// message. |
| 18 | +// |
| 19 | +// https://git-scm.com/book/en/v2/Git-Internals-Git-References#Tags |
| 20 | +type Tag struct { |
| 21 | + Hash core.Hash |
| 22 | + Type core.ObjectType |
| 23 | + Name string |
| 24 | + Tagger Signature |
| 25 | + Message string |
| 26 | + |
| 27 | + object core.Hash |
| 28 | + r *Repository |
| 29 | +} |
| 30 | + |
| 31 | +// Decode transforms a core.Object into a Tag struct. |
| 32 | +func (t *Tag) Decode(o core.Object) error { |
| 33 | + if o.Type() != core.TagObject { |
| 34 | + return ErrUnsupportedObject |
| 35 | + } |
| 36 | + |
| 37 | + t.Hash = o.Hash() |
| 38 | + |
| 39 | + r := bufio.NewReader(o.Reader()) |
| 40 | + for { |
| 41 | + line, err := r.ReadSlice('\n') |
| 42 | + if err != nil && err != io.EOF { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + line = bytes.TrimSpace(line) |
| 47 | + if len(line) == 0 { |
| 48 | + break // Start of message |
| 49 | + } |
| 50 | + |
| 51 | + split := bytes.SplitN(line, []byte{' '}, 2) |
| 52 | + switch string(split[0]) { |
| 53 | + case "object": |
| 54 | + t.object = core.NewHash(string(split[1])) |
| 55 | + case "type": |
| 56 | + t.Type, err = core.ParseObjectType(string(split[1])) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + case "tag": |
| 61 | + t.Name = string(split[1]) |
| 62 | + case "tagger": |
| 63 | + t.Tagger.Decode(split[1]) |
| 64 | + } |
| 65 | + |
| 66 | + if err == io.EOF { |
| 67 | + return nil |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + data, err := ioutil.ReadAll(r) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + t.Message = string(data) |
| 76 | + |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +// Commit returns the commit pointed to by the tag. If the tag points to a |
| 81 | +// different type of object ErrUnsupportedObject will be returned. |
| 82 | +func (t *Tag) Commit() (*Commit, error) { |
| 83 | + if t.Type != core.CommitObject { |
| 84 | + return nil, ErrUnsupportedObject |
| 85 | + } |
| 86 | + return t.r.Commit(t.object) |
| 87 | +} |
| 88 | + |
| 89 | +// Tree returns the tree pointed to by the tag. If the tag points to a commit |
| 90 | +// object the tree of that commit will be returned. If the tag does not point |
| 91 | +// to a commit or tree object ErrUnsupportedObject will be returned. |
| 92 | +func (t *Tag) Tree() (*Tree, error) { |
| 93 | + // TODO: If the tag is of type commit, follow the commit to its tree? |
| 94 | + switch t.Type { |
| 95 | + case core.CommitObject: |
| 96 | + commit, err := t.r.Commit(t.object) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + return commit.Tree(), nil |
| 101 | + case core.TreeObject: |
| 102 | + return t.r.Tree(t.object) |
| 103 | + default: |
| 104 | + return nil, ErrUnsupportedObject |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// Blob returns the blob pointed to by the tag. If the tag points to a |
| 109 | +// different type of object ErrUnsupportedObject will be returned. |
| 110 | +func (t *Tag) Blob() (*Blob, error) { |
| 111 | + if t.Type != core.BlobObject { |
| 112 | + return nil, ErrUnsupportedObject |
| 113 | + } |
| 114 | + return t.r.Blob(t.object) |
| 115 | +} |
| 116 | + |
| 117 | +// Object returns the object pointed to by the tag. |
| 118 | +func (t *Tag) Object() (core.Object, error) { |
| 119 | + return t.r.Storage.Get(t.object) |
| 120 | +} |
| 121 | + |
| 122 | +// String returns the meta information contained in the tag as a formatted |
| 123 | +// string. |
| 124 | +func (t *Tag) String() string { |
| 125 | + return fmt.Sprintf( |
| 126 | + "%s %s\nObject: %s\nType: %s\nTag: %s\nTagger: %s\nDate: %s\n", |
| 127 | + core.TagObject, t.Hash, t.object, t.Type, t.Name, t.Tagger.String(), t.Tagger.When, |
| 128 | + ) |
| 129 | +} |
| 130 | + |
| 131 | +// TagIter provides an iterator for a set of tags. |
| 132 | +type TagIter struct { |
| 133 | + core.ObjectIter |
| 134 | + r *Repository |
| 135 | +} |
| 136 | + |
| 137 | +// NewTagIter returns a new TagIter for the given Repository and ObjectIter. |
| 138 | +func NewTagIter(r *Repository, iter core.ObjectIter) *TagIter { |
| 139 | + return &TagIter{iter, r} |
| 140 | +} |
| 141 | + |
| 142 | +// Next moves the iterator to the next tag and returns a pointer to it. If it |
| 143 | +// has reached the end of the set it will return io.EOF. |
| 144 | +func (iter *TagIter) Next() (*Tag, error) { |
| 145 | + obj, err := iter.ObjectIter.Next() |
| 146 | + if err != nil { |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + |
| 150 | + tag := &Tag{r: iter.r} |
| 151 | + return tag, tag.Decode(obj) |
| 152 | +} |
| 153 | + |
| 154 | +// Close releases any resources used by the iterator. |
| 155 | +func (iter *TagIter) Close() { |
| 156 | + iter.Close() |
| 157 | +} |
0 commit comments