Fix RemoteRefUpdate to delete local tracking ref upon successful deletion
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / lib / Constants.java
blobf31688195a1fb953d0585675a88aa410fc9be605
1 /*
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * Copyright (C) 2008, Google Inc.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
10 * conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials provided
18 * with the distribution.
20 * - Neither the name of the Git Development Community nor the
21 * names of its contributors may be used to endorse or promote
22 * products derived from this software without specific prior
23 * written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
26 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 package org.spearce.jgit.lib;
42 import java.nio.ByteBuffer;
43 import java.nio.charset.Charset;
44 import java.security.MessageDigest;
45 import java.security.NoSuchAlgorithmException;
47 import org.spearce.jgit.errors.CorruptObjectException;
48 import org.spearce.jgit.util.MutableInteger;
50 /** Misc. constants used throughout JGit. */
51 public final class Constants {
52 /** Hash function used natively by Git for all objects. */
53 private static final String HASH_FUNCTION = "SHA-1";
55 /** Length of an object hash. */
56 public static final int OBJECT_ID_LENGTH = 20;
58 /** Special name for the "HEAD" symbolic-ref. */
59 public static final String HEAD = "HEAD";
61 /**
62 * Text string that identifies an object as a commit.
63 * <p>
64 * Commits connect trees into a string of project histories, where each
65 * commit is an assertion that the best way to continue is to use this other
66 * tree (set of files).
68 public static final String TYPE_COMMIT = "commit";
70 /**
71 * Text string that identifies an object as a blob.
72 * <p>
73 * Blobs store whole file revisions. They are used for any user file, as
74 * well as for symlinks. Blobs form the bulk of any project's storage space.
76 public static final String TYPE_BLOB = "blob";
78 /**
79 * Text string that identifies an object as a tree.
80 * <p>
81 * Trees attach object ids (hashes) to names and file modes. The normal use
82 * for a tree is to store a version of a directory and its contents.
84 public static final String TYPE_TREE = "tree";
86 /**
87 * Text string that identifies an object as an annotated tag.
88 * <p>
89 * Annotated tags store a pointer to any other object, and an additional
90 * message. It is most commonly used to record a stable release of the
91 * project.
93 public static final String TYPE_TAG = "tag";
95 private static final byte[] ENCODED_TYPE_COMMIT = encodeASCII(TYPE_COMMIT);
97 private static final byte[] ENCODED_TYPE_BLOB = encodeASCII(TYPE_BLOB);
99 private static final byte[] ENCODED_TYPE_TREE = encodeASCII(TYPE_TREE);
101 private static final byte[] ENCODED_TYPE_TAG = encodeASCII(TYPE_TAG);
103 /** An unknown or invalid object type code. */
104 public static final int OBJ_BAD = -1;
107 * In-pack object type: extended types.
108 * <p>
109 * This header code is reserved for future expansion. It is currently
110 * undefined/unsupported.
112 public static final int OBJ_EXT = 0;
115 * In-pack object type: commit.
116 * <p>
117 * Indicates the associated object is a commit.
118 * <p>
119 * <b>This constant is fixed and is defined by the Git packfile format.</b>
121 * @see #TYPE_COMMIT
123 public static final int OBJ_COMMIT = 1;
126 * In-pack object type: tree.
127 * <p>
128 * Indicates the associated object is a tree.
129 * <p>
130 * <b>This constant is fixed and is defined by the Git packfile format.</b>
132 * @see #TYPE_BLOB
134 public static final int OBJ_TREE = 2;
137 * In-pack object type: blob.
138 * <p>
139 * Indicates the associated object is a blob.
140 * <p>
141 * <b>This constant is fixed and is defined by the Git packfile format.</b>
143 * @see #TYPE_BLOB
145 public static final int OBJ_BLOB = 3;
148 * In-pack object type: annotated tag.
149 * <p>
150 * Indicates the associated object is an annotated tag.
151 * <p>
152 * <b>This constant is fixed and is defined by the Git packfile format.</b>
154 * @see #TYPE_TAG
156 public static final int OBJ_TAG = 4;
158 /** In-pack object type: reserved for future use. */
159 public static final int OBJ_TYPE_5 = 5;
162 * In-pack object type: offset delta
163 * <p>
164 * Objects stored with this type actually have a different type which must
165 * be obtained from their delta base object. Delta objects store only the
166 * changes needed to apply to the base object in order to recover the
167 * original object.
168 * <p>
169 * An offset delta uses a negative offset from the start of this object to
170 * refer to its delta base. The base object must exist in this packfile
171 * (even in the case of a thin pack).
172 * <p>
173 * <b>This constant is fixed and is defined by the Git packfile format.</b>
175 public static final int OBJ_OFS_DELTA = 6;
178 * In-pack object type: reference delta
179 * <p>
180 * Objects stored with this type actually have a different type which must
181 * be obtained from their delta base object. Delta objects store only the
182 * changes needed to apply to the base object in order to recover the
183 * original object.
184 * <p>
185 * A reference delta uses a full object id (hash) to reference the delta
186 * base. The base object is allowed to be omitted from the packfile, but
187 * only in the case of a thin pack being transferred over the network.
188 * <p>
189 * <b>This constant is fixed and is defined by the Git packfile format.</b>
191 public static final int OBJ_REF_DELTA = 7;
194 * Pack file signature that occurs at file header - identifies file as Git
195 * packfile formatted.
196 * <p>
197 * <b>This constant is fixed and is defined by the Git packfile format.</b>
199 public static final byte[] PACK_SIGNATURE = { 'P', 'A', 'C', 'K' };
201 /** Native character encoding for commit messages, file names... */
202 public static final String CHARACTER_ENCODING = "UTF-8";
204 /** Native character encoding for commit messages, file names... */
205 public static final Charset CHARSET;
207 /** Default main branch name */
208 public static final String MASTER = "master";
210 /** Prefix for branch refs */
211 public static final String R_HEADS = "refs/heads/";
213 /** Prefix for remotes refs */
214 public static final String R_REMOTES = "refs/remotes/";
216 /** Prefix for tag refs */
217 public static final String R_TAGS = "refs/tags/";
219 /** Logs folder name */
220 public static final String LOGS = "logs";
222 /** Info refs folder */
223 public static final String INFO_REFS = "info/refs";
225 /** Packed refs file */
226 public static final String PACKED_REFS = "packed-refs";
229 * Create a new digest function for objects.
231 * @return a new digest object.
232 * @throws RuntimeException
233 * this Java virtual machine does not support the required hash
234 * function. Very unlikely given that JGit uses a hash function
235 * that is in the Java reference specification.
237 public static MessageDigest newMessageDigest() {
238 try {
239 return MessageDigest.getInstance(HASH_FUNCTION);
240 } catch (NoSuchAlgorithmException nsae) {
241 throw new RuntimeException("Required hash function "
242 + HASH_FUNCTION + " not available.", nsae);
247 * Convert an OBJ_* type constant to a TYPE_* type constant.
249 * @param typeCode the type code, from a pack representation.
250 * @return the canonical string name of this type.
252 public static String typeString(final int typeCode) {
253 switch (typeCode) {
254 case OBJ_COMMIT:
255 return TYPE_COMMIT;
256 case OBJ_TREE:
257 return TYPE_TREE;
258 case OBJ_BLOB:
259 return TYPE_BLOB;
260 case OBJ_TAG:
261 return TYPE_TAG;
262 default:
263 throw new IllegalArgumentException("Bad object type: " + typeCode);
268 * Convert an OBJ_* type constant to an ASCII encoded string constant.
269 * <p>
270 * The ASCII encoded string is often the canonical representation of
271 * the type within a loose object header, or within a tag header.
273 * @param typeCode the type code, from a pack representation.
274 * @return the canonical ASCII encoded name of this type.
276 public static byte[] encodedTypeString(final int typeCode) {
277 switch (typeCode) {
278 case OBJ_COMMIT:
279 return ENCODED_TYPE_COMMIT;
280 case OBJ_TREE:
281 return ENCODED_TYPE_TREE;
282 case OBJ_BLOB:
283 return ENCODED_TYPE_BLOB;
284 case OBJ_TAG:
285 return ENCODED_TYPE_TAG;
286 default:
287 throw new IllegalArgumentException("Bad object type: " + typeCode);
292 * Parse an encoded type string into a type constant.
294 * @param id
295 * object id this type string came from; may be null if that is
296 * not known at the time the parse is occurring.
297 * @param typeString
298 * string version of the type code.
299 * @param endMark
300 * character immediately following the type string. Usually ' '
301 * (space) or '\n' (line feed).
302 * @param offset
303 * position within <code>typeString</code> where the parse
304 * should start. Updated with the new position (just past
305 * <code>endMark</code> when the parse is successful.
306 * @return a type code constant (one of {@link #OBJ_BLOB},
307 * {@link #OBJ_COMMIT}, {@link #OBJ_TAG}, {@link #OBJ_TREE}.
308 * @throws CorruptObjectException
309 * there is no valid type identified by <code>typeString</code>.
311 public static int decodeTypeString(final ObjectId id,
312 final byte[] typeString, final byte endMark,
313 final MutableInteger offset) throws CorruptObjectException {
314 try {
315 int position = offset.value;
316 switch (typeString[position]) {
317 case 'b':
318 if (typeString[position + 1] != 'l'
319 || typeString[position + 2] != 'o'
320 || typeString[position + 3] != 'b'
321 || typeString[position + 4] != endMark)
322 throw new CorruptObjectException(id, "invalid type");
323 offset.value = position + 5;
324 return Constants.OBJ_BLOB;
326 case 'c':
327 if (typeString[position + 1] != 'o'
328 || typeString[position + 2] != 'm'
329 || typeString[position + 3] != 'm'
330 || typeString[position + 4] != 'i'
331 || typeString[position + 5] != 't'
332 || typeString[position + 6] != endMark)
333 throw new CorruptObjectException(id, "invalid type");
334 offset.value = position + 7;
335 return Constants.OBJ_COMMIT;
337 case 't':
338 switch (typeString[position + 1]) {
339 case 'a':
340 if (typeString[position + 2] != 'g'
341 || typeString[position + 3] != endMark)
342 throw new CorruptObjectException(id, "invalid type");
343 offset.value = position + 4;
344 return Constants.OBJ_TAG;
346 case 'r':
347 if (typeString[position + 2] != 'e'
348 || typeString[position + 3] != 'e'
349 || typeString[position + 4] != endMark)
350 throw new CorruptObjectException(id, "invalid type");
351 offset.value = position + 5;
352 return Constants.OBJ_TREE;
354 default:
355 throw new CorruptObjectException(id, "invalid type");
358 default:
359 throw new CorruptObjectException(id, "invalid type");
361 } catch (ArrayIndexOutOfBoundsException bad) {
362 throw new CorruptObjectException(id, "invalid type");
367 * Convert an integer into its decimal representation.
369 * @param s
370 * the integer to convert.
371 * @return a decimal representation of the input integer. The returned array
372 * is the smallest array that will hold the value.
374 public static byte[] encodeASCII(final long s) {
375 return encodeASCII(Long.toString(s));
379 * Convert a string to US-ASCII encoding.
381 * @param s
382 * the string to convert. Must not contain any characters over
383 * 127 (outside of 7-bit ASCII).
384 * @return a byte array of the same length as the input string, holding the
385 * same characters, in the same order.
386 * @throws IllegalArgumentException
387 * the input string contains one or more characters outside of
388 * the 7-bit ASCII character space.
390 public static byte[] encodeASCII(final String s) {
391 final byte[] r = new byte[s.length()];
392 for (int k = r.length - 1; k >= 0; k--) {
393 final char c = s.charAt(k);
394 if (c > 127)
395 throw new IllegalArgumentException("Not ASCII string: " + s);
396 r[k] = (byte) c;
398 return r;
402 * Convert a string to a byte array in the standard character encoding.
404 * @param str
405 * the string to convert. May contain any Unicode characters.
406 * @return a byte array representing the requested string, encoded using the
407 * default character encoding (UTF-8).
408 * @see #CHARACTER_ENCODING
410 public static byte[] encode(final String str) {
411 final ByteBuffer bb = Constants.CHARSET.encode(str);
412 final int len = bb.limit();
413 if (bb.hasArray() && bb.arrayOffset() == 0) {
414 final byte[] arr = bb.array();
415 if (arr.length == len)
416 return arr;
419 final byte[] arr = new byte[len];
420 bb.get(arr);
421 return arr;
424 static {
425 if (OBJECT_ID_LENGTH != newMessageDigest().getDigestLength())
426 throw new LinkageError("Incorrect OBJECT_ID_LENGTH.");
427 CHARSET = Charset.forName(CHARACTER_ENCODING);
430 private Constants() {
431 // Hide the default constructor