GitCloneTask.java

  1. /*
  2.  * Copyright (C) 2011, Ketan Padegaonkar <KetanPadegaonkar@gmail.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.ant.tasks;

  11. import java.io.File;

  12. import org.apache.tools.ant.BuildException;
  13. import org.apache.tools.ant.Project;
  14. import org.apache.tools.ant.Task;
  15. import org.eclipse.jgit.api.CloneCommand;
  16. import org.eclipse.jgit.api.Git;
  17. import org.eclipse.jgit.api.errors.GitAPIException;
  18. import org.eclipse.jgit.api.errors.JGitInternalException;
  19. import org.eclipse.jgit.lib.Constants;
  20. import org.eclipse.jgit.transport.URIish;

  21. /**
  22.  * Clone a repository into a new directory.
  23.  *
  24.  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html"
  25.  *      >git-clone(1)</a>
  26.  */
  27. public class GitCloneTask extends Task {

  28.     private String uri;
  29.     private File destination;
  30.     private boolean bare;
  31.     private String branch = Constants.HEAD;

  32.     /**
  33.      * Set the <code>uri</code>.
  34.      *
  35.      * @param uri
  36.      *            the uri to clone from
  37.      */
  38.     public void setUri(String uri) {
  39.         this.uri = uri;
  40.     }

  41.     /**
  42.      * The optional directory associated with the clone operation. If the
  43.      * directory isn't set, a name associated with the source uri will be used.
  44.      *
  45.      * @see URIish#getHumanishName()
  46.      * @param destination
  47.      *            the directory to clone to
  48.      */
  49.     public void setDest(File destination) {
  50.         this.destination = destination;
  51.     }

  52.     /**
  53.      * Set <code>bare</code>
  54.      *
  55.      * @param bare
  56.      *            whether the cloned repository is bare or not
  57.      */
  58.     public void setBare(boolean bare) {
  59.         this.bare = bare;
  60.     }

  61.     /**
  62.      * Set the <code>branch</code>
  63.      *
  64.      * @param branch
  65.      *            the initial branch to check out when cloning the repository
  66.      */
  67.     public void setBranch(String branch) {
  68.         this.branch = branch;
  69.     }

  70.     /** {@inheritDoc} */
  71.     @Override
  72.     public void execute() throws BuildException {
  73.         log("Cloning repository " + uri);

  74.         CloneCommand clone = Git.cloneRepository();
  75.         try {
  76.             clone.setURI(uri).setDirectory(destination).setBranch(branch).setBare(bare);
  77.             clone.call().getRepository().close();
  78.         } catch (GitAPIException | JGitInternalException e) {
  79.             log("Could not clone repository: " + e, e, Project.MSG_ERR);
  80.             throw new BuildException("Could not clone repository: " + e.getMessage(), e);
  81.         }
  82.     }
  83. }