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

StashDropCommand.java 8.0KB

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