Browse Source

Add configuration parameter to enable built-in hooks/filters

If the configuration parameter filter.<filterDriverName>.useJGitBuiltin
is set to true then for all corresponding filters JGit will try to
execute the built-in filter instead of the filter-command which is
defined in git configuration. It will fallback to the non-built-in
filters if no built-in filters are registered or if constructing them
leads to exceptions. If set to false JGit will not try to execute
built-in filters for the specified filter driver.

Example: The configuration contains the following lines

  [filter "lfs"]
    clean = git-lfs clean -- %f
    smudge = git-lfs smudge -- %f
    useJGitBuiltin = true

Addtionally the .gitattributes file in the root of the working tree
contains:

    *.bin filter=lfs

In this case when new content is added similar to "git add 1.bin" then
the following will happen:

  - jgit will check whether a built-in command factory was registered
    for the command "jgit://builtin/lfs/clean". If that is true the
    factory is used to create a built-in filter command and that
    command is used to filter the content
  - Otherwise jgit will call the external program "git lfs clean ..."
    to do the filtering

Change-Id: Idadb1db06b1e89e7031d7ed6319904973c367d38
stable-4.6
Christian Halstrick 9 years ago committed by Matthias Sohn
parent
commit
b70f3a7457
  1. 9
      org.eclipse.jgit/src/org/eclipse/jgit/attributes/FilterCommandRegistry.java
  2. 7
      org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java
  3. 18
      org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java

9
org.eclipse.jgit/src/org/eclipse/jgit/attributes/FilterCommandRegistry.java

@ -45,6 +45,7 @@ package org.eclipse.jgit.attributes;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
@ -102,6 +103,14 @@ public class FilterCommandRegistry {
return filterCommandRegistry.containsKey(filterCommandName); return filterCommandRegistry.containsKey(filterCommandName);
} }
/**
* @return Set of commandNames for which a {@link FilterCommandFactory} is
* registered
*/
public static Set<String> getRegisteredFilterCommands() {
return filterCommandRegistry.keySet();
}
/** /**
* Creates a new {@link FilterCommand} for the given name. A factory must be * Creates a new {@link FilterCommand} for the given name. A factory must be
* registered for the name in advance. * registered for the name in advance.

7
org.eclipse.jgit/src/org/eclipse/jgit/lib/Constants.java

@ -391,6 +391,13 @@ public final class Constants {
*/ */
public static final String ATTR_FILTER_TYPE_SMUDGE = "smudge"; public static final String ATTR_FILTER_TYPE_SMUDGE = "smudge";
/**
* Whether to use JGit's implementations of filters and hooks
*
* @since 4.6
*/
public static final String ATTR_FILTER_USE_BUILTIN = "useJGitBuiltin";
/** /**
* Builtin filter commands start with this prefix * Builtin filter commands start with this prefix
* *

18
org.eclipse.jgit/src/org/eclipse/jgit/treewalk/TreeWalk.java

@ -55,6 +55,7 @@ import org.eclipse.jgit.attributes.Attribute;
import org.eclipse.jgit.attributes.Attributes; import org.eclipse.jgit.attributes.Attributes;
import org.eclipse.jgit.attributes.AttributesNodeProvider; import org.eclipse.jgit.attributes.AttributesNodeProvider;
import org.eclipse.jgit.attributes.AttributesProvider; import org.eclipse.jgit.attributes.AttributesProvider;
import org.eclipse.jgit.attributes.FilterCommandRegistry;
import org.eclipse.jgit.dircache.DirCacheBuildIterator; import org.eclipse.jgit.dircache.DirCacheBuildIterator;
import org.eclipse.jgit.attributes.AttributesHandler; import org.eclipse.jgit.attributes.AttributesHandler;
import org.eclipse.jgit.dircache.DirCacheIterator; import org.eclipse.jgit.dircache.DirCacheIterator;
@ -313,6 +314,8 @@ public class TreeWalk implements AutoCloseable, AttributesProvider {
private Config config; private Config config;
private Set<String> filterCommands;
/** /**
* Create a new tree walker for a given repository. * Create a new tree walker for a given repository.
* *
@ -357,6 +360,8 @@ public class TreeWalk implements AutoCloseable, AttributesProvider {
if (repo != null) { if (repo != null) {
config = repo.getConfig(); config = repo.getConfig();
attributesNodeProvider = repo.createAttributesNodeProvider(); attributesNodeProvider = repo.createAttributesNodeProvider();
filterCommands = FilterCommandRegistry
.getRegisteredFilterCommands();
} else { } else {
config = null; config = null;
attributesNodeProvider = null; attributesNodeProvider = null;
@ -1369,8 +1374,19 @@ public class TreeWalk implements AutoCloseable, AttributesProvider {
return filterCommand; return filterCommand;
filterCommand = config.getString(Constants.ATTR_FILTER, filterCommand = config.getString(Constants.ATTR_FILTER,
filterDriverName, filterCommandType); filterDriverName, filterCommandType);
if (filterCommand != null) boolean useBuiltin = config.getBoolean(Constants.ATTR_FILTER,
filterDriverName, Constants.ATTR_FILTER_USE_BUILTIN, false);
if (useBuiltin) {
String builtinFilterCommand = Constants.BUILTIN_FILTER_PREFIX
+ filterDriverName + '/' + filterCommandType;
if (filterCommands != null
&& filterCommands.contains(builtinFilterCommand)) {
filterCommand = builtinFilterCommand;
}
}
if (filterCommand != null) {
filterCommandsByNameDotType.put(key, filterCommand); filterCommandsByNameDotType.put(key, filterCommand);
}
return filterCommand; return filterCommand;
} }
} }

Loading…
Cancel
Save