* The message will be formatted with the current branch, abbreviated commit * id, and short commit message when used. * * @param message * @return {@code this} */ public StashCreateCommand setIndexMessage(String message) { indexMessage = message; return this; } /** * Set the message used when committing working directory changes *
* The message will be formatted with the current branch, abbreviated commit * id, and short commit message when used. * * @param message * @return {@code this} */ public StashCreateCommand setWorkingDirectoryMessage(String message) { workingDirectoryMessage = message; return this; } /** * Set the person to use as the author and committer in the commits made * * @param person * @return {@code this} */ public StashCreateCommand setPerson(PersonIdent person) { this.person = person; return this; } /** * Set the reference to update with the stashed commit id * If null, no reference is updated *
* This value defaults to {@link Constants#R_STASH}
*
* @param ref
* @return {@code this}
*/
public StashCreateCommand setRef(String ref) {
this.ref = ref;
return this;
}
/**
* Whether to include untracked files in the stash.
*
* @param includeUntracked
* @return {@code this}
* @since 3.4
*/
public StashCreateCommand setIncludeUntracked(boolean includeUntracked) {
this.includeUntracked = includeUntracked;
return this;
}
private RevCommit parseCommit(final ObjectReader reader,
final ObjectId headId) throws IOException {
try (final RevWalk walk = new RevWalk(reader)) {
return walk.parseCommit(headId);
}
}
private CommitBuilder createBuilder() {
CommitBuilder builder = new CommitBuilder();
PersonIdent author = person;
if (author == null)
author = new PersonIdent(repo);
builder.setAuthor(author);
builder.setCommitter(author);
return builder;
}
private void updateStashRef(ObjectId commitId, PersonIdent refLogIdent,
String refLogMessage) throws IOException {
if (ref == null)
return;
Ref currentRef = repo.findRef(ref);
RefUpdate refUpdate = repo.updateRef(ref);
refUpdate.setNewObjectId(commitId);
refUpdate.setRefLogIdent(refLogIdent);
refUpdate.setRefLogMessage(refLogMessage, false);
if (currentRef != null)
refUpdate.setExpectedOldObjectId(currentRef.getObjectId());
else
refUpdate.setExpectedOldObjectId(ObjectId.zeroId());
refUpdate.forceUpdate();
}
private Ref getHead() throws GitAPIException {
try {
Ref head = repo.exactRef(Constants.HEAD);
if (head == null || head.getObjectId() == null)
throw new NoHeadException(JGitText.get().headRequiredToStash);
return head;
} catch (IOException e) {
throw new JGitInternalException(JGitText.get().stashFailed, e);
}
}
/**
* Stash the contents on the working directory and index in separate commits
* and reset to the current HEAD commit.
*
* @return stashed commit or null if no changes to stash
* @throws GitAPIException
*/
@Override
public RevCommit call() throws GitAPIException {
checkCallable();
Ref head = getHead();
try (ObjectReader reader = repo.newObjectReader()) {
RevCommit headCommit = parseCommit(reader, head.getObjectId());
DirCache cache = repo.lockDirCache();
ObjectId commitId;
try (ObjectInserter inserter = repo.newObjectInserter();
TreeWalk treeWalk = new TreeWalk(repo, reader)) {
treeWalk.setRecursive(true);
treeWalk.addTree(headCommit.getTree());
treeWalk.addTree(new DirCacheIterator(cache));
treeWalk.addTree(new FileTreeIterator(repo));
treeWalk.getTree(2, FileTreeIterator.class)
.setDirCacheIterator(treeWalk, 1);
treeWalk.setFilter(AndTreeFilter.create(new SkipWorkTreeFilter(
1), new IndexDiffFilter(1, 2)));
// Return null if no local changes to stash
if (!treeWalk.next())
return null;
MutableObjectId id = new MutableObjectId();
List