2 * Copyright (C) 2006 Shawn Pearce <spearce@spearce.org>
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
.nio
.charset
.Charset
;
20 import java
.security
.MessageDigest
;
21 import java
.security
.NoSuchAlgorithmException
;
23 /** Misc. constants used throughout JGit. */
24 public final class Constants
{
25 /** Hash function used natively by Git for all objects. */
26 private static final String HASH_FUNCTION
= "SHA-1";
28 /** Length of an object hash. */
29 public static final int OBJECT_ID_LENGTH
= 20;
31 /** Special name for the "HEAD" symbolic-ref. */
32 public static final String HEAD
= "HEAD";
35 * Text string that identifies an object as a commit.
37 * Commits connect trees into a string of project histories, where each
38 * commit is an assertion that the best way to continue is to use this other
39 * tree (set of files).
41 public static final String TYPE_COMMIT
= "commit";
44 * Text string that identifies an object as a blob.
46 * Blobs store whole file revisions. They are used for any user file, as
47 * well as for symlinks. Blobs form the bulk of any project's storage space.
49 public static final String TYPE_BLOB
= "blob";
52 * Text string that identifies an object as a tree.
54 * Trees attach object ids (hashes) to names and file modes. The normal use
55 * for a tree is to store a version of a directory and its contents.
57 public static final String TYPE_TREE
= "tree";
60 * Text string that identifies an object as an annotated tag.
62 * Annotated tags store a pointer to any other object, and an additional
63 * message. It is most commonly used to record a stable release of the
66 public static final String TYPE_TAG
= "tag";
68 /** An unknown or invalid object type code. */
69 public static final int OBJ_BAD
= -1;
72 * In-pack object type: extended types.
74 * This header code is reserved for future expansion. It is currently
75 * undefined/unsupported.
77 public static final int OBJ_EXT
= 0;
80 * In-pack object type: commit.
82 * Indicates the associated object is a commit.
84 * <b>This constant is fixed and is defined by the Git packfile format.</b>
88 public static final int OBJ_COMMIT
= 1;
91 * In-pack object type: tree.
93 * Indicates the associated object is a tree.
95 * <b>This constant is fixed and is defined by the Git packfile format.</b>
99 public static final int OBJ_TREE
= 2;
102 * In-pack object type: blob.
104 * Indicates the associated object is a blob.
106 * <b>This constant is fixed and is defined by the Git packfile format.</b>
110 public static final int OBJ_BLOB
= 3;
113 * In-pack object type: annotated tag.
115 * Indicates the associated object is an annotated tag.
117 * <b>This constant is fixed and is defined by the Git packfile format.</b>
121 public static final int OBJ_TAG
= 4;
123 /** In-pack object type: reserved for future use. */
124 public static final int OBJ_TYPE_5
= 5;
127 * In-pack object type: offset delta
129 * Objects stored with this type actually have a different type which must
130 * be obtained from their delta base object. Delta objects store only the
131 * changes needed to apply to the base object in order to recover the
134 * An offset delta uses a negative offset from the start of this object to
135 * refer to its delta base. The base object must exist in this packfile
136 * (even in the case of a thin pack).
138 * <b>This constant is fixed and is defined by the Git packfile format.</b>
140 public static final int OBJ_OFS_DELTA
= 6;
143 * In-pack object type: reference delta
145 * Objects stored with this type actually have a different type which must
146 * be obtained from their delta base object. Delta objects store only the
147 * changes needed to apply to the base object in order to recover the
150 * A reference delta uses a full object id (hash) to reference the delta
151 * base. The base object is allowed to be omitted from the packfile, but
152 * only in the case of a thin pack being transferred over the network.
154 * <b>This constant is fixed and is defined by the Git packfile format.</b>
156 public static final int OBJ_REF_DELTA
= 7;
158 /** Native character encoding for commit messages, file names... */
159 public static final String CHARACTER_ENCODING
= "UTF-8";
161 /** Native character encoding for commit messages, file names... */
162 public static final Charset CHARSET
;
164 /** Default main branch name */
165 public static final String MASTER
= "master";
167 /** Prefix for branch refs */
168 public static final String HEADS_PREFIX
= "refs/heads";
170 /** Prefix for remotes refs */
171 public static String REMOTES_PREFIX
= "refs/remotes";
174 * Create a new digest function for objects.
176 * @return a new digest object.
177 * @throws RuntimeException
178 * this Java virtual machine does not support the required hash
179 * function. Very unlikely given that JGit uses a hash function
180 * that is in the Java reference specification.
182 public static MessageDigest
newMessageDigest() {
184 return MessageDigest
.getInstance(HASH_FUNCTION
);
185 } catch (NoSuchAlgorithmException nsae
) {
186 throw new RuntimeException("Required hash function "
187 + HASH_FUNCTION
+ " not available.", nsae
);
192 * Convert an OBJ_* type constant to a TYPE_* type constant.
194 * @param typeCode the type code, from a pack representation.
195 * @return the canonical string name of this type.
197 public static String
typeString(final int typeCode
) {
208 throw new IllegalArgumentException("Bad object type: " + typeCode
);
213 * Convert an integer into its decimal representation.
216 * the integer to convert.
217 * @return a decimal representation of the input integer. The returned array
218 * is the smallest array that will hold the value.
220 public static byte[] encodeASCII(final long s
) {
221 return encodeASCII(Long
.toString(s
));
225 * Convert a string to US-ASCII encoding.
228 * the string to convert. Must not contain any characters over
229 * 127 (outside of 7-bit ASCII).
230 * @return a byte array of the same length as the input string, holding the
231 * same characters, in the same order.
232 * @throws IllegalArgumentException
233 * the input string contains one or more characters outside of
234 * the 7-bit ASCII character space.
236 public static byte[] encodeASCII(final String s
) {
237 final byte[] r
= new byte[s
.length()];
238 for (int k
= r
.length
- 1; k
>= 0; k
--) {
239 final char c
= s
.charAt(k
);
241 throw new IllegalArgumentException("Not ASCII string: " + s
);
248 if (OBJECT_ID_LENGTH
!= newMessageDigest().getDigestLength())
249 throw new LinkageError("Incorrect OBJECT_ID_LENGTH.");
250 CHARSET
= Charset
.forName(CHARACTER_ENCODING
);
253 private Constants() {
254 // Hide the default constructor