xref: /JGit/org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitInitTask.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
1fdeb3272SKetan Padegaonkar /*
2*5c5f7c6bSMatthias Sohn  * Copyright (C) 2011, Ketan Padegaonkar <KetanPadegaonkar@gmail.com> and others
3fdeb3272SKetan 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.
7fdeb3272SKetan Padegaonkar  *
8*5c5f7c6bSMatthias Sohn  * SPDX-License-Identifier: BSD-3-Clause
9fdeb3272SKetan Padegaonkar  */
10fdeb3272SKetan Padegaonkar package org.eclipse.jgit.ant.tasks;
11fdeb3272SKetan Padegaonkar 
12fdeb3272SKetan Padegaonkar import java.io.File;
13fdeb3272SKetan Padegaonkar 
14fdeb3272SKetan Padegaonkar import org.apache.tools.ant.BuildException;
15fdeb3272SKetan Padegaonkar import org.apache.tools.ant.Task;
16fdeb3272SKetan Padegaonkar import org.eclipse.jgit.api.Git;
17fdeb3272SKetan Padegaonkar import org.eclipse.jgit.api.InitCommand;
18fdeb3272SKetan Padegaonkar 
19dce5f96cSKetan Padegaonkar /**
20dce5f96cSKetan Padegaonkar  * Create an empty git repository.
21dce5f96cSKetan Padegaonkar  *
22dce5f96cSKetan Padegaonkar  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html"
23dce5f96cSKetan Padegaonkar  *      >git-init(1)</a>
24dce5f96cSKetan Padegaonkar  */
25fdeb3272SKetan Padegaonkar public class GitInitTask extends Task {
26fdeb3272SKetan Padegaonkar 	private File destination;
27fdeb3272SKetan Padegaonkar 	private boolean bare;
28fdeb3272SKetan Padegaonkar 
29fdeb3272SKetan Padegaonkar 	/**
30dce5f96cSKetan Padegaonkar 	 * Set the destination git repository.
31dce5f96cSKetan Padegaonkar 	 *
32fdeb3272SKetan Padegaonkar 	 * @param dest
33dce5f96cSKetan Padegaonkar 	 *            the destination directory that should be initialized with the
34dce5f96cSKetan Padegaonkar 	 *            git repository.
35fdeb3272SKetan Padegaonkar 	 */
setDest(File dest)36fdeb3272SKetan Padegaonkar 	public void setDest(File dest) {
37fdeb3272SKetan Padegaonkar 		this.destination = dest;
38fdeb3272SKetan Padegaonkar 	}
39fdeb3272SKetan Padegaonkar 
40fdeb3272SKetan Padegaonkar 	/**
41c7093f5cSMatthias Sohn 	 * Configure if the repository should be <code>bare</code>
42c7093f5cSMatthias Sohn 	 *
43fdeb3272SKetan Padegaonkar 	 * @param bare
44dce5f96cSKetan Padegaonkar 	 *            whether the repository should be initialized to a bare
45dce5f96cSKetan Padegaonkar 	 *            repository or not.
46fdeb3272SKetan Padegaonkar 	 */
setBare(boolean bare)47fdeb3272SKetan Padegaonkar 	public void setBare(boolean bare) {
48fdeb3272SKetan Padegaonkar 		this.bare = bare;
49fdeb3272SKetan Padegaonkar 	}
50fdeb3272SKetan Padegaonkar 
51c7093f5cSMatthias Sohn 	/** {@inheritDoc} */
52fdeb3272SKetan Padegaonkar 	@Override
execute()53fdeb3272SKetan Padegaonkar 	public void execute() throws BuildException {
54fdeb3272SKetan Padegaonkar 		if (bare) {
55fdeb3272SKetan Padegaonkar 			log("Initializing bare repository at " + destination);
56fdeb3272SKetan Padegaonkar 		} else {
57fdeb3272SKetan Padegaonkar 			log("Initializing repository at " + destination);
58fdeb3272SKetan Padegaonkar 		}
5904df086dSKetan Padegaonkar 		try {
60fdeb3272SKetan Padegaonkar 			InitCommand init = Git.init();
61fdeb3272SKetan Padegaonkar 			init.setBare(bare).setDirectory(destination);
62fdeb3272SKetan Padegaonkar 			init.call();
634e1454deSRobin Rosenberg 		} catch (Exception e) {
6404df086dSKetan Padegaonkar 			throw new BuildException("Could not initialize repository", e);
6504df086dSKetan Padegaonkar 		}
66fdeb3272SKetan Padegaonkar 	}
67fdeb3272SKetan Padegaonkar }
68