Skip to content

Commit 3121391

Browse files
authored
Merge pull request #948 from MarkEWaite/no-empty-dirs-on-cache-lookup
[JENKINS-63541] Don't create empty dirs in chooser
2 parents 6745835 + 2cf7ef7 commit 3121391

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,15 +1219,23 @@ protected String getCacheEntry() {
12191219
}
12201220

12211221
protected static File getCacheDir(String cacheEntry) {
1222+
return getCacheDir(cacheEntry, true);
1223+
}
1224+
1225+
protected static File getCacheDir(String cacheEntry, boolean createDirectory) {
12221226
Jenkins jenkins = Jenkins.getInstanceOrNull();
12231227
if (jenkins == null) {
12241228
return null;
12251229
}
12261230
File cacheDir = new File(new File(jenkins.getRootDir(), "caches"), cacheEntry);
12271231
if (!cacheDir.isDirectory()) {
1228-
boolean ok = cacheDir.mkdirs();
1229-
if (!ok) {
1230-
LOGGER.log(Level.WARNING, "Failed mkdirs of {0}", cacheDir);
1232+
if (createDirectory) {
1233+
boolean ok = cacheDir.mkdirs();
1234+
if (!ok) {
1235+
LOGGER.log(Level.WARNING, "Failed mkdirs of {0}", cacheDir);
1236+
}
1237+
} else {
1238+
cacheDir = null;
12311239
}
12321240
}
12331241
return cacheDir;

src/main/java/jenkins/plugins/git/GitToolChooser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public GitToolChooser(String remoteName, Item projectContext, String credentials
7575
private boolean decideAndUseCache(String remoteName) throws IOException, InterruptedException {
7676
boolean useCache = false;
7777
String cacheEntry = AbstractGitSCMSource.getCacheEntry(remoteName);
78-
File cacheDir = AbstractGitSCMSource.getCacheDir(cacheEntry);
78+
File cacheDir = AbstractGitSCMSource.getCacheDir(cacheEntry, false);
7979
if (cacheDir != null) {
8080
Git git = Git.with(TaskListener.NULL, new EnvVars(EnvVars.masterEnvVars)).in(cacheDir).using("git");
8181
GitClient client = git.getClient();

src/test/java/jenkins/plugins/git/GitToolChooserTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@
2121
import org.junit.Before;
2222
import org.junit.Rule;
2323
import org.junit.Test;
24+
import org.jvnet.hudson.test.Issue;
2425
import org.jvnet.hudson.test.JenkinsRule;
2526
import org.jvnet.hudson.test.TestExtension;
2627
import org.mockito.Mockito;
2728

29+
import java.io.File;
2830
import java.io.IOException;
2931
import java.util.Collections;
3032
import java.util.List;
3133

34+
import static org.hamcrest.io.FileMatchers.*;
3235
import static org.hamcrest.MatcherAssert.assertThat;
3336
import static org.hamcrest.Matchers.*;
3437
import static org.junit.Assert.assertEquals;
@@ -420,6 +423,29 @@ public void testGitToolChooserWithAllTools_2() throws Exception {
420423
GitToolChooser sizeEstimator = new GitToolChooser(remote, list.get(0), "github", gitExe, true);
421424
assertThat(sizeEstimator.getGitTool(), is(SystemUtils.IS_OS_WINDOWS ? "git.exe" : "git"));
422425
}
426+
427+
@Test
428+
@Issue("JENKINS-63541")
429+
public void getCacheDirCreatesNoDirectory() throws Exception {
430+
// Generate a unique repository name and compute expected cache directory
431+
String remoteName = "https://github.com/jenkinsci/git-plugin-" + java.util.UUID.randomUUID().toString() + ".git";
432+
String cacheEntry = AbstractGitSCMSource.getCacheEntry(remoteName);
433+
File expectedCacheDir = new File(new File(jenkins.jenkins.getRootDir(), "caches"), cacheEntry);
434+
435+
// Directory should not exist
436+
assertThat(expectedCacheDir, is(not(anExistingFileOrDirectory())));
437+
438+
// Getting the cache directory will not create an empty directory
439+
File nullCacheDir = AbstractGitSCMSource.getCacheDir(cacheEntry, false);
440+
assertThat(nullCacheDir, is(nullValue()));
441+
assertThat(expectedCacheDir, is(not(anExistingFileOrDirectory())));
442+
443+
// Getting the cache directory will create an empty directory
444+
File cacheDir = AbstractGitSCMSource.getCacheDir(cacheEntry, true);
445+
assertThat(cacheDir, is(anExistingDirectory()));
446+
assertThat(expectedCacheDir, is(anExistingDirectory()));
447+
}
448+
423449
/*
424450
A test extension implemented to clone the behavior of a plugin extending the capability of providing the size of
425451
repo from a remote URL of "Github".

0 commit comments

Comments
 (0)