You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LfsPointer.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2016, Christian Halstrick <christian.halstrick@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;
  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import java.io.BufferedReader;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.InputStreamReader;
  16. import java.io.OutputStream;
  17. import java.io.PrintStream;
  18. import java.io.UnsupportedEncodingException;
  19. import java.util.Locale;
  20. import org.eclipse.jgit.annotations.Nullable;
  21. import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
  22. import org.eclipse.jgit.lfs.lib.Constants;
  23. import org.eclipse.jgit.lfs.lib.LongObjectId;
  24. /**
  25. * Represents an LFS pointer file
  26. *
  27. * @since 4.6
  28. */
  29. public class LfsPointer implements Comparable<LfsPointer> {
  30. /**
  31. * The version of the LfsPointer file format
  32. */
  33. public static final String VERSION = "https://git-lfs.github.com/spec/v1"; //$NON-NLS-1$
  34. /**
  35. * The version of the LfsPointer file format using legacy URL
  36. * @since 4.7
  37. */
  38. public static final String VERSION_LEGACY = "https://hawser.github.com/spec/v1"; //$NON-NLS-1$
  39. /**
  40. * Don't inspect files that are larger than this threshold to avoid
  41. * excessive reading. No pointer file should be larger than this.
  42. * @since 4.11
  43. */
  44. public static final int SIZE_THRESHOLD = 200;
  45. /**
  46. * The name of the hash function as used in the pointer files. This will
  47. * evaluate to "sha256"
  48. */
  49. public static final String HASH_FUNCTION_NAME = Constants.LONG_HASH_FUNCTION
  50. .toLowerCase(Locale.ROOT).replace("-", ""); //$NON-NLS-1$ //$NON-NLS-2$
  51. private AnyLongObjectId oid;
  52. private long size;
  53. /**
  54. * <p>Constructor for LfsPointer.</p>
  55. *
  56. * @param oid
  57. * the id of the content
  58. * @param size
  59. * the size of the content
  60. */
  61. public LfsPointer(AnyLongObjectId oid, long size) {
  62. this.oid = oid;
  63. this.size = size;
  64. }
  65. /**
  66. * <p>Getter for the field <code>oid</code>.</p>
  67. *
  68. * @return the id of the content
  69. */
  70. public AnyLongObjectId getOid() {
  71. return oid;
  72. }
  73. /**
  74. * <p>Getter for the field <code>size</code>.</p>
  75. *
  76. * @return the size of the content
  77. */
  78. public long getSize() {
  79. return size;
  80. }
  81. /**
  82. * Encode this object into the LFS format defined by {@link #VERSION}
  83. *
  84. * @param out
  85. * the {@link java.io.OutputStream} into which the encoded data should be
  86. * written
  87. */
  88. public void encode(OutputStream out) {
  89. try (PrintStream ps = new PrintStream(out, false,
  90. UTF_8.name())) {
  91. ps.print("version "); //$NON-NLS-1$
  92. ps.print(VERSION + "\n"); //$NON-NLS-1$
  93. ps.print("oid " + HASH_FUNCTION_NAME + ":"); //$NON-NLS-1$ //$NON-NLS-2$
  94. ps.print(oid.name() + "\n"); //$NON-NLS-1$
  95. ps.print("size "); //$NON-NLS-1$
  96. ps.print(size + "\n"); //$NON-NLS-1$
  97. } catch (UnsupportedEncodingException e) {
  98. // should not happen, we are using a standard charset
  99. }
  100. }
  101. /**
  102. * Try to parse the data provided by an InputStream to the format defined by
  103. * {@link #VERSION}
  104. *
  105. * @param in
  106. * the {@link java.io.InputStream} from where to read the data
  107. * @return an {@link org.eclipse.jgit.lfs.LfsPointer} or <code>null</code>
  108. * if the stream was not parseable as LfsPointer
  109. * @throws java.io.IOException
  110. */
  111. @Nullable
  112. public static LfsPointer parseLfsPointer(InputStream in)
  113. throws IOException {
  114. boolean versionLine = false;
  115. LongObjectId id = null;
  116. long sz = -1;
  117. try (BufferedReader br = new BufferedReader(
  118. new InputStreamReader(in, UTF_8))) {
  119. for (String s = br.readLine(); s != null; s = br.readLine()) {
  120. if (s.startsWith("#") || s.length() == 0) { //$NON-NLS-1$
  121. continue;
  122. } else if (s.startsWith("version") && s.length() > 8 //$NON-NLS-1$
  123. && (s.substring(8).trim().equals(VERSION) ||
  124. s.substring(8).trim().equals(VERSION_LEGACY))) {
  125. versionLine = true;
  126. } else if (s.startsWith("oid sha256:")) { //$NON-NLS-1$
  127. id = LongObjectId.fromString(s.substring(11).trim());
  128. } else if (s.startsWith("size") && s.length() > 5) { //$NON-NLS-1$
  129. sz = Long.parseLong(s.substring(5).trim());
  130. }
  131. }
  132. if (versionLine && id != null && sz > -1) {
  133. return new LfsPointer(id, sz);
  134. }
  135. }
  136. return null;
  137. }
  138. /** {@inheritDoc} */
  139. @Override
  140. public String toString() {
  141. return "LfsPointer: oid=" + oid.name() + ", size=" //$NON-NLS-1$ //$NON-NLS-2$
  142. + size;
  143. }
  144. /**
  145. * @since 4.11
  146. */
  147. @Override
  148. public int compareTo(LfsPointer o) {
  149. int x = getOid().compareTo(o.getOid());
  150. if (x != 0) {
  151. return x;
  152. }
  153. return Long.compare(getSize(), o.getSize());
  154. }
  155. }