View Javadoc
1   /*
2    * Copyright (C) 2008, Google Inc.
3    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2006-2017, Shawn O. Pearce <spearce@spearce.org>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44   */
45  
46  package org.eclipse.jgit.lib;
47  
48  import static java.nio.charset.StandardCharsets.UTF_8;
49  
50  import java.nio.ByteBuffer;
51  import java.nio.charset.Charset;
52  import java.security.MessageDigest;
53  import java.security.NoSuchAlgorithmException;
54  import java.text.MessageFormat;
55  
56  import org.eclipse.jgit.errors.CorruptObjectException;
57  import org.eclipse.jgit.internal.JGitText;
58  import org.eclipse.jgit.util.MutableInteger;
59  
60  /**
61   * Misc. constants and helpers used throughout JGit.
62   */
63  @SuppressWarnings("nls")
64  public final class Constants {
65  	/** Hash function used natively by Git for all objects. */
66  	private static final String HASH_FUNCTION = "SHA-1";
67  
68  	/**
69  	 * A Git object hash is 160 bits, i.e. 20 bytes.
70  	 * <p>
71  	 * Changing this assumption is not going to be as easy as changing this
72  	 * declaration.
73  	 */
74  	public static final int OBJECT_ID_LENGTH = 20;
75  
76  	/**
77  	 * A Git object can be expressed as a 40 character string of hexadecimal
78  	 * digits.
79  	 *
80  	 * @see #OBJECT_ID_LENGTH
81  	 */
82  	public static final int OBJECT_ID_STRING_LENGTH = OBJECT_ID_LENGTH * 2;
83  
84  	/** Special name for the "HEAD" symbolic-ref. */
85  	public static final String HEAD = "HEAD";
86  
87  	/** Special name for the "FETCH_HEAD" symbolic-ref. */
88  	public static final String FETCH_HEAD = "FETCH_HEAD";
89  
90  	/**
91  	 * Text string that identifies an object as a commit.
92  	 * <p>
93  	 * Commits connect trees into a string of project histories, where each
94  	 * commit is an assertion that the best way to continue is to use this other
95  	 * tree (set of files).
96  	 */
97  	public static final String TYPE_COMMIT = "commit";
98  
99  	/**
100 	 * Text string that identifies an object as a blob.
101 	 * <p>
102 	 * Blobs store whole file revisions. They are used for any user file, as
103 	 * well as for symlinks. Blobs form the bulk of any project's storage space.
104 	 */
105 	public static final String TYPE_BLOB = "blob";
106 
107 	/**
108 	 * Text string that identifies an object as a tree.
109 	 * <p>
110 	 * Trees attach object ids (hashes) to names and file modes. The normal use
111 	 * for a tree is to store a version of a directory and its contents.
112 	 */
113 	public static final String TYPE_TREE = "tree";
114 
115 	/**
116 	 * Text string that identifies an object as an annotated tag.
117 	 * <p>
118 	 * Annotated tags store a pointer to any other object, and an additional
119 	 * message. It is most commonly used to record a stable release of the
120 	 * project.
121 	 */
122 	public static final String TYPE_TAG = "tag";
123 
124 	private static final byte[] ENCODED_TYPE_COMMIT = encodeASCII(TYPE_COMMIT);
125 
126 	private static final byte[] ENCODED_TYPE_BLOB = encodeASCII(TYPE_BLOB);
127 
128 	private static final byte[] ENCODED_TYPE_TREE = encodeASCII(TYPE_TREE);
129 
130 	private static final byte[] ENCODED_TYPE_TAG = encodeASCII(TYPE_TAG);
131 
132 	/** An unknown or invalid object type code. */
133 	public static final int OBJ_BAD = -1;
134 
135 	/**
136 	 * In-pack object type: extended types.
137 	 * <p>
138 	 * This header code is reserved for future expansion. It is currently
139 	 * undefined/unsupported.
140 	 */
141 	public static final int OBJ_EXT = 0;
142 
143 	/**
144 	 * In-pack object type: commit.
145 	 * <p>
146 	 * Indicates the associated object is a commit.
147 	 * <p>
148 	 * <b>This constant is fixed and is defined by the Git packfile format.</b>
149 	 *
150 	 * @see #TYPE_COMMIT
151 	 */
152 	public static final int OBJ_COMMIT = 1;
153 
154 	/**
155 	 * In-pack object type: tree.
156 	 * <p>
157 	 * Indicates the associated object is a tree.
158 	 * <p>
159 	 * <b>This constant is fixed and is defined by the Git packfile format.</b>
160 	 *
161 	 * @see #TYPE_BLOB
162 	 */
163 	public static final int OBJ_TREE = 2;
164 
165 	/**
166 	 * In-pack object type: blob.
167 	 * <p>
168 	 * Indicates the associated object is a blob.
169 	 * <p>
170 	 * <b>This constant is fixed and is defined by the Git packfile format.</b>
171 	 *
172 	 * @see #TYPE_BLOB
173 	 */
174 	public static final int OBJ_BLOB = 3;
175 
176 	/**
177 	 * In-pack object type: annotated tag.
178 	 * <p>
179 	 * Indicates the associated object is an annotated tag.
180 	 * <p>
181 	 * <b>This constant is fixed and is defined by the Git packfile format.</b>
182 	 *
183 	 * @see #TYPE_TAG
184 	 */
185 	public static final int OBJ_TAG = 4;
186 
187 	/** In-pack object type: reserved for future use. */
188 	public static final int OBJ_TYPE_5 = 5;
189 
190 	/**
191 	 * In-pack object type: offset delta
192 	 * <p>
193 	 * Objects stored with this type actually have a different type which must
194 	 * be obtained from their delta base object. Delta objects store only the
195 	 * changes needed to apply to the base object in order to recover the
196 	 * original object.
197 	 * <p>
198 	 * An offset delta uses a negative offset from the start of this object to
199 	 * refer to its delta base. The base object must exist in this packfile
200 	 * (even in the case of a thin pack).
201 	 * <p>
202 	 * <b>This constant is fixed and is defined by the Git packfile format.</b>
203 	 */
204 	public static final int OBJ_OFS_DELTA = 6;
205 
206 	/**
207 	 * In-pack object type: reference delta
208 	 * <p>
209 	 * Objects stored with this type actually have a different type which must
210 	 * be obtained from their delta base object. Delta objects store only the
211 	 * changes needed to apply to the base object in order to recover the
212 	 * original object.
213 	 * <p>
214 	 * A reference delta uses a full object id (hash) to reference the delta
215 	 * base. The base object is allowed to be omitted from the packfile, but
216 	 * only in the case of a thin pack being transferred over the network.
217 	 * <p>
218 	 * <b>This constant is fixed and is defined by the Git packfile format.</b>
219 	 */
220 	public static final int OBJ_REF_DELTA = 7;
221 
222 	/**
223 	 * Pack file signature that occurs at file header - identifies file as Git
224 	 * packfile formatted.
225 	 * <p>
226 	 * <b>This constant is fixed and is defined by the Git packfile format.</b>
227 	 */
228 	public static final byte[] PACK_SIGNATURE = { 'P', 'A', 'C', 'K' };
229 
230 	/**
231 	 * Native character encoding for commit messages, file names...
232 	 *
233 	 * @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_8} directly
234 	 *             instead.
235 	 */
236 	@Deprecated
237 	public static final Charset CHARSET;
238 
239 	/**
240 	 * Native character encoding for commit messages, file names...
241 	 *
242 	 * @deprecated Use {@link java.nio.charset.StandardCharsets#UTF_8} directly
243 	 *             instead.
244 	 */
245 	@Deprecated
246 	public static final String CHARACTER_ENCODING;
247 
248 	/** Default main branch name */
249 	public static final String MASTER = "master";
250 
251 	/** Default stash branch name */
252 	public static final String STASH = "stash";
253 
254 	/** Prefix for branch refs */
255 	public static final String R_HEADS = "refs/heads/";
256 
257 	/** Prefix for remotes refs */
258 	public static final String R_REMOTES = "refs/remotes/";
259 
260 	/** Prefix for tag refs */
261 	public static final String R_TAGS = "refs/tags/";
262 
263 	/** Prefix for notes refs */
264 	public static final String R_NOTES = "refs/notes/";
265 
266 	/** Standard notes ref */
267 	public static final String R_NOTES_COMMITS = R_NOTES + "commits";
268 
269 	/** Prefix for any ref */
270 	public static final String R_REFS = "refs/";
271 
272 	/** Standard stash ref */
273 	public static final String R_STASH = R_REFS + STASH;
274 
275 	/** Logs folder name */
276 	public static final String LOGS = "logs";
277 
278 	/** Info refs folder */
279 	public static final String INFO_REFS = "info/refs";
280 
281 	/** Packed refs file */
282 	public static final String PACKED_REFS = "packed-refs";
283 
284 	/**
285 	 * Excludes-file
286 	 *
287 	 * @since 3.0
288 	 */
289 	public static final String INFO_EXCLUDE = "info/exclude";
290 
291 	/**
292 	 * Attributes-override-file
293 	 *
294 	 * @since 4.2
295 	 */
296 	public static final String INFO_ATTRIBUTES = "info/attributes";
297 
298 	/**
299 	 * The system property that contains the system user name
300 	 *
301 	 * @since 3.6
302 	 */
303 	public static final String OS_USER_DIR = "user.dir";
304 
305 	/** The system property that contains the system user name */
306 	public static final String OS_USER_NAME_KEY = "user.name";
307 
308 	/** The environment variable that contains the author's name */
309 	public static final String GIT_AUTHOR_NAME_KEY = "GIT_AUTHOR_NAME";
310 
311 	/** The environment variable that contains the author's email */
312 	public static final String GIT_AUTHOR_EMAIL_KEY = "GIT_AUTHOR_EMAIL";
313 
314 	/** The environment variable that contains the commiter's name */
315 	public static final String GIT_COMMITTER_NAME_KEY = "GIT_COMMITTER_NAME";
316 
317 	/** The environment variable that contains the commiter's email */
318 	public static final String GIT_COMMITTER_EMAIL_KEY = "GIT_COMMITTER_EMAIL";
319 
320 	/**
321 	 * The environment variable that blocks use of the system config file
322 	 *
323 	 * @since 3.3
324 	 */
325 	public static final String GIT_CONFIG_NOSYSTEM_KEY = "GIT_CONFIG_NOSYSTEM";
326 
327 	/**
328 	 * The environment variable that limits how close to the root of the file
329 	 * systems JGit will traverse when looking for a repository root.
330 	 */
331 	public static final String GIT_CEILING_DIRECTORIES_KEY = "GIT_CEILING_DIRECTORIES";
332 
333 	/**
334 	 * The environment variable that tells us which directory is the ".git"
335 	 * directory
336 	 */
337 	public static final String GIT_DIR_KEY = "GIT_DIR";
338 
339 	/**
340 	 * The environment variable that tells us which directory is the working
341 	 * directory.
342 	 */
343 	public static final String GIT_WORK_TREE_KEY = "GIT_WORK_TREE";
344 
345 	/**
346 	 * The environment variable that tells us which file holds the Git index.
347 	 */
348 	public static final String GIT_INDEX_FILE_KEY = "GIT_INDEX_FILE";
349 
350 	/**
351 	 * The environment variable that tells us where objects are stored
352 	 */
353 	public static final String GIT_OBJECT_DIRECTORY_KEY = "GIT_OBJECT_DIRECTORY";
354 
355 	/**
356 	 * The environment variable that tells us where to look for objects, besides
357 	 * the default objects directory.
358 	 */
359 	public static final String GIT_ALTERNATE_OBJECT_DIRECTORIES_KEY = "GIT_ALTERNATE_OBJECT_DIRECTORIES";
360 
361 	/** Default value for the user name if no other information is available */
362 	public static final String UNKNOWN_USER_DEFAULT = "unknown-user";
363 
364 	/** Beginning of the common "Signed-off-by: " commit message line */
365 	public static final String SIGNED_OFF_BY_TAG = "Signed-off-by: ";
366 
367 	/** A gitignore file name */
368 	public static final String GITIGNORE_FILENAME = ".gitignore";
369 
370 	/** Default remote name used by clone, push and fetch operations */
371 	public static final String DEFAULT_REMOTE_NAME = "origin";
372 
373 	/** Default name for the Git repository directory */
374 	public static final String DOT_GIT = ".git";
375 
376 	/** Default name for the Git repository configuration */
377 	public static final String CONFIG = "config";
378 
379 	/** A bare repository typically ends with this string */
380 	public static final String DOT_GIT_EXT = ".git";
381 
382 	/**
383 	 * Name of the attributes file
384 	 *
385 	 * @since 3.7
386 	 */
387 	public static final String DOT_GIT_ATTRIBUTES = ".gitattributes";
388 
389 	/**
390 	 * Key for filters in .gitattributes
391 	 *
392 	 * @since 4.2
393 	 */
394 	public static final String ATTR_FILTER = "filter";
395 
396 	/**
397 	 * clean command name, used to call filter driver
398 	 *
399 	 * @since 4.2
400 	 */
401 	public static final String ATTR_FILTER_TYPE_CLEAN = "clean";
402 
403 	/**
404 	 * smudge command name, used to call filter driver
405 	 *
406 	 * @since 4.2
407 	 */
408 	public static final String ATTR_FILTER_TYPE_SMUDGE = "smudge";
409 
410 	/**
411 	 * Builtin filter commands start with this prefix
412 	 *
413 	 * @since 4.6
414 	 */
415 	public static final String BUILTIN_FILTER_PREFIX = "jgit://builtin/";
416 
417 	/** Name of the ignore file */
418 	public static final String DOT_GIT_IGNORE = ".gitignore";
419 
420 	/** Name of the submodules file */
421 	public static final String DOT_GIT_MODULES = ".gitmodules";
422 
423 	/** Name of the .git/shallow file */
424 	public static final String SHALLOW = "shallow";
425 
426 	/**
427 	 * Prefix of the first line in a ".git" file
428 	 *
429 	 * @since 3.6
430 	 */
431 	public static final String GITDIR = "gitdir: ";
432 
433 	/**
434 	 * Name of the folder (inside gitDir) where submodules are stored
435 	 *
436 	 * @since 3.6
437 	 */
438 	public static final String MODULES = "modules";
439 
440 	/**
441 	 * Name of the folder (inside gitDir) where the hooks are stored.
442 	 *
443 	 * @since 3.7
444 	 */
445 	public static final String HOOKS = "hooks";
446 
447 	/**
448 	 * Merge attribute.
449 	 *
450 	 * @since 4.9
451 	 */
452 	public static final String ATTR_MERGE = "merge"; //$NON-NLS-1$
453 
454 	/**
455 	 * Diff attribute.
456 	 *
457 	 * @since 4.11
458 	 */
459 	public static final String ATTR_DIFF = "diff"; //$NON-NLS-1$
460 
461 	/**
462 	 * Binary value for custom merger.
463 	 *
464 	 * @since 4.9
465 	 */
466 	public static final String ATTR_BUILTIN_BINARY_MERGER = "binary"; //$NON-NLS-1$
467 
468 	/**
469 	 * Create a new digest function for objects.
470 	 *
471 	 * @return a new digest object.
472 	 * @throws java.lang.RuntimeException
473 	 *             this Java virtual machine does not support the required hash
474 	 *             function. Very unlikely given that JGit uses a hash function
475 	 *             that is in the Java reference specification.
476 	 */
477 	public static MessageDigest newMessageDigest() {
478 		try {
479 			return MessageDigest.getInstance(HASH_FUNCTION);
480 		} catch (NoSuchAlgorithmException nsae) {
481 			throw new RuntimeException(MessageFormat.format(
482 					JGitText.get().requiredHashFunctionNotAvailable, HASH_FUNCTION), nsae);
483 		}
484 	}
485 
486 	/**
487 	 * Convert an OBJ_* type constant to a TYPE_* type constant.
488 	 *
489 	 * @param typeCode the type code, from a pack representation.
490 	 * @return the canonical string name of this type.
491 	 */
492 	public static String typeString(int typeCode) {
493 		switch (typeCode) {
494 		case OBJ_COMMIT:
495 			return TYPE_COMMIT;
496 		case OBJ_TREE:
497 			return TYPE_TREE;
498 		case OBJ_BLOB:
499 			return TYPE_BLOB;
500 		case OBJ_TAG:
501 			return TYPE_TAG;
502 		default:
503 			throw new IllegalArgumentException(MessageFormat.format(
504 					JGitText.get().badObjectType, Integer.valueOf(typeCode)));
505 		}
506 	}
507 
508 	/**
509 	 * Convert an OBJ_* type constant to an ASCII encoded string constant.
510 	 * <p>
511 	 * The ASCII encoded string is often the canonical representation of
512 	 * the type within a loose object header, or within a tag header.
513 	 *
514 	 * @param typeCode the type code, from a pack representation.
515 	 * @return the canonical ASCII encoded name of this type.
516 	 */
517 	public static byte[] encodedTypeString(int typeCode) {
518 		switch (typeCode) {
519 		case OBJ_COMMIT:
520 			return ENCODED_TYPE_COMMIT;
521 		case OBJ_TREE:
522 			return ENCODED_TYPE_TREE;
523 		case OBJ_BLOB:
524 			return ENCODED_TYPE_BLOB;
525 		case OBJ_TAG:
526 			return ENCODED_TYPE_TAG;
527 		default:
528 			throw new IllegalArgumentException(MessageFormat.format(
529 					JGitText.get().badObjectType, Integer.valueOf(typeCode)));
530 		}
531 	}
532 
533 	/**
534 	 * Parse an encoded type string into a type constant.
535 	 *
536 	 * @param id
537 	 *            object id this type string came from; may be null if that is
538 	 *            not known at the time the parse is occurring.
539 	 * @param typeString
540 	 *            string version of the type code.
541 	 * @param endMark
542 	 *            character immediately following the type string. Usually ' '
543 	 *            (space) or '\n' (line feed).
544 	 * @param offset
545 	 *            position within <code>typeString</code> where the parse
546 	 *            should start. Updated with the new position (just past
547 	 *            <code>endMark</code> when the parse is successful.
548 	 * @return a type code constant (one of {@link #OBJ_BLOB},
549 	 *         {@link #OBJ_COMMIT}, {@link #OBJ_TAG}, {@link #OBJ_TREE}.
550 	 * @throws org.eclipse.jgit.errors.CorruptObjectException
551 	 *             there is no valid type identified by <code>typeString</code>.
552 	 */
553 	public static int decodeTypeString(final AnyObjectId id,
554 			final byte[] typeString, final byte endMark,
555 			final MutableInteger offset) throws CorruptObjectException {
556 		try {
557 			int position = offset.value;
558 			switch (typeString[position]) {
559 			case 'b':
560 				if (typeString[position + 1] != 'l'
561 						|| typeString[position + 2] != 'o'
562 						|| typeString[position + 3] != 'b'
563 						|| typeString[position + 4] != endMark)
564 					throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
565 				offset.value = position + 5;
566 				return Constants.OBJ_BLOB;
567 
568 			case 'c':
569 				if (typeString[position + 1] != 'o'
570 						|| typeString[position + 2] != 'm'
571 						|| typeString[position + 3] != 'm'
572 						|| typeString[position + 4] != 'i'
573 						|| typeString[position + 5] != 't'
574 						|| typeString[position + 6] != endMark)
575 					throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
576 				offset.value = position + 7;
577 				return Constants.OBJ_COMMIT;
578 
579 			case 't':
580 				switch (typeString[position + 1]) {
581 				case 'a':
582 					if (typeString[position + 2] != 'g'
583 							|| typeString[position + 3] != endMark)
584 						throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
585 					offset.value = position + 4;
586 					return Constants.OBJ_TAG;
587 
588 				case 'r':
589 					if (typeString[position + 2] != 'e'
590 							|| typeString[position + 3] != 'e'
591 							|| typeString[position + 4] != endMark)
592 						throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
593 					offset.value = position + 5;
594 					return Constants.OBJ_TREE;
595 
596 				default:
597 					throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
598 				}
599 
600 			default:
601 				throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
602 			}
603 		} catch (ArrayIndexOutOfBoundsException bad) {
604 			throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
605 		}
606 	}
607 
608 	/**
609 	 * Convert an integer into its decimal representation.
610 	 *
611 	 * @param s
612 	 *            the integer to convert.
613 	 * @return a decimal representation of the input integer. The returned array
614 	 *         is the smallest array that will hold the value.
615 	 */
616 	public static byte[] encodeASCII(long s) {
617 		return encodeASCII(Long.toString(s));
618 	}
619 
620 	/**
621 	 * Convert a string to US-ASCII encoding.
622 	 *
623 	 * @param s
624 	 *            the string to convert. Must not contain any characters over
625 	 *            127 (outside of 7-bit ASCII).
626 	 * @return a byte array of the same length as the input string, holding the
627 	 *         same characters, in the same order.
628 	 * @throws java.lang.IllegalArgumentException
629 	 *             the input string contains one or more characters outside of
630 	 *             the 7-bit ASCII character space.
631 	 */
632 	public static byte[] encodeASCII(String s) {
633 		final byte[] r = new byte[s.length()];
634 		for (int k = r.length - 1; k >= 0; k--) {
635 			final char c = s.charAt(k);
636 			if (c > 127)
637 				throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notASCIIString, s));
638 			r[k] = (byte) c;
639 		}
640 		return r;
641 	}
642 
643 	/**
644 	 * Convert a string to a byte array in the standard character encoding.
645 	 *
646 	 * @param str
647 	 *            the string to convert. May contain any Unicode characters.
648 	 * @return a byte array representing the requested string, encoded using the
649 	 *         default character encoding (UTF-8).
650 	 * @see #CHARACTER_ENCODING
651 	 */
652 	public static byte[] encode(String str) {
653 		final ByteBuffer bb = UTF_8.encode(str);
654 		final int len = bb.limit();
655 		if (bb.hasArray() && bb.arrayOffset() == 0) {
656 			final byte[] arr = bb.array();
657 			if (arr.length == len)
658 				return arr;
659 		}
660 
661 		final byte[] arr = new byte[len];
662 		bb.get(arr);
663 		return arr;
664 	}
665 
666 	static {
667 		if (OBJECT_ID_LENGTH != newMessageDigest().getDigestLength())
668 			throw new LinkageError(JGitText.get().incorrectOBJECT_ID_LENGTH);
669 		CHARSET = UTF_8;
670 		CHARACTER_ENCODING = UTF_8.name();
671 	}
672 
673 	/** name of the file containing the commit msg for a merge commit */
674 	public static final String MERGE_MSG = "MERGE_MSG";
675 
676 	/** name of the file containing the IDs of the parents of a merge commit */
677 	public static final String MERGE_HEAD = "MERGE_HEAD";
678 
679 	/** name of the file containing the ID of a cherry pick commit in case of conflicts */
680 	public static final String CHERRY_PICK_HEAD = "CHERRY_PICK_HEAD";
681 
682 	/** name of the file containing the commit msg for a squash commit */
683 	public static final String SQUASH_MSG = "SQUASH_MSG";
684 
685 	/** name of the file containing the ID of a revert commit in case of conflicts */
686 	public static final String REVERT_HEAD = "REVERT_HEAD";
687 
688 	/**
689 	 * name of the ref ORIG_HEAD used by certain commands to store the original
690 	 * value of HEAD
691 	 */
692 	public static final String ORIG_HEAD = "ORIG_HEAD";
693 
694 	/**
695 	 * Name of the file in which git commands and hooks store and read the
696 	 * message prepared for the upcoming commit.
697 	 *
698 	 * @since 4.0
699 	 */
700 	public static final String COMMIT_EDITMSG = "COMMIT_EDITMSG";
701 
702 	/**
703 	 * Well-known object ID for the empty blob.
704 	 *
705 	 * @since 0.9.1
706 	 */
707 	public static final ObjectId/../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId EMPTY_BLOB_ID = ObjectId
708 			.fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
709 
710 	/**
711 	 * Well-known object ID for the empty tree.
712 	 *
713 	 * @since 5.1
714 	 */
715 	public static final ObjectId/../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId EMPTY_TREE_ID = ObjectId
716 			.fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904");
717 
718 	/**
719 	 * Suffix of lock file name
720 	 *
721 	 * @since 4.7
722 	 */
723 	public static final String LOCK_SUFFIX = ".lock"; //$NON-NLS-1$
724 
725 	private Constants() {
726 		// Hide the default constructor
727 	}
728 }