]> source.dussan.org Git - jgit.git/commitdiff
MergeCommand should create missing branches 41/2341/1
authorChristian Halstrick <christian.halstrick@sap.com>
Wed, 26 Jan 2011 15:30:13 +0000 (16:30 +0100)
committerChristian Halstrick <christian.halstrick@sap.com>
Wed, 26 Jan 2011 16:17:44 +0000 (17:17 +0100)
If HEAD exists but points to an not-existing branch the merge
command should silently create the missing branch and check
it out. This happens if you pull into freshly initalized repo.
HEAD points to refs/heads/master but refs/heads/master doesn't
exist. If you know merge a commit X into HEAD then the branch
master should be created (pointing to X) the working tree should
be updated to reflect X. That is achieved by checkout with one
tree only (HEAD is missing).

A test for this functionality will come the the next proposal
in PullCommandTest.

Change-Id: Id4a0d56d944e0acebd4b3157428bb50bd3fdd872
Signed-off-by: Christian Halstrick <christian.halstrick@sap.com>
org.eclipse.jgit/src/org/eclipse/jgit/api/MergeCommand.java

index 42e1a51ac92fb98a48ee2488821e782059fef3c2..29ce00e4482ae3f99fa885b05058c1ab1d4df2bb 100644 (file)
@@ -131,7 +131,6 @@ public class MergeCommand extends GitCommand<MergeResult> {
 
                        // Check for FAST_FORWARD, ALREADY_UP_TO_DATE
                        revWalk = new RevWalk(repo);
-                       RevCommit headCommit = revWalk.lookupCommit(head.getObjectId());
 
                        // we know for now there is only one commit
                        Ref ref = commits.get(0);
@@ -144,6 +143,30 @@ public class MergeCommand extends GitCommand<MergeResult> {
                                objectId = ref.getObjectId();
 
                        RevCommit srcCommit = revWalk.lookupCommit(objectId);
+
+                       ObjectId headId = head.getObjectId();
+                       if (headId == null) {
+                               revWalk.parseHeaders(srcCommit);
+                               DirCacheCheckout dco = new DirCacheCheckout(repo,
+                                               repo.lockDirCache(), srcCommit.getTree());
+                               dco.setFailOnConflict(true);
+                               dco.checkout();
+                               RefUpdate refUpdate = repo
+                                               .updateRef(head.getTarget().getName());
+                               refUpdate.setNewObjectId(objectId);
+                               refUpdate.setExpectedOldObjectId(null);
+                               refUpdate.setRefLogMessage("initial pull", false);
+                               if (refUpdate.update() != Result.NEW)
+                                       throw new NoHeadException(
+                                                       JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
+                               setCallable(false);
+                               return new MergeResult(srcCommit, srcCommit, new ObjectId[] {
+                                               null, srcCommit }, MergeStatus.FAST_FORWARD,
+                                               mergeStrategy, null, null);
+                       }
+
+                       RevCommit headCommit = revWalk.lookupCommit(headId);
+
                        if (revWalk.isMergedInto(srcCommit, headCommit)) {
                                setCallable(false);
                                return new MergeResult(headCommit, srcCommit, new ObjectId[] {
@@ -159,7 +182,7 @@ public class MergeCommand extends GitCommand<MergeResult> {
                                dco.setFailOnConflict(true);
                                dco.checkout();
 
-                               updateHead(refLogMessage, srcCommit, head.getObjectId());
+                               updateHead(refLogMessage, srcCommit, headId);
                                setCallable(false);
                                return new MergeResult(srcCommit, srcCommit, new ObjectId[] {
                                                headCommit, srcCommit }, MergeStatus.FAST_FORWARD,