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

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