Support tag objects
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / lib / Tag.java
blob4b734b862a0487d4edc2022069e63f4c7fd6ab2c
1 /*
2 * Copyright (C) 20067 Robin Rosenberg <robin.rosenberg@dewire.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.lib;
19 import java.io.BufferedReader;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
24 import org.spearce.jgit.errors.CorruptObjectException;
26 public class Tag {
27 private final Repository objdb;
29 private ObjectId tagId;
31 private PersonIdent tagger;
33 private String message;
35 private byte[] raw;
37 private String type;
39 private String tag;
41 private ObjectId objId;
43 public Tag(final Repository db) {
44 objdb = db;
47 public Tag(final Repository db, final ObjectId id, final byte[] raw) {
48 objdb = db;
49 tagId = id;
50 objId = ObjectId.fromString(raw, 7);
51 this.raw = raw;
54 public PersonIdent getAuthor() {
55 decode();
56 return tagger;
59 public void setAuthor(final PersonIdent a) {
60 tagger = a;
63 public String getMessage() {
64 decode();
65 return message;
68 private void decode() {
69 // FIXME: handle I/O errors
70 if (raw != null) {
71 try {
72 BufferedReader br = new BufferedReader(new InputStreamReader(
73 new ByteArrayInputStream(raw)));
74 String n = br.readLine();
75 if (n == null || !n.startsWith("object ")) {
76 throw new CorruptObjectException(tagId, "no object");
78 objId = new ObjectId(n.substring(7));
79 n = br.readLine();
80 if (n == null || !n.startsWith("type ")) {
81 throw new CorruptObjectException(tagId, "no type");
83 type = n.substring("type ".length());
84 n = br.readLine();
86 if (n == null || !n.startsWith("tag ")) {
87 throw new CorruptObjectException(tagId, "no tag name");
89 tag = n.substring("tag ".length());
90 n = br.readLine();
92 if (n == null || !n.startsWith("tagger ")) {
93 throw new CorruptObjectException(tagId, "no tagger");
95 tagger = new PersonIdent(n.substring("tagger ".length()));
97 n = br.readLine();
98 if (n == null || !n.equals("")) {
99 throw new CorruptObjectException(tagId,
100 "malformed header");
102 StringBuffer tempMessage = new StringBuffer();
103 char[] readBuf = new char[2048];
104 int readLen;
105 while ((readLen = br.read(readBuf)) > 0) {
106 tempMessage.append(readBuf, 0, readLen);
108 message = tempMessage.toString();
109 } catch (IOException e) {
110 e.printStackTrace();
111 } finally {
112 raw = null;
117 public void setMessage(final String m) {
118 message = m;
121 public void tag() throws IOException {
122 if (getTagId() != null)
123 throw new IllegalStateException("exists " + getTagId());
124 setTagId(new ObjectWriter(objdb).writeTag(this));
127 public String toString() {
128 return "tag[" + getTag() + getType() + getObjId() + " " + getAuthor() + "]";
131 public ObjectId getTagId() {
132 return tagId;
135 public void setTagId(ObjectId tagId) {
136 this.tagId = tagId;
139 public PersonIdent getTagger() {
140 decode();
141 return tagger;
144 public void setTagger(PersonIdent tagger) {
145 this.tagger = tagger;
148 public String getType() {
149 decode();
150 return type;
153 public void setType(String type) {
154 this.type = type;
157 public String getTag() {
158 return tag;
161 public void setTag(String tag) {
162 this.tag = tag;
165 public ObjectId getObjId() {
166 return objId;
169 public void setObjId(ObjectId objId) {
170 this.objId = objId;