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

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