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 12 import java.io.File; 13 14 import org.apache.tools.ant.BuildException; 15 import org.apache.tools.ant.Project; 16 import org.apache.tools.ant.Task; 17 import org.eclipse.jgit.api.CloneCommand; 18 import org.eclipse.jgit.api.Git; 19 import org.eclipse.jgit.api.errors.GitAPIException; 20 import org.eclipse.jgit.api.errors.JGitInternalException; 21 import org.eclipse.jgit.lib.Constants; 22 import org.eclipse.jgit.transport.URIish; 23 24 /** 25 * Clone a repository into a new directory. 26 * 27 * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html" 28 * >git-clone(1)</a> 29 */ 30 public class GitCloneTask extends Task { 31 32 private String uri; 33 private File destination; 34 private boolean bare; 35 private String branch = Constants.HEAD; 36 37 /** 38 * Set the <code>uri</code>. 39 * 40 * @param uri 41 * the uri to clone from 42 */ setUri(String uri)43 public void setUri(String uri) { 44 this.uri = uri; 45 } 46 47 /** 48 * The optional directory associated with the clone operation. If the 49 * directory isn't set, a name associated with the source uri will be used. 50 * 51 * @see URIish#getHumanishName() 52 * @param destination 53 * the directory to clone to 54 */ setDest(File destination)55 public void setDest(File destination) { 56 this.destination = destination; 57 } 58 59 /** 60 * Set <code>bare</code> 61 * 62 * @param bare 63 * whether the cloned repository is bare or not 64 */ setBare(boolean bare)65 public void setBare(boolean bare) { 66 this.bare = bare; 67 } 68 69 /** 70 * Set the <code>branch</code> 71 * 72 * @param branch 73 * the initial branch to check out when cloning the repository 74 */ setBranch(String branch)75 public void setBranch(String branch) { 76 this.branch = branch; 77 } 78 79 /** {@inheritDoc} */ 80 @Override execute()81 public void execute() throws BuildException { 82 log("Cloning repository " + uri); 83 84 CloneCommand clone = Git.cloneRepository(); 85 try { 86 clone.setURI(uri).setDirectory(destination).setBranch(branch).setBare(bare); 87 clone.call().getRepository().close(); 88 } catch (GitAPIException | JGitInternalException e) { 89 log("Could not clone repository: " + e, e, Project.MSG_ERR); 90 throw new BuildException("Could not clone repository: " + e.getMessage(), e); 91 } 92 } 93 } 94