GitAddTask.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.Project;
  15. import org.apache.tools.ant.Task;
  16. import org.apache.tools.ant.types.DirSet;
  17. import org.apache.tools.ant.types.FileSet;
  18. import org.apache.tools.ant.types.resources.Union;
  19. import org.eclipse.jgit.api.AddCommand;
  20. import org.eclipse.jgit.api.Git;
  21. import org.eclipse.jgit.api.errors.GitAPIException;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.lib.RepositoryCache;
  24. import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
  25. import org.eclipse.jgit.util.FS;

  26. /**
  27.  * Adds a file to the git index.
  28.  *
  29.  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-add.html"
  30.  *      >git-add(1)</a>
  31.  */
  32. public class GitAddTask extends Task {

  33.     private File src;
  34.     private Union path;

  35.     /**
  36.      * <p>Set the field <code>src</code>.</p>
  37.      *
  38.      * @param src
  39.      *            the src to set
  40.      */
  41.     public void setSrc(File src) {
  42.         this.src = src;
  43.     }

  44.     /**
  45.      * Add a set of files to add.
  46.      *
  47.      * @param set
  48.      *            a set of files to add.
  49.      */
  50.     public void addFileset(FileSet set) {
  51.         getPath().add(set);
  52.     }

  53.     /**
  54.      * Add a set of files to add.
  55.      *
  56.      * @param set
  57.      *            a set of files to add.
  58.      */
  59.     public void addDirset(DirSet set) {
  60.         getPath().add(set);
  61.     }

  62.     private synchronized Union getPath() {
  63.         if (path == null) {
  64.             path = new Union();
  65.             path.setProject(getProject());
  66.         }
  67.         return path;
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public void execute() throws BuildException {
  72.         if (src == null) {
  73.             throw new BuildException("Repository path not specified.");
  74.         }
  75.         if (!RepositoryCache.FileKey.isGitRepository(new File(src, ".git"),
  76.                 FS.DETECTED)) {
  77.             throw new BuildException("Specified path (" + src
  78.                     + ") is not a git repository.");
  79.         }

  80.         AddCommand gitAdd;
  81.         try (Repository repo = new FileRepositoryBuilder().readEnvironment()
  82.                 .findGitDir(src).build();
  83.             Git git = new Git(repo);) {
  84.             gitAdd = git.add();
  85.         } catch (IOException e) {
  86.             throw new BuildException("Could not access repository " + src, e);
  87.         }

  88.         try {
  89.             String prefix = src.getCanonicalPath();
  90.             String[] allFiles = getPath().list();

  91.             for (String file : allFiles) {
  92.                 String toAdd = translateFilePathUsingPrefix(file, prefix);
  93.                 log("Adding " + toAdd, Project.MSG_VERBOSE);
  94.                 gitAdd.addFilepattern(toAdd);
  95.             }
  96.             gitAdd.call();
  97.         } catch (IOException | GitAPIException e) {
  98.             throw new BuildException("Could not add files to index." + src, e);
  99.         }

  100.     }

  101.     private String translateFilePathUsingPrefix(String file, String prefix)
  102.             throws IOException {
  103.         if (file.equals(prefix)) {
  104.             return ".";
  105.         }
  106.         return new File(file).getCanonicalPath().substring(prefix.length() + 1);
  107.     }

  108. }