Skip to content

Commit 77dceda

Browse files
committed
Improve matching in DockerfileCOPYParentsEmulator
Fixes #134
1 parent e8083fd commit 77dceda

File tree

4 files changed

+59
-61
lines changed

4 files changed

+59
-61
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
# 2.0.1
2+
* Improve matching in ``DockerfileCOPYParentsEmulator`` #134
3+
* Now should properly handle ``./``
4+
15
# 2.0.0
26
* Changed ignore backend to utilize [JGit](https://github.com/eclipse-jgit/jgit)
37
* This should now behave exactly like a ``.gitignore``
48
* Overall performance should be a lot faster
59
* Make it possible to modify transferred files
6-
* Provide an option to emulate [``COPY --parents``](https://docs.docker.com/reference/dockerfile/#copy---parents) (which is currently not supported by Docker out of the box)
10+
* Provide an option to emulate [``COPY --parents``](https://docs.docker.com/reference/dockerfile/#copy---parents) using ``DockerfileCOPYParentsEmulator`` (which is currently not supported by Docker out of the box)
711
* This option is required to utilize Docker's cache properly
812
```docker
913
# syntax=docker/dockerfile:1-labs

testcontainers-advanced-imagebuilder/src/main/java/software/xdev/testcontainers/imagebuilder/compat/DockerfileCOPYParentsEmulator.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import java.util.List;
1919
import java.util.Map;
2020
import java.util.Set;
21+
import java.util.regex.Pattern;
2122
import java.util.stream.Stream;
2223

23-
import software.xdev.testcontainers.imagebuilder.glob.GlobMatcher;
24+
import software.xdev.testcontainers.imagebuilder.jgit.errors.InvalidPatternException;
25+
import software.xdev.testcontainers.imagebuilder.jgit.ignore.internal.Strings;
2426
import software.xdev.testcontainers.imagebuilder.transfer.DockerFileLineModifier;
2527

2628

@@ -118,9 +120,9 @@ protected Stream<String> handleLine(final String line, final Set<String> relativ
118120
return Stream.of(args)
119121
.limit((long)args.length - (isLastArg ? 1 : 0))
120122
.flatMap(source -> {
121-
if(!source.contains("*") && !source.contains("/"))
123+
if(!source.contains("*"))
122124
{
123-
return Stream.of(Map.entry(source, targetPathFinal));
125+
return Stream.of(Map.entry(source, targetPathFinalForRelative + removeRelativeStart(source)));
124126
}
125127

126128
final GlobMatcher matcher = new GlobMatcher(source);
@@ -133,4 +135,38 @@ protected Stream<String> handleLine(final String line, final Set<String> relativ
133135
+ (!lineAfterArgsFinal.isEmpty() ? " " + lineAfterArgsFinal : "")
134136
+ " " + e.getValue());
135137
}
138+
139+
protected static String removeRelativeStart(final String path)
140+
{
141+
return path.startsWith("./") ? path.substring(2) : path;
142+
}
143+
144+
public static class GlobMatcher
145+
{
146+
protected final Pattern pattern;
147+
148+
public GlobMatcher(final String pattern)
149+
{
150+
try
151+
{
152+
this.pattern = Pattern.compile("\\/?" + Strings.convertGlob(removeRelativeStart(pattern)));
153+
}
154+
catch(final InvalidPatternException e)
155+
{
156+
throw new IllegalArgumentException(e);
157+
}
158+
}
159+
160+
@SuppressWarnings("checkstyle:FinalParameters")
161+
protected String correctPathForMatching(String path)
162+
{
163+
path = removeRelativeStart(path);
164+
return path.startsWith("/") ? path : ("/" + path);
165+
}
166+
167+
public boolean matches(final String path)
168+
{
169+
return this.pattern.matcher(this.correctPathForMatching(path)).matches();
170+
}
171+
}
136172
}

testcontainers-advanced-imagebuilder/src/main/java/software/xdev/testcontainers/imagebuilder/glob/GlobMatcher.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

testcontainers-advanced-imagebuilder/src/test/java/software/xdev/testcontainers/imagebuilder/compat/DockerfileCOPYParentsEmulatorTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,39 @@ void simpleCheck()
3333
"FROM alpine:3",
3434
"COPY --parents mvnw .mvn/** --abc ./",
3535
"COPY --parents **/pom.xml ./",
36-
"COPY --parents abc/def.txt ./"
36+
"COPY --parents abc/def.txt ./",
37+
"COPY --parents ./d/e/** ./",
38+
"COPY ./d/e/** ./", // Keep original
39+
"COPY --parents ./it/mvnw ./it/.mvn/** ./xx"
3740
), Set.of(
38-
".mvn/wrapper/maven-wrapper.properties",
3941
"mvnw",
42+
".mvn/wrapper/maven-wrapper.properties",
4043
"Dockerfile",
4144
"pom.xml",
4245
"a/pom.xml",
4346
"a/b/pom.xml",
4447
"a/b/c/pom.xml",
4548
"abc/def.txt",
46-
"ignoreme.txt"
49+
"ignoreme.txt",
50+
"d/e/example.txt",
51+
"it/mvnw",
52+
"it/.mvn/wrapper/maven-wrapper.properties"
4753
));
4854
Assertions.assertIterableEquals(
4955
List.of(
5056
"# syntax=docker/dockerfile:1-labs",
5157
"FROM alpine:3",
52-
"COPY mvnw --abc ./",
58+
"COPY mvnw --abc ./mvnw",
5359
"COPY .mvn/wrapper/maven-wrapper.properties --abc ./.mvn/wrapper/maven-wrapper.properties",
5460
"COPY a/b/c/pom.xml ./a/b/c/pom.xml",
5561
"COPY a/b/pom.xml ./a/b/pom.xml",
5662
"COPY a/pom.xml ./a/pom.xml",
5763
"COPY pom.xml ./pom.xml",
58-
"COPY abc/def.txt ./abc/def.txt"
64+
"COPY abc/def.txt ./abc/def.txt",
65+
"COPY d/e/example.txt ./d/e/example.txt",
66+
"COPY ./d/e/** ./", // Keep original
67+
"COPY ./it/mvnw ./xx/it/mvnw",
68+
"COPY it/.mvn/wrapper/maven-wrapper.properties ./xx/it/.mvn/wrapper/maven-wrapper.properties"
5969
),
6070
lines);
6171
}

0 commit comments

Comments
 (0)