Skip to content

Commit

Permalink
Merge pull request #153 from pauxus/JENKINS-25444-fetch-clean
Browse files Browse the repository at this point in the history
[JENKINS-25444] CliGit implementation of fetch/deleteBranch does remove
  • Loading branch information
MarkEWaite committed Nov 7, 2014
2 parents 797f600 + b8725a5 commit 88ca6b4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Expand Up @@ -60,6 +60,7 @@
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.diff.RenameDetector;
import org.eclipse.jgit.errors.InvalidPatternException;
import org.eclipse.jgit.errors.LockFailedException;
import org.eclipse.jgit.errors.NotSupportedException;
import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.fnmatch.FileNameMatcher;
Expand Down Expand Up @@ -227,7 +228,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 @@ -240,9 +241,24 @@ private void doCheckout(String ref) throws GitException {
while (true) {
try {
repo = getRepository();
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
git(repo).reset().setMode(HARD).call();
} catch (GitAPIException e) {
throw new GitException("Could not reset the workspace before checkout of " + ref, e);
} catch (JGitInternalException e) {
if (e.getCause() instanceof LockFailedException){
throw new GitLockFailedException("Could not lock repository. Please try again", e);
} else {
throw e;
}
}

if (repo.resolve(ref) != null) {
// ref is either an existing reference or a shortcut to a tag or branch (without refs/heads/
// ref is either an existing reference or a shortcut to a tag or branch (without refs/heads/)
git(repo).checkout().setName(ref).setForce(true).call();
return;
}
Expand Down Expand Up @@ -324,13 +340,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 @@ -339,12 +353,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
15 changes: 15 additions & 0 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Expand Up @@ -50,6 +50,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
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 @@ -2197,6 +2198,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 88ca6b4

Please sign in to comment.