13344b93cSKetan Padegaonkar /* 2*5c5f7c6bSMatthias Sohn * Copyright (C) 2011, Ketan Padegaonkar <KetanPadegaonkar@gmail.com> and others 33344b93cSKetan 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. 73344b93cSKetan Padegaonkar * 8*5c5f7c6bSMatthias Sohn * SPDX-License-Identifier: BSD-3-Clause 93344b93cSKetan Padegaonkar */ 103344b93cSKetan Padegaonkar package org.eclipse.jgit.ant.tasks; 113344b93cSKetan Padegaonkar 123344b93cSKetan Padegaonkar import java.io.File; 133344b93cSKetan Padegaonkar import java.io.IOException; 143344b93cSKetan Padegaonkar 153344b93cSKetan Padegaonkar import org.apache.tools.ant.BuildException; 163344b93cSKetan Padegaonkar import org.apache.tools.ant.Project; 173344b93cSKetan Padegaonkar import org.apache.tools.ant.Task; 183344b93cSKetan Padegaonkar import org.apache.tools.ant.types.DirSet; 193344b93cSKetan Padegaonkar import org.apache.tools.ant.types.FileSet; 203344b93cSKetan Padegaonkar import org.apache.tools.ant.types.resources.Union; 213344b93cSKetan Padegaonkar import org.eclipse.jgit.api.AddCommand; 223344b93cSKetan Padegaonkar import org.eclipse.jgit.api.Git; 23a3c0a7f9SDavid Pursehouse import org.eclipse.jgit.api.errors.GitAPIException; 243344b93cSKetan Padegaonkar import org.eclipse.jgit.lib.Repository; 253344b93cSKetan Padegaonkar import org.eclipse.jgit.lib.RepositoryCache; 26a45cfee7SDavid Pursehouse import org.eclipse.jgit.storage.file.FileRepositoryBuilder; 273344b93cSKetan Padegaonkar import org.eclipse.jgit.util.FS; 283344b93cSKetan Padegaonkar 293344b93cSKetan Padegaonkar /** 303344b93cSKetan Padegaonkar * Adds a file to the git index. 313344b93cSKetan Padegaonkar * 323344b93cSKetan Padegaonkar * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-add.html" 333344b93cSKetan Padegaonkar * >git-add(1)</a> 343344b93cSKetan Padegaonkar */ 353344b93cSKetan Padegaonkar public class GitAddTask extends Task { 363344b93cSKetan Padegaonkar 373344b93cSKetan Padegaonkar private File src; 383344b93cSKetan Padegaonkar private Union path; 393344b93cSKetan Padegaonkar 403344b93cSKetan Padegaonkar /** 41c7093f5cSMatthias Sohn * <p>Set the field <code>src</code>.</p> 42c7093f5cSMatthias Sohn * 433344b93cSKetan Padegaonkar * @param src 443344b93cSKetan Padegaonkar * the src to set 453344b93cSKetan Padegaonkar */ setSrc(File src)463344b93cSKetan Padegaonkar public void setSrc(File src) { 473344b93cSKetan Padegaonkar this.src = src; 483344b93cSKetan Padegaonkar } 493344b93cSKetan Padegaonkar 503344b93cSKetan Padegaonkar /** 513344b93cSKetan Padegaonkar * Add a set of files to add. 523344b93cSKetan Padegaonkar * 533344b93cSKetan Padegaonkar * @param set 543344b93cSKetan Padegaonkar * a set of files to add. 553344b93cSKetan Padegaonkar */ addFileset(FileSet set)563344b93cSKetan Padegaonkar public void addFileset(FileSet set) { 573344b93cSKetan Padegaonkar getPath().add(set); 583344b93cSKetan Padegaonkar } 593344b93cSKetan Padegaonkar 603344b93cSKetan Padegaonkar /** 613344b93cSKetan Padegaonkar * Add a set of files to add. 623344b93cSKetan Padegaonkar * 633344b93cSKetan Padegaonkar * @param set 643344b93cSKetan Padegaonkar * a set of files to add. 653344b93cSKetan Padegaonkar */ addDirset(DirSet set)663344b93cSKetan Padegaonkar public void addDirset(DirSet set) { 673344b93cSKetan Padegaonkar getPath().add(set); 683344b93cSKetan Padegaonkar } 693344b93cSKetan Padegaonkar getPath()703344b93cSKetan Padegaonkar private synchronized Union getPath() { 713344b93cSKetan Padegaonkar if (path == null) { 723344b93cSKetan Padegaonkar path = new Union(); 733344b93cSKetan Padegaonkar path.setProject(getProject()); 743344b93cSKetan Padegaonkar } 753344b93cSKetan Padegaonkar return path; 763344b93cSKetan Padegaonkar } 773344b93cSKetan Padegaonkar 78c7093f5cSMatthias Sohn /** {@inheritDoc} */ 793344b93cSKetan Padegaonkar @Override execute()803344b93cSKetan Padegaonkar public void execute() throws BuildException { 813344b93cSKetan Padegaonkar if (src == null) { 823344b93cSKetan Padegaonkar throw new BuildException("Repository path not specified."); 833344b93cSKetan Padegaonkar } 843344b93cSKetan Padegaonkar if (!RepositoryCache.FileKey.isGitRepository(new File(src, ".git"), 853344b93cSKetan Padegaonkar FS.DETECTED)) { 863344b93cSKetan Padegaonkar throw new BuildException("Specified path (" + src 873344b93cSKetan Padegaonkar + ") is not a git repository."); 883344b93cSKetan Padegaonkar } 893344b93cSKetan Padegaonkar 903344b93cSKetan Padegaonkar AddCommand gitAdd; 9158e29086SDavid Pursehouse try (Repository repo = new FileRepositoryBuilder().readEnvironment() 923344b93cSKetan Padegaonkar .findGitDir(src).build(); 9358e29086SDavid Pursehouse Git git = new Git(repo);) { 9458e29086SDavid Pursehouse gitAdd = git.add(); 953344b93cSKetan Padegaonkar } catch (IOException e) { 963344b93cSKetan Padegaonkar throw new BuildException("Could not access repository " + src, e); 973344b93cSKetan Padegaonkar } 983344b93cSKetan Padegaonkar 993344b93cSKetan Padegaonkar try { 1003344b93cSKetan Padegaonkar String prefix = src.getCanonicalPath(); 1013344b93cSKetan Padegaonkar String[] allFiles = getPath().list(); 1023344b93cSKetan Padegaonkar 1033344b93cSKetan Padegaonkar for (String file : allFiles) { 1043344b93cSKetan Padegaonkar String toAdd = translateFilePathUsingPrefix(file, prefix); 1053344b93cSKetan Padegaonkar log("Adding " + toAdd, Project.MSG_VERBOSE); 1063344b93cSKetan Padegaonkar gitAdd.addFilepattern(toAdd); 1073344b93cSKetan Padegaonkar } 1083344b93cSKetan Padegaonkar gitAdd.call(); 109a3c0a7f9SDavid Pursehouse } catch (IOException | GitAPIException e) { 1103344b93cSKetan Padegaonkar throw new BuildException("Could not add files to index." + src, e); 1113344b93cSKetan Padegaonkar } 1123344b93cSKetan Padegaonkar 1133344b93cSKetan Padegaonkar } 1143344b93cSKetan Padegaonkar translateFilePathUsingPrefix(String file, String prefix)1153344b93cSKetan Padegaonkar private String translateFilePathUsingPrefix(String file, String prefix) 1163344b93cSKetan Padegaonkar throws IOException { 1173344b93cSKetan Padegaonkar if (file.equals(prefix)) { 1183344b93cSKetan Padegaonkar return "."; 1193344b93cSKetan Padegaonkar } 1203344b93cSKetan Padegaonkar return new File(file).getCanonicalPath().substring(prefix.length() + 1); 1213344b93cSKetan Padegaonkar } 1223344b93cSKetan Padegaonkar 1233344b93cSKetan Padegaonkar } 124