String branch = Repository.shortenRefName(head.getTarget()
.getName());
// Commit index changes
CommitBuilder builder = createBuilder();
builder.setParentId(headCommit);
builder.setTreeId(cache.writeTree(inserter));
builder.setMessage(MessageFormat.format(indexMessage, branch,
headCommit.abbreviate(7).name(),
headCommit.getShortMessage()));
ObjectId indexCommit = inserter.insert(builder);
// Commit untracked changes
ObjectId untrackedCommit = null;
if (!untracked.isEmpty()) {
DirCache untrackedDirCache = DirCache.newInCore();
DirCacheBuilder untrackedBuilder = untrackedDirCache
.builder();
for (DirCacheEntry entry : untracked)
untrackedBuilder.add(entry);
untrackedBuilder.finish();
builder.setParentIds(new ObjectId[0]);
builder.setTreeId(untrackedDirCache.writeTree(inserter));
builder.setMessage(MessageFormat.format(MSG_UNTRACKED,
branch, headCommit.abbreviate(7).name(),
headCommit.getShortMessage()));
untrackedCommit = inserter.insert(builder);
}
// Commit working tree changes
if (!wtEdits.isEmpty() || !wtDeletes.isEmpty()) {
DirCacheEditor editor = cache.editor();
for (PathEdit edit : wtEdits)
editor.add(edit);
for (String path : wtDeletes)
editor.add(new DeletePath(path));
editor.finish();
}
builder.setParentId(headCommit);
builder.addParentId(indexCommit);
if (untrackedCommit != null)
builder.addParentId(untrackedCommit);
builder.setMessage(MessageFormat.format(
workingDirectoryMessage, branch,
headCommit.abbreviate(7).name(),
headCommit.getShortMessage()));
builder.setTreeId(cache.writeTree(inserter));
commitId = inserter.insert(builder);
inserter.flush();
updateStashRef(commitId, builder.getAuthor(),
builder.getMessage());
// Remove untracked files
if (includeUntracked) {
for (DirCacheEntry entry : untracked) {
File file = new File(repo.getWorkTree(),