if (indexIter != null && wtIter.idEqual(indexIter))
continue;
if (headIter != null && wtIter.idEqual(headIter))
continue;
treeWalk.getObjectId(id, 0);
final DirCacheEntry entry = new DirCacheEntry(
treeWalk.getRawPath());
entry.setLength(wtIter.getEntryLength());
entry.setLastModified(wtIter.getEntryLastModified());
entry.setFileMode(wtIter.getEntryFileMode());
long contentLength = wtIter.getEntryContentLength();
InputStream in = wtIter.openEntryStream();
try {
entry.setObjectId(inserter.insert(
Constants.OBJ_BLOB, contentLength, in));
} finally {
in.close();
}
if (indexIter == null && headIter == null)
untracked.add(entry);
else
wtEdits.add(new PathEdit(entry) {
public void apply(DirCacheEntry ent) {
ent.copyMetaData(entry);
}
});
}
hasChanges = true;
if (wtIter == null && headIter != null)
wtDeletes.add(treeWalk.getPathString());
} while (treeWalk.next());
if (!hasChanges)
return null;
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(),
entry.getPathString());
FileUtils.delete(file);
}
}
} finally {