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 	/**
279 	 * Objects folder name
280 	 * @since 5.5
281 	 */
282 	public static final String OBJECTS = "objects";
283 
284 	/** Info refs folder */
285 	public static final String INFO_REFS = "info/refs";
286 
287 	/**
288 	 * Info alternates file (goes under OBJECTS)
289 	 * @since 5.5
290 	 */
291 	public static final String INFO_ALTERNATES = "info/alternates";
292 
293 	/**
294 	 * HTTP alternates file (goes under OBJECTS)
295 	 * @since 5.5
296 	 */
297 	public static final String INFO_HTTP_ALTERNATES = "info/http-alternates";
298 
299 	/** Packed refs file */
300 	public static final String PACKED_REFS = "packed-refs";
301 
302 	/**
303 	 * Excludes-file
304 	 *
305 	 * @since 3.0
306 	 */
307 	public static final String INFO_EXCLUDE = "info/exclude";
308 
309 	/**
310 	 * Attributes-override-file
311 	 *
312 	 * @since 4.2
313 	 */
314 	public static final String INFO_ATTRIBUTES = "info/attributes";
315 
316 	/**
317 	 * The system property that contains the system user name
318 	 *
319 	 * @since 3.6
320 	 */
321 	public static final String OS_USER_DIR = "user.dir";
322 
323 	/** The system property that contains the system user name */
324 	public static final String OS_USER_NAME_KEY = "user.name";
325 
326 	/** The environment variable that contains the author's name */
327 	public static final String GIT_AUTHOR_NAME_KEY = "GIT_AUTHOR_NAME";
328 
329 	/** The environment variable that contains the author's email */
330 	public static final String GIT_AUTHOR_EMAIL_KEY = "GIT_AUTHOR_EMAIL";
331 
332 	/** The environment variable that contains the commiter's name */
333 	public static final String GIT_COMMITTER_NAME_KEY = "GIT_COMMITTER_NAME";
334 
335 	/** The environment variable that contains the commiter's email */
336 	public static final String GIT_COMMITTER_EMAIL_KEY = "GIT_COMMITTER_EMAIL";
337 
338 	/**
339 	 * The environment variable that blocks use of the system config file
340 	 *
341 	 * @since 3.3
342 	 */
343 	public static final String GIT_CONFIG_NOSYSTEM_KEY = "GIT_CONFIG_NOSYSTEM";
344 
345 	/**
346 	 * The environment variable that limits how close to the root of the file
347 	 * systems JGit will traverse when looking for a repository root.
348 	 */
349 	public static final String GIT_CEILING_DIRECTORIES_KEY = "GIT_CEILING_DIRECTORIES";
350 
351 	/**
352 	 * The environment variable that tells us which directory is the ".git"
353 	 * directory
354 	 */
355 	public static final String GIT_DIR_KEY = "GIT_DIR";
356 
357 	/**
358 	 * The environment variable that tells us which directory is the working
359 	 * directory.
360 	 */
361 	public static final String GIT_WORK_TREE_KEY = "GIT_WORK_TREE";
362 
363 	/**
364 	 * The environment variable that tells us which file holds the Git index.
365 	 */
366 	public static final String GIT_INDEX_FILE_KEY = "GIT_INDEX_FILE";
367 
368 	/**
369 	 * The environment variable that tells us where objects are stored
370 	 */
371 	public static final String GIT_OBJECT_DIRECTORY_KEY = "GIT_OBJECT_DIRECTORY";
372 
373 	/**
374 	 * The environment variable that tells us where to look for objects, besides
375 	 * the default objects directory.
376 	 */
377 	public static final String GIT_ALTERNATE_OBJECT_DIRECTORIES_KEY = "GIT_ALTERNATE_OBJECT_DIRECTORIES";
378 
379 	/** Default value for the user name if no other information is available */
380 	public static final String UNKNOWN_USER_DEFAULT = "unknown-user";
381 
382 	/** Beginning of the common "Signed-off-by: " commit message line */
383 	public static final String SIGNED_OFF_BY_TAG = "Signed-off-by: ";
384 
385 	/** A gitignore file name */
386 	public static final String GITIGNORE_FILENAME = ".gitignore";
387 
388 	/** Default remote name used by clone, push and fetch operations */
389 	public static final String DEFAULT_REMOTE_NAME = "origin";
390 
391 	/** Default name for the Git repository directory */
392 	public static final String DOT_GIT = ".git";
393 
394 	/** Default name for the Git repository configuration */
395 	public static final String CONFIG = "config";
396 
397 	/** A bare repository typically ends with this string */
398 	public static final String DOT_GIT_EXT = ".git";
399 
400 	/**
401 	 * Name of the attributes file
402 	 *
403 	 * @since 3.7
404 	 */
405 	public static final String DOT_GIT_ATTRIBUTES = ".gitattributes";
406 
407 	/**
408 	 * Key for filters in .gitattributes
409 	 *
410 	 * @since 4.2
411 	 */
412 	public static final String ATTR_FILTER = "filter";
413 
414 	/**
415 	 * clean command name, used to call filter driver
416 	 *
417 	 * @since 4.2
418 	 */
419 	public static final String ATTR_FILTER_TYPE_CLEAN = "clean";
420 
421 	/**
422 	 * smudge command name, used to call filter driver
423 	 *
424 	 * @since 4.2
425 	 */
426 	public static final String ATTR_FILTER_TYPE_SMUDGE = "smudge";
427 
428 	/**
429 	 * Builtin filter commands start with this prefix
430 	 *
431 	 * @since 4.6
432 	 */
433 	public static final String BUILTIN_FILTER_PREFIX = "jgit://builtin/";
434 
435 	/** Name of the ignore file */
436 	public static final String DOT_GIT_IGNORE = ".gitignore";
437 
438 	/** Name of the submodules file */
439 	public static final String DOT_GIT_MODULES = ".gitmodules";
440 
441 	/** Name of the .git/shallow file */
442 	public static final String SHALLOW = "shallow";
443 
444 	/**
445 	 * Prefix of the first line in a ".git" file
446 	 *
447 	 * @since 3.6
448 	 */
449 	public static final String GITDIR = "gitdir: ";
450 
451 	/**
452 	 * Name of the folder (inside gitDir) where submodules are stored
453 	 *
454 	 * @since 3.6
455 	 */
456 	public static final String MODULES = "modules";
457 
458 	/**
459 	 * Name of the folder (inside gitDir) where the hooks are stored.
460 	 *
461 	 * @since 3.7
462 	 */
463 	public static final String HOOKS = "hooks";
464 
465 	/**
466 	 * Merge attribute.
467 	 *
468 	 * @since 4.9
469 	 */
470 	public static final String ATTR_MERGE = "merge"; //$NON-NLS-1$
471 
472 	/**
473 	 * Diff attribute.
474 	 *
475 	 * @since 4.11
476 	 */
477 	public static final String ATTR_DIFF = "diff"; //$NON-NLS-1$
478 
479 	/**
480 	 * Binary value for custom merger.
481 	 *
482 	 * @since 4.9
483 	 */
484 	public static final String ATTR_BUILTIN_BINARY_MERGER = "binary"; //$NON-NLS-1$
485 
486 	/**
487 	 * Create a new digest function for objects.
488 	 *
489 	 * @return a new digest object.
490 	 * @throws java.lang.RuntimeException
491 	 *             this Java virtual machine does not support the required hash
492 	 *             function. Very unlikely given that JGit uses a hash function
493 	 *             that is in the Java reference specification.
494 	 */
495 	public static MessageDigest newMessageDigest() {
496 		try {
497 			return MessageDigest.getInstance(HASH_FUNCTION);
498 		} catch (NoSuchAlgorithmException nsae) {
499 			throw new RuntimeException(MessageFormat.format(
500 					JGitText.get().requiredHashFunctionNotAvailable, HASH_FUNCTION), nsae);
501 		}
502 	}
503 
504 	/**
505 	 * Convert an OBJ_* type constant to a TYPE_* type constant.
506 	 *
507 	 * @param typeCode the type code, from a pack representation.
508 	 * @return the canonical string name of this type.
509 	 */
510 	public static String typeString(int typeCode) {
511 		switch (typeCode) {
512 		case OBJ_COMMIT:
513 			return TYPE_COMMIT;
514 		case OBJ_TREE:
515 			return TYPE_TREE;
516 		case OBJ_BLOB:
517 			return TYPE_BLOB;
518 		case OBJ_TAG:
519 			return TYPE_TAG;
520 		default:
521 			throw new IllegalArgumentException(MessageFormat.format(
522 					JGitText.get().badObjectType, Integer.valueOf(typeCode)));
523 		}
524 	}
525 
526 	/**
527 	 * Convert an OBJ_* type constant to an ASCII encoded string constant.
528 	 * <p>
529 	 * The ASCII encoded string is often the canonical representation of
530 	 * the type within a loose object header, or within a tag header.
531 	 *
532 	 * @param typeCode the type code, from a pack representation.
533 	 * @return the canonical ASCII encoded name of this type.
534 	 */
535 	public static byte[] encodedTypeString(int typeCode) {
536 		switch (typeCode) {
537 		case OBJ_COMMIT:
538 			return ENCODED_TYPE_COMMIT;
539 		case OBJ_TREE:
540 			return ENCODED_TYPE_TREE;
541 		case OBJ_BLOB:
542 			return ENCODED_TYPE_BLOB;
543 		case OBJ_TAG:
544 			return ENCODED_TYPE_TAG;
545 		default:
546 			throw new IllegalArgumentException(MessageFormat.format(
547 					JGitText.get().badObjectType, Integer.valueOf(typeCode)));
548 		}
549 	}
550 
551 	/**
552 	 * Parse an encoded type string into a type constant.
553 	 *
554 	 * @param id
555 	 *            object id this type string came from; may be null if that is
556 	 *            not known at the time the parse is occurring.
557 	 * @param typeString
558 	 *            string version of the type code.
559 	 * @param endMark
560 	 *            character immediately following the type string. Usually ' '
561 	 *            (space) or '\n' (line feed).
562 	 * @param offset
563 	 *            position within <code>typeString</code> where the parse
564 	 *            should start. Updated with the new position (just past
565 	 *            <code>endMark</code> when the parse is successful.
566 	 * @return a type code constant (one of {@link #OBJ_BLOB},
567 	 *         {@link #OBJ_COMMIT}, {@link #OBJ_TAG}, {@link #OBJ_TREE}.
568 	 * @throws org.eclipse.jgit.errors.CorruptObjectException
569 	 *             there is no valid type identified by <code>typeString</code>.
570 	 */
571 	public static int decodeTypeString(final AnyObjectId id,
572 			final byte[] typeString, final byte endMark,
573 			final MutableInteger offset) throws CorruptObjectException {
574 		try {
575 			int position = offset.value;
576 			switch (typeString[position]) {
577 			case 'b':
578 				if (typeString[position + 1] != 'l'
579 						|| typeString[position + 2] != 'o'
580 						|| typeString[position + 3] != 'b'
581 						|| typeString[position + 4] != endMark)
582 					throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
583 				offset.value = position + 5;
584 				return Constants.OBJ_BLOB;
585 
586 			case 'c':
587 				if (typeString[position + 1] != 'o'
588 						|| typeString[position + 2] != 'm'
589 						|| typeString[position + 3] != 'm'
590 						|| typeString[position + 4] != 'i'
591 						|| typeString[position + 5] != 't'
592 						|| typeString[position + 6] != endMark)
593 					throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
594 				offset.value = position + 7;
595 				return Constants.OBJ_COMMIT;
596 
597 			case 't':
598 				switch (typeString[position + 1]) {
599 				case 'a':
600 					if (typeString[position + 2] != 'g'
601 							|| typeString[position + 3] != endMark)
602 						throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
603 					offset.value = position + 4;
604 					return Constants.OBJ_TAG;
605 
606 				case 'r':
607 					if (typeString[position + 2] != 'e'
608 							|| typeString[position + 3] != 'e'
609 							|| typeString[position + 4] != endMark)
610 						throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
611 					offset.value = position + 5;
612 					return Constants.OBJ_TREE;
613 
614 				default:
615 					throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
616 				}
617 
618 			default:
619 				throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
620 			}
621 		} catch (ArrayIndexOutOfBoundsException bad) {
622 			throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
623 		}
624 	}
625 
626 	/**
627 	 * Convert an integer into its decimal representation.
628 	 *
629 	 * @param s
630 	 *            the integer to convert.
631 	 * @return a decimal representation of the input integer. The returned array
632 	 *         is the smallest array that will hold the value.
633 	 */
634 	public static byte[] encodeASCII(long s) {
635 		return encodeASCII(Long.toString(s));
636 	}
637 
638 	/**
639 	 * Convert a string to US-ASCII encoding.
640 	 *
641 	 * @param s
642 	 *            the string to convert. Must not contain any characters over
643 	 *            127 (outside of 7-bit ASCII).
644 	 * @return a byte array of the same length as the input string, holding the
645 	 *         same characters, in the same order.
646 	 * @throws java.lang.IllegalArgumentException
647 	 *             the input string contains one or more characters outside of
648 	 *             the 7-bit ASCII character space.
649 	 */
650 	public static byte[] encodeASCII(String s) {
651 		final byte[] r = new byte[s.length()];
652 		for (int k = r.length - 1; k >= 0; k--) {
653 			final char c = s.charAt(k);
654 			if (c > 127)
655 				throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notASCIIString, s));
656 			r[k] = (byte) c;
657 		}
658 		return r;
659 	}
660 
661 	/**
662 	 * Convert a string to a byte array in the standard character encoding.
663 	 *
664 	 * @param str
665 	 *            the string to convert. May contain any Unicode characters.
666 	 * @return a byte array representing the requested string, encoded using the
667 	 *         default character encoding (UTF-8).
668 	 * @see #CHARACTER_ENCODING
669 	 */
670 	public static byte[] encode(String str) {
671 		final ByteBuffer bb = UTF_8.encode(str);
672 		final int len = bb.limit();
673 		if (bb.hasArray() && bb.arrayOffset() == 0) {
674 			final byte[] arr = bb.array();
675 			if (arr.length == len)
676 				return arr;
677 		}
678 
679 		final byte[] arr = new byte[len];
680 		bb.get(arr);
681 		return arr;
682 	}
683 
684 	static {
685 		if (OBJECT_ID_LENGTH != newMessageDigest().getDigestLength())
686 			throw new LinkageError(JGitText.get().incorrectOBJECT_ID_LENGTH);
687 		CHARSET = UTF_8;
688 		CHARACTER_ENCODING = UTF_8.name();
689 	}
690 
691 	/** name of the file containing the commit msg for a merge commit */
692 	public static final String MERGE_MSG = "MERGE_MSG";
693 
694 	/** name of the file containing the IDs of the parents of a merge commit */
695 	public static final String MERGE_HEAD = "MERGE_HEAD";
696 
697 	/** name of the file containing the ID of a cherry pick commit in case of conflicts */
698 	public static final String CHERRY_PICK_HEAD = "CHERRY_PICK_HEAD";
699 
700 	/** name of the file containing the commit msg for a squash commit */
701 	public static final String SQUASH_MSG = "SQUASH_MSG";
702 
703 	/** name of the file containing the ID of a revert commit in case of conflicts */
704 	public static final String REVERT_HEAD = "REVERT_HEAD";
705 
706 	/**
707 	 * name of the ref ORIG_HEAD used by certain commands to store the original
708 	 * value of HEAD
709 	 */
710 	public static final String ORIG_HEAD = "ORIG_HEAD";
711 
712 	/**
713 	 * Name of the file in which git commands and hooks store and read the
714 	 * message prepared for the upcoming commit.
715 	 *
716 	 * @since 4.0
717 	 */
718 	public static final String COMMIT_EDITMSG = "COMMIT_EDITMSG";
719 
720 	/**
721 	 * Well-known object ID for the empty blob.
722 	 *
723 	 * @since 0.9.1
724 	 */
725 	public static final ObjectId/../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId EMPTY_BLOB_ID = ObjectId
726 			.fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
727 
728 	/**
729 	 * Well-known object ID for the empty tree.
730 	 *
731 	 * @since 5.1
732 	 */
733 	public static final ObjectId/../org/eclipse/jgit/lib/ObjectId.html#ObjectId">ObjectId EMPTY_TREE_ID = ObjectId
734 			.fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904");
735 
736 	/**
737 	 * Suffix of lock file name
738 	 *
739 	 * @since 4.7
740 	 */
741 	public static final String LOCK_SUFFIX = ".lock"; //$NON-NLS-1$
742 
743 	private Constants() {
744 		// Hide the default constructor
745 	}
746 }