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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 limits how close to the root of the file
  245. * systems JGit will traverse when looking for a repository root.
  246. */
  247. public static final String GIT_CEILING_DIRECTORIES_KEY = "GIT_CEILING_DIRECTORIES";
  248. /**
  249. * The environment variable that tells us which directory is the ".git"
  250. * directory
  251. */
  252. public static final String GIT_DIR_KEY = "GIT_DIR";
  253. /**
  254. * The environment variable that tells us which directory is the working
  255. * directory.
  256. */
  257. public static final String GIT_WORK_TREE_KEY = "GIT_WORK_TREE";
  258. /**
  259. * The environment variable that tells us which file holds the Git index.
  260. */
  261. public static final String GIT_INDEX_FILE_KEY = "GIT_INDEX_FILE";
  262. /**
  263. * The environment variable that tells us where objects are stored
  264. */
  265. public static final String GIT_OBJECT_DIRECTORY_KEY = "GIT_OBJECT_DIRECTORY";
  266. /**
  267. * The environment variable that tells us where to look for objects, besides
  268. * the default objects directory.
  269. */
  270. public static final String GIT_ALTERNATE_OBJECT_DIRECTORIES_KEY = "GIT_ALTERNATE_OBJECT_DIRECTORIES";
  271. /** Default value for the user name if no other information is available */
  272. public static final String UNKNOWN_USER_DEFAULT = "unknown-user";
  273. /** Beginning of the common "Signed-off-by: " commit message line */
  274. public static final String SIGNED_OFF_BY_TAG = "Signed-off-by: ";
  275. /** A gitignore file name */
  276. public static final String GITIGNORE_FILENAME = ".gitignore";
  277. /** Default remote name used by clone, push and fetch operations */
  278. public static final String DEFAULT_REMOTE_NAME = "origin";
  279. /** Default name for the Git repository directory */
  280. public static final String DOT_GIT = ".git";
  281. /** Default name for the Git repository configuration */
  282. public static final String CONFIG = "config";
  283. /** A bare repository typically ends with this string */
  284. public static final String DOT_GIT_EXT = ".git";
  285. /** Name of the ignore file */
  286. public static final String DOT_GIT_IGNORE = ".gitignore";
  287. /** Name of the submodules file */
  288. public static final String DOT_GIT_MODULES = ".gitmodules";
  289. /** Name of the .git/shallow file */
  290. public static final String SHALLOW = "shallow";
  291. /**
  292. * Create a new digest function for objects.
  293. *
  294. * @return a new digest object.
  295. * @throws RuntimeException
  296. * this Java virtual machine does not support the required hash
  297. * function. Very unlikely given that JGit uses a hash function
  298. * that is in the Java reference specification.
  299. */
  300. public static MessageDigest newMessageDigest() {
  301. try {
  302. return MessageDigest.getInstance(HASH_FUNCTION);
  303. } catch (NoSuchAlgorithmException nsae) {
  304. throw new RuntimeException(MessageFormat.format(
  305. JGitText.get().requiredHashFunctionNotAvailable, HASH_FUNCTION), nsae);
  306. }
  307. }
  308. /**
  309. * Convert an OBJ_* type constant to a TYPE_* type constant.
  310. *
  311. * @param typeCode the type code, from a pack representation.
  312. * @return the canonical string name of this type.
  313. */
  314. public static String typeString(final int typeCode) {
  315. switch (typeCode) {
  316. case OBJ_COMMIT:
  317. return TYPE_COMMIT;
  318. case OBJ_TREE:
  319. return TYPE_TREE;
  320. case OBJ_BLOB:
  321. return TYPE_BLOB;
  322. case OBJ_TAG:
  323. return TYPE_TAG;
  324. default:
  325. throw new IllegalArgumentException(MessageFormat.format(
  326. JGitText.get().badObjectType, Integer.valueOf(typeCode)));
  327. }
  328. }
  329. /**
  330. * Convert an OBJ_* type constant to an ASCII encoded string constant.
  331. * <p>
  332. * The ASCII encoded string is often the canonical representation of
  333. * the type within a loose object header, or within a tag header.
  334. *
  335. * @param typeCode the type code, from a pack representation.
  336. * @return the canonical ASCII encoded name of this type.
  337. */
  338. public static byte[] encodedTypeString(final int typeCode) {
  339. switch (typeCode) {
  340. case OBJ_COMMIT:
  341. return ENCODED_TYPE_COMMIT;
  342. case OBJ_TREE:
  343. return ENCODED_TYPE_TREE;
  344. case OBJ_BLOB:
  345. return ENCODED_TYPE_BLOB;
  346. case OBJ_TAG:
  347. return ENCODED_TYPE_TAG;
  348. default:
  349. throw new IllegalArgumentException(MessageFormat.format(
  350. JGitText.get().badObjectType, Integer.valueOf(typeCode)));
  351. }
  352. }
  353. /**
  354. * Parse an encoded type string into a type constant.
  355. *
  356. * @param id
  357. * object id this type string came from; may be null if that is
  358. * not known at the time the parse is occurring.
  359. * @param typeString
  360. * string version of the type code.
  361. * @param endMark
  362. * character immediately following the type string. Usually ' '
  363. * (space) or '\n' (line feed).
  364. * @param offset
  365. * position within <code>typeString</code> where the parse
  366. * should start. Updated with the new position (just past
  367. * <code>endMark</code> when the parse is successful.
  368. * @return a type code constant (one of {@link #OBJ_BLOB},
  369. * {@link #OBJ_COMMIT}, {@link #OBJ_TAG}, {@link #OBJ_TREE}.
  370. * @throws CorruptObjectException
  371. * there is no valid type identified by <code>typeString</code>.
  372. */
  373. public static int decodeTypeString(final AnyObjectId id,
  374. final byte[] typeString, final byte endMark,
  375. final MutableInteger offset) throws CorruptObjectException {
  376. try {
  377. int position = offset.value;
  378. switch (typeString[position]) {
  379. case 'b':
  380. if (typeString[position + 1] != 'l'
  381. || typeString[position + 2] != 'o'
  382. || typeString[position + 3] != 'b'
  383. || typeString[position + 4] != endMark)
  384. throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
  385. offset.value = position + 5;
  386. return Constants.OBJ_BLOB;
  387. case 'c':
  388. if (typeString[position + 1] != 'o'
  389. || typeString[position + 2] != 'm'
  390. || typeString[position + 3] != 'm'
  391. || typeString[position + 4] != 'i'
  392. || typeString[position + 5] != 't'
  393. || typeString[position + 6] != endMark)
  394. throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
  395. offset.value = position + 7;
  396. return Constants.OBJ_COMMIT;
  397. case 't':
  398. switch (typeString[position + 1]) {
  399. case 'a':
  400. if (typeString[position + 2] != 'g'
  401. || typeString[position + 3] != endMark)
  402. throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
  403. offset.value = position + 4;
  404. return Constants.OBJ_TAG;
  405. case 'r':
  406. if (typeString[position + 2] != 'e'
  407. || typeString[position + 3] != 'e'
  408. || typeString[position + 4] != endMark)
  409. throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
  410. offset.value = position + 5;
  411. return Constants.OBJ_TREE;
  412. default:
  413. throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
  414. }
  415. default:
  416. throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
  417. }
  418. } catch (ArrayIndexOutOfBoundsException bad) {
  419. throw new CorruptObjectException(id, JGitText.get().corruptObjectInvalidType);
  420. }
  421. }
  422. /**
  423. * Convert an integer into its decimal representation.
  424. *
  425. * @param s
  426. * the integer to convert.
  427. * @return a decimal representation of the input integer. The returned array
  428. * is the smallest array that will hold the value.
  429. */
  430. public static byte[] encodeASCII(final long s) {
  431. return encodeASCII(Long.toString(s));
  432. }
  433. /**
  434. * Convert a string to US-ASCII encoding.
  435. *
  436. * @param s
  437. * the string to convert. Must not contain any characters over
  438. * 127 (outside of 7-bit ASCII).
  439. * @return a byte array of the same length as the input string, holding the
  440. * same characters, in the same order.
  441. * @throws IllegalArgumentException
  442. * the input string contains one or more characters outside of
  443. * the 7-bit ASCII character space.
  444. */
  445. public static byte[] encodeASCII(final String s) {
  446. final byte[] r = new byte[s.length()];
  447. for (int k = r.length - 1; k >= 0; k--) {
  448. final char c = s.charAt(k);
  449. if (c > 127)
  450. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().notASCIIString, s));
  451. r[k] = (byte) c;
  452. }
  453. return r;
  454. }
  455. /**
  456. * Convert a string to a byte array in the standard character encoding.
  457. *
  458. * @param str
  459. * the string to convert. May contain any Unicode characters.
  460. * @return a byte array representing the requested string, encoded using the
  461. * default character encoding (UTF-8).
  462. * @see #CHARACTER_ENCODING
  463. */
  464. public static byte[] encode(final String str) {
  465. final ByteBuffer bb = Constants.CHARSET.encode(str);
  466. final int len = bb.limit();
  467. if (bb.hasArray() && bb.arrayOffset() == 0) {
  468. final byte[] arr = bb.array();
  469. if (arr.length == len)
  470. return arr;
  471. }
  472. final byte[] arr = new byte[len];
  473. bb.get(arr);
  474. return arr;
  475. }
  476. static {
  477. if (OBJECT_ID_LENGTH != newMessageDigest().getDigestLength())
  478. throw new LinkageError(JGitText.get().incorrectOBJECT_ID_LENGTH);
  479. CHARSET = Charset.forName(CHARACTER_ENCODING);
  480. }
  481. /** name of the file containing the commit msg for a merge commit */
  482. public static final String MERGE_MSG = "MERGE_MSG";
  483. /** name of the file containing the IDs of the parents of a merge commit */
  484. public static final String MERGE_HEAD = "MERGE_HEAD";
  485. /** name of the file containing the ID of a cherry pick commit in case of conflicts */
  486. public static final String CHERRY_PICK_HEAD = "CHERRY_PICK_HEAD";
  487. /** name of the file containing the commit msg for a squash commit */
  488. public static final String SQUASH_MSG = "SQUASH_MSG";
  489. /** name of the file containing the ID of a revert commit in case of conflicts */
  490. public static final String REVERT_HEAD = "REVERT_HEAD";
  491. /**
  492. * name of the ref ORIG_HEAD used by certain commands to store the original
  493. * value of HEAD
  494. */
  495. public static final String ORIG_HEAD = "ORIG_HEAD";
  496. /** objectid for the empty blob */
  497. public static final ObjectId EMPTY_BLOB_ID = ObjectId
  498. .fromString("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391");
  499. private Constants() {
  500. // Hide the default constructor
  501. }
  502. }