You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Constants.java 23KB

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