GitCheckoutTask.java

  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. import java.io.File;
  12. import java.io.IOException;

  13. import org.apache.tools.ant.BuildException;
  14. import org.apache.tools.ant.Task;
  15. import org.eclipse.jgit.api.CheckoutCommand;
  16. import org.eclipse.jgit.api.Git;
  17. import org.eclipse.jgit.lib.Repository;
  18. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

  19. /**
  20.  * Checkout a branch or paths to the working tree.
  21.  *
  22.  * @see <a
  23.  *      href="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html"
  24.  *      >git-checkout(1)</a>
  25.  */
  26. public class GitCheckoutTask extends Task {

  27.     private File src;
  28.     private String branch;
  29.     private boolean createBranch;
  30.     private boolean force;

  31.     /**
  32.      * Set the <code>src</code>
  33.      *
  34.      * @param src
  35.      *            the src to set
  36.      */
  37.     public void setSrc(File src) {
  38.         this.src = src;
  39.     }

  40.     /**
  41.      * Set <code>branch</code>
  42.      *
  43.      * @param branch
  44.      *            the initial branch to check out
  45.      */
  46.     public void setBranch(String branch) {
  47.         this.branch = branch;
  48.     }

  49.     /**
  50.      * Set if branch should be created if not yet existing
  51.      *
  52.      * @param createBranch
  53.      *            whether the branch should be created if it does not already
  54.      *            exist
  55.      */
  56.     public void setCreateBranch(boolean createBranch) {
  57.         this.createBranch = createBranch;
  58.     }

  59.     /**
  60.      * Set <code>force</code>
  61.      *
  62.      * @param force
  63.      *            if <code>true</code> and the branch with the given name
  64.      *            already exists, the start-point of an existing branch will be
  65.      *            set to a new start-point; if false, the existing branch will
  66.      *            not be changed
  67.      */
  68.     public void setForce(boolean force) {
  69.         this.force = force;
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public void execute() throws BuildException {
  74.         CheckoutCommand checkout;
  75.         try (Repository repo = new FileRepositoryBuilder().readEnvironment()
  76.                 .findGitDir(src).build();
  77.             Git git = new Git(repo)) {
  78.             checkout = git.checkout();
  79.         } catch (IOException e) {
  80.             throw new BuildException("Could not access repository " + src, e);
  81.         }

  82.         try {
  83.             checkout.setCreateBranch(createBranch).setForceRefUpdate(force)
  84.                     .setName(branch);
  85.             checkout.call();
  86.         } catch (Exception e) {
  87.             throw new BuildException("Could not checkout repository " + src, e);
  88.         }
  89.     }

  90. }