]> source.dussan.org Git - jgit.git/commitdiff
Fixed creation of branch from a tag 16/3816/3
authorSasa Zivkov <sasa.zivkov@sap.com>
Tue, 5 Jul 2011 12:06:49 +0000 (14:06 +0200)
committerChris Aniszczyk <caniszczyk@gmail.com>
Tue, 12 Jul 2011 15:48:59 +0000 (10:48 -0500)
Creation of a branch X from an annotated tag, as the starting point,
resulted into .git/refs/heads/X containing the ID of the annotated tag
instead of the ID of the tagged commit.

This fix peels the tag ref before using it as the starting point for
the newly created branch.

Bug: 340836
Change-Id: I01c7325770ecb37f5bf8ddb2a22f802466524f24
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest.java
org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java

index 7464a5ea3bb7395b33479fb96ea09e3f2a0cd51a..7760ec02d92d0f7bcb906eb9d308f43c32776f5c 100644 (file)
@@ -60,12 +60,14 @@ import org.eclipse.jgit.api.errors.NotMergedException;
 import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
 import org.eclipse.jgit.api.errors.RefNotFoundException;
 import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.RefUpdate;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.lib.RepositoryTestCase;
 import org.eclipse.jgit.lib.StoredConfig;
 import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevTag;
 import org.eclipse.jgit.transport.FetchResult;
 import org.eclipse.jgit.transport.RefSpec;
 import org.eclipse.jgit.transport.RemoteConfig;
@@ -247,6 +249,28 @@ public class BranchCommandTest extends RepositoryTestCase {
                assertEquals(newBranch.getTarget().getObjectId(), initialCommit.getId());
        }
 
+       @Test
+       public void testCreateFromLightweightTag() throws Exception {
+               RefUpdate rup = db.updateRef("refs/tags/V10");
+               rup.setNewObjectId(initialCommit);
+               rup.setExpectedOldObjectId(ObjectId.zeroId());
+               rup.update();
+
+               Ref branch = git.branchCreate().setName("FromLightweightTag")
+                               .setStartPoint("refs/tags/V10").call();
+               assertEquals(initialCommit.getId(), branch.getObjectId());
+
+       }
+
+       @Test
+       public void testCreateFromAnnotatetdTag() throws Exception {
+               RevTag tag = git.tag().setName("V10").setObjectId(secondCommit).call();
+               Ref branch = git.branchCreate().setName("FromAnnotatedTag")
+                               .setStartPoint("refs/tags/V10").call();
+               assertFalse(tag.getId().equals(branch.getObjectId()));
+               assertEquals(secondCommit.getId(), branch.getObjectId());
+       }
+
        @Test
        public void testDelete() throws Exception {
                createBranch(git, "ForDelete", false, "master", null);
index beee9a4ad5b9f851b88bcd6f787ed3e5c69ec514..ece16489f5b9d68b4de099c1f4a37fb76a30bc5e 100644 (file)
@@ -123,6 +123,7 @@ public class CreateBranchCommand extends GitCommand<Ref> {
                        RefNotFoundException, InvalidRefNameException {
                checkCallable();
                processOptions();
+               RevWalk revWalk = new RevWalk(repo);
                try {
                        Ref refToCheck = repo.getRef(name);
                        boolean exists = refToCheck != null
@@ -148,7 +149,7 @@ public class CreateBranchCommand extends GitCommand<Ref> {
                                if (startCommit != null)
                                        baseCommit = startCommit.getShortMessage();
                                else {
-                                       RevCommit commit = new RevWalk(repo).parseCommit(repo
+                                       RevCommit commit = revWalk.parseCommit(repo
                                                        .resolve(startPoint));
                                        baseCommit = commit.getShortMessage();
                                }
@@ -167,6 +168,7 @@ public class CreateBranchCommand extends GitCommand<Ref> {
                                else
                                        refLogMessage = "branch: Created from branch " + baseBranch;
                        } else {
+                               startAt = revWalk.peel(revWalk.parseAny(startAt));
                                if (exists)
                                        refLogMessage = "branch: Reset start-point to tag "
                                                        + startPointFullName;
@@ -268,6 +270,8 @@ public class CreateBranchCommand extends GitCommand<Ref> {
                        return result;
                } catch (IOException ioe) {
                        throw new JGitInternalException(ioe.getMessage(), ioe);
+               } finally {
+                       revWalk.release();
                }
        }