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.

LfsFactory.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright (C) 2018, Markus Duft <markus.duft@ssi-schaefer.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.io.PrintStream;
  47. import java.text.MessageFormat;
  48. import org.eclipse.jgit.annotations.Nullable;
  49. import org.eclipse.jgit.attributes.Attribute;
  50. import org.eclipse.jgit.attributes.Attributes;
  51. import org.eclipse.jgit.hooks.PrePushHook;
  52. import org.eclipse.jgit.internal.JGitText;
  53. import org.eclipse.jgit.lib.ObjectLoader;
  54. import org.eclipse.jgit.lib.Repository;
  55. import org.eclipse.jgit.revwalk.RevCommit;
  56. import org.eclipse.jgit.treewalk.FileTreeIterator;
  57. import org.eclipse.jgit.treewalk.TreeWalk;
  58. import org.eclipse.jgit.treewalk.filter.PathFilter;
  59. /**
  60. * Represents an optionally present LFS support implementation
  61. *
  62. * @since 4.11
  63. */
  64. public class LfsFactory {
  65. private static LfsFactory instance = new LfsFactory();
  66. /**
  67. * Constructor
  68. */
  69. protected LfsFactory() {
  70. }
  71. /**
  72. * @return the current LFS implementation
  73. */
  74. public static LfsFactory getInstance() {
  75. return instance;
  76. }
  77. /**
  78. * @param instance
  79. * register a {@link LfsFactory} instance as the
  80. * {@link LfsFactory} implementation to use.
  81. */
  82. public static void setInstance(LfsFactory instance) {
  83. LfsFactory.instance = instance;
  84. }
  85. /**
  86. * @return whether LFS support is available
  87. */
  88. public boolean isAvailable() {
  89. return false;
  90. }
  91. /**
  92. * Apply clean filtering to the given stream, writing the file content to
  93. * the LFS storage if required and returning a stream to the LFS pointer
  94. * instead.
  95. *
  96. * @param db
  97. * the repository
  98. * @param input
  99. * the original input
  100. * @param length
  101. * the expected input stream length
  102. * @param attribute
  103. * the attribute used to check for LFS enablement (i.e. "merge",
  104. * "diff", "filter" from .gitattributes).
  105. * @return a stream to the content that should be written to the object
  106. * store along with the expected length of the stream. the original
  107. * stream is not applicable.
  108. * @throws IOException
  109. * in case of an error
  110. */
  111. public LfsInputStream applyCleanFilter(Repository db,
  112. InputStream input, long length, Attribute attribute)
  113. throws IOException {
  114. return new LfsInputStream(input, length);
  115. }
  116. /**
  117. * Apply smudge filtering to a given loader, potentially redirecting it to a
  118. * LFS blob which is downloaded on demand.
  119. *
  120. * @param db
  121. * the repository
  122. * @param loader
  123. * the loader for the blob
  124. * @param attribute
  125. * the attribute used to check for LFS enablement (i.e. "merge",
  126. * "diff", "filter" from .gitattributes).
  127. * @return a loader for the actual data of a blob, or the original loader in
  128. * case LFS is not applicable.
  129. * @throws IOException
  130. */
  131. public ObjectLoader applySmudgeFilter(Repository db,
  132. ObjectLoader loader, Attribute attribute) throws IOException {
  133. return loader;
  134. }
  135. /**
  136. * Retrieve a pre-push hook to be applied.
  137. *
  138. * @param repo
  139. * the {@link Repository} the hook is applied to.
  140. * @param outputStream
  141. * @return a {@link PrePushHook} implementation or <code>null</code>
  142. */
  143. public @Nullable PrePushHook getPrePushHook(Repository repo,
  144. PrintStream outputStream) {
  145. return null;
  146. }
  147. /**
  148. * @param db
  149. * the repository
  150. * @param path
  151. * the path to find attributes for
  152. * @return the {@link Attributes} for the given path.
  153. * @throws IOException
  154. * in case of an error
  155. */
  156. public static Attributes getAttributesForPath(Repository db, String path)
  157. throws IOException {
  158. try (TreeWalk walk = new TreeWalk(db)) {
  159. walk.addTree(new FileTreeIterator(db));
  160. PathFilter f = PathFilter.create(path);
  161. walk.setFilter(f);
  162. walk.setRecursive(false);
  163. Attributes attr = null;
  164. while (walk.next()) {
  165. if (f.isDone(walk)) {
  166. attr = walk.getAttributes();
  167. break;
  168. } else if (walk.isSubtree()) {
  169. walk.enterSubtree();
  170. }
  171. }
  172. if (attr == null) {
  173. throw new IOException(MessageFormat
  174. .format(JGitText.get().noPathAttributesFound, path));
  175. }
  176. return attr;
  177. }
  178. }
  179. /**
  180. * Get attributes for given path and commit
  181. *
  182. * @param db
  183. * the repository
  184. * @param path
  185. * the path to find attributes for
  186. * @param commit
  187. * the commit to inspect.
  188. * @return the {@link Attributes} for the given path.
  189. * @throws IOException
  190. * in case of an error
  191. */
  192. public static Attributes getAttributesForPath(Repository db, String path,
  193. RevCommit commit) throws IOException {
  194. if (commit == null) {
  195. return getAttributesForPath(db, path);
  196. }
  197. try (TreeWalk walk = TreeWalk.forPath(db, path, commit.getTree())) {
  198. Attributes attr = walk == null ? null : walk.getAttributes();
  199. if (attr == null) {
  200. throw new IOException(MessageFormat
  201. .format(JGitText.get().noPathAttributesFound, path));
  202. }
  203. return attr;
  204. }
  205. }
  206. /**
  207. * Encapsulate a potentially exchanged {@link InputStream} along with the
  208. * expected stream content length.
  209. */
  210. public static final class LfsInputStream extends InputStream {
  211. /**
  212. * The actual stream.
  213. */
  214. private InputStream stream;
  215. /**
  216. * The expected stream content length.
  217. */
  218. private long length;
  219. /**
  220. * Create a new wrapper around a certain stream
  221. *
  222. * @param stream
  223. * the stream to wrap. the stream will be closed on
  224. * {@link #close()}.
  225. * @param length
  226. * the expected length of the stream
  227. */
  228. public LfsInputStream(InputStream stream, long length) {
  229. this.stream = stream;
  230. this.length = length;
  231. }
  232. /**
  233. * Create a new wrapper around a temporary buffer.
  234. *
  235. * @param buffer
  236. * the buffer to initialize stream and length from. The
  237. * buffer will be destroyed on {@link #close()}
  238. * @throws IOException
  239. * in case of an error opening the stream to the buffer.
  240. */
  241. public LfsInputStream(TemporaryBuffer buffer) throws IOException {
  242. this.stream = buffer.openInputStreamWithAutoDestroy();
  243. this.length = buffer.length();
  244. }
  245. @Override
  246. public void close() throws IOException {
  247. stream.close();
  248. }
  249. @Override
  250. public int read() throws IOException {
  251. return stream.read();
  252. }
  253. /**
  254. * @return the length of the stream
  255. */
  256. public long getLength() {
  257. return length;
  258. }
  259. }
  260. }