import org.eclipse.jgit.errors.IncorrectObjectTypeException;\r
import org.eclipse.jgit.errors.MissingObjectException;\r
import org.eclipse.jgit.errors.StopWalkException;\r
+import org.eclipse.jgit.lib.CommitBuilder;\r
import org.eclipse.jgit.lib.Constants;\r
import org.eclipse.jgit.lib.FileMode;\r
import org.eclipse.jgit.lib.ObjectId;\r
+import org.eclipse.jgit.lib.ObjectInserter;\r
import org.eclipse.jgit.lib.ObjectLoader;\r
import org.eclipse.jgit.lib.PersonIdent;\r
import org.eclipse.jgit.lib.Ref;\r
+import org.eclipse.jgit.lib.RefUpdate;\r
+import org.eclipse.jgit.lib.RefUpdate.Result;\r
import org.eclipse.jgit.lib.Repository;\r
import org.eclipse.jgit.lib.RepositoryCache.FileKey;\r
import org.eclipse.jgit.lib.StoredConfig;\r
+import org.eclipse.jgit.lib.TreeFormatter;\r
import org.eclipse.jgit.revwalk.RevBlob;\r
import org.eclipse.jgit.revwalk.RevCommit;\r
import org.eclipse.jgit.revwalk.RevObject;\r
}\r
\r
/**\r
- * Create an orphaned branch in a repository. This code does not work.\r
+ * Create an orphaned branch in a repository.\r
* \r
* @param repository\r
- * @param name\r
- * @return\r
+ * @param branchName\r
+ * @param author\r
+ * if unspecified, Gitblit will be the author of this new branch\r
+ * @return true if successful\r
*/\r
- public static boolean createOrphanBranch(Repository repository, String name) {\r
- return true;\r
- // boolean success = false;\r
- // try {\r
- // ObjectId prev = repository.resolve(Constants.HEAD + "^1");\r
- // // create the orphan branch\r
- // RefUpdate orphanRef = repository.updateRef(Constants.R_HEADS + name);\r
- // orphanRef.setNewObjectId(prev);\r
- // orphanRef.setExpectedOldObjectId(ObjectId.zeroId());\r
- // Result updateResult = orphanRef.update();\r
- //\r
- // switch (updateResult) {\r
- // case NEW:\r
- // success = true;\r
- // break;\r
- // case NO_CHANGE:\r
- // default:\r
- // break;\r
- // }\r
- //\r
- // } catch (Throwable t) {\r
- // error(t, repository, "{0} failed to create orphaned branch {1}",\r
- // name);\r
- // }\r
- // return success;\r
+ public static boolean createOrphanBranch(Repository repository, String branchName,\r
+ PersonIdent author) {\r
+ boolean success = false;\r
+ String message = "Created branch " + branchName;\r
+ if (author == null) {\r
+ author = new PersonIdent("Gitblit", "gitblit@localhost");\r
+ }\r
+ try {\r
+ ObjectInserter odi = repository.newObjectInserter();\r
+ try {\r
+ // Create a blob object to insert into a tree\r
+ ObjectId blobId = odi.insert(Constants.OBJ_BLOB,\r
+ message.getBytes(Constants.CHARACTER_ENCODING));\r
+\r
+ // Create a tree object to reference from a commit\r
+ TreeFormatter tree = new TreeFormatter();\r
+ tree.append("NEWBRANCH", FileMode.REGULAR_FILE, blobId);\r
+ ObjectId treeId = odi.insert(tree);\r
+\r
+ // Create a commit object\r
+ CommitBuilder commit = new CommitBuilder();\r
+ commit.setAuthor(author);\r
+ commit.setCommitter(author);\r
+ commit.setEncoding(Constants.CHARACTER_ENCODING);\r
+ commit.setMessage(message);\r
+ commit.setTreeId(treeId);\r
+\r
+ // Insert the commit into the repository\r
+ ObjectId commitId = odi.insert(commit);\r
+ odi.flush();\r
+\r
+ RevWalk revWalk = new RevWalk(repository);\r
+ try {\r
+ RevCommit revCommit = revWalk.parseCommit(commitId);\r
+ if (!branchName.startsWith("refs/")) {\r
+ branchName = "refs/heads/" + branchName;\r
+ }\r
+ RefUpdate ru = repository.updateRef(branchName);\r
+ ru.setNewObjectId(commitId);\r
+ ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);\r
+ Result rc = ru.forceUpdate();\r
+ switch (rc) {\r
+ case NEW:\r
+ case FORCED:\r
+ case FAST_FORWARD:\r
+ success = true;\r
+ break;\r
+ default:\r
+ success = false;\r
+ }\r
+ } finally {\r
+ revWalk.release();\r
+ }\r
+ } finally {\r
+ odi.release();\r
+ }\r
+ } catch (Throwable t) {\r
+ error(t, repository, "Failed to create orphan branch {1} in repository {0}", branchName);\r
+ }\r
+ return success;\r
}\r
\r
/**\r