]> source.dussan.org Git - gitblit.git/commitdiff
New function to create an orphan branch (for future use)
authorJames Moger <james.moger@gitblit.com>
Tue, 10 Jan 2012 03:06:42 +0000 (22:06 -0500)
committerJames Moger <james.moger@gitblit.com>
Tue, 10 Jan 2012 03:06:42 +0000 (22:06 -0500)
src/com/gitblit/utils/JGitUtils.java
tests/com/gitblit/tests/JGitUtilsTest.java

index 2a99c97368c17b4d86219a3e40046f28f4be83db..a540c2aa481b44d4e343f0a2d380b1fbdf329849 100644 (file)
@@ -47,15 +47,20 @@ import org.eclipse.jgit.errors.ConfigInvalidException;
 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
@@ -1360,37 +1365,74 @@ public class JGitUtils {
        }\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
index 4e85b49ca5808a2d5bfee9d045782c886b0f5184..7c3f8ab0af563f917eaccf40d56f9432de0e29a9 100644 (file)
@@ -217,8 +217,8 @@ public class JGitUtilsTest {
        public void testCreateOrphanedBranch() throws Exception {\r
                Repository repository = JGitUtils.createRepository(GitBlitSuite.REPOSITORIES, "orphantest");\r
                assertTrue(JGitUtils.createOrphanBranch(repository,\r
-                               "x" + Long.toHexString(System.currentTimeMillis()).toUpperCase()));\r
-               FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);\r
+                               "x" + Long.toHexString(System.currentTimeMillis()).toUpperCase(), null));\r
+                FileUtils.delete(repository.getDirectory(), FileUtils.RECURSIVE);\r
        }\r
 \r
        @Test\r