xref: /JGit/org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitInitTask.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
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.Task;
16 import org.eclipse.jgit.api.Git;
17 import org.eclipse.jgit.api.InitCommand;
18 
19 /**
20  * Create an empty git repository.
21  *
22  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html"
23  *      >git-init(1)</a>
24  */
25 public class GitInitTask extends Task {
26 	private File destination;
27 	private boolean bare;
28 
29 	/**
30 	 * Set the destination git repository.
31 	 *
32 	 * @param dest
33 	 *            the destination directory that should be initialized with the
34 	 *            git repository.
35 	 */
setDest(File dest)36 	public void setDest(File dest) {
37 		this.destination = dest;
38 	}
39 
40 	/**
41 	 * Configure if the repository should be <code>bare</code>
42 	 *
43 	 * @param bare
44 	 *            whether the repository should be initialized to a bare
45 	 *            repository or not.
46 	 */
setBare(boolean bare)47 	public void setBare(boolean bare) {
48 		this.bare = bare;
49 	}
50 
51 	/** {@inheritDoc} */
52 	@Override
execute()53 	public void execute() throws BuildException {
54 		if (bare) {
55 			log("Initializing bare repository at " + destination);
56 		} else {
57 			log("Initializing repository at " + destination);
58 		}
59 		try {
60 			InitCommand init = Git.init();
61 			init.setBare(bare).setDirectory(destination);
62 			init.call();
63 		} catch (Exception e) {
64 			throw new BuildException("Could not initialize repository", e);
65 		}
66 	}
67 }
68