You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AddCommand.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  3. * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.api;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.util.Collection;
  48. import java.util.LinkedList;
  49. import org.eclipse.jgit.api.errors.GitAPIException;
  50. import org.eclipse.jgit.api.errors.JGitInternalException;
  51. import org.eclipse.jgit.api.errors.NoFilepatternException;
  52. import org.eclipse.jgit.dircache.DirCache;
  53. import org.eclipse.jgit.dircache.DirCacheBuildIterator;
  54. import org.eclipse.jgit.dircache.DirCacheBuilder;
  55. import org.eclipse.jgit.dircache.DirCacheEntry;
  56. import org.eclipse.jgit.dircache.DirCacheIterator;
  57. import org.eclipse.jgit.internal.JGitText;
  58. import org.eclipse.jgit.lib.Constants;
  59. import org.eclipse.jgit.lib.FileMode;
  60. import org.eclipse.jgit.lib.ObjectInserter;
  61. import org.eclipse.jgit.lib.Repository;
  62. import org.eclipse.jgit.treewalk.FileTreeIterator;
  63. import org.eclipse.jgit.treewalk.TreeWalk;
  64. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  65. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  66. /**
  67. * A class used to execute a {@code Add} command. It has setters for all
  68. * supported options and arguments of this command and a {@link #call()} method
  69. * to finally execute the command. Each instance of this class should only be
  70. * used for one invocation of the command (means: one call to {@link #call()})
  71. *
  72. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-add.html"
  73. * >Git documentation about Add</a>
  74. */
  75. public class AddCommand extends GitCommand<DirCache> {
  76. private Collection<String> filepatterns;
  77. private WorkingTreeIterator workingTreeIterator;
  78. private boolean update = false;
  79. /**
  80. *
  81. * @param repo
  82. */
  83. public AddCommand(Repository repo) {
  84. super(repo);
  85. filepatterns = new LinkedList<String>();
  86. }
  87. /**
  88. * Add a path to a file/directory whose content should be added.
  89. * <p>
  90. * A directory name (e.g. <code>dir</code> to add <code>dir/file1</code> and
  91. * <code>dir/file2</code>) can also be given to add all files in the
  92. * directory, recursively. Fileglobs (e.g. *.c) are not yet supported.
  93. *
  94. * @param filepattern
  95. * repository-relative path of file/directory to add (with
  96. * <code>/</code> as separator)
  97. * @return {@code this}
  98. */
  99. public AddCommand addFilepattern(String filepattern) {
  100. checkCallable();
  101. filepatterns.add(filepattern);
  102. return this;
  103. }
  104. /**
  105. * Allow clients to provide their own implementation of a FileTreeIterator
  106. * @param f
  107. * @return {@code this}
  108. */
  109. public AddCommand setWorkingTreeIterator(WorkingTreeIterator f) {
  110. workingTreeIterator = f;
  111. return this;
  112. }
  113. /**
  114. * Executes the {@code Add} command. Each instance of this class should only
  115. * be used for one invocation of the command. Don't call this method twice
  116. * on an instance.
  117. *
  118. * @return the DirCache after Add
  119. */
  120. public DirCache call() throws GitAPIException, NoFilepatternException {
  121. if (filepatterns.isEmpty())
  122. throw new NoFilepatternException(JGitText.get().atLeastOnePatternIsRequired);
  123. checkCallable();
  124. DirCache dc = null;
  125. boolean addAll = false;
  126. if (filepatterns.contains(".")) //$NON-NLS-1$
  127. addAll = true;
  128. ObjectInserter inserter = repo.newObjectInserter();
  129. try {
  130. dc = repo.lockDirCache();
  131. DirCacheIterator c;
  132. DirCacheBuilder builder = dc.builder();
  133. final TreeWalk tw = new TreeWalk(repo);
  134. tw.addTree(new DirCacheBuildIterator(builder));
  135. if (workingTreeIterator == null)
  136. workingTreeIterator = new FileTreeIterator(repo);
  137. tw.addTree(workingTreeIterator);
  138. tw.setRecursive(true);
  139. if (!addAll)
  140. tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));
  141. String lastAddedFile = null;
  142. while (tw.next()) {
  143. String path = tw.getPathString();
  144. WorkingTreeIterator f = tw.getTree(1, WorkingTreeIterator.class);
  145. if (tw.getTree(0, DirCacheIterator.class) == null &&
  146. f != null && f.isEntryIgnored()) {
  147. // file is not in index but is ignored, do nothing
  148. }
  149. // In case of an existing merge conflict the
  150. // DirCacheBuildIterator iterates over all stages of
  151. // this path, we however want to add only one
  152. // new DirCacheEntry per path.
  153. else if (!(path.equals(lastAddedFile))) {
  154. if (!(update && tw.getTree(0, DirCacheIterator.class) == null)) {
  155. c = tw.getTree(0, DirCacheIterator.class);
  156. if (f != null) { // the file exists
  157. long sz = f.getEntryLength();
  158. DirCacheEntry entry = new DirCacheEntry(path);
  159. if (c == null || c.getDirCacheEntry() == null
  160. || !c.getDirCacheEntry().isAssumeValid()) {
  161. FileMode mode = f.getIndexFileMode(c);
  162. entry.setFileMode(mode);
  163. if (FileMode.GITLINK != mode) {
  164. entry.setLength(sz);
  165. entry.setLastModified(f
  166. .getEntryLastModified());
  167. long contentSize = f
  168. .getEntryContentLength();
  169. InputStream in = f.openEntryStream();
  170. try {
  171. entry.setObjectId(inserter.insert(
  172. Constants.OBJ_BLOB, contentSize, in));
  173. } finally {
  174. in.close();
  175. }
  176. } else
  177. entry.setObjectId(f.getEntryObjectId());
  178. builder.add(entry);
  179. lastAddedFile = path;
  180. } else {
  181. builder.add(c.getDirCacheEntry());
  182. }
  183. } else if (c != null
  184. && (!update || FileMode.GITLINK == c
  185. .getEntryFileMode()))
  186. builder.add(c.getDirCacheEntry());
  187. }
  188. }
  189. }
  190. inserter.flush();
  191. builder.commit();
  192. setCallable(false);
  193. } catch (IOException e) {
  194. throw new JGitInternalException(
  195. JGitText.get().exceptionCaughtDuringExecutionOfAddCommand, e);
  196. } finally {
  197. inserter.release();
  198. if (dc != null)
  199. dc.unlock();
  200. }
  201. return dc;
  202. }
  203. /**
  204. * @param update
  205. * If set to true, the command only matches {@code filepattern}
  206. * against already tracked files in the index rather than the
  207. * working tree. That means that it will never stage new files,
  208. * but that it will stage modified new contents of tracked files
  209. * and that it will remove files from the index if the
  210. * corresponding files in the working tree have been removed.
  211. * In contrast to the git command line a {@code filepattern} must
  212. * exist also if update is set to true as there is no
  213. * concept of a working directory here.
  214. *
  215. * @return {@code this}
  216. */
  217. public AddCommand setUpdate(boolean update) {
  218. this.update = update;
  219. return this;
  220. }
  221. /**
  222. * @return is the parameter update is set
  223. */
  224. public boolean isUpdate() {
  225. return update;
  226. }
  227. }