View Javadoc
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  
45  import java.io.IOException;
46  import java.text.MessageFormat;
47  
48  import org.eclipse.jgit.api.errors.GitAPIException;
49  import org.eclipse.jgit.api.errors.InvalidRefNameException;
50  import org.eclipse.jgit.api.errors.JGitInternalException;
51  import org.eclipse.jgit.api.errors.NoHeadException;
52  import org.eclipse.jgit.api.errors.StashApplyFailureException;
53  import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
54  import org.eclipse.jgit.dircache.DirCache;
55  import org.eclipse.jgit.dircache.DirCacheBuilder;
56  import org.eclipse.jgit.dircache.DirCacheCheckout;
57  import org.eclipse.jgit.dircache.DirCacheEntry;
58  import org.eclipse.jgit.dircache.DirCacheIterator;
59  import org.eclipse.jgit.errors.CheckoutConflictException;
60  import org.eclipse.jgit.internal.JGitText;
61  import org.eclipse.jgit.lib.Constants;
62  import org.eclipse.jgit.lib.ObjectId;
63  import org.eclipse.jgit.lib.ObjectReader;
64  import org.eclipse.jgit.lib.Repository;
65  import org.eclipse.jgit.lib.RepositoryState;
66  import org.eclipse.jgit.merge.MergeStrategy;
67  import org.eclipse.jgit.merge.ResolveMerger;
68  import org.eclipse.jgit.revwalk.RevCommit;
69  import org.eclipse.jgit.revwalk.RevTree;
70  import org.eclipse.jgit.revwalk.RevWalk;
71  import org.eclipse.jgit.treewalk.AbstractTreeIterator;
72  import org.eclipse.jgit.treewalk.FileTreeIterator;
73  import org.eclipse.jgit.treewalk.TreeWalk;
74  
75  /**
76   * Command class to apply a stashed commit.
77   *
78   * This class behaves like <em>git stash apply --index</em>, i.e. it tries to
79   * recover the stashed index state in addition to the working tree state.
80   *
81   * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-stash.html"
82   *      >Git documentation about Stash</a>
83   *
84   * @since 2.0
85   */
86  public class StashApplyCommand extends GitCommand<ObjectId> {
87  
88  	private static final String DEFAULT_REF = Constants.STASH + "@{0}"; //$NON-NLS-1$
89  
90  	private String stashRef;
91  
92  	private boolean applyIndex = true;
93  
94  	private boolean applyUntracked = true;
95  
96  	private boolean ignoreRepositoryState;
97  
98  	private MergeStrategy strategy = MergeStrategy.RECURSIVE;
99  
100 	/**
101 	 * Create command to apply the changes of a stashed commit
102 	 *
103 	 * @param repo
104 	 */
105 	public StashApplyCommand(final Repository repo) {
106 		super(repo);
107 	}
108 
109 	/**
110 	 * Set the stash reference to apply
111 	 * <p>
112 	 * This will default to apply the latest stashed commit (stash@{0}) if
113 	 * unspecified
114 	 *
115 	 * @param stashRef
116 	 * @return {@code this}
117 	 */
118 	public StashApplyCommand setStashRef(final String stashRef) {
119 		this.stashRef = stashRef;
120 		return this;
121 	}
122 
123 	/**
124 	 * @param willIgnoreRepositoryState
125 	 * @return {@code this}
126 	 * @since 3.2
127 	 */
128 	public StashApplyCommand ignoreRepositoryState(boolean willIgnoreRepositoryState) {
129 		this.ignoreRepositoryState = willIgnoreRepositoryState;
130 		return this;
131 	}
132 
133 	private ObjectId getStashId() throws GitAPIException {
134 		final String revision = stashRef != null ? stashRef : DEFAULT_REF;
135 		final ObjectId stashId;
136 		try {
137 			stashId = repo.resolve(revision);
138 		} catch (IOException e) {
139 			throw new InvalidRefNameException(MessageFormat.format(
140 					JGitText.get().stashResolveFailed, revision), e);
141 		}
142 		if (stashId == null)
143 			throw new InvalidRefNameException(MessageFormat.format(
144 					JGitText.get().stashResolveFailed, revision));
145 		return stashId;
146 	}
147 
148 	/**
149 	 * Apply the changes in a stashed commit to the working directory and index
150 	 *
151 	 * @return id of stashed commit that was applied TODO: Does anyone depend on
152 	 *         this, or could we make it more like Merge/CherryPick/Revert?
153 	 * @throws GitAPIException
154 	 * @throws WrongRepositoryStateException
155 	 * @throws NoHeadException
156 	 * @throws StashApplyFailureException
157 	 */
158 	public ObjectId call() throws GitAPIException,
159 			WrongRepositoryStateException, NoHeadException,
160 			StashApplyFailureException {
161 		checkCallable();
162 
163 		if (!ignoreRepositoryState
164 				&& repo.getRepositoryState() != RepositoryState.SAFE)
165 			throw new WrongRepositoryStateException(MessageFormat.format(
166 					JGitText.get().stashApplyOnUnsafeRepository,
167 					repo.getRepositoryState()));
168 
169 		try (ObjectReader reader = repo.newObjectReader();
170 				RevWalk revWalk = new RevWalk(reader)) {
171 
172 			ObjectId headCommit = repo.resolve(Constants.HEAD);
173 			if (headCommit == null)
174 				throw new NoHeadException(JGitText.get().stashApplyWithoutHead);
175 
176 			final ObjectId stashId = getStashId();
177 			RevCommit stashCommit = revWalk.parseCommit(stashId);
178 			if (stashCommit.getParentCount() < 2
179 					|| stashCommit.getParentCount() > 3)
180 				throw new JGitInternalException(MessageFormat.format(
181 						JGitText.get().stashCommitIncorrectNumberOfParents,
182 						stashId.name(),
183 						Integer.valueOf(stashCommit.getParentCount())));
184 
185 			ObjectId headTree = repo.resolve(Constants.HEAD + "^{tree}"); //$NON-NLS-1$
186 			ObjectId stashIndexCommit = revWalk.parseCommit(stashCommit
187 					.getParent(1));
188 			ObjectId stashHeadCommit = stashCommit.getParent(0);
189 			ObjectId untrackedCommit = null;
190 			if (applyUntracked && stashCommit.getParentCount() == 3)
191 				untrackedCommit = revWalk.parseCommit(stashCommit.getParent(2));
192 
193 			ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo);
194 			merger.setCommitNames(new String[] { "stashed HEAD", "HEAD", //$NON-NLS-1$ //$NON-NLS-2$
195 					"stash" }); //$NON-NLS-1$
196 			merger.setBase(stashHeadCommit);
197 			merger.setWorkingTreeIterator(new FileTreeIterator(repo));
198 			if (merger.merge(headCommit, stashCommit)) {
199 				DirCache dc = repo.lockDirCache();
200 				DirCacheCheckout dco = new DirCacheCheckout(repo, headTree,
201 						dc, merger.getResultTreeId());
202 				dco.setFailOnConflict(true);
203 				dco.checkout(); // Ignoring failed deletes....
204 				if (applyIndex) {
205 					ResolveMerger ixMerger = (ResolveMerger) strategy
206 							.newMerger(repo, true);
207 					ixMerger.setCommitNames(new String[] { "stashed HEAD", //$NON-NLS-1$
208 							"HEAD", "stashed index" }); //$NON-NLS-1$//$NON-NLS-2$
209 					ixMerger.setBase(stashHeadCommit);
210 					boolean ok = ixMerger.merge(headCommit, stashIndexCommit);
211 					if (ok) {
212 						resetIndex(revWalk
213 								.parseTree(ixMerger.getResultTreeId()));
214 					} else {
215 						throw new StashApplyFailureException(
216 								JGitText.get().stashApplyConflict);
217 					}
218 				}
219 
220 				if (untrackedCommit != null) {
221 					ResolveMerger untrackedMerger = (ResolveMerger) strategy
222 							.newMerger(repo, true);
223 					untrackedMerger.setCommitNames(new String[] {
224 							"stashed HEAD", "HEAD", "untracked files" }); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
225 					untrackedMerger.setBase(stashHeadCommit);
226 					boolean ok = untrackedMerger.merge(headCommit,
227 							untrackedCommit);
228 					if (ok)
229 						try {
230 							RevTree untrackedTree = revWalk
231 									.parseTree(untrackedMerger
232 											.getResultTreeId());
233 							resetUntracked(untrackedTree);
234 						} catch (CheckoutConflictException e) {
235 							throw new StashApplyFailureException(
236 									JGitText.get().stashApplyConflict);
237 						}
238 					else
239 						throw new StashApplyFailureException(
240 								JGitText.get().stashApplyConflict);
241 				}
242 			} else {
243 				throw new StashApplyFailureException(
244 						JGitText.get().stashApplyConflict);
245 			}
246 			return stashId;
247 
248 		} catch (JGitInternalException e) {
249 			throw e;
250 		} catch (IOException e) {
251 			throw new JGitInternalException(JGitText.get().stashApplyFailed, e);
252 		}
253 	}
254 
255 	/**
256 	 * @param applyIndex
257 	 *            true (default) if the command should restore the index state
258 	 */
259 	public void setApplyIndex(boolean applyIndex) {
260 		this.applyIndex = applyIndex;
261 	}
262 
263 	/**
264 	 * @param strategy
265 	 *            The merge strategy to use in order to merge during this
266 	 *            command execution.
267 	 * @return {@code this}
268 	 * @since 3.4
269 	 */
270 	public StashApplyCommand setStrategy(MergeStrategy strategy) {
271 		this.strategy = strategy;
272 		return this;
273 	}
274 
275 	/**
276 	 * @param applyUntracked
277 	 *            true (default) if the command should restore untracked files
278 	 * @since 3.4
279 	 */
280 	public void setApplyUntracked(boolean applyUntracked) {
281 		this.applyUntracked = applyUntracked;
282 	}
283 
284 	private void resetIndex(RevTree tree) throws IOException {
285 		DirCache dc = repo.lockDirCache();
286 		try (TreeWalk walk = new TreeWalk(repo)) {
287 			DirCacheBuilder builder = dc.builder();
288 
289 			walk.addTree(tree);
290 			walk.addTree(new DirCacheIterator(dc));
291 			walk.setRecursive(true);
292 
293 			while (walk.next()) {
294 				AbstractTreeIterator cIter = walk.getTree(0,
295 						AbstractTreeIterator.class);
296 				if (cIter == null) {
297 					// Not in commit, don't add to new index
298 					continue;
299 				}
300 
301 				final DirCacheEntry entry = new DirCacheEntry(walk.getRawPath());
302 				entry.setFileMode(cIter.getEntryFileMode());
303 				entry.setObjectIdFromRaw(cIter.idBuffer(), cIter.idOffset());
304 
305 				DirCacheIterator dcIter = walk.getTree(1,
306 						DirCacheIterator.class);
307 				if (dcIter != null && dcIter.idEqual(cIter)) {
308 					DirCacheEntry indexEntry = dcIter.getDirCacheEntry();
309 					entry.setLastModified(indexEntry.getLastModified());
310 					entry.setLength(indexEntry.getLength());
311 				}
312 
313 				builder.add(entry);
314 			}
315 
316 			builder.commit();
317 		} finally {
318 			dc.unlock();
319 		}
320 	}
321 
322 	private void resetUntracked(RevTree tree) throws CheckoutConflictException,
323 			IOException {
324 		// TODO maybe NameConflictTreeWalk ?
325 		try (TreeWalk walk = new TreeWalk(repo)) {
326 			walk.addTree(tree);
327 			walk.addTree(new FileTreeIterator(repo));
328 			walk.setRecursive(true);
329 
330 			final ObjectReader reader = walk.getObjectReader();
331 
332 			while (walk.next()) {
333 				final AbstractTreeIterator cIter = walk.getTree(0,
334 						AbstractTreeIterator.class);
335 				if (cIter == null)
336 					// Not in commit, don't create untracked
337 					continue;
338 
339 				final DirCacheEntry entry = new DirCacheEntry(walk.getRawPath());
340 				entry.setFileMode(cIter.getEntryFileMode());
341 				entry.setObjectIdFromRaw(cIter.idBuffer(), cIter.idOffset());
342 
343 				FileTreeIterator fIter = walk
344 						.getTree(1, FileTreeIterator.class);
345 				if (fIter != null) {
346 					if (fIter.isModified(entry, true, reader)) {
347 						// file exists and is dirty
348 						throw new CheckoutConflictException(
349 								entry.getPathString());
350 					}
351 				}
352 
353 				checkoutPath(entry, reader);
354 			}
355 		}
356 	}
357 
358 	private void checkoutPath(DirCacheEntry entry, ObjectReader reader) {
359 		try {
360 			DirCacheCheckout.checkoutEntry(repo, entry, reader);
361 		} catch (IOException e) {
362 			throw new JGitInternalException(MessageFormat.format(
363 					JGitText.get().checkoutConflictWithFile,
364 					entry.getPathString()), e);
365 		}
366 	}
367 }