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