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