public static void updateGhPages(File repositoryFolder, File sourceFolder,
boolean obliterate) {
String ghpages = "refs/heads/gh-pages";
try {
File gitDir = FileKey.resolve(repositoryFolder, FS.DETECTED);
Repository repository = new FileRepository(gitDir);
ObjectId objectId = repository.resolve(ghpages);
if (objectId == null) {
JGitUtils.createOrphanBranch(repository, "gh-pages", null);
}
System.out.println("Updating gh-pages branch...");
ObjectId headId = repository.resolve(ghpages + "^{commit}");
ObjectInserter odi = repository.newObjectInserter();
try {
// Create the in-memory index of the new/updated issue.
DirCache index = createIndex(repository, headId, sourceFolder,
obliterate);
ObjectId indexTreeId = index.writeTree(odi);
// Create a commit object
PersonIdent author = new PersonIdent("Moxie",
"moxie@localhost");
CommitBuilder commit = new CommitBuilder();
commit.setAuthor(author);
commit.setCommitter(author);
commit.setEncoding(Constants.CHARACTER_ENCODING);
commit.setMessage("updated pages");
commit.setParentId(headId);
commit.setTreeId(indexTreeId);
// Insert the commit into the repository
ObjectId commitId = odi.insert(commit);
odi.flush();
RevWalk revWalk = new RevWalk(repository);
try {
RevCommit revCommit = revWalk.parseCommit(commitId);
RefUpdate ru = repository.updateRef(ghpages);
ru.setNewObjectId(commitId);
ru.setExpectedOldObjectId(headId);
ru.setRefLogMessage(
"commit: " + revCommit.getShortMessage(), false);
Result rc = ru.forceUpdate();