View Javadoc
1   /*
2    * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.lfs.lib;
11  
12  import java.security.MessageDigest;
13  import java.security.NoSuchAlgorithmException;
14  import java.text.MessageFormat;
15  
16  import org.eclipse.jgit.lfs.internal.LfsText;
17  
18  /**
19   * Misc. constants used throughout JGit LFS extension.
20   *
21   * @since 4.3
22   */
23  @SuppressWarnings("nls")
24  public final class Constants {
25  	/**
26  	 * lfs folder/section/filter name
27  	 *
28  	 * @since 4.6
29  	 */
30  	public static final String LFS = "lfs";
31  
32  	/**
33  	 * Hash function used natively by Git LFS extension for large objects.
34  	 *
35  	 * @since 4.6
36  	 */
37  	public static final String LONG_HASH_FUNCTION = "SHA-256";
38  
39  	/**
40  	 * A Git LFS large object hash is 256 bits, i.e. 32 bytes.
41  	 * <p>
42  	 * Changing this assumption is not going to be as easy as changing this
43  	 * declaration.
44  	 */
45  	public static final int LONG_OBJECT_ID_LENGTH = 32;
46  
47  	/**
48  	 * A Git LFS large object can be expressed as a 64 character string of
49  	 * hexadecimal digits.
50  	 *
51  	 * @see #LONG_OBJECT_ID_LENGTH
52  	 */
53  	public static final int LONG_OBJECT_ID_STRING_LENGTH = LONG_OBJECT_ID_LENGTH
54  			* 2;
55  
56  	/**
57  	 * LFS upload operation.
58  	 *
59  	 * @since 4.7
60  	 */
61  	public static final String UPLOAD = "upload";
62  
63  	/**
64  	 * LFS download operation.
65  	 *
66  	 * @since 4.7
67  	 */
68  	public static final String DOWNLOAD = "download";
69  
70  	/**
71  	 * LFS verify operation.
72  	 *
73  	 * @since 4.7
74  	 */
75  	public static final String VERIFY = "verify";
76  
77  	/**
78  	 * Prefix for all LFS related filters.
79  	 *
80  	 * @since 4.11
81  	 */
82  	public static final String ATTR_FILTER_DRIVER_PREFIX = "lfs/";
83  
84  	/**
85  	 * Create a new digest function for objects.
86  	 *
87  	 * @return a new digest object.
88  	 * @throws java.lang.RuntimeException
89  	 *             this Java virtual machine does not support the required hash
90  	 *             function. Very unlikely given that JGit uses a hash function
91  	 *             that is in the Java reference specification.
92  	 */
93  	public static MessageDigest newMessageDigest() {
94  		try {
95  			return MessageDigest.getInstance(LONG_HASH_FUNCTION);
96  		} catch (NoSuchAlgorithmException nsae) {
97  			throw new RuntimeException(MessageFormat.format(
98  					LfsText.get().requiredHashFunctionNotAvailable,
99  					LONG_HASH_FUNCTION), nsae);
100 		}
101 	}
102 
103 	static {
104 		if (LONG_OBJECT_ID_LENGTH != newMessageDigest().getDigestLength())
105 			throw new LinkageError(
106 					LfsText.get().incorrectLONG_OBJECT_ID_LENGTH);
107 	}
108 
109 	/**
110 	 * Content type used by LFS REST API as defined in <a href=
111 	 * "https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md">
112 	 * https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md</a>
113 	 */
114 	public static final String CONTENT_TYPE_GIT_LFS_JSON = "application/vnd.git-lfs+json";
115 
116 	/**
117 	 * "Arbitrary binary data" as defined in
118 	 * <a href="https://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a>
119 	 */
120 	public static final String HDR_APPLICATION_OCTET_STREAM = "application/octet-stream";
121 }