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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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.text.MessageFormat;
  48. import java.util.List;
  49. import org.eclipse.jgit.api.errors.GitAPIException;
  50. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  51. import org.eclipse.jgit.api.errors.JGitInternalException;
  52. import org.eclipse.jgit.errors.LockFailedException;
  53. import org.eclipse.jgit.internal.JGitText;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.Ref;
  56. import org.eclipse.jgit.lib.RefUpdate;
  57. import org.eclipse.jgit.lib.RefUpdate.Result;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.storage.file.ReflogEntry;
  60. import org.eclipse.jgit.storage.file.ReflogReader;
  61. import org.eclipse.jgit.storage.file.ReflogWriter;
  62. import org.eclipse.jgit.util.FileUtils;
  63. /**
  64. * Command class to delete a stashed commit reference
  65. *
  66. * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-stash.html"
  67. * >Git documentation about Stash</a>
  68. */
  69. public class StashDropCommand extends GitCommand<ObjectId> {
  70. private int stashRefEntry;
  71. private boolean all;
  72. /**
  73. * @param repo
  74. */
  75. public StashDropCommand(Repository repo) {
  76. super(repo);
  77. }
  78. /**
  79. * Set the stash reference to drop (0-based).
  80. * <p>
  81. * This will default to drop the latest stashed commit (stash@{0}) if
  82. * unspecified
  83. *
  84. * @param stashRef
  85. * @return {@code this}
  86. */
  87. public StashDropCommand setStashRef(final int stashRef) {
  88. if (stashRef < 0)
  89. throw new IllegalArgumentException();
  90. stashRefEntry = stashRef;
  91. return this;
  92. }
  93. /**
  94. * Set wheter drop all stashed commits
  95. *
  96. * @param all
  97. * true to drop all stashed commits, false to drop only the
  98. * stashed commit set via calling {@link #setStashRef(int)}
  99. * @return {@code this}
  100. */
  101. public StashDropCommand setAll(final boolean all) {
  102. this.all = all;
  103. return this;
  104. }
  105. private Ref getRef() throws GitAPIException {
  106. try {
  107. return repo.getRef(R_STASH);
  108. } catch (IOException e) {
  109. throw new InvalidRefNameException(MessageFormat.format(
  110. JGitText.get().cannotRead, R_STASH), e);
  111. }
  112. }
  113. private RefUpdate createRefUpdate(final Ref stashRef) throws IOException {
  114. RefUpdate update = repo.updateRef(R_STASH);
  115. update.disableRefLog();
  116. update.setExpectedOldObjectId(stashRef.getObjectId());
  117. update.setForceUpdate(true);
  118. return update;
  119. }
  120. private void deleteRef(final Ref stashRef) {
  121. try {
  122. Result result = createRefUpdate(stashRef).delete();
  123. if (Result.FORCED != result)
  124. throw new JGitInternalException(MessageFormat.format(
  125. JGitText.get().stashDropDeleteRefFailed, result));
  126. } catch (IOException e) {
  127. throw new JGitInternalException(JGitText.get().stashDropFailed, e);
  128. }
  129. }
  130. private void updateRef(Ref stashRef, ObjectId newId) {
  131. try {
  132. RefUpdate update = createRefUpdate(stashRef);
  133. update.setNewObjectId(newId);
  134. Result result = update.update();
  135. switch (result) {
  136. case FORCED:
  137. case NEW:
  138. case NO_CHANGE:
  139. return;
  140. default:
  141. throw new JGitInternalException(MessageFormat.format(
  142. JGitText.get().updatingRefFailed, R_STASH, newId,
  143. result));
  144. }
  145. } catch (IOException e) {
  146. throw new JGitInternalException(JGitText.get().stashDropFailed, e);
  147. }
  148. }
  149. /**
  150. * Drop the configured entry from the stash reflog and return value of the
  151. * stash reference after the drop occurs
  152. *
  153. * @return commit id of stash reference or null if no more stashed changes
  154. */
  155. public ObjectId call() throws GitAPIException, JGitInternalException {
  156. checkCallable();
  157. Ref stashRef = getRef();
  158. if (stashRef == null)
  159. return null;
  160. if (all) {
  161. deleteRef(stashRef);
  162. return null;
  163. }
  164. ReflogReader reader = new ReflogReader(repo, R_STASH);
  165. List<ReflogEntry> entries;
  166. try {
  167. entries = reader.getReverseEntries();
  168. } catch (IOException e) {
  169. throw new JGitInternalException(JGitText.get().stashDropFailed, e);
  170. }
  171. if (stashRefEntry >= entries.size())
  172. throw new JGitInternalException(
  173. JGitText.get().stashDropMissingReflog);
  174. if (entries.size() == 1) {
  175. deleteRef(stashRef);
  176. return null;
  177. }
  178. ReflogWriter writer = new ReflogWriter(repo, true);
  179. String stashLockRef = ReflogWriter.refLockFor(R_STASH);
  180. File stashLockFile = writer.logFor(stashLockRef);
  181. File stashFile = writer.logFor(R_STASH);
  182. if (stashLockFile.exists())
  183. throw new JGitInternalException(JGitText.get().stashDropFailed,
  184. new LockFailedException(stashFile));
  185. entries.remove(stashRefEntry);
  186. ObjectId entryId = ObjectId.zeroId();
  187. try {
  188. for (int i = entries.size() - 1; i >= 0; i--) {
  189. ReflogEntry entry = entries.get(i);
  190. writer.log(stashLockRef, entryId, entry.getNewId(),
  191. entry.getWho(), entry.getComment());
  192. entryId = entry.getNewId();
  193. }
  194. if (!stashLockFile.renameTo(stashFile)) {
  195. FileUtils.delete(stashFile);
  196. if (!stashLockFile.renameTo(stashFile))
  197. throw new JGitInternalException(MessageFormat.format(
  198. JGitText.get().couldNotWriteFile,
  199. stashLockFile.getPath(), stashFile.getPath()));
  200. }
  201. } catch (IOException e) {
  202. throw new JGitInternalException(JGitText.get().stashDropFailed, e);
  203. }
  204. updateRef(stashRef, entryId);
  205. try {
  206. Ref newStashRef = repo.getRef(R_STASH);
  207. return newStashRef != null ? newStashRef.getObjectId() : null;
  208. } catch (IOException e) {
  209. throw new InvalidRefNameException(MessageFormat.format(
  210. JGitText.get().cannotRead, R_STASH), e);
  211. }
  212. }
  213. }