View Javadoc
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  
45  import java.io.IOException;
46  import java.io.InputStream;
47  import java.io.PrintStream;
48  import java.text.MessageFormat;
49  import java.util.concurrent.Callable;
50  
51  import org.eclipse.jgit.annotations.Nullable;
52  import org.eclipse.jgit.attributes.Attribute;
53  import org.eclipse.jgit.attributes.Attributes;
54  import org.eclipse.jgit.hooks.PrePushHook;
55  import org.eclipse.jgit.internal.JGitText;
56  import org.eclipse.jgit.lib.ObjectLoader;
57  import org.eclipse.jgit.lib.Repository;
58  import org.eclipse.jgit.revwalk.RevCommit;
59  import org.eclipse.jgit.treewalk.FileTreeIterator;
60  import org.eclipse.jgit.treewalk.TreeWalk;
61  import org.eclipse.jgit.treewalk.filter.PathFilter;
62  
63  /**
64   * Represents an optionally present LFS support implementation
65   *
66   * @since 4.11
67   */
68  public class LfsFactory {
69  
70  	private static LfsFactorytml#LfsFactory">LfsFactory instance = new LfsFactory();
71  
72  	/**
73  	 * Constructor
74  	 */
75  	protected LfsFactory() {
76  	}
77  
78  	/**
79  	 * @return the current LFS implementation
80  	 */
81  	public static LfsFactory getInstance() {
82  		return instance;
83  	}
84  
85  	/**
86  	 * @param instance
87  	 *            register a {@link LfsFactory} instance as the
88  	 *            {@link LfsFactory} implementation to use.
89  	 */
90  	public static void setInstance(LfsFactory instance) {
91  		LfsFactory.instance = instance;
92  	}
93  
94  	/**
95  	 * @return whether LFS support is available
96  	 */
97  	public boolean isAvailable() {
98  		return false;
99  	}
100 
101 	/**
102 	 * Apply clean filtering to the given stream, writing the file content to
103 	 * the LFS storage if required and returning a stream to the LFS pointer
104 	 * instead.
105 	 *
106 	 * @param db
107 	 *            the repository
108 	 * @param input
109 	 *            the original input
110 	 * @param length
111 	 *            the expected input stream length
112 	 * @param attribute
113 	 *            the attribute used to check for LFS enablement (i.e. "merge",
114 	 *            "diff", "filter" from .gitattributes).
115 	 * @return a stream to the content that should be written to the object
116 	 *         store along with the expected length of the stream. the original
117 	 *         stream is not applicable.
118 	 * @throws IOException
119 	 *             in case of an error
120 	 */
121 	public LfsInputStream applyCleanFilter(Repository db,
122 			InputStream input, long length, Attribute attribute)
123 			throws IOException {
124 		return new LfsInputStream(input, length);
125 	}
126 
127 	/**
128 	 * Apply smudge filtering to a given loader, potentially redirecting it to a
129 	 * LFS blob which is downloaded on demand.
130 	 *
131 	 * @param db
132 	 *            the repository
133 	 * @param loader
134 	 *            the loader for the blob
135 	 * @param attribute
136 	 *            the attribute used to check for LFS enablement (i.e. "merge",
137 	 *            "diff", "filter" from .gitattributes).
138 	 * @return a loader for the actual data of a blob, or the original loader in
139 	 *         case LFS is not applicable.
140 	 * @throws IOException
141 	 */
142 	public ObjectLoader applySmudgeFilter(Repository db,
143 			ObjectLoader loader, Attribute attribute) throws IOException {
144 		return loader;
145 	}
146 
147 	/**
148 	 * Retrieve a pre-push hook to be applied.
149 	 *
150 	 * @param repo
151 	 *            the {@link Repository} the hook is applied to.
152 	 * @param outputStream
153 	 * @return a {@link PrePushHook} implementation or <code>null</code>
154 	 */
155 	@Nullable
156 	public PrePushHook getPrePushHook(Repository repo,
157 			PrintStream outputStream) {
158 		return null;
159 	}
160 
161 	/**
162 	 * Retrieve an {@link LfsInstallCommand} which can be used to enable LFS
163 	 * support (if available) either per repository or for the user.
164 	 *
165 	 * @return a command to install LFS support.
166 	 */
167 	@Nullable
168 	public LfsInstallCommand getInstallCommand() {
169 		return null;
170 	}
171 
172 	/**
173 	 * @param db
174 	 *            the repository to check
175 	 * @return whether LFS is enabled for the given repository locally or
176 	 *         globally.
177 	 */
178 	public boolean isEnabled(Repository db) {
179 		return false;
180 	}
181 
182 	/**
183 	 * @param db
184 	 *            the repository
185 	 * @param path
186 	 *            the path to find attributes for
187 	 * @return the {@link Attributes} for the given path.
188 	 * @throws IOException
189 	 *             in case of an error
190 	 */
191 	public static Attributes getAttributesForPath(Repository db, String path)
192 			throws IOException {
193 		try (TreeWalkeeWalk.html#TreeWalk">TreeWalk walk = new TreeWalk(db)) {
194 			walk.addTree(new FileTreeIterator(db));
195 			PathFilter f = PathFilter.create(path);
196 			walk.setFilter(f);
197 			walk.setRecursive(false);
198 			Attributes attr = null;
199 			while (walk.next()) {
200 				if (f.isDone(walk)) {
201 					attr = walk.getAttributes();
202 					break;
203 				} else if (walk.isSubtree()) {
204 					walk.enterSubtree();
205 				}
206 			}
207 			if (attr == null) {
208 				throw new IOException(MessageFormat
209 						.format(JGitText.get().noPathAttributesFound, path));
210 			}
211 
212 			return attr;
213 		}
214 	}
215 
216 	/**
217 	 * Get attributes for given path and commit
218 	 *
219 	 * @param db
220 	 *            the repository
221 	 * @param path
222 	 *            the path to find attributes for
223 	 * @param commit
224 	 *            the commit to inspect.
225 	 * @return the {@link Attributes} for the given path.
226 	 * @throws IOException
227 	 *             in case of an error
228 	 */
229 	public static Attributes getAttributesForPath(Repository db, String path,
230 			RevCommit commit) throws IOException {
231 		if (commit == null) {
232 			return getAttributesForPath(db, path);
233 		}
234 
235 		try (TreeWalk walk = TreeWalk.forPath(db, path, commit.getTree())) {
236 			Attributes attr = walk == null ? null : walk.getAttributes();
237 			if (attr == null) {
238 				throw new IOException(MessageFormat
239 						.format(JGitText.get().noPathAttributesFound, path));
240 			}
241 
242 			return attr;
243 		}
244 	}
245 
246 	/**
247 	 * Encapsulate a potentially exchanged {@link InputStream} along with the
248 	 * expected stream content length.
249 	 */
250 	public static final class LfsInputStream extends InputStream {
251 		/**
252 		 * The actual stream.
253 		 */
254 		private InputStream stream;
255 
256 		/**
257 		 * The expected stream content length.
258 		 */
259 		private long length;
260 
261 		/**
262 		 * Create a new wrapper around a certain stream
263 		 *
264 		 * @param stream
265 		 *            the stream to wrap. the stream will be closed on
266 		 *            {@link #close()}.
267 		 * @param length
268 		 *            the expected length of the stream
269 		 */
270 		public LfsInputStream(InputStream stream, long length) {
271 			this.stream = stream;
272 			this.length = length;
273 		}
274 
275 		/**
276 		 * Create a new wrapper around a temporary buffer.
277 		 *
278 		 * @param buffer
279 		 *            the buffer to initialize stream and length from. The
280 		 *            buffer will be destroyed on {@link #close()}
281 		 * @throws IOException
282 		 *             in case of an error opening the stream to the buffer.
283 		 */
284 		public LfsInputStream(TemporaryBuffer buffer) throws IOException {
285 			this.stream = buffer.openInputStreamWithAutoDestroy();
286 			this.length = buffer.length();
287 		}
288 
289 		@Override
290 		public void close() throws IOException {
291 			stream.close();
292 		}
293 
294 		@Override
295 		public int read() throws IOException {
296 			return stream.read();
297 		}
298 
299 		@Override
300 		public int read(byte b[], int off, int len) throws IOException {
301 			return stream.read(b, off, len);
302 		}
303 
304 		/**
305 		 * @return the length of the stream
306 		 */
307 		public long getLength() {
308 			return length;
309 		}
310 	}
311 
312 	/**
313 	 * A command to enable LFS. Optionally set a {@link Repository} to enable
314 	 * locally on the repository only.
315 	 */
316 	public interface LfsInstallCommand extends Callable<Void> {
317 		/**
318 		 * @param repo
319 		 *            the repository to enable support for.
320 		 * @return The {@link LfsInstallCommand} for chaining.
321 		 */
322 		public LfsInstallCommand setRepository(Repository repo);
323 	}
324 
325 }