View Javadoc
1   /*
2    * Copyright (C) 2017, Markus Duft <markus.duft@ssi-schaefer.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  
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.io.PrintStream;
15  
16  import org.eclipse.jgit.annotations.Nullable;
17  import org.eclipse.jgit.attributes.Attribute;
18  import org.eclipse.jgit.hooks.PrePushHook;
19  import org.eclipse.jgit.lib.ConfigConstants;
20  import org.eclipse.jgit.lib.ObjectLoader;
21  import org.eclipse.jgit.lib.Repository;
22  import org.eclipse.jgit.util.LfsFactory;
23  
24  /**
25   * Implementation of {@link LfsFactory}, using built-in (optional) LFS support.
26   *
27   * @since 4.11
28   */
29  public class BuiltinLFS extends LfsFactory {
30  
31  	private BuiltinLFS() {
32  		SmudgeFilter.register();
33  		CleanFilter.register();
34  	}
35  
36  	/**
37  	 * Activates the built-in LFS support.
38  	 */
39  	public static void register() {
40  		setInstance(new BuiltinLFS());
41  	}
42  
43  	@Override
44  	public boolean isAvailable() {
45  		return true;
46  	}
47  
48  	@Override
49  	public ObjectLoaderlib/ObjectLoader.html#ObjectLoader">ObjectLoader applySmudgeFilter(Repository db, ObjectLoader loader,
50  			Attribute attribute) throws IOException {
51  		if (isEnabled(db) && (attribute == null || isEnabled(db, attribute))) {
52  			return LfsBlobFilter.smudgeLfsBlob(db, loader);
53  		}
54  		return loader;
55  	}
56  
57  	@Override
58  	public LfsInputStream applyCleanFilter(Repository db, InputStream input,
59  			long length, Attribute attribute) throws IOException {
60  		if (isEnabled(db, attribute)) {
61  			return new LfsInputStream(LfsBlobFilter.cleanLfsBlob(db, input));
62  		}
63  		return new LfsInputStream(input, length);
64  	}
65  
66  	@Override
67  	@Nullable
68  	public PrePushHook getPrePushHook(Repository repo,
69  			PrintStream outputStream) {
70  		if (isEnabled(repo)) {
71  			return new LfsPrePushHook(repo, outputStream);
72  		}
73  		return null;
74  	}
75  
76  	@Override
77  	@Nullable
78  	public PrePushHook getPrePushHook(Repository repo, PrintStream outputStream,
79  			PrintStream errorStream) {
80  		if (isEnabled(repo)) {
81  			return new LfsPrePushHook(repo, outputStream, errorStream);
82  		}
83  		return null;
84  	}
85  
86  	/**
87  	 * @param db
88  	 *            the repository
89  	 * @return whether LFS is requested for the given repo.
90  	 */
91  	@Override
92  	public boolean isEnabled(Repository db) {
93  		if (db == null) {
94  			return false;
95  		}
96  		return db.getConfig().getBoolean(ConfigConstants.CONFIG_FILTER_SECTION,
97  				ConfigConstants.CONFIG_SECTION_LFS,
98  				ConfigConstants.CONFIG_KEY_USEJGITBUILTIN,
99  				false);
100 	}
101 
102 	/**
103 	 * @param db
104 	 *            the repository
105 	 * @param attribute
106 	 *            the attribute to check
107 	 * @return whether LFS filter is enabled for the given .gitattribute
108 	 *         attribute.
109 	 */
110 	private boolean isEnabled(Repository db, Attribute attribute) {
111 		if (attribute == null) {
112 			return false;
113 		}
114 		return isEnabled(db) && ConfigConstants.CONFIG_SECTION_LFS
115 				.equals(attribute.getValue());
116 	}
117 
118 	@Override
119 	public LfsInstallCommand getInstallCommand() {
120 		return new InstallBuiltinLfsCommand();
121 	}
122 
123 }