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.

StashDropCommand.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (C) 2012, GitHub Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.eclipse.jgit.lib.Constants.R_STASH;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.nio.file.StandardCopyOption;
  48. import java.text.MessageFormat;
  49. import java.util.List;
  50. import org.eclipse.jgit.api.errors.GitAPIException;
  51. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  52. import org.eclipse.jgit.api.errors.JGitInternalException;
  53. import org.eclipse.jgit.api.errors.RefNotFoundException;
  54. import org.eclipse.jgit.errors.LockFailedException;
  55. import org.eclipse.jgit.internal.JGitText;
  56. import org.eclipse.jgit.internal.storage.file.ReflogWriter;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.lib.Ref;
  59. import org.eclipse.jgit.lib.RefUpdate;
  60. import org.eclipse.jgit.lib.RefUpdate.Result;
  61. import org.eclipse.jgit.lib.ReflogEntry;
  62. import org.eclipse.jgit.lib.ReflogReader;
  63. import org.eclipse.jgit.lib.Repository;
  64. import org.eclipse.jgit.util.FileUtils;
  65. /**
  66. * Command class to delete a stashed commit reference
  67. *
  68. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-stash.html"
  69. * >Git documentation about Stash</a>
  70. * @since 2.0
  71. */
  72. public class StashDropCommand extends GitCommand<ObjectId> {
  73. private int stashRefEntry;
  74. private boolean all;
  75. /**
  76. * @param repo
  77. */
  78. public StashDropCommand(Repository repo) {
  79. super(repo);
  80. }
  81. /**
  82. * Set the stash reference to drop (0-based).
  83. * <p>
  84. * This will default to drop the latest stashed commit (stash@{0}) if
  85. * unspecified
  86. *
  87. * @param stashRef
  88. * @return {@code this}
  89. */
  90. public StashDropCommand setStashRef(final int stashRef) {
  91. if (stashRef < 0)
  92. throw new IllegalArgumentException();
  93. stashRefEntry = stashRef;
  94. return this;
  95. }
  96. /**
  97. * Set wheter drop all stashed commits
  98. *
  99. * @param all
  100. * true to drop all stashed commits, false to drop only the
  101. * stashed commit set via calling {@link #setStashRef(int)}
  102. * @return {@code this}
  103. */
  104. public StashDropCommand setAll(final boolean all) {
  105. this.all = all;
  106. return this;
  107. }
  108. private Ref getRef() throws GitAPIException {
  109. try {
  110. return repo.exactRef(R_STASH);
  111. } catch (IOException e) {
  112. throw new InvalidRefNameException(MessageFormat.format(
  113. JGitText.get().cannotRead, R_STASH), e);
  114. }
  115. }
  116. private RefUpdate createRefUpdate(final Ref stashRef) throws IOException {
  117. RefUpdate update = repo.updateRef(R_STASH);
  118. update.disableRefLog();
  119. update.setExpectedOldObjectId(stashRef.getObjectId());
  120. update.setForceUpdate(true);
  121. return update;
  122. }
  123. private void deleteRef(final Ref stashRef) {
  124. try {
  125. Result result = createRefUpdate(stashRef).delete();
  126. if (Result.FORCED != result)
  127. throw new JGitInternalException(MessageFormat.format(
  128. JGitText.get().stashDropDeleteRefFailed, result));
  129. } catch (IOException e) {
  130. throw new JGitInternalException(JGitText.get().stashDropFailed, e);
  131. }
  132. }
  133. private void updateRef(Ref stashRef, ObjectId newId) {
  134. try {
  135. RefUpdate update = createRefUpdate(stashRef);
  136. update.setNewObjectId(newId);
  137. Result result = update.update();
  138. switch (result) {
  139. case FORCED:
  140. case NEW:
  141. case NO_CHANGE:
  142. return;
  143. default:
  144. throw new JGitInternalException(MessageFormat.format(
  145. JGitText.get().updatingRefFailed, R_STASH, newId,
  146. result));
  147. }
  148. } catch (IOException e) {
  149. throw new JGitInternalException(JGitText.get().stashDropFailed, e);
  150. }
  151. }
  152. /**
  153. * Drop the configured entry from the stash reflog and return value of the
  154. * stash reference after the drop occurs
  155. *
  156. * @return commit id of stash reference or null if no more stashed changes
  157. * @throws GitAPIException
  158. */
  159. public ObjectId call() throws GitAPIException {
  160. checkCallable();
  161. Ref stashRef = getRef();
  162. if (stashRef == null)
  163. return null;
  164. if (all) {
  165. deleteRef(stashRef);
  166. return null;
  167. }
  168. List<ReflogEntry> entries;
  169. try {
  170. ReflogReader reader = repo.getReflogReader(R_STASH);
  171. if (reader == null) {
  172. throw new RefNotFoundException(MessageFormat
  173. .format(JGitText.get().refNotResolved, stashRef));
  174. }
  175. entries = reader.getReverseEntries();
  176. } catch (IOException e) {
  177. throw new JGitInternalException(JGitText.get().stashDropFailed, e);
  178. }
  179. if (stashRefEntry >= entries.size())
  180. throw new JGitInternalException(
  181. JGitText.get().stashDropMissingReflog);
  182. if (entries.size() == 1) {
  183. deleteRef(stashRef);
  184. return null;
  185. }
  186. ReflogWriter writer = new ReflogWriter(repo, true);
  187. String stashLockRef = ReflogWriter.refLockFor(R_STASH);
  188. File stashLockFile = writer.logFor(stashLockRef);
  189. File stashFile = writer.logFor(R_STASH);
  190. if (stashLockFile.exists())
  191. throw new JGitInternalException(JGitText.get().stashDropFailed,
  192. new LockFailedException(stashFile));
  193. entries.remove(stashRefEntry);
  194. ObjectId entryId = ObjectId.zeroId();
  195. try {
  196. for (int i = entries.size() - 1; i >= 0; i--) {
  197. ReflogEntry entry = entries.get(i);
  198. writer.log(stashLockRef, entryId, entry.getNewId(),
  199. entry.getWho(), entry.getComment());
  200. entryId = entry.getNewId();
  201. }
  202. try {
  203. FileUtils.rename(stashLockFile, stashFile,
  204. StandardCopyOption.ATOMIC_MOVE);
  205. } catch (IOException e) {
  206. throw new JGitInternalException(MessageFormat.format(
  207. JGitText.get().renameFileFailed,
  208. stashLockFile.getPath(), stashFile.getPath()),
  209. e);
  210. }
  211. } catch (IOException e) {
  212. throw new JGitInternalException(JGitText.get().stashDropFailed, e);
  213. }
  214. updateRef(stashRef, entryId);
  215. try {
  216. Ref newStashRef = repo.exactRef(R_STASH);
  217. return newStashRef != null ? newStashRef.getObjectId() : null;
  218. } catch (IOException e) {
  219. throw new InvalidRefNameException(MessageFormat.format(
  220. JGitText.get().cannotRead, R_STASH), e);
  221. }
  222. }
  223. }