GitInitTask.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 org.apache.tools.ant.BuildException;
  13. import org.apache.tools.ant.Task;
  14. import org.eclipse.jgit.api.Git;
  15. import org.eclipse.jgit.api.InitCommand;

  16. /**
  17.  * Create an empty git repository.
  18.  *
  19.  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-init.html"
  20.  *      >git-init(1)</a>
  21.  */
  22. public class GitInitTask extends Task {
  23.     private File destination;
  24.     private boolean bare;

  25.     /**
  26.      * Set the destination git repository.
  27.      *
  28.      * @param dest
  29.      *            the destination directory that should be initialized with the
  30.      *            git repository.
  31.      */
  32.     public void setDest(File dest) {
  33.         this.destination = dest;
  34.     }

  35.     /**
  36.      * Configure if the repository should be <code>bare</code>
  37.      *
  38.      * @param bare
  39.      *            whether the repository should be initialized to a bare
  40.      *            repository or not.
  41.      */
  42.     public void setBare(boolean bare) {
  43.         this.bare = bare;
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public void execute() throws BuildException {
  48.         if (bare) {
  49.             log("Initializing bare repository at " + destination);
  50.         } else {
  51.             log("Initializing repository at " + destination);
  52.         }
  53.         try {
  54.             InitCommand init = Git.init();
  55.             init.setBare(bare).setDirectory(destination);
  56.             init.call();
  57.         } catch (Exception e) {
  58.             throw new BuildException("Could not initialize repository", e);
  59.         }
  60.     }
  61. }