Skip to content

Commit

Permalink
[FIXED JENKINS-19108]
Browse files Browse the repository at this point in the history
JGit was not honoring the branch in the checkoutBranch method
  • Loading branch information
kohsuke committed Sep 21, 2013
1 parent ec2fdfd commit 43bf22c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
@@ -1,7 +1,6 @@
package org.jenkinsci.plugins.gitclient;

import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import hudson.FilePath;
Expand Down Expand Up @@ -212,7 +211,7 @@ public void checkoutBranch(String branch, String ref) throws GitException {
throw new GitException("Could not update " + branch + " to " + ref);
}

checkout(ref);
checkout(branch);
} catch (IOException e) {
throw new GitException("Could not checkout " + branch + " with start point " + ref, e);
}
Expand Down
35 changes: 32 additions & 3 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Expand Up @@ -13,12 +13,13 @@
import hudson.util.StreamTaskListener;
import junit.framework.TestCase;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.RefSpec;
import org.junit.rules.ExpectedException;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.TemporaryDirectoryAllocator;

Expand Down Expand Up @@ -131,6 +132,13 @@ protected CliGitAPIImpl cgit() throws Exception {
return (CliGitAPIImpl)Git.with(listener, env).in(repo).using("git").getClient();
}

/**
* Creates a {@link Repository} object out of it.
*/
protected FileRepository repo() throws IOException {
return new FileRepository(new File(repo,".git"));
}

public void checkout(String branch) throws IOException, InterruptedException {
cmd("git checkout " + branch);
}
Expand All @@ -139,7 +147,11 @@ public void checkout(String branch) throws IOException, InterruptedException {
* Obtain the current HEAD revision
*/
ObjectId head() throws IOException, InterruptedException {
return ObjectId.fromString(w.cmd("git rev-parse HEAD").substring(0,40));
return revParse("HEAD");
}

ObjectId revParse(String commit) throws IOException, InterruptedException {
return ObjectId.fromString(w.cmd("git rev-parse "+commit).substring(0,40));
}

/**
Expand Down Expand Up @@ -772,6 +784,23 @@ public void test_branchContaining() throws Exception {
assertEquals("X",formatBranches(w.igit().getBranchesContaining("X")));
}

@Bug(19108)
public void test_checkoutBranch() throws Exception {
w.init();
w.commit("c1");
w.tag("t1");
w.commit("c2");

w.git.checkoutBranch("foo", "t1");

assertEquals(w.head(),w.revParse("t1"));
assertEquals(w.head(),w.revParse("foo"));

Ref head = w.repo().getRef("HEAD");
assertTrue(head.isSymbolic());
assertEquals("refs/heads/foo",head.getTarget().getName());
}

private String formatBranches(List<Branch> branches) {
Set<String> names = new TreeSet<String>();
for (Branch b : branches) {
Expand Down

0 comments on commit 43bf22c

Please sign in to comment.