DirCache dc = null;
boolean addAll = false;
if (filepatterns.contains("."))
addAll = true;
ObjectInserter inserter = repo.newObjectInserter();
try {
dc = repo.lockDirCache();
DirCacheIterator c;
DirCacheBuilder builder = dc.builder();
final TreeWalk tw = new TreeWalk(repo);
tw.addTree(new DirCacheBuildIterator(builder));
if (workingTreeIterator == null)
workingTreeIterator = new FileTreeIterator(repo);
tw.addTree(workingTreeIterator);
tw.setRecursive(true);
if (!addAll)
tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));
String lastAddedFile = null;
while (tw.next()) {
String path = tw.getPathString();
WorkingTreeIterator f = tw.getTree(1, WorkingTreeIterator.class);
if (tw.getTree(0, DirCacheIterator.class) == null &&
f != null && f.isEntryIgnored()) {
// file is not in index but is ignored, do nothing
}
// In case of an existing merge conflict the
// DirCacheBuildIterator iterates over all stages of
// this path, we however want to add only one
// new DirCacheEntry per path.
else if (!(path.equals(lastAddedFile))) {
if (!(update && tw.getTree(0, DirCacheIterator.class) == null)) {
c = tw.getTree(0, DirCacheIterator.class);
if (f != null) { // the file exists
long sz = f.getEntryLength();
DirCacheEntry entry = new DirCacheEntry(path);
if (c == null || c.getDirCacheEntry() == null
|| !c.getDirCacheEntry().isAssumeValid()) {
FileMode mode = f.getIndexFileMode(c);
entry.setFileMode(mode);
if (FileMode.GITLINK != mode) {
entry.setLength(sz);
entry.setLastModified(f
.getEntryLastModified());
InputStream in = f.openEntryStream();
try {
entry.setObjectId(inserter.insert(
Constants.OBJ_BLOB, sz, in));
} finally {
in.close();
}
builder.add(entry);
lastAddedFile = path;
} else {
Repository subRepo = Git.open(
new File(repo.getWorkTree(), path))
.getRepository();
ObjectId subRepoHead = subRepo
.resolve(Constants.HEAD);
if (subRepoHead != null) {
entry.setObjectId(subRepoHead);
builder.add(entry);
lastAddedFile = path;
}
}
} else {
builder.add(c.getDirCacheEntry());
}
} else if (!update){
builder.add(c.getDirCacheEntry());
}
}
}
}
inserter.flush();
builder.commit();
setCallable(false);
} catch (IOException e) {
throw new JGitInternalException(
JGitText.get().exceptionCaughtDuringExecutionOfAddCommand, e);
} finally {
inserter.release();
if (dc != null)
dc.unlock();
}
return dc;