Skip to content

Commit

Permalink
[JENKINS-25444] CliGit implementation of fetch/deleteBranch does remove
Browse files Browse the repository at this point in the history
open changes, JGit does not

Created testcase, perform a reset --hard in case of ref checkout an
branch with delete checkout to simulate CLI behaviour.
  • Loading branch information
pauxus committed Nov 5, 2014
1 parent 6296a9a commit b96e07e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
28 changes: 18 additions & 10 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Expand Up @@ -206,7 +206,7 @@ public void execute() throws GitException, InterruptedException {
if (branch == null)
doCheckout(ref);
else if (deleteBranch)
doCheckoutBranch(branch, ref);
doCheckoutCleanBranch(branch, ref);
else
doCheckout(ref, branch);
}
Expand All @@ -219,7 +219,17 @@ private void doCheckout(String ref) throws GitException {
while (true) {
try {
repo = getRepository();
git(repo).checkout().setName(ref).setForce(true).call();
try {
// force in Jgit is "-B" in Git CLI, meaning no forced switch,
// but forces recreation of the branch.
// we need to take back all open changes to get the equivalent
// of git checkout -f
new Git(repo).reset().setMode(HARD).call();
} catch (GitAPIException e) {
throw new GitException("Could not reset the workspace before checkout of " + ref, e);
}

git(repo).checkout().setName(ref).call();
return;
} catch (CheckoutConflictException e) {
if (repo != null) {
Expand Down Expand Up @@ -271,13 +281,11 @@ private void doCheckout(String ref, String branch) throws GitException {
}
}

private void doCheckoutBranch(String branch, String ref) throws GitException {
private void doCheckoutCleanBranch(String branch, String ref) throws GitException {
Repository repo = null;
try {
repo = getRepository();
RefUpdate refUpdate =
branch == null ? repo.updateRef(Constants.HEAD, true)
: repo.updateRef(R_HEADS + branch);
RefUpdate refUpdate = repo.updateRef(R_HEADS + branch);
refUpdate.setNewObjectId(repo.resolve(ref));
switch (refUpdate.forceUpdate()) {
case NOT_ATTEMPTED:
Expand All @@ -286,12 +294,12 @@ private void doCheckoutBranch(String branch, String ref) throws GitException {
case REJECTED_CURRENT_BRANCH:
case IO_FAILURE:
case RENAMED:
throw new GitException("Could not update " + (branch!= null ? branch : "") + " to " + ref);
throw new GitException("Could not update " + branch + " to " + ref);
}

if (branch != null) doCheckout(branch);
doCheckout(branch);
} catch (IOException e) {
throw new GitException("Could not checkout " + (branch!= null ? branch : "") + " with start point " + ref, e);
throw new GitException("Could not checkout " + branch + " with start point " + ref, e);
} finally {
if (repo != null) repo.close();
}
Expand Down Expand Up @@ -1681,7 +1689,7 @@ public void execute() throws GitException, InterruptedException {
listener.getLogger().println("[ERROR] JGit doesn't support submodule update --reference yet.");
throw new UnsupportedOperationException("not implemented yet");
}

try {
repo = getRepository();
git(repo).submoduleUpdate().call();
Expand Down
41 changes: 28 additions & 13 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Expand Up @@ -27,6 +27,7 @@
import static org.junit.Assert.assertNotEquals;

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ConfigConstants;
Expand Down Expand Up @@ -68,7 +69,7 @@
public abstract class GitAPITestCase extends TestCase {

public final TemporaryDirectoryAllocator temporaryDirectoryAllocator = new TemporaryDirectoryAllocator();

protected hudson.EnvVars env = new hudson.EnvVars();
protected TaskListener listener;

Expand All @@ -81,15 +82,15 @@ public abstract class GitAPITestCase extends TestCase {
/**
* One local workspace of a Git repository on a temporary directory
* that gets automatically cleaned up in the end.
*
*
* Every test case automatically gets one in {@link #w} but additional ones can be created if multi-repository
* interactions need to be tested.
*/
class WorkingArea {
final File repo;
final GitClient git;
boolean bare = false;

WorkingArea() throws Exception {
this(temporaryDirectoryAllocator.allocate());
}
Expand Down Expand Up @@ -120,7 +121,7 @@ private void setupProxy(GitClient gitClient)
gitClient.setProxy(proxyConfig);
}

private void setField(Class<?> clazz, String fieldName, Object object, Object value)
private void setField(Class<?> clazz, String fieldName, Object object, Object value)
throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException
{
Field declaredField = clazz.getDeclaredField(fieldName);
Expand All @@ -140,11 +141,11 @@ private String getSystemProperty(String ... keyVariants)
String cmd(String args) throws IOException, InterruptedException {
return launchCommand(args.split(" "));
}

String cmd(boolean ignoreError, String args) throws IOException, InterruptedException {
return launchCommand(ignoreError, args.split(" "));
}

String launchCommand(String... args) throws IOException, InterruptedException {
return launchCommand(false, args);
}
Expand All @@ -166,7 +167,7 @@ String launchCommand(boolean ignoreError, String... args) throws IOException, In
String repoPath() {
return repo.getAbsolutePath();
}

WorkingArea init() throws IOException, InterruptedException {
git.init();
return this;
Expand Down Expand Up @@ -262,7 +263,7 @@ void adaptCliGitClone(String repoName) throws IOException, InterruptedException
}
}
}

private WorkingArea w;

WorkingArea clone(String src) throws Exception {
Expand Down Expand Up @@ -326,7 +327,7 @@ protected void setUp() throws Exception {
/* HEAD ref of local mirror - all read access should use getMirrorHead */
private static ObjectId mirrorHead = null;

private ObjectId getMirrorHead() throws IOException, InterruptedException
private ObjectId getMirrorHead() throws IOException, InterruptedException
{
if (mirrorHead == null) {
final String mirrorPath = new File(localMirror()).getAbsolutePath();
Expand Down Expand Up @@ -606,7 +607,7 @@ public void test_lsTree_recursive() throws IOException, InterruptedException {
assertEquals("Wrong origin default remote", "origin", w.igit().getDefaultRemote("origin"));
assertEquals("Wrong invalid default remote", "origin", w.igit().getDefaultRemote("invalid"));
}

@Deprecated
public void test_getRemoteURL_two_args() throws Exception {
w.init();
Expand Down Expand Up @@ -872,7 +873,7 @@ public void test_fetch() throws Exception {
* and later, it should be bareCommit5. */
assertEquals("null refSpec fetch modified local repo", expectedHead, newArea.head());

try {
try {
/* Fetch into newArea repo with invalid repo name and no RefSpec */
newArea.git.fetch("invalid-remote-name");
fail("Should have thrown an exception");
Expand Down Expand Up @@ -1614,7 +1615,7 @@ public void test_trackingSubmodule() throws Exception {
r.touch("file1", "content1");
r.git.add("file1");
r.git.commit("submod-commit1");

// Add new GIT repo to w
String subModDir = "submod1-" + java.util.UUID.randomUUID().toString();
w.git.addSubmodule(r.repoPath(), subModDir);
Expand Down Expand Up @@ -1898,7 +1899,7 @@ private void checkSymlinkSetting(WorkingArea area) throws IOException {
}
assertEquals(expected, symlinkValue);
}

public void test_init() throws Exception {
assertFalse(w.file(".git").exists());
w.git.init();
Expand Down Expand Up @@ -2195,6 +2196,20 @@ public void test_getHeadRev() throws Exception {
assertEquals("Wrong SHA1 for git-client-1.10.0 tag", expectedTag, knownTag);
}

@Bug(25444)
public void test_fetch_delete_cleans() throws Exception {
w.init();
w.touch("file1", "old");
w.git.add("file1");
w.git.commit("commit1");
w.touch("file1", "new");
w.git.checkout().branch("other").ref(Constants.HEAD).deleteBranchIfExist(true).execute();;

Status status = new org.eclipse.jgit.api.Git(w.repo()).status().call();

assertTrue("Workspace must be clean", status.isClean());
}

/**
* Test getHeadRev with wildcard matching in the branch name.
* Relies on the branches in the git-client-plugin repository
Expand Down

0 comments on commit b96e07e

Please sign in to comment.