| 1 |
package gitobj |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"encoding/hex" |
| 7 |
"fmt" |
| 8 |
"hash" |
| 9 |
"io" |
| 10 |
"strings" |
| 11 |
) |
| 12 |
|
| 13 |
type Tag struct { |
| 14 |
Object []byte |
| 15 |
ObjectType ObjectType |
| 16 |
Name string |
| 17 |
Tagger string |
| 18 |
|
| 19 |
Message string |
| 20 |
} |
| 21 |
|
| 22 |
// Decode implements Object.Decode and decodes the uncompressed tag being |
| 23 |
// read. It returns the number of uncompressed bytes being consumed off of the |
| 24 |
// stream, which should be strictly equal to the size given. |
| 25 |
// |
| 26 |
// If any error was encountered along the way it will be returned, and the |
| 27 |
// receiving *Tag is considered invalid. |
| 28 |
func (t *Tag) Decode(hash hash.Hash, r io.Reader, size int64) (int, error) { |
| 29 |
scanner := bufio.NewScanner(io.LimitReader(r, size)) |
| 30 |
|
| 31 |
var ( |
| 32 |
finishedHeaders bool |
| 33 |
message []string |
| 34 |
) |
| 35 |
|
| 36 |
for scanner.Scan() { |
| 37 |
if finishedHeaders { |
| 38 |
message = append(message, scanner.Text()) |
| 39 |
} else { |
| 40 |
if len(scanner.Bytes()) == 0 { |
| 41 |
finishedHeaders = true |
| 42 |
continue |
| 43 |
} |
| 44 |
|
| 45 |
parts := strings.SplitN(scanner.Text(), " ", 2) |
| 46 |
if len(parts) < 2 { |
| 47 |
return 0, fmt.Errorf("gitobj: invalid tag header: %s", scanner.Text()) |
| 48 |
} |
| 49 |
|
| 50 |
switch parts[0] { |
| 51 |
case "object": |
| 52 |
sha, err := hex.DecodeString(parts[1]) |
| 53 |
if err != nil { |
| 54 |
return 0, fmt.Errorf("gitobj: unable to decode SHA-1: %s", err) |
| 55 |
} |
| 56 |
|
| 57 |
t.Object = sha |
| 58 |
case "type": |
| 59 |
t.ObjectType = ObjectTypeFromString(parts[1]) |
| 60 |
case "tag": |
| 61 |
t.Name = parts[1] |
| 62 |
case "tagger": |
| 63 |
t.Tagger = parts[1] |
| 64 |
default: |
| 65 |
return 0, fmt.Errorf("gitobj: unknown tag header: %s", parts[0]) |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if err := scanner.Err(); err != nil { |
| 71 |
return 0, err |
| 72 |
} |
| 73 |
|
| 74 |
t.Message = strings.Join(message, "\n") |
| 75 |
|
| 76 |
return int(size), nil |
| 77 |
} |
| 78 |
|
| 79 |
// Encode encodes the Tag's contents to the given io.Writer, "w". If there was |
| 80 |
// any error copying the Tag's contents, that error will be returned. |
| 81 |
// |
| 82 |
// Otherwise, the number of bytes written will be returned. |
| 83 |
func (t *Tag) Encode(w io.Writer) (int, error) { |
| 84 |
headers := []string{ |
| 85 |
fmt.Sprintf("object %s", hex.EncodeToString(t.Object)), |
| 86 |
fmt.Sprintf("type %s", t.ObjectType), |
| 87 |
fmt.Sprintf("tag %s", t.Name), |
| 88 |
fmt.Sprintf("tagger %s", t.Tagger), |
| 89 |
} |
| 90 |
|
| 91 |
return fmt.Fprintf(w, "%s\n\n%s", strings.Join(headers, "\n"), t.Message) |
| 92 |
} |
| 93 |
|
| 94 |
// Equal returns whether the receiving and given Tags are equal, or in other |
| 95 |
// words, whether they are represented by the same SHA-1 when saved to the |
| 96 |
// object database. |
| 97 |
func (t *Tag) Equal(other *Tag) bool { |
| 98 |
if (t == nil) != (other == nil) { |
| 99 |
return false |
| 100 |
} |
| 101 |
|
| 102 |
if t != nil { |
| 103 |
return bytes.Equal(t.Object, other.Object) && |
| 104 |
t.ObjectType == other.ObjectType && |
| 105 |
t.Name == other.Name && |
| 106 |
t.Tagger == other.Tagger && |
| 107 |
t.Message == other.Message |
| 108 |
} |
| 109 |
|
| 110 |
return true |
| 111 |
} |
| 112 |
|
| 113 |
// Type implements Object.ObjectType by returning the correct object type for |
| 114 |
// Tags, TagObjectType. |
| 115 |
func (t *Tag) Type() ObjectType { |
| 116 |
return TagObjectType |
| 117 |
} |