You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tag.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (C) 2006-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import java.io.BufferedReader;
  46. import java.io.ByteArrayInputStream;
  47. import java.io.IOException;
  48. import java.io.InputStreamReader;
  49. import org.eclipse.jgit.errors.CorruptObjectException;
  50. import org.eclipse.jgit.errors.ObjectWritingException;
  51. /**
  52. * Represents a named reference to another Git object of any type.
  53. */
  54. public class Tag {
  55. private final Repository objdb;
  56. private ObjectId tagId;
  57. private PersonIdent tagger;
  58. private String message;
  59. private byte[] raw;
  60. private String type;
  61. private String tag;
  62. private ObjectId objId;
  63. /**
  64. * Construct a new, yet unnamed Tag.
  65. *
  66. * @param db
  67. */
  68. public Tag(final Repository db) {
  69. objdb = db;
  70. }
  71. /**
  72. * Construct a Tag representing an existing with a known name referencing an known object.
  73. * This could be either a simple or annotated tag.
  74. *
  75. * @param db {@link Repository}
  76. * @param id target id.
  77. * @param refName tag name or null
  78. * @param raw data of an annotated tag.
  79. */
  80. public Tag(final Repository db, final ObjectId id, String refName, final byte[] raw) {
  81. objdb = db;
  82. if (raw != null) {
  83. tagId = id;
  84. objId = ObjectId.fromString(raw, 7);
  85. } else
  86. objId = id;
  87. if (refName != null && refName.startsWith("refs/tags/"))
  88. refName = refName.substring(10);
  89. tag = refName;
  90. this.raw = raw;
  91. }
  92. /**
  93. * @return tagger of a annotated tag or null
  94. */
  95. public PersonIdent getAuthor() {
  96. decode();
  97. return tagger;
  98. }
  99. /**
  100. * Set author of an annotated tag.
  101. * @param a author identifier as a {@link PersonIdent}
  102. */
  103. public void setAuthor(final PersonIdent a) {
  104. tagger = a;
  105. }
  106. /**
  107. * @return comment of an annotated tag, or null
  108. */
  109. public String getMessage() {
  110. decode();
  111. return message;
  112. }
  113. private void decode() {
  114. // FIXME: handle I/O errors
  115. if (raw != null) {
  116. try {
  117. BufferedReader br = new BufferedReader(new InputStreamReader(
  118. new ByteArrayInputStream(raw)));
  119. String n = br.readLine();
  120. if (n == null || !n.startsWith("object ")) {
  121. throw new CorruptObjectException(tagId, "no object");
  122. }
  123. objId = ObjectId.fromString(n.substring(7));
  124. n = br.readLine();
  125. if (n == null || !n.startsWith("type ")) {
  126. throw new CorruptObjectException(tagId, "no type");
  127. }
  128. type = n.substring("type ".length());
  129. n = br.readLine();
  130. if (n == null || !n.startsWith("tag ")) {
  131. throw new CorruptObjectException(tagId, "no tag name");
  132. }
  133. tag = n.substring("tag ".length());
  134. n = br.readLine();
  135. // We should see a "tagger" header here, but some repos have tags
  136. // without it.
  137. if (n == null)
  138. throw new CorruptObjectException(tagId, "no tagger header");
  139. if (n.length()>0)
  140. if (n.startsWith("tagger "))
  141. tagger = new PersonIdent(n.substring("tagger ".length()));
  142. else
  143. throw new CorruptObjectException(tagId, "no tagger/bad header");
  144. // Message should start with an empty line, but
  145. StringBuffer tempMessage = new StringBuffer();
  146. char[] readBuf = new char[2048];
  147. int readLen;
  148. while ((readLen = br.read(readBuf)) > 0) {
  149. tempMessage.append(readBuf, 0, readLen);
  150. }
  151. message = tempMessage.toString();
  152. if (message.startsWith("\n"))
  153. message = message.substring(1);
  154. } catch (IOException e) {
  155. e.printStackTrace();
  156. } finally {
  157. raw = null;
  158. }
  159. }
  160. }
  161. /**
  162. * Set the message of an annotated tag
  163. * @param m
  164. */
  165. public void setMessage(final String m) {
  166. message = m;
  167. }
  168. /**
  169. * Store a tag.
  170. * If author, message or type is set make the tag an annotated tag.
  171. *
  172. * @throws IOException
  173. */
  174. public void tag() throws IOException {
  175. if (getTagId() != null)
  176. throw new IllegalStateException("exists " + getTagId());
  177. final ObjectId id;
  178. final RefUpdate ru;
  179. if (tagger!=null || message!=null || type!=null) {
  180. ObjectId tagid = new ObjectWriter(objdb).writeTag(this);
  181. setTagId(tagid);
  182. id = tagid;
  183. } else {
  184. id = objId;
  185. }
  186. ru = objdb.updateRef(Constants.R_TAGS + getTag());
  187. ru.setNewObjectId(id);
  188. ru.setRefLogMessage("tagged " + getTag(), false);
  189. if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE)
  190. throw new ObjectWritingException("Unable to lock tag " + getTag());
  191. }
  192. public String toString() {
  193. return "tag[" + getTag() + getType() + getObjId() + " " + getAuthor() + "]";
  194. }
  195. /**
  196. * @return SHA-1 of this tag (if annotated and stored).
  197. */
  198. public ObjectId getTagId() {
  199. return tagId;
  200. }
  201. /**
  202. * Set SHA-1 of this tag. Used by writer.
  203. *
  204. * @param tagId
  205. */
  206. public void setTagId(ObjectId tagId) {
  207. this.tagId = tagId;
  208. }
  209. /**
  210. * @return creator of this tag.
  211. */
  212. public PersonIdent getTagger() {
  213. decode();
  214. return tagger;
  215. }
  216. /**
  217. * Set the creator of this tag
  218. *
  219. * @param tagger
  220. */
  221. public void setTagger(PersonIdent tagger) {
  222. this.tagger = tagger;
  223. }
  224. /**
  225. * @return tag target type
  226. */
  227. public String getType() {
  228. decode();
  229. return type;
  230. }
  231. /**
  232. * Set tag target type
  233. * @param type
  234. */
  235. public void setType(String type) {
  236. this.type = type;
  237. }
  238. /**
  239. * @return name of the tag.
  240. */
  241. public String getTag() {
  242. return tag;
  243. }
  244. /**
  245. * Set the name of this tag.
  246. *
  247. * @param tag
  248. */
  249. public void setTag(String tag) {
  250. this.tag = tag;
  251. }
  252. /**
  253. * @return the SHA'1 of the object this tag refers to.
  254. */
  255. public ObjectId getObjId() {
  256. return objId;
  257. }
  258. /**
  259. * Set the id of the object this tag refers to.
  260. *
  261. * @param objId
  262. */
  263. public void setObjId(ObjectId objId) {
  264. this.objId = objId;
  265. }
  266. }