TransportBundleFile.java

  1. /*
  2.  * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
  3.  * Copyright (C) 2008, Google Inc.
  4.  * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  5.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  6.  *
  7.  * This program and the accompanying materials are made available under the
  8.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  9.  * https://www.eclipse.org/org/documents/edl-v10.php.
  10.  *
  11.  * SPDX-License-Identifier: BSD-3-Clause
  12.  */

  13. package org.eclipse.jgit.transport;

  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileNotFoundException;
  17. import java.io.InputStream;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.LinkedHashSet;
  21. import java.util.Set;

  22. import org.eclipse.jgit.errors.NotSupportedException;
  23. import org.eclipse.jgit.errors.TransportException;
  24. import org.eclipse.jgit.internal.JGitText;
  25. import org.eclipse.jgit.lib.Repository;
  26. import org.eclipse.jgit.util.FS;

  27. class TransportBundleFile extends Transport implements TransportBundle {
  28.     static final TransportProtocol PROTO_BUNDLE = new TransportProtocol() {
  29.         private final String[] schemeNames = { "bundle", "file" }; //$NON-NLS-1$ //$NON-NLS-2$

  30.         private final Set<String> schemeSet = Collections
  31.                 .unmodifiableSet(new LinkedHashSet<>(Arrays
  32.                         .asList(schemeNames)));

  33.         @Override
  34.         public String getName() {
  35.             return JGitText.get().transportProtoBundleFile;
  36.         }

  37.         @Override
  38.         public Set<String> getSchemes() {
  39.             return schemeSet;
  40.         }

  41.         @Override
  42.         public boolean canHandle(URIish uri, Repository local, String remoteName) {
  43.             if (uri.getPath() == null
  44.                     || uri.getPort() > 0
  45.                     || uri.getUser() != null
  46.                     || uri.getPass() != null
  47.                     || uri.getHost() != null
  48.                     || (uri.getScheme() != null && !getSchemes().contains(uri.getScheme())))
  49.                 return false;
  50.             return true;
  51.         }

  52.         @Override
  53.         public Transport open(URIish uri, Repository local, String remoteName)
  54.                 throws NotSupportedException, TransportException {
  55.             if ("bundle".equals(uri.getScheme())) { //$NON-NLS-1$
  56.                 File path = FS.DETECTED.resolve(new File("."), uri.getPath()); //$NON-NLS-1$
  57.                 return new TransportBundleFile(local, uri, path);
  58.             }

  59.             // This is an ambiguous reference, it could be a bundle file
  60.             // or it could be a Git repository. Allow TransportLocal to
  61.             // resolve the path and figure out which type it is by testing
  62.             // the target.
  63.             //
  64.             return TransportLocal.PROTO_LOCAL.open(uri, local, remoteName);
  65.         }

  66.         @Override
  67.         public Transport open(URIish uri) throws NotSupportedException,
  68.                 TransportException {
  69.             if ("bundle".equals(uri.getScheme())) { //$NON-NLS-1$
  70.                 File path = FS.DETECTED.resolve(new File("."), uri.getPath()); //$NON-NLS-1$
  71.                 return new TransportBundleFile(uri, path);
  72.             }
  73.             return TransportLocal.PROTO_LOCAL.open(uri);
  74.         }
  75.     };

  76.     private final File bundle;

  77.     TransportBundleFile(Repository local, URIish uri, File bundlePath) {
  78.         super(local, uri);
  79.         bundle = bundlePath;
  80.     }

  81.     /**
  82.      * Constructor for TransportBundleFile.
  83.      *
  84.      * @param uri
  85.      *            a {@link org.eclipse.jgit.transport.URIish} object.
  86.      * @param bundlePath
  87.      *            transport bundle path
  88.      */
  89.     public TransportBundleFile(URIish uri, File bundlePath) {
  90.         super(uri);
  91.         bundle = bundlePath;
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public FetchConnection openFetch() throws NotSupportedException,
  96.             TransportException {
  97.         final InputStream src;
  98.         try {
  99.             src = new FileInputStream(bundle);
  100.         } catch (FileNotFoundException err) {
  101.             TransportException te = new TransportException(uri,
  102.                     JGitText.get().notFound);
  103.             te.initCause(err);
  104.             throw te;
  105.         }
  106.         return new BundleFetchConnection(this, src);
  107.     }

  108.     /** {@inheritDoc} */
  109.     @Override
  110.     public PushConnection openPush() throws NotSupportedException {
  111.         throw new NotSupportedException(
  112.                 JGitText.get().pushIsNotSupportedForBundleTransport);
  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     public void close() {
  117.         // Resources must be established per-connection.
  118.     }

  119. }