Constants.java

  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. import java.security.MessageDigest;
  12. import java.security.NoSuchAlgorithmException;
  13. import java.text.MessageFormat;

  14. import org.eclipse.jgit.lfs.internal.LfsText;

  15. /**
  16.  * Misc. constants used throughout JGit LFS extension.
  17.  *
  18.  * @since 4.3
  19.  */
  20. @SuppressWarnings("nls")
  21. public final class Constants {
  22.     /**
  23.      * lfs folder/section/filter name
  24.      *
  25.      * @since 4.6
  26.      */
  27.     public static final String LFS = "lfs";

  28.     /**
  29.      * Hash function used natively by Git LFS extension for large objects.
  30.      *
  31.      * @since 4.6
  32.      */
  33.     public static final String LONG_HASH_FUNCTION = "SHA-256";

  34.     /**
  35.      * A Git LFS large object hash is 256 bits, i.e. 32 bytes.
  36.      * <p>
  37.      * Changing this assumption is not going to be as easy as changing this
  38.      * declaration.
  39.      */
  40.     public static final int LONG_OBJECT_ID_LENGTH = 32;

  41.     /**
  42.      * A Git LFS large object can be expressed as a 64 character string of
  43.      * hexadecimal digits.
  44.      *
  45.      * @see #LONG_OBJECT_ID_LENGTH
  46.      */
  47.     public static final int LONG_OBJECT_ID_STRING_LENGTH = LONG_OBJECT_ID_LENGTH
  48.             * 2;

  49.     /**
  50.      * LFS upload operation.
  51.      *
  52.      * @since 4.7
  53.      */
  54.     public static final String UPLOAD = "upload";

  55.     /**
  56.      * LFS download operation.
  57.      *
  58.      * @since 4.7
  59.      */
  60.     public static final String DOWNLOAD = "download";

  61.     /**
  62.      * LFS verify operation.
  63.      *
  64.      * @since 4.7
  65.      */
  66.     public static final String VERIFY = "verify";

  67.     /**
  68.      * Prefix for all LFS related filters.
  69.      *
  70.      * @since 4.11
  71.      */
  72.     public static final String ATTR_FILTER_DRIVER_PREFIX = "lfs/";

  73.     /**
  74.      * Create a new digest function for objects.
  75.      *
  76.      * @return a new digest object.
  77.      * @throws java.lang.RuntimeException
  78.      *             this Java virtual machine does not support the required hash
  79.      *             function. Very unlikely given that JGit uses a hash function
  80.      *             that is in the Java reference specification.
  81.      */
  82.     public static MessageDigest newMessageDigest() {
  83.         try {
  84.             return MessageDigest.getInstance(LONG_HASH_FUNCTION);
  85.         } catch (NoSuchAlgorithmException nsae) {
  86.             throw new RuntimeException(MessageFormat.format(
  87.                     LfsText.get().requiredHashFunctionNotAvailable,
  88.                     LONG_HASH_FUNCTION), nsae);
  89.         }
  90.     }

  91.     static {
  92.         if (LONG_OBJECT_ID_LENGTH != newMessageDigest().getDigestLength())
  93.             throw new LinkageError(
  94.                     LfsText.get().incorrectLONG_OBJECT_ID_LENGTH);
  95.     }

  96.     /**
  97.      * Content type used by LFS REST API as defined in <a href=
  98.      * "https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md">
  99.      * https://github.com/github/git-lfs/blob/master/docs/api/v1/http-v1-batch.md</a>
  100.      */
  101.     public static final String CONTENT_TYPE_GIT_LFS_JSON = "application/vnd.git-lfs+json";

  102.     /**
  103.      * "Arbitrary binary data" as defined in
  104.      * <a href="https://www.ietf.org/rfc/rfc2046.txt">RFC 2046</a>
  105.      */
  106.     public static final String HDR_APPLICATION_OCTET_STREAM = "application/octet-stream";
  107. }