diff options
author | Matthias Sohn <matthias.sohn@sap.com> | 2015-12-22 16:11:43 +0100 |
---|---|---|
committer | Saša Živkov <sasa.zivkov@sap.com> | 2016-02-04 17:49:43 +0100 |
commit | 3bae524f6f80f9cd9b4a29f456005b51fdaba1ee (patch) | |
tree | c0c145c9d026a5a04010dd80c90ab7fe3f6c4352 /org.eclipse.jgit.pgm/src/org | |
parent | 377616aca702144f6f79e1b376f3342ec81ec42b (diff) | |
download | jgit-3bae524f6f80f9cd9b4a29f456005b51fdaba1ee.tar.gz jgit-3bae524f6f80f9cd9b4a29f456005b51fdaba1ee.zip |
Support LFS protocol and a file system based LFS storage
Implement LfsProtocolServlet handling the "Git LFS v1 Batch API"
protocol [1]. Add a simple file system based LFS content store and the
debug-lfs-store command to simplify testing.
Introduce a LargeFileRepository interface to enable additional storage
implementation while reusing the same protocol implementation.
At the client side we have to configure the lfs.url, specify that
we use the batch API and we don't use authentication:
[lfs]
url = http://host:port/lfs
batch = true
[lfs "http://host:port/lfs"]
access = none
the git-lfs client appends the "objects/batch" to the lfs.url.
Hard code an Authorization header in the FileLfsRepository.getAction
because then git-lfs client will skip asking for credentials. It will
just forward the Authorization header from the response to the
download/upload request.
The FileLfsServlet supports file content storage for "Large File
Storage" (LFS) server as defined by the Github LFS API [2].
- upload and download of large files is probably network bound hence use
an asynchronous servlet for good scalability
- simple object storage in file system with 2 level fan-out
- use LockFile to protect writing large objects against multiple
concurrent uploads of the same object
- to prevent corrupt uploads the uploaded file is rejected if its hash
doesn't match id given in URL
The debug-lfs-store command is used to run the LfsProtocolServlet and,
optionally, the FileLfsServlet which makes it easier to setup a
local test server.
[1]
https://github.com/github/git-lfs/blob/master/docs/api/http-v1-batch.md
[2] https://github.com/github/git-lfs/tree/master/docs/api
Bug: 472961
Change-Id: I7378da5575159d2195138d799704880c5c82d5f3
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Signed-off-by: Sasa Zivkov <sasa.zivkov@sap.com>
Diffstat (limited to 'org.eclipse.jgit.pgm/src/org')
-rw-r--r-- | org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/LfsStore.java | 249 |
1 files changed, 249 insertions, 0 deletions
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/LfsStore.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/LfsStore.java new file mode 100644 index 0000000000..18398f664e --- /dev/null +++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/debug/LfsStore.java @@ -0,0 +1,249 @@ +/* + * Copyright (C) 2015, Sasa Zivkov <sasa.zivkov@sap.com> + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.eclipse.jgit.pgm.debug; + +import java.net.InetAddress; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.UnknownHostException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.eclipse.jetty.server.Connector; +import org.eclipse.jetty.server.HttpConfiguration; +import org.eclipse.jetty.server.HttpConnectionFactory; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jgit.lfs.server.LargeFileRepository; +import org.eclipse.jgit.lfs.server.LfsProtocolServlet; +import org.eclipse.jgit.lfs.server.fs.FileLfsServlet; +import org.eclipse.jgit.lfs.server.fs.FileLfsRepository; +import org.eclipse.jgit.pgm.Command; +import org.eclipse.jgit.pgm.TextBuiltin; +import org.kohsuke.args4j.Argument; +import org.kohsuke.args4j.Option; + +@Command(common = true, usage = "usage_runLfsStore") +class LfsStore extends TextBuiltin { + + /** + * Tiny web application server for testing + */ + class AppServer { + + private final Server server; + + private final ServerConnector connector; + + private final ContextHandlerCollection contexts; + + private URI uri; + + AppServer(int port) { + server = new Server(); + + HttpConfiguration http_config = new HttpConfiguration(); + http_config.setOutputBufferSize(32768); + + connector = new ServerConnector(server, + new HttpConnectionFactory(http_config)); + connector.setPort(port); + try { + String host = InetAddress.getByName("localhost") //$NON-NLS-1$ + .getHostAddress(); + connector.setHost(host); + if (host.contains(":") && !host.startsWith("[")) //$NON-NLS-1$ //$NON-NLS-2$ + host = "[" + host + "]"; //$NON-NLS-1$//$NON-NLS-2$ + uri = new URI("http://" + host + ":" + port); //$NON-NLS-1$ //$NON-NLS-2$ + } catch (UnknownHostException e) { + throw new RuntimeException("Cannot find localhost", e); //$NON-NLS-1$ + } catch (URISyntaxException e) { + throw new RuntimeException("Unexpected URI error on " + uri, e); //$NON-NLS-1$ + } + + contexts = new ContextHandlerCollection(); + server.setHandler(contexts); + server.setConnectors(new Connector[] { connector }); + } + + /** + * Create a new servlet context within the server. + * <p> + * This method should be invoked before the server is started, once for + * each context the caller wants to register. + * + * @param path + * path of the context; use "/" for the root context if + * binding to the root is desired. + * @return the context to add servlets into. + */ + ServletContextHandler addContext(String path) { + assertNotRunning(); + if ("".equals(path)) //$NON-NLS-1$ + path = "/"; //$NON-NLS-1$ + + ServletContextHandler ctx = new ServletContextHandler(); + ctx.setContextPath(path); + contexts.addHandler(ctx); + + return ctx; + } + + void start() throws Exception { + server.start(); + } + + void stop() throws Exception { + server.stop(); + } + + URI getURI() { + return uri; + } + + private void assertNotRunning() { + if (server.isRunning()) { + throw new IllegalStateException("server is running"); //$NON-NLS-1$ + } + } + } + + private static enum StoreType { + FS; + } + + private static final String OBJECTS = "objects/"; //$NON-NLS-1$ + + private static final String STORE_PATH = "/" + OBJECTS + "*"; //$NON-NLS-1$//$NON-NLS-2$ + + private static final String PROTOCOL_PATH = "/lfs/objects/batch"; //$NON-NLS-1$ + + @Option(name = "--port", aliases = {"-p" }, metaVar = "metaVar_port", + usage = "usage_LFSPort") + int port; + + @Option(name = "--store", metaVar = "metaVar_lfsStorage", usage = "usage_LFSRunStore") + StoreType storeType; + + @Option(name = "--store-url", aliases = {"-u" }, metaVar = "metaVar_url", + usage = "usage_LFSStoreUrl") + String storeUrl; + + @Argument(required = false, metaVar = "metaVar_directory", usage = "usage_LFSDirectory") + String directory; + + String protocolUrl; + + String accessKey; + + String secretKey; + + @Override + protected boolean requiresRepository() { + return false; + } + + protected void run() throws Exception { + AppServer server = new AppServer(port); + URI baseURI = server.getURI(); + ServletContextHandler app = server.addContext("/"); //$NON-NLS-1$ + + final LargeFileRepository repository; + switch (storeType) { + case FS: + Path dir = Paths.get(directory); + FileLfsRepository fsRepo = new FileLfsRepository( + getStoreUrl(baseURI), dir); + FileLfsServlet content = new FileLfsServlet(fsRepo, 30000); + app.addServlet(new ServletHolder(content), STORE_PATH); + repository = fsRepo; + break; + + default: + throw new IllegalArgumentException( + "Unknown store type: " + storeType); //$NON-NLS-1$ + } + + LfsProtocolServlet protocol = new LfsProtocolServlet() { + + private static final long serialVersionUID = 1L; + + @Override + protected LargeFileRepository getLargeFileRepository() { + return repository; + } + + }; + app.addServlet(new ServletHolder(protocol), PROTOCOL_PATH); + + server.start(); + + outw.println("LFS protocol URL: " + getProtocolUrl(baseURI)); //$NON-NLS-1$ + if (storeType == StoreType.FS) { + outw.println("LFS objects located in: " + directory); //$NON-NLS-1$ + outw.println("LFS store URL: " + getStoreUrl(baseURI)); //$NON-NLS-1$ + } + } + + private String getStoreUrl(URI baseURI) { + if (storeUrl == null) { + if (storeType == StoreType.FS) { + storeUrl = baseURI + "/" + OBJECTS; //$NON-NLS-1$ + } else { + die("Local store not running and no --store-url specified"); //$NON-NLS-1$ + } + } + return storeUrl; + } + + private String getProtocolUrl(URI baseURI) { + if (protocolUrl == null) { + protocolUrl = baseURI + PROTOCOL_PATH; + } + return protocolUrl; + } +} |