View Javadoc
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  import java.io.IOException;
14  
15  import org.apache.tools.ant.BuildException;
16  import org.apache.tools.ant.Task;
17  import org.eclipse.jgit.api.CheckoutCommand;
18  import org.eclipse.jgit.api.Git;
19  import org.eclipse.jgit.lib.Repository;
20  import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
21  
22  /**
23   * Checkout a branch or paths to the working tree.
24   *
25   * @see <a
26   *      href="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html"
27   *      >git-checkout(1)</a>
28   */
29  public class GitCheckoutTask extends Task {
30  
31  	private File src;
32  	private String branch;
33  	private boolean createBranch;
34  	private boolean force;
35  
36  	/**
37  	 * Set the <code>src</code>
38  	 *
39  	 * @param src
40  	 *            the src to set
41  	 */
42  	public void setSrc(File src) {
43  		this.src = src;
44  	}
45  
46  	/**
47  	 * Set <code>branch</code>
48  	 *
49  	 * @param branch
50  	 *            the initial branch to check out
51  	 */
52  	public void setBranch(String branch) {
53  		this.branch = branch;
54  	}
55  
56  	/**
57  	 * Set if branch should be created if not yet existing
58  	 *
59  	 * @param createBranch
60  	 *            whether the branch should be created if it does not already
61  	 *            exist
62  	 */
63  	public void setCreateBranch(boolean createBranch) {
64  		this.createBranch = createBranch;
65  	}
66  
67  	/**
68  	 * Set <code>force</code>
69  	 *
70  	 * @param force
71  	 *            if <code>true</code> and the branch with the given name
72  	 *            already exists, the start-point of an existing branch will be
73  	 *            set to a new start-point; if false, the existing branch will
74  	 *            not be changed
75  	 */
76  	public void setForce(boolean force) {
77  		this.force = force;
78  	}
79  
80  	/** {@inheritDoc} */
81  	@Override
82  	public void execute() throws BuildException {
83  		CheckoutCommand checkout;
84  		try (Repository repo = new FileRepositoryBuilder().readEnvironment()
85  				.findGitDir(src).build();
86  			Git git = new Git(repo)) {
87  			checkout = git.checkout();
88  		} catch (IOException e) {
89  			throw new BuildException("Could not access repository " + src, e);
90  		}
91  
92  		try {
93  			checkout.setCreateBranch(createBranch).setForceRefUpdate(force)
94  					.setName(branch);
95  			checkout.call();
96  		} catch (Exception e) {
97  			throw new BuildException("Could not checkout repository " + src, e);
98  		}
99  	}
100 
101 }