xref: /JGit/org.eclipse.jgit.ant/src/org/eclipse/jgit/ant/tasks/GitCheckoutTask.java (revision 5c5f7c6b146b24f2bd4afae1902df85ad6e57ea3)
1130ecaadSKetan Padegaonkar /*
2*5c5f7c6bSMatthias Sohn  * Copyright (C) 2011, Ketan Padegaonkar <KetanPadegaonkar@gmail.com> and others
3130ecaadSKetan 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.
7130ecaadSKetan Padegaonkar  *
8*5c5f7c6bSMatthias Sohn  * SPDX-License-Identifier: BSD-3-Clause
9130ecaadSKetan Padegaonkar  */
10130ecaadSKetan Padegaonkar package org.eclipse.jgit.ant.tasks;
11130ecaadSKetan Padegaonkar 
12130ecaadSKetan Padegaonkar import java.io.File;
13130ecaadSKetan Padegaonkar import java.io.IOException;
14130ecaadSKetan Padegaonkar 
15130ecaadSKetan Padegaonkar import org.apache.tools.ant.BuildException;
16130ecaadSKetan Padegaonkar import org.apache.tools.ant.Task;
17130ecaadSKetan Padegaonkar import org.eclipse.jgit.api.CheckoutCommand;
18130ecaadSKetan Padegaonkar import org.eclipse.jgit.api.Git;
19130ecaadSKetan Padegaonkar import org.eclipse.jgit.lib.Repository;
20130ecaadSKetan Padegaonkar import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
21130ecaadSKetan Padegaonkar 
22130ecaadSKetan Padegaonkar /**
23130ecaadSKetan Padegaonkar  * Checkout a branch or paths to the working tree.
24130ecaadSKetan Padegaonkar  *
25130ecaadSKetan Padegaonkar  * @see <a
26130ecaadSKetan Padegaonkar  *      href="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html"
27130ecaadSKetan Padegaonkar  *      >git-checkout(1)</a>
28130ecaadSKetan Padegaonkar  */
29130ecaadSKetan Padegaonkar public class GitCheckoutTask extends Task {
30130ecaadSKetan Padegaonkar 
31130ecaadSKetan Padegaonkar 	private File src;
32130ecaadSKetan Padegaonkar 	private String branch;
33130ecaadSKetan Padegaonkar 	private boolean createBranch;
34130ecaadSKetan Padegaonkar 	private boolean force;
35130ecaadSKetan Padegaonkar 
36130ecaadSKetan Padegaonkar 	/**
37c7093f5cSMatthias Sohn 	 * Set the <code>src</code>
38c7093f5cSMatthias Sohn 	 *
39130ecaadSKetan Padegaonkar 	 * @param src
40130ecaadSKetan Padegaonkar 	 *            the src to set
41130ecaadSKetan Padegaonkar 	 */
setSrc(File src)42130ecaadSKetan Padegaonkar 	public void setSrc(File src) {
43130ecaadSKetan Padegaonkar 		this.src = src;
44130ecaadSKetan Padegaonkar 	}
45130ecaadSKetan Padegaonkar 
46130ecaadSKetan Padegaonkar 	/**
47c7093f5cSMatthias Sohn 	 * Set <code>branch</code>
48c7093f5cSMatthias Sohn 	 *
49130ecaadSKetan Padegaonkar 	 * @param branch
50130ecaadSKetan Padegaonkar 	 *            the initial branch to check out
51130ecaadSKetan Padegaonkar 	 */
setBranch(String branch)52130ecaadSKetan Padegaonkar 	public void setBranch(String branch) {
53130ecaadSKetan Padegaonkar 		this.branch = branch;
54130ecaadSKetan Padegaonkar 	}
55130ecaadSKetan Padegaonkar 
56130ecaadSKetan Padegaonkar 	/**
57c7093f5cSMatthias Sohn 	 * Set if branch should be created if not yet existing
58c7093f5cSMatthias Sohn 	 *
59130ecaadSKetan Padegaonkar 	 * @param createBranch
60130ecaadSKetan Padegaonkar 	 *            whether the branch should be created if it does not already
61130ecaadSKetan Padegaonkar 	 *            exist
62130ecaadSKetan Padegaonkar 	 */
setCreateBranch(boolean createBranch)63130ecaadSKetan Padegaonkar 	public void setCreateBranch(boolean createBranch) {
64130ecaadSKetan Padegaonkar 		this.createBranch = createBranch;
65130ecaadSKetan Padegaonkar 	}
66130ecaadSKetan Padegaonkar 
67130ecaadSKetan Padegaonkar 	/**
68c7093f5cSMatthias Sohn 	 * Set <code>force</code>
69c7093f5cSMatthias Sohn 	 *
70130ecaadSKetan Padegaonkar 	 * @param force
71130ecaadSKetan Padegaonkar 	 *            if <code>true</code> and the branch with the given name
72130ecaadSKetan Padegaonkar 	 *            already exists, the start-point of an existing branch will be
73130ecaadSKetan Padegaonkar 	 *            set to a new start-point; if false, the existing branch will
74130ecaadSKetan Padegaonkar 	 *            not be changed
75130ecaadSKetan Padegaonkar 	 */
setForce(boolean force)76130ecaadSKetan Padegaonkar 	public void setForce(boolean force) {
77130ecaadSKetan Padegaonkar 		this.force = force;
78130ecaadSKetan Padegaonkar 	}
79130ecaadSKetan Padegaonkar 
80c7093f5cSMatthias Sohn 	/** {@inheritDoc} */
81130ecaadSKetan Padegaonkar 	@Override
execute()82130ecaadSKetan Padegaonkar 	public void execute() throws BuildException {
83130ecaadSKetan Padegaonkar 		CheckoutCommand checkout;
8458e29086SDavid Pursehouse 		try (Repository repo = new FileRepositoryBuilder().readEnvironment()
85130ecaadSKetan Padegaonkar 				.findGitDir(src).build();
8658e29086SDavid Pursehouse 			Git git = new Git(repo)) {
8758e29086SDavid Pursehouse 			checkout = git.checkout();
88130ecaadSKetan Padegaonkar 		} catch (IOException e) {
89130ecaadSKetan Padegaonkar 			throw new BuildException("Could not access repository " + src, e);
90130ecaadSKetan Padegaonkar 		}
91130ecaadSKetan Padegaonkar 
92130ecaadSKetan Padegaonkar 		try {
93e406d500SChristian Halstrick 			checkout.setCreateBranch(createBranch).setForceRefUpdate(force)
94130ecaadSKetan Padegaonkar 					.setName(branch);
95130ecaadSKetan Padegaonkar 			checkout.call();
96130ecaadSKetan Padegaonkar 		} catch (Exception e) {
97130ecaadSKetan Padegaonkar 			throw new BuildException("Could not checkout repository " + src, e);
98130ecaadSKetan Padegaonkar 		}
99130ecaadSKetan Padegaonkar 	}
100130ecaadSKetan Padegaonkar 
101130ecaadSKetan Padegaonkar }
102