Handle old tags with some missing fields
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / lib / Tag.java
blob92c3e650927d3eeca2f876edb3f4f87b0e4e959b
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;
25 import org.spearce.jgit.errors.ObjectWritingException;
27 public class Tag {
28 private final Repository objdb;
30 private ObjectId tagId;
32 private PersonIdent tagger;
34 private String message;
36 private byte[] raw;
38 private String type;
40 private String tag;
42 private ObjectId objId;
44 public Tag(final Repository db) {
45 objdb = db;
48 public Tag(final Repository db, final ObjectId id, String refName, final byte[] raw) {
49 objdb = db;
50 if (raw != null) {
51 tagId = id;
52 objId = ObjectId.fromString(raw, 7);
53 } else
54 objId = id;
55 if (refName.startsWith("refs/tags/"))
56 refName = refName.substring(10);
57 tag = refName;
58 this.raw = raw;
61 public PersonIdent getAuthor() {
62 decode();
63 return tagger;
66 public void setAuthor(final PersonIdent a) {
67 tagger = a;
70 public String getMessage() {
71 decode();
72 return message;
75 private void decode() {
76 // FIXME: handle I/O errors
77 if (raw != null) {
78 try {
79 BufferedReader br = new BufferedReader(new InputStreamReader(
80 new ByteArrayInputStream(raw)));
81 String n = br.readLine();
82 if (n == null || !n.startsWith("object ")) {
83 throw new CorruptObjectException(tagId, "no object");
85 objId = new ObjectId(n.substring(7));
86 n = br.readLine();
87 if (n == null || !n.startsWith("type ")) {
88 throw new CorruptObjectException(tagId, "no type");
90 type = n.substring("type ".length());
91 n = br.readLine();
93 if (n == null || !n.startsWith("tag ")) {
94 throw new CorruptObjectException(tagId, "no tag name");
96 tag = n.substring("tag ".length());
97 n = br.readLine();
99 // We should see a "tagger" header here, but some repos have tags
100 // without it.
101 if (n == null)
102 throw new CorruptObjectException(tagId, "no tagger header");
104 if (n.length()>0)
105 if (n.startsWith("tagger "))
106 tagger = new PersonIdent(n.substring("tagger ".length()));
107 else
108 throw new CorruptObjectException(tagId, "no tagger/bad header");
110 // Message should start with an empty line, but
111 StringBuffer tempMessage = new StringBuffer();
112 char[] readBuf = new char[2048];
113 int readLen;
114 while ((readLen = br.read(readBuf)) > 0) {
115 tempMessage.append(readBuf, 0, readLen);
117 message = tempMessage.toString();
118 if (message.startsWith("\n"))
119 message = message.substring(1);
120 } catch (IOException e) {
121 e.printStackTrace();
122 } finally {
123 raw = null;
128 public void setMessage(final String m) {
129 message = m;
132 public void tag() throws IOException {
133 if (getTagId() != null)
134 throw new IllegalStateException("exists " + getTagId());
135 final ObjectId id;
136 if (tagger!=null || message!=null || type!=null) {
137 ObjectId tagid = new ObjectWriter(objdb).writeTag(this);
138 setTagId(tagid);
139 id = tagid;
140 } else {
141 id = objId;
143 final RefLock lck = objdb.lockRef("refs/tags/" + getTag());
144 if (lck == null)
145 throw new ObjectWritingException("Unable to lock tag " + getTag());
146 lck.write(id);
147 if (!lck.commit())
148 throw new ObjectWritingException("Unable to write tag " + getTag());
151 public String toString() {
152 return "tag[" + getTag() + getType() + getObjId() + " " + getAuthor() + "]";
155 public ObjectId getTagId() {
156 return tagId;
159 public void setTagId(ObjectId tagId) {
160 this.tagId = tagId;
163 public PersonIdent getTagger() {
164 decode();
165 return tagger;
168 public void setTagger(PersonIdent tagger) {
169 this.tagger = tagger;
172 public String getType() {
173 decode();
174 return type;
177 public void setType(String type) {
178 this.type = type;
181 public String getTag() {
182 return tag;
185 public void setTag(String tag) {
186 this.tag = tag;
189 public ObjectId getObjId() {
190 return objId;
193 public void setObjId(ObjectId objId) {
194 this.objId = objId;