PushCommand: Test for update of tracking branch

Bug 317411 (Push does not update remote tracking branch) is assigned to
JGit. This test verifies that JGit does the right thing.

Bug: 317411
Change-Id: I8f632e3e6c8a4f16a1170b1dba92e8fd3d6267d0
This commit is contained in:
Robin Stocker 2011-02-28 19:12:37 +01:00
parent 18833252c6
commit 4fb185ab67
1 changed files with 53 additions and 0 deletions

View File

@ -43,6 +43,7 @@
package org.eclipse.jgit.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.IOException;
@ -51,13 +52,16 @@
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.errors.MissingObjectException;
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.PushResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.TrackingRefUpdate;
import org.eclipse.jgit.transport.URIish;
import org.junit.Test;
@ -99,4 +103,53 @@ public void testPush() throws JGitInternalException, IOException,
assertEquals(tag.getId(), db2.resolve(tag.getId().getName()));
}
@Test
public void testTrackingUpdate() throws Exception {
Repository db2 = createBareRepository();
String remote = "origin";
String branch = "refs/heads/master";
String trackingBranch = "refs/remotes/" + remote + "/master";
Git git = new Git(db);
RevCommit commit1 = git.commit().setMessage("Initial commit")
.call();
RefUpdate branchRefUpdate = db.updateRef(branch);
branchRefUpdate.setNewObjectId(commit1.getId());
branchRefUpdate.update();
RefUpdate trackingBranchRefUpdate = db.updateRef(trackingBranch);
trackingBranchRefUpdate.setNewObjectId(commit1.getId());
trackingBranchRefUpdate.update();
final StoredConfig config = db.getConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, remote);
URIish uri = new URIish(db2.getDirectory().toURI().toURL());
remoteConfig.addURI(uri);
remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
+ remote + "/*"));
remoteConfig.update(config);
config.save();
RevCommit commit2 = git.commit().setMessage("Commit to push").call();
RefSpec spec = new RefSpec(branch + ":" + branch);
Iterable<PushResult> resultIterable = git.push().setRemote(remote)
.setRefSpecs(spec).call();
PushResult result = resultIterable.iterator().next();
TrackingRefUpdate trackingRefUpdate = result
.getTrackingRefUpdate(trackingBranch);
assertNotNull(trackingRefUpdate);
assertEquals(trackingBranch, trackingRefUpdate.getLocalName());
assertEquals(branch, trackingRefUpdate.getRemoteName());
assertEquals(commit2.getId(), trackingRefUpdate.getNewObjectId());
assertEquals(commit2.getId(), db.resolve(trackingBranch));
assertEquals(commit2.getId(), db2.resolve(branch));
}
}