1f8b89a92SKetan Padegaonkar /* 2*5c5f7c6bSMatthias Sohn * Copyright (C) 2011, Ketan Padegaonkar <KetanPadegaonkar@gmail.com> and others 3f8b89a92SKetan Padegaonkar * 4*5c5f7c6bSMatthias Sohn * This program and the accompanying materials are made available under the 5*5c5f7c6bSMatthias Sohn * terms of the Eclipse Distribution License v. 1.0 which is available at 6*5c5f7c6bSMatthias Sohn * https://www.eclipse.org/org/documents/edl-v10.php. 7f8b89a92SKetan Padegaonkar * 8*5c5f7c6bSMatthias Sohn * SPDX-License-Identifier: BSD-3-Clause 9f8b89a92SKetan Padegaonkar */ 10f8b89a92SKetan Padegaonkar package org.eclipse.jgit.ant.tasks; 11f8b89a92SKetan Padegaonkar 12f8b89a92SKetan Padegaonkar import java.io.File; 13f8b89a92SKetan Padegaonkar 14f8b89a92SKetan Padegaonkar import org.apache.tools.ant.BuildException; 15d60001c8SKetan Padegaonkar import org.apache.tools.ant.Project; 16f8b89a92SKetan Padegaonkar import org.apache.tools.ant.Task; 17f8b89a92SKetan Padegaonkar import org.eclipse.jgit.api.CloneCommand; 18f8b89a92SKetan Padegaonkar import org.eclipse.jgit.api.Git; 19a3c0a7f9SDavid Pursehouse import org.eclipse.jgit.api.errors.GitAPIException; 20a3c0a7f9SDavid Pursehouse import org.eclipse.jgit.api.errors.JGitInternalException; 21f8b89a92SKetan Padegaonkar import org.eclipse.jgit.lib.Constants; 22f8b89a92SKetan Padegaonkar import org.eclipse.jgit.transport.URIish; 23f8b89a92SKetan Padegaonkar 24f8b89a92SKetan Padegaonkar /** 25f8b89a92SKetan Padegaonkar * Clone a repository into a new directory. 26f8b89a92SKetan Padegaonkar * 27f8b89a92SKetan Padegaonkar * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html" 28f8b89a92SKetan Padegaonkar * >git-clone(1)</a> 29f8b89a92SKetan Padegaonkar */ 30f8b89a92SKetan Padegaonkar public class GitCloneTask extends Task { 31f8b89a92SKetan Padegaonkar 32f8b89a92SKetan Padegaonkar private String uri; 33f8b89a92SKetan Padegaonkar private File destination; 34f8b89a92SKetan Padegaonkar private boolean bare; 35f8b89a92SKetan Padegaonkar private String branch = Constants.HEAD; 36f8b89a92SKetan Padegaonkar 37f8b89a92SKetan Padegaonkar /** 38c7093f5cSMatthias Sohn * Set the <code>uri</code>. 39c7093f5cSMatthias Sohn * 40f8b89a92SKetan Padegaonkar * @param uri 41f8b89a92SKetan Padegaonkar * the uri to clone from 42f8b89a92SKetan Padegaonkar */ setUri(String uri)43f8b89a92SKetan Padegaonkar public void setUri(String uri) { 44f8b89a92SKetan Padegaonkar this.uri = uri; 45f8b89a92SKetan Padegaonkar } 46f8b89a92SKetan Padegaonkar 47f8b89a92SKetan Padegaonkar /** 48f8b89a92SKetan Padegaonkar * The optional directory associated with the clone operation. If the 49f8b89a92SKetan Padegaonkar * directory isn't set, a name associated with the source uri will be used. 50f8b89a92SKetan Padegaonkar * 51f8b89a92SKetan Padegaonkar * @see URIish#getHumanishName() 52f8b89a92SKetan Padegaonkar * @param destination 53f8b89a92SKetan Padegaonkar * the directory to clone to 54f8b89a92SKetan Padegaonkar */ setDest(File destination)55f8b89a92SKetan Padegaonkar public void setDest(File destination) { 56f8b89a92SKetan Padegaonkar this.destination = destination; 57f8b89a92SKetan Padegaonkar } 58f8b89a92SKetan Padegaonkar 59f8b89a92SKetan Padegaonkar /** 60c7093f5cSMatthias Sohn * Set <code>bare</code> 61c7093f5cSMatthias Sohn * 62f8b89a92SKetan Padegaonkar * @param bare 63f8b89a92SKetan Padegaonkar * whether the cloned repository is bare or not 64f8b89a92SKetan Padegaonkar */ setBare(boolean bare)65f8b89a92SKetan Padegaonkar public void setBare(boolean bare) { 66f8b89a92SKetan Padegaonkar this.bare = bare; 67f8b89a92SKetan Padegaonkar } 68f8b89a92SKetan Padegaonkar 69f8b89a92SKetan Padegaonkar /** 70c7093f5cSMatthias Sohn * Set the <code>branch</code> 71c7093f5cSMatthias Sohn * 72f8b89a92SKetan Padegaonkar * @param branch 73f8b89a92SKetan Padegaonkar * the initial branch to check out when cloning the repository 74f8b89a92SKetan Padegaonkar */ setBranch(String branch)75f8b89a92SKetan Padegaonkar public void setBranch(String branch) { 76f8b89a92SKetan Padegaonkar this.branch = branch; 77f8b89a92SKetan Padegaonkar } 78f8b89a92SKetan Padegaonkar 79c7093f5cSMatthias Sohn /** {@inheritDoc} */ 80f8b89a92SKetan Padegaonkar @Override execute()81f8b89a92SKetan Padegaonkar public void execute() throws BuildException { 82f8b89a92SKetan Padegaonkar log("Cloning repository " + uri); 83f8b89a92SKetan Padegaonkar 84f8b89a92SKetan Padegaonkar CloneCommand clone = Git.cloneRepository(); 85f8b89a92SKetan Padegaonkar try { 86d60001c8SKetan Padegaonkar clone.setURI(uri).setDirectory(destination).setBranch(branch).setBare(bare); 8780113c7bSChristian Halstrick clone.call().getRepository().close(); 88a3c0a7f9SDavid Pursehouse } catch (GitAPIException | JGitInternalException e) { 89d60001c8SKetan Padegaonkar log("Could not clone repository: " + e, e, Project.MSG_ERR); 90f8b89a92SKetan Padegaonkar throw new BuildException("Could not clone repository: " + e.getMessage(), e); 91f8b89a92SKetan Padegaonkar } 92f8b89a92SKetan Padegaonkar } 93f8b89a92SKetan Padegaonkar } 94