您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Constants.java 21KB

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