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 19KB

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