1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.util;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.PrintStream;
15 import java.text.MessageFormat;
16 import java.util.concurrent.Callable;
17
18 import org.eclipse.jgit.annotations.Nullable;
19 import org.eclipse.jgit.attributes.Attribute;
20 import org.eclipse.jgit.attributes.Attributes;
21 import org.eclipse.jgit.hooks.PrePushHook;
22 import org.eclipse.jgit.internal.JGitText;
23 import org.eclipse.jgit.lib.ObjectLoader;
24 import org.eclipse.jgit.lib.Repository;
25 import org.eclipse.jgit.revwalk.RevCommit;
26 import org.eclipse.jgit.treewalk.FileTreeIterator;
27 import org.eclipse.jgit.treewalk.TreeWalk;
28 import org.eclipse.jgit.treewalk.filter.PathFilter;
29
30
31
32
33
34
35 public class LfsFactory {
36
37 private static LfsFactory instance = new LfsFactory();
38
39
40
41
42 protected LfsFactory() {
43 }
44
45
46
47
48 public static LfsFactory getInstance() {
49 return instance;
50 }
51
52
53
54
55
56
57 public static void setInstance(LfsFactory instance) {
58 LfsFactory.instance = instance;
59 }
60
61
62
63
64 public boolean isAvailable() {
65 return false;
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public LfsInputStream applyCleanFilter(Repository db,
89 InputStream input, long length, Attribute attribute)
90 throws IOException {
91 return new LfsInputStream(input, length);
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public ObjectLoader applySmudgeFilter(Repository db,
110 ObjectLoader loader, Attribute attribute) throws IOException {
111 return loader;
112 }
113
114
115
116
117
118
119
120
121
122 @Nullable
123 public PrePushHook getPrePushHook(Repository repo,
124 PrintStream outputStream) {
125 return null;
126 }
127
128
129
130
131
132
133
134
135
136
137
138 @Nullable
139 public PrePushHook getPrePushHook(Repository repo, PrintStream outputStream,
140 PrintStream errorStream) {
141 return getPrePushHook(repo, outputStream);
142 }
143
144
145
146
147
148
149
150 @Nullable
151 public LfsInstallCommand getInstallCommand() {
152 return null;
153 }
154
155
156
157
158
159
160
161 public boolean isEnabled(Repository db) {
162 return false;
163 }
164
165
166
167
168
169
170
171
172
173
174 public static Attributes getAttributesForPath(Repository db, String path)
175 throws IOException {
176 try (TreeWalk walk = new TreeWalk(db)) {
177 walk.addTree(new FileTreeIterator(db));
178 PathFilter f = PathFilter.create(path);
179 walk.setFilter(f);
180 walk.setRecursive(false);
181 Attributes attr = null;
182 while (walk.next()) {
183 if (f.isDone(walk)) {
184 attr = walk.getAttributes();
185 break;
186 } else if (walk.isSubtree()) {
187 walk.enterSubtree();
188 }
189 }
190 if (attr == null) {
191 throw new IOException(MessageFormat
192 .format(JGitText.get().noPathAttributesFound, path));
193 }
194
195 return attr;
196 }
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 public static Attributes getAttributesForPath(Repository db, String path,
213 RevCommit commit) throws IOException {
214 if (commit == null) {
215 return getAttributesForPath(db, path);
216 }
217
218 try (TreeWalk walk = TreeWalk.forPath(db, path, commit.getTree())) {
219 Attributes attr = walk == null ? null : walk.getAttributes();
220 if (attr == null) {
221 throw new IOException(MessageFormat
222 .format(JGitText.get().noPathAttributesFound, path));
223 }
224
225 return attr;
226 }
227 }
228
229
230
231
232
233 public static final class LfsInputStream extends InputStream {
234
235
236
237 private InputStream stream;
238
239
240
241
242 private long length;
243
244
245
246
247
248
249
250
251
252
253 public LfsInputStream(InputStream stream, long length) {
254 this.stream = stream;
255 this.length = length;
256 }
257
258
259
260
261
262
263
264
265
266
267 public LfsInputStream(TemporaryBuffer buffer) throws IOException {
268 this.stream = buffer.openInputStreamWithAutoDestroy();
269 this.length = buffer.length();
270 }
271
272 @Override
273 public void close() throws IOException {
274 stream.close();
275 }
276
277 @Override
278 public int read() throws IOException {
279 return stream.read();
280 }
281
282 @Override
283 public int read(byte[] b, int off, int len) throws IOException {
284 return stream.read(b, off, len);
285 }
286
287
288
289
290 public long getLength() {
291 return length;
292 }
293 }
294
295
296
297
298
299 public interface LfsInstallCommand extends Callable<Void> {
300
301
302
303
304
305 public LfsInstallCommand setRepository(Repository repo);
306 }
307
308 }