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
;
28 private final Repository objdb
;
30 private ObjectId tagId
;
32 private PersonIdent tagger
;
34 private String message
;
42 private ObjectId objId
;
44 public Tag(final Repository db
) {
48 public Tag(final Repository db
, final ObjectId id
, String refName
, final byte[] raw
) {
52 objId
= ObjectId
.fromString(raw
, 7);
55 if (refName
.startsWith("refs/tags/"))
56 refName
= refName
.substring(10);
61 public PersonIdent
getAuthor() {
66 public void setAuthor(final PersonIdent a
) {
70 public String
getMessage() {
75 private void decode() {
76 // FIXME: handle I/O errors
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));
87 if (n
== null || !n
.startsWith("type ")) {
88 throw new CorruptObjectException(tagId
, "no type");
90 type
= n
.substring("type ".length());
93 if (n
== null || !n
.startsWith("tag ")) {
94 throw new CorruptObjectException(tagId
, "no tag name");
96 tag
= n
.substring("tag ".length());
99 // We should see a "tagger" header here, but some repos have tags
102 throw new CorruptObjectException(tagId
, "no tagger header");
105 if (n
.startsWith("tagger "))
106 tagger
= new PersonIdent(n
.substring("tagger ".length()));
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];
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
) {
128 public void setMessage(final String m
) {
132 public void tag() throws IOException
{
133 if (getTagId() != null)
134 throw new IllegalStateException("exists " + getTagId());
136 if (tagger
!=null || message
!=null || type
!=null) {
137 ObjectId tagid
= new ObjectWriter(objdb
).writeTag(this);
143 final RefLock lck
= objdb
.lockRef("refs/tags/" + getTag());
145 throw new ObjectWritingException("Unable to lock tag " + getTag());
148 throw new ObjectWritingException("Unable to write tag " + getTag());
151 public String
toString() {
152 return "tag[" + getTag() + getType() + getObjId() + " " + getAuthor() + "]";
155 public ObjectId
getTagId() {
159 public void setTagId(ObjectId tagId
) {
163 public PersonIdent
getTagger() {
168 public void setTagger(PersonIdent tagger
) {
169 this.tagger
= tagger
;
172 public String
getType() {
177 public void setType(String type
) {
181 public String
getTag() {
185 public void setTag(String tag
) {
189 public ObjectId
getObjId() {
193 public void setObjId(ObjectId objId
) {