Class GitIdentifiers

java.lang.Object
org.apache.commons.codec.digest.GitIdentifiers

public class GitIdentifiers extends Object
Computes Git object identifiers and their generalizations described by the SWHID specification.

When the hash algorithm is SHA-1, the identifiers produced by this class are identical to those used by Git. Other hash algorithms produce generalized identifiers as described by the SWHID specification.

Git and SWHID treat file names and symbolic link targets as opaque byte sequences with no defined encoding. The identifiers produced here coincide with Git's or SWHID's own identifiers only if the original names and targets were UTF-8 encoded.

This class is immutable and thread-safe. However, the MessageDigest instances passed to it generally won't be.

Since:
1.22.0
See Also:
  • Method Details

    • blobId

      public static byte[] blobId(MessageDigest messageDigest, byte[] data)
      Reads through a byte array and returns a generalized Git blob identifier.

      The identifier is computed in the way described by the SWHID contents identifier, but it can use any hash algorithm.

      When the hash algorithm is SHA-1, the identifier is identical to Git blob identifier and SWHID contents identifier.

      Parameters:
      messageDigest - The MessageDigest to use (for example SHA-1).
      data - Data to digest.
      Returns:
      A generalized Git blob identifier.
    • blobId

      public static byte[] blobId(MessageDigest messageDigest, long dataSize, InputStream data) throws IOException
      Reads through a stream of known size and returns a generalized Git blob identifier, without buffering.

      When the size of the content is known in advance, this overload streams data directly through the digest without buffering the full content in memory.

      The stream is drained to its end. If the number of bytes read differs from dataSize, an IOException is thrown.

      When the hash algorithm is SHA-1, the identifier is identical to Git blob identifier and SWHID contents identifier.

      Parameters:
      messageDigest - The MessageDigest to use (for example SHA-1).
      dataSize - The exact number of bytes in data.
      data - Stream to digest.
      Returns:
      A generalized Git blob identifier.
      Throws:
      IOException - Thrown on error reading the stream, or if the stream does not contain exactly dataSize bytes.
    • blobId

      public static byte[] blobId(MessageDigest messageDigest, Path data) throws IOException
      Reads through a file and returns a generalized Git blob identifier.

      The identifier is computed in the way described by the SWHID contents identifier, but it can use any hash algorithm.

      When the hash algorithm is SHA-1, the identifier is identical to Git blob identifier and SWHID contents identifier.

      Parameters:
      messageDigest - The MessageDigest to use (for example SHA-1).
      data - Path to the file to digest.
      Returns:
      A generalized Git blob identifier.
      Throws:
      IOException - Thrown on error accessing the file, or if the number of bytes read differs from its measured size.
    • treeId

      public static byte[] treeId(MessageDigest messageDigest, Path data) throws IOException
      Reads through a directory and returns a generalized Git tree identifier.

      The identifier is computed in the way described by the SWHID directory identifier, but it can use any hash algorithm.

      When the hash algorithm is SHA-1, the identifier is identical to Git tree identifier and SWHID directory identifier.

      Parameters:
      messageDigest - The MessageDigest to use (for example SHA-1).
      data - Path to the directory to digest.
      Returns:
      A generalized Git tree identifier.
      Throws:
      IOException - Thrown on error accessing the directory or its contents.
    • treeIdBuilder

      Returns a new GitIdentifiers.TreeIdBuilder for constructing a generalized Git tree identifier from a virtual directory structure, such as the contents of an archive.

      The identifier is computed in the way described by the SWHID directory identifier, but it can use any hash algorithm.

      When the hash algorithm is SHA-1, the identifier is identical to Git tree identifier and SWHID directory identifier.

      Parameters:
      messageDigest - The MessageDigest to use (for example SHA-1).
      Returns:
      A new GitIdentifiers.TreeIdBuilder.