Skip to content

Commit

Permalink
Clean takes a flag that will add a --force flag to clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
roguishmountain committed Nov 17, 2016
1 parent c269ab8 commit 58a3225
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/main/java/hudson/plugins/git/GitAPI.java
Expand Up @@ -308,7 +308,8 @@ public void submoduleClean(boolean recursive) throws GitException {

/** {@inheritDoc} */
public void clean() throws GitException, InterruptedException {
if (Git.USE_CLI) super.clean(); else jgit.clean();
// false provides original functionality
if (Git.USE_CLI) super.clean(false); else jgit.clean(false);
}

/** {@inheritDoc} */
Expand Down
Expand Up @@ -670,12 +670,16 @@ public void execute() throws GitException, InterruptedException {
* Remove untracked files and directories, including files listed
* in the ignore rules.
*
* @param cleanSubmodule flag to add extra -f
* @throws hudson.plugins.git.GitException if underlying git operation fails.
* @throws java.lang.InterruptedException if interrupted.
*/
public void clean() throws GitException, InterruptedException {
public void clean(boolean cleanSubmodule) throws GitException, InterruptedException {
reset(true);
launchCommand("clean", "-fdx");
String cmd = "-fdx";
if (cleanSubmodule) cmd = "-ffdx";

launchCommand("clean", cmd);
}

/** {@inheritDoc} */
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java
Expand Up @@ -430,10 +430,11 @@ public interface GitClient {
* <a href="https://www.kernel.org/pub/software/scm/git/docs/git-clean.html">git-clean(1)</a> for working copy to
* match a fresh clone.
*
* @param cleanSubmodule flag to add extra -f
* @throws hudson.plugins.git.GitException if underlying git operation fails.
* @throws java.lang.InterruptedException if interrupted.
*/
void clean() throws GitException, InterruptedException;
void clean(boolean cleanSubmodule) throws GitException, InterruptedException;



Expand Down
Expand Up @@ -1178,13 +1178,14 @@ void format(RevCommit commit, @Nullable RevCommit parent, PrintWriter pw, Boolea
/**
* clean.
*
* @param cleanSubmodule flag to add extra -f
* @throws hudson.plugins.git.GitException if underlying git operation fails.
*/
public void clean() throws GitException {
public void clean(boolean cleanSubmodule) throws GitException {
try (Repository repo = getRepository()) {
Git git = git(repo);
git.reset().setMode(HARD).call();
git.clean().setCleanDirectories(true).setIgnore(false).call();
git.clean().setCleanDirectories(true).setIgnore(false).setForce(cleanSubmodule).call();
} catch (GitAPIException e) {
throw new GitException(e);
}
Expand Down Expand Up @@ -1914,7 +1915,7 @@ private Iterable<JGitAPIImpl> submodules() throws IOException {
public void submoduleClean(boolean recursive) throws GitException {
try {
for (JGitAPIImpl sub : submodules()) {
sub.clean();
sub.clean(false);
if (recursive) {
sub.submoduleClean(true);
}
Expand Down
Expand Up @@ -445,8 +445,8 @@ public void prune(RemoteConfig repository) throws GitException, InterruptedExcep
* @throws hudson.plugins.git.GitException if underlying git operation fails.
* @throws java.lang.InterruptedException if interrupted.
*/
public void clean() throws GitException, InterruptedException {
proxy.clean();
public void clean(boolean cleanSubmodule) throws GitException, InterruptedException {
proxy.clean(cleanSubmodule);
}

/** {@inheritDoc} */
Expand Down
Expand Up @@ -828,16 +828,27 @@ public void test_clean() throws Exception {
w.touch(fileName2);
w.touch(fileName, "new content");

w.git.clean();
String dirName3 = "dir-with-submodule";
File submodule = w.file(dirName3);
assertTrue("Did not create dir " + dirName3, submodule.mkdir());
WorkingArea workingArea = new WorkingArea(submodule);
workingArea.init();
workingArea.commitEmpty("init");

w.git.clean(false);
assertFalse(w.exists(dirName1));
assertFalse(w.exists(fileName1));
assertFalse(w.exists(fileName2));
assertTrue(w.exists(dirName3));
assertEquals("content " + fileName, w.contentOf(fileName));
assertEquals("content " + fileNameFace, w.contentOf(fileNameFace));
assertEquals("content " + fileNameSwim, w.contentOf(fileNameSwim));
String status = w.cmd("git status");
assertTrue("unexpected status " + status, status.contains("working directory clean") || status.contains("working tree clean"));

w.git.clean(true);
assertFalse(w.exists(dirName3));

/* A few poorly placed tests of hudson.FilePath - testing JENKINS-22434 */
FilePath fp = new FilePath(w.file(fileName));
assertTrue(fp + " missing", fp.exists());
Expand Down

0 comments on commit 58a3225

Please sign in to comment.