Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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