* This will default to drop the latest stashed commit (stash@{0}) if
* unspecified
*
* @param stashRef
* @return {@code this}
*/
public StashDropCommand setStashRef(final int stashRef) {
if (stashRef < 0)
throw new IllegalArgumentException();
stashRefEntry = stashRef;
return this;
}
/**
* Set wheter drop all stashed commits
*
* @param all
* true to drop all stashed commits, false to drop only the
* stashed commit set via calling {@link #setStashRef(int)}
* @return {@code this}
*/
public StashDropCommand setAll(final boolean all) {
this.all = all;
return this;
}
private Ref getRef() throws GitAPIException {
try {
return repo.getRef(R_STASH);
} catch (IOException e) {
throw new InvalidRefNameException(MessageFormat.format(
JGitText.get().cannotRead, R_STASH), e);
}
}
private RefUpdate createRefUpdate(final Ref stashRef) throws IOException {
RefUpdate update = repo.updateRef(R_STASH);
update.disableRefLog();
update.setExpectedOldObjectId(stashRef.getObjectId());
update.setForceUpdate(true);
return update;
}
private void deleteRef(final Ref stashRef) {
try {
Result result = createRefUpdate(stashRef).delete();
if (Result.FORCED != result)
throw new JGitInternalException(MessageFormat.format(
JGitText.get().stashDropDeleteRefFailed, result));
} catch (IOException e) {
throw new JGitInternalException(JGitText.get().stashDropFailed, e);
}
}
private void updateRef(Ref stashRef, ObjectId newId) {
try {
RefUpdate update = createRefUpdate(stashRef);
update.setNewObjectId(newId);
Result result = update.update();
switch (result) {
case FORCED:
case NEW:
case NO_CHANGE:
return;
default:
throw new JGitInternalException(MessageFormat.format(
JGitText.get().updatingRefFailed, R_STASH, newId,
result));
}
} catch (IOException e) {
throw new JGitInternalException(JGitText.get().stashDropFailed, e);
}
}
/**
* Drop the configured entry from the stash reflog and return value of the
* stash reference after the drop occurs
*
* @return commit id of stash reference or null if no more stashed changes
* @throws GitAPIException
*/
public ObjectId call() throws GitAPIException {
checkCallable();
Ref stashRef = getRef();
if (stashRef == null)
return null;
if (all) {
deleteRef(stashRef);
return null;
}
List