Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AddCommand.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.File;
  46. import java.io.IOException;
  47. import java.util.Collection;
  48. import java.util.LinkedList;
  49. import org.eclipse.jgit.JGitText;
  50. import org.eclipse.jgit.dircache.DirCache;
  51. import org.eclipse.jgit.dircache.DirCacheBuildIterator;
  52. import org.eclipse.jgit.dircache.DirCacheBuilder;
  53. import org.eclipse.jgit.dircache.DirCacheEntry;
  54. import org.eclipse.jgit.dircache.DirCacheIterator;
  55. import org.eclipse.jgit.lib.ObjectWriter;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.treewalk.FileTreeIterator;
  58. import org.eclipse.jgit.treewalk.TreeWalk;
  59. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  60. /**
  61. * A class used to execute a {@code Add} command. It has setters for all
  62. * supported options and arguments of this command and a {@link #call()} method
  63. * to finally execute the command. Each instance of this class should only be
  64. * used for one invocation of the command (means: one call to {@link #call()})
  65. *
  66. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-add.html"
  67. * >Git documentation about Add</a>
  68. */
  69. public class AddCommand extends GitCommand<DirCache> {
  70. private Collection<String> filepatterns;
  71. /**
  72. *
  73. * @param repo
  74. */
  75. public AddCommand(Repository repo) {
  76. super(repo);
  77. filepatterns = new LinkedList<String>();
  78. }
  79. /**
  80. * @param filepattern
  81. * File to add content from. Also a leading directory name (e.g.
  82. * dir to add dir/file1 and dir/file2) can be given to add all
  83. * files in the directory, recursively. Fileglobs (e.g. *.c) are
  84. * not yet supported.
  85. * @return {@code this}
  86. */
  87. public AddCommand addFilepattern(String filepattern) {
  88. checkCallable();
  89. filepatterns.add(filepattern);
  90. return this;
  91. }
  92. /**
  93. * Executes the {@code Add} command. Each instance of this class should only
  94. * be used for one invocation of the command. Don't call this method twice
  95. * on an instance.
  96. *
  97. * @return the DirCache after Add
  98. */
  99. public DirCache call() throws NoFilepatternException {
  100. if (filepatterns.isEmpty())
  101. throw new NoFilepatternException(JGitText.get().atLeastOnePatternIsRequired);
  102. checkCallable();
  103. DirCache dc = null;
  104. try {
  105. dc = DirCache.lock(repo);
  106. ObjectWriter ow = new ObjectWriter(repo);
  107. DirCacheIterator c;
  108. DirCacheBuilder builder = dc.builder();
  109. final TreeWalk tw = new TreeWalk(repo);
  110. tw.reset();
  111. tw.addTree(new DirCacheBuildIterator(builder));
  112. FileTreeIterator fileTreeIterator = new FileTreeIterator(repo);
  113. tw.addTree(fileTreeIterator);
  114. tw.setRecursive(true);
  115. tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));
  116. String lastAddedFile = null;
  117. while (tw.next()) {
  118. String path = tw.getPathString();
  119. final File file = new File(repo.getWorkDir(), path);
  120. // In case of an existing merge conflict the
  121. // DirCacheBuildIterator iterates over all stages of
  122. // this path, we however want to add only one
  123. // new DirCacheEntry per path.
  124. if (!(path.equals(lastAddedFile))) {
  125. FileTreeIterator f = tw.getTree(1, FileTreeIterator.class);
  126. if (f != null) { // the file exists
  127. DirCacheEntry entry = new DirCacheEntry(path);
  128. entry.setLength((int)f.getEntryLength());
  129. entry.setLastModified(f.getEntryLastModified());
  130. entry.setFileMode(f.getEntryFileMode());
  131. entry.setObjectId(ow.writeBlob(file));
  132. builder.add(entry);
  133. lastAddedFile = path;
  134. } else {
  135. c = tw.getTree(0, DirCacheIterator.class);
  136. builder.add(c.getDirCacheEntry());
  137. }
  138. }
  139. }
  140. builder.commit();
  141. setCallable(false);
  142. } catch (IOException e) {
  143. throw new JGitInternalException(
  144. JGitText.get().exceptionCaughtDuringExecutionOfAddCommand, e);
  145. } finally {
  146. if (dc != null)
  147. dc.unlock();
  148. }
  149. return dc;
  150. }
  151. }