LfsFactory.java

  1. /*
  2.  * Copyright (C) 2018, 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.util;

  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.PrintStream;
  14. import java.text.MessageFormat;
  15. import java.util.concurrent.Callable;

  16. import org.eclipse.jgit.annotations.Nullable;
  17. import org.eclipse.jgit.attributes.Attribute;
  18. import org.eclipse.jgit.attributes.Attributes;
  19. import org.eclipse.jgit.hooks.PrePushHook;
  20. import org.eclipse.jgit.internal.JGitText;
  21. import org.eclipse.jgit.lib.ObjectLoader;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.revwalk.RevCommit;
  24. import org.eclipse.jgit.treewalk.FileTreeIterator;
  25. import org.eclipse.jgit.treewalk.TreeWalk;
  26. import org.eclipse.jgit.treewalk.filter.PathFilter;

  27. /**
  28.  * Represents an optionally present LFS support implementation
  29.  *
  30.  * @since 4.11
  31.  */
  32. public class LfsFactory {

  33.     private static LfsFactory instance = new LfsFactory();

  34.     /**
  35.      * Constructor
  36.      */
  37.     protected LfsFactory() {
  38.     }

  39.     /**
  40.      * @return the current LFS implementation
  41.      */
  42.     public static LfsFactory getInstance() {
  43.         return instance;
  44.     }

  45.     /**
  46.      * @param instance
  47.      *            register a {@link LfsFactory} instance as the
  48.      *            {@link LfsFactory} implementation to use.
  49.      */
  50.     public static void setInstance(LfsFactory instance) {
  51.         LfsFactory.instance = instance;
  52.     }

  53.     /**
  54.      * @return whether LFS support is available
  55.      */
  56.     public boolean isAvailable() {
  57.         return false;
  58.     }

  59.     /**
  60.      * Apply clean filtering to the given stream, writing the file content to
  61.      * the LFS storage if required and returning a stream to the LFS pointer
  62.      * instead.
  63.      *
  64.      * @param db
  65.      *            the repository
  66.      * @param input
  67.      *            the original input
  68.      * @param length
  69.      *            the expected input stream length
  70.      * @param attribute
  71.      *            the attribute used to check for LFS enablement (i.e. "merge",
  72.      *            "diff", "filter" from .gitattributes).
  73.      * @return a stream to the content that should be written to the object
  74.      *         store along with the expected length of the stream. the original
  75.      *         stream is not applicable.
  76.      * @throws IOException
  77.      *             in case of an error
  78.      */
  79.     public LfsInputStream applyCleanFilter(Repository db,
  80.             InputStream input, long length, Attribute attribute)
  81.             throws IOException {
  82.         return new LfsInputStream(input, length);
  83.     }

  84.     /**
  85.      * Apply smudge filtering to a given loader, potentially redirecting it to a
  86.      * LFS blob which is downloaded on demand.
  87.      *
  88.      * @param db
  89.      *            the repository
  90.      * @param loader
  91.      *            the loader for the blob
  92.      * @param attribute
  93.      *            the attribute used to check for LFS enablement (i.e. "merge",
  94.      *            "diff", "filter" from .gitattributes).
  95.      * @return a loader for the actual data of a blob, or the original loader in
  96.      *         case LFS is not applicable.
  97.      * @throws IOException
  98.      */
  99.     public ObjectLoader applySmudgeFilter(Repository db,
  100.             ObjectLoader loader, Attribute attribute) throws IOException {
  101.         return loader;
  102.     }

  103.     /**
  104.      * Retrieve a pre-push hook to be applied using the default error stream.
  105.      *
  106.      * @param repo
  107.      *            the {@link Repository} the hook is applied to.
  108.      * @param outputStream
  109.      * @return a {@link PrePushHook} implementation or <code>null</code>
  110.      */
  111.     @Nullable
  112.     public PrePushHook getPrePushHook(Repository repo,
  113.             PrintStream outputStream) {
  114.         return null;
  115.     }

  116.     /**
  117.      * Retrieve a pre-push hook to be applied.
  118.      *
  119.      * @param repo
  120.      *            the {@link Repository} the hook is applied to.
  121.      * @param outputStream
  122.      * @param errorStream
  123.      * @return a {@link PrePushHook} implementation or <code>null</code>
  124.      * @since 5.6
  125.      */
  126.     @Nullable
  127.     public PrePushHook getPrePushHook(Repository repo, PrintStream outputStream,
  128.             PrintStream errorStream) {
  129.         return getPrePushHook(repo, outputStream);
  130.     }

  131.     /**
  132.      * Retrieve an {@link LfsInstallCommand} which can be used to enable LFS
  133.      * support (if available) either per repository or for the user.
  134.      *
  135.      * @return a command to install LFS support.
  136.      */
  137.     @Nullable
  138.     public LfsInstallCommand getInstallCommand() {
  139.         return null;
  140.     }

  141.     /**
  142.      * @param db
  143.      *            the repository to check
  144.      * @return whether LFS is enabled for the given repository locally or
  145.      *         globally.
  146.      */
  147.     public boolean isEnabled(Repository db) {
  148.         return false;
  149.     }

  150.     /**
  151.      * @param db
  152.      *            the repository
  153.      * @param path
  154.      *            the path to find attributes for
  155.      * @return the {@link Attributes} for the given path.
  156.      * @throws IOException
  157.      *             in case of an error
  158.      */
  159.     public static Attributes getAttributesForPath(Repository db, String path)
  160.             throws IOException {
  161.         try (TreeWalk walk = new TreeWalk(db)) {
  162.             walk.addTree(new FileTreeIterator(db));
  163.             PathFilter f = PathFilter.create(path);
  164.             walk.setFilter(f);
  165.             walk.setRecursive(false);
  166.             Attributes attr = null;
  167.             while (walk.next()) {
  168.                 if (f.isDone(walk)) {
  169.                     attr = walk.getAttributes();
  170.                     break;
  171.                 } else if (walk.isSubtree()) {
  172.                     walk.enterSubtree();
  173.                 }
  174.             }
  175.             if (attr == null) {
  176.                 throw new IOException(MessageFormat
  177.                         .format(JGitText.get().noPathAttributesFound, path));
  178.             }

  179.             return attr;
  180.         }
  181.     }

  182.     /**
  183.      * Get attributes for given path and commit
  184.      *
  185.      * @param db
  186.      *            the repository
  187.      * @param path
  188.      *            the path to find attributes for
  189.      * @param commit
  190.      *            the commit to inspect.
  191.      * @return the {@link Attributes} for the given path.
  192.      * @throws IOException
  193.      *             in case of an error
  194.      */
  195.     public static Attributes getAttributesForPath(Repository db, String path,
  196.             RevCommit commit) throws IOException {
  197.         if (commit == null) {
  198.             return getAttributesForPath(db, path);
  199.         }

  200.         try (TreeWalk walk = TreeWalk.forPath(db, path, commit.getTree())) {
  201.             Attributes attr = walk == null ? null : walk.getAttributes();
  202.             if (attr == null) {
  203.                 throw new IOException(MessageFormat
  204.                         .format(JGitText.get().noPathAttributesFound, path));
  205.             }

  206.             return attr;
  207.         }
  208.     }

  209.     /**
  210.      * Encapsulate a potentially exchanged {@link InputStream} along with the
  211.      * expected stream content length.
  212.      */
  213.     public static final class LfsInputStream extends InputStream {
  214.         /**
  215.          * The actual stream.
  216.          */
  217.         private InputStream stream;

  218.         /**
  219.          * The expected stream content length.
  220.          */
  221.         private long length;

  222.         /**
  223.          * Create a new wrapper around a certain stream
  224.          *
  225.          * @param stream
  226.          *            the stream to wrap. the stream will be closed on
  227.          *            {@link #close()}.
  228.          * @param length
  229.          *            the expected length of the stream
  230.          */
  231.         public LfsInputStream(InputStream stream, long length) {
  232.             this.stream = stream;
  233.             this.length = length;
  234.         }

  235.         /**
  236.          * Create a new wrapper around a temporary buffer.
  237.          *
  238.          * @param buffer
  239.          *            the buffer to initialize stream and length from. The
  240.          *            buffer will be destroyed on {@link #close()}
  241.          * @throws IOException
  242.          *             in case of an error opening the stream to the buffer.
  243.          */
  244.         public LfsInputStream(TemporaryBuffer buffer) throws IOException {
  245.             this.stream = buffer.openInputStreamWithAutoDestroy();
  246.             this.length = buffer.length();
  247.         }

  248.         @Override
  249.         public void close() throws IOException {
  250.             stream.close();
  251.         }

  252.         @Override
  253.         public int read() throws IOException {
  254.             return stream.read();
  255.         }

  256.         @Override
  257.         public int read(byte[] b, int off, int len) throws IOException {
  258.             return stream.read(b, off, len);
  259.         }

  260.         /**
  261.          * @return the length of the stream
  262.          */
  263.         public long getLength() {
  264.             return length;
  265.         }
  266.     }

  267.     /**
  268.      * A command to enable LFS. Optionally set a {@link Repository} to enable
  269.      * locally on the repository only.
  270.      */
  271.     public interface LfsInstallCommand extends Callable<Void> {
  272.         /**
  273.          * @param repo
  274.          *            the repository to enable support for.
  275.          * @return The {@link LfsInstallCommand} for chaining.
  276.          */
  277.         public LfsInstallCommand setRepository(Repository repo);
  278.     }

  279. }