use of org.eclipse.jgit.lib.TreeFormatter in project gitblit by gitblit.
the class JGitUtils method createOrphanBranch.
/**
* Create an orphaned branch in a repository.
*
* @param repository
* @param branchName
* @param author
* if unspecified, Gitblit will be the author of this new branch
* @return true if successful
*/
public static boolean createOrphanBranch(Repository repository, String branchName, PersonIdent author) {
boolean success = false;
String message = "Created branch " + branchName;
if (author == null) {
author = new PersonIdent("Gitblit", "gitblit@localhost");
}
try {
ObjectInserter odi = repository.newObjectInserter();
try {
// Create a blob object to insert into a tree
ObjectId blobId = odi.insert(Constants.OBJ_BLOB, message.getBytes(Constants.CHARACTER_ENCODING));
// Create a tree object to reference from a commit
TreeFormatter tree = new TreeFormatter();
tree.append(".branch", FileMode.REGULAR_FILE, blobId);
ObjectId treeId = odi.insert(tree);
// Create a commit object
CommitBuilder commit = new CommitBuilder();
commit.setAuthor(author);
commit.setCommitter(author);
commit.setEncoding(Constants.CHARACTER_ENCODING);
commit.setMessage(message);
commit.setTreeId(treeId);
// Insert the commit into the repository
ObjectId commitId = odi.insert(commit);
odi.flush();
RevWalk revWalk = new RevWalk(repository);
try {
RevCommit revCommit = revWalk.parseCommit(commitId);
if (!branchName.startsWith("refs/")) {
branchName = "refs/heads/" + branchName;
}
RefUpdate ru = repository.updateRef(branchName);
ru.setNewObjectId(commitId);
ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
Result rc = ru.forceUpdate();
switch(rc) {
case NEW:
case FORCED:
case FAST_FORWARD:
success = true;
break;
default:
success = false;
}
} finally {
revWalk.close();
}
} finally {
odi.close();
}
} catch (Throwable t) {
error(t, repository, "Failed to create orphan branch {1} in repository {0}", branchName);
}
return success;
}
Aggregations