Skip to content

Commit 170f24f

Browse files
authored
Merge pull request #1273 from dwnusbaum/JENKINS-68562
[JENKINS-68562] Fix Git checkouts for controllers running on Windows
2 parents 4c49869 + 83580ff commit 170f24f

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/main/java/hudson/plugins/git/GitSCM.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import java.io.Serializable;
7979
import java.io.Writer;
8080
import java.nio.file.Files;
81+
import java.nio.file.InvalidPathException;
8182
import java.nio.file.Paths;
8283
import java.text.MessageFormat;
8384
import java.util.AbstractList;
@@ -1393,17 +1394,32 @@ public void checkout(Run<?, ?> build, Launcher launcher, FilePath workspace, Tas
13931394
}
13941395
}
13951396

1396-
private void abortIfSourceIsLocal() throws AbortException {
1397+
/* Package protected for test access */
1398+
void abortIfSourceIsLocal() throws AbortException {
13971399
for (UserRemoteConfig userRemoteConfig: getUserRemoteConfigs()) {
13981400
String remoteUrl = userRemoteConfig.getUrl();
1399-
if (remoteUrl != null && (remoteUrl.toLowerCase(Locale.ENGLISH).startsWith("file://") || Files.exists(Paths.get(remoteUrl)))) {
1401+
if (!isRemoteUrlValid(remoteUrl)) {
14001402
throw new AbortException("Checkout of Git remote '" + remoteUrl + "' aborted because it references a local directory, " +
14011403
"which may be insecure. You can allow local checkouts anyway by setting the system property '" +
14021404
ALLOW_LOCAL_CHECKOUT_PROPERTY + "' to true.");
14031405
}
14041406
}
14051407
}
14061408

1409+
private static boolean isRemoteUrlValid(String remoteUrl) {
1410+
if (remoteUrl == null) {
1411+
return true;
1412+
} else if (remoteUrl.toLowerCase(Locale.ENGLISH).startsWith("file://")) {
1413+
return false;
1414+
}
1415+
try {
1416+
// Check for local remotes with no protocol like /path/to/repo.git/
1417+
return !Files.exists(Paths.get(remoteUrl));
1418+
} catch (InvalidPathException e) {
1419+
return true;
1420+
}
1421+
}
1422+
14071423
private void printCommitMessageToLog(TaskListener listener, GitClient git, final Build revToBuild)
14081424
throws IOException {
14091425
try {

src/test/java/hudson/plugins/git/GitSCMUnitTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package hudson.plugins.git;
2525

26+
import hudson.AbortException;
2627
import hudson.EnvVars;
2728
import static hudson.plugins.git.GitSCM.createRepoList;
2829
import hudson.plugins.git.browser.GitRepositoryBrowser;
@@ -43,6 +44,8 @@
4344
import org.junit.Test;
4445
import static org.junit.Assert.assertFalse;
4546
import static org.junit.Assert.assertTrue;
47+
import static org.junit.Assert.fail;
48+
import org.jvnet.hudson.test.Issue;
4649
import static org.hamcrest.MatcherAssert.*;
4750
import static org.hamcrest.Matchers.*;
4851

@@ -338,4 +341,17 @@ public void testSetDoGenerateSubmoduleConfigurations() {
338341
/* Confirms the passed value `true` is ignored */
339342
assertFalse(gitSCM.getDoGenerateSubmoduleConfigurations());
340343
}
344+
345+
@Issue("JENKINS-68562")
346+
@Test
347+
public void testAbortIfSourceIsLocal() {
348+
GitSCM gitSCM = new GitSCM(createRepoList(repoURL, null),
349+
Collections.singletonList(new BranchSpec("master")),
350+
null, null, Collections.emptyList());
351+
try {
352+
gitSCM.abortIfSourceIsLocal();
353+
} catch (AbortException e) {
354+
fail("https remote URLs should always be valid");
355+
}
356+
}
341357
}

0 commit comments

Comments
 (0)