I'm trying to commit added/modified/deleted/missing files (like commit -a). When I use the code below added/deleted/missing files are commited. Modified files are not commited within this step.
DirCache dirc = DirCache.lock(localRepository);
ObjectId tree = dirc.writeTree(new ObjectWriter(localRepository));
dirc.write();
dirc.commit();
ObjectId currentHead = localRepository.resolve(Constants.HEAD);
log.info("Current head ObjectId: " + currentHead.name());
ObjectId[] parentIds;
if (currentHead != null) {
parentIds = new ObjectId[] { currentHead };
} else {
parentIds = new ObjectId[0];
}
Commit commit = new Commit(localRepository, parentIds);
PersonIdent ident = new PersonIdent(developerName, developerEmail);
commit.setAuthor(ident);
commit.setCommitter(ident);
commit.setMessage(commitMessage);
commit.setTreeId(tree);
commit.commit();
log.info("Commited changes");
ObjectId id = commit.getCommitId();
log.info("ObjectId of this commit is: " + id.name());
RefUpdate ru = localRepository.updateRef(Constants.HEAD);
ru.setNewObjectId(id);
ru.setRefLogIdent(ident);
ru.setRefLogMessage(commitMessage, true);
Result result = ru.update();
log.info("Result is " + result.toString());
I also tried to use the DirCacheEditor and made a subclass MyPathEdit and implemented apply (see below) which obviously fails because the index file is locked by DirCache.
public void apply(DirCacheEntry ent) {
log.info("Apply invoked for entry " + ent.getPathString());
String entPath = repository.getWorkDir().getAbsolutePath() + File.separator + ent.getPathString();
try {
GitIndex index = repository.getIndex();
index.add(repository.getWorkDir(), new File(entPath));
index.write();
} catch (IOException e) {
e.printStackTrace();
}
}
So does someone have an idea on how it is meant to be done?