浏览代码

Fixed creation of branch from a tag

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>
tags/v1.1.0.201109011030-rc2
Sasa Zivkov 13 年前
父节点
当前提交
1d4a1fe772

+ 24
- 0
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/BranchCommandTest.java 查看文件

import org.eclipse.jgit.api.errors.RefAlreadyExistsException; import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
import org.eclipse.jgit.api.errors.RefNotFoundException; import org.eclipse.jgit.api.errors.RefNotFoundException;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryTestCase; import org.eclipse.jgit.lib.RepositoryTestCase;
import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.transport.FetchResult; import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.RefSpec; import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.RemoteConfig;
assertEquals(newBranch.getTarget().getObjectId(), initialCommit.getId()); 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 @Test
public void testDelete() throws Exception { public void testDelete() throws Exception {
createBranch(git, "ForDelete", false, "master", null); createBranch(git, "ForDelete", false, "master", null);

+ 5
- 1
org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java 查看文件

RefNotFoundException, InvalidRefNameException { RefNotFoundException, InvalidRefNameException {
checkCallable(); checkCallable();
processOptions(); processOptions();
RevWalk revWalk = new RevWalk(repo);
try { try {
Ref refToCheck = repo.getRef(name); Ref refToCheck = repo.getRef(name);
boolean exists = refToCheck != null boolean exists = refToCheck != null
if (startCommit != null) if (startCommit != null)
baseCommit = startCommit.getShortMessage(); baseCommit = startCommit.getShortMessage();
else { else {
RevCommit commit = new RevWalk(repo).parseCommit(repo
RevCommit commit = revWalk.parseCommit(repo
.resolve(startPoint)); .resolve(startPoint));
baseCommit = commit.getShortMessage(); baseCommit = commit.getShortMessage();
} }
else else
refLogMessage = "branch: Created from branch " + baseBranch; refLogMessage = "branch: Created from branch " + baseBranch;
} else { } else {
startAt = revWalk.peel(revWalk.parseAny(startAt));
if (exists) if (exists)
refLogMessage = "branch: Reset start-point to tag " refLogMessage = "branch: Reset start-point to tag "
+ startPointFullName; + startPointFullName;
return result; return result;
} catch (IOException ioe) { } catch (IOException ioe) {
throw new JGitInternalException(ioe.getMessage(), ioe); throw new JGitInternalException(ioe.getMessage(), ioe);
} finally {
revWalk.release();
} }
} }



正在加载...
取消
保存