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

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