Skip to content

Commit

Permalink
Introduce support for reference repository paths ending with /${GIT_U…
Browse files Browse the repository at this point in the history
…RL} to replace by url => funny dir subtree in filesystem
  • Loading branch information
jimklimov committed Dec 8, 2020
1 parent cf116df commit 694ee90
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,39 @@ public void execute() throws GitException, InterruptedException {

if (reference != null && !reference.isEmpty()) {
File referencePath = new File(reference);
if (!referencePath.exists()) {
if (reference.endsWith("/${GIT_URL}")) {
// See also JGitAPIImpl.java and keep the two blocks in
// sync (TODO: refactor into a method both would use?)
// For mass-configured jobs, like Organization Folders,
// allow to support parameterized paths to many refrepos.
// End-users can set up webs of symlinks to same repos
// known by different URLs (and/or including their forks
// also cached in same index). Luckily all URL chars are
// valid parts of path name... in Unix... Maybe parse or
// escape chars for URLs=>paths with Windows in mind?
// https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
// Further ideas: beside "GIT_URL" other meta variables
// can be introduced, e.g. to escape non-ascii chars for
// portability? Support SHA or MD5 hashes of URLs as
// pathnames?
reference = reference.replaceAll("\\$\\{GIT_URL\\}$", url).replaceAll("/*$", "").replaceAll(".git$", "");
referencePath = new File(reference);
if (!referencePath.exists()) {
// Normalize the URLs with or without .git suffix to
// be served by same dir with the refrepo contents
reference += ".git";
referencePath = new File(reference);
}
// Note: both these logs are needed, they are used in selftest
if (referencePath.exists()) {
listener.getLogger().println("[WARNING] Parameterized reference path replaced with: " + reference);
} else {
listener.getLogger().println("[WARNING] Parameterized reference path replaced with: " + reference + " was not found");
}
}
}

if (!referencePath.exists())
listener.getLogger().println("[WARNING] Reference path does not exist: " + reference);
else if (!referencePath.isDirectory())
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,27 @@ public void execute() throws GitException, InterruptedException {
// the repository builder does not create the alternates file
if (reference != null && !reference.isEmpty()) {
File referencePath = new File(reference);
if (!referencePath.exists()) {
if (reference.endsWith("/${GIT_URL}")) {
// See comments in CliGitAPIImpl.java for details
// Keep the two blocks in sync (refactor into method?)
reference = reference.replaceAll("\\$\\{GIT_URL\\}$", url).replaceAll("/*$", "").replaceAll(".git$", "");
referencePath = new File(reference);
if (!referencePath.exists()) {
// Normalize the URLs with or without .git suffix to
// be served by same dir with the refrepo contents
reference += ".git";
referencePath = new File(reference);
}
// Note: both these logs are needed, they are used in selftest
if (referencePath.exists()) {
listener.getLogger().println("[WARNING] Parameterized reference path replaced with: " + reference);
} else {
listener.getLogger().println("[WARNING] Parameterized reference path replaced with: " + reference + " was not found");
}
}
}

if (!referencePath.exists())
listener.getLogger().println("[WARNING] Reference path does not exist: " + reference);
else if (!referencePath.isDirectory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,21 @@ public void test_clone_reference() throws Exception, IOException, InterruptedExc
assertThat("Reference repo logged in: " + messages, handler.containsMessageSubstring("Using reference repository: "), is(true));
}

@Test
public void test_clone_reference_parameterized() throws Exception, IOException, InterruptedException {
testGitClient.clone_().url(workspace.localMirror()).repositoryName("origin").reference(workspace.localMirror() + "/${GIT_URL}").execute();
testGitClient.checkout().ref("origin/master").branch("master").execute();
check_remote_url(workspace, testGitClient, "origin");
// Verify JENKINS-46737 expected log message is written
String messages = StringUtils.join(handler.getMessages(), ";");
assertThat("Reference repo name-parsing logged in: " + messages, handler.containsMessageSubstring("Parameterized reference path replaced with: "), is(true));
// Skip: Missing if clone failed - currently would, with bogus path above and not pre-created path structure
//assertThat("Reference repo logged in: " + messages, handler.containsMessageSubstring("Using reference repository: "), is(true));
//assertAlternateFilePointsToLocalMirror();
//assertBranchesExist(testGitClient.getBranches(), "master");
//assertNoObjectsInRepository();
}

private static final String SRC_DIR = (new File(".")).getAbsolutePath();

@Test
Expand Down

0 comments on commit 694ee90

Please sign in to comment.