Skip to content

Commit 526aca2

Browse files
ahornacevladak
authored andcommitted
Simplify repository copying code
1 parent 9a303f1 commit 526aca2

File tree

18 files changed

+38
-30
lines changed

18 files changed

+38
-30
lines changed

opengrok-indexer/src/test/java/org/opengrok/indexer/util/TestRepository.java

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,23 @@ public class TestRepository {
5555
private static final char JAR_PATH_DELIMITER = '!';
5656

5757
private static final Map<Path, Path> renameMappings = new LinkedHashMap<>(Map.of(
58-
Path.of("bazaar", "bzr"), Path.of("bazaar", ".bzr"),
59-
60-
Path.of("bitkeeper", "bk", "BitKeeper", "etc", "SCCS_dir"),
61-
Path.of("bitkeeper", ".bk", "BitKeeper", "etc", "SCCS"),
62-
Path.of("bitkeeper", "bk", "SCCS_dir"),
63-
Path.of("bitkeeper", ".bk", "SCCS"),
64-
65-
Path.of("bitkeeper", "bk"), Path.of("bitkeeper", ".bk"),
66-
Path.of("mercurial", "hg"), Path.of("mercurial", ".hg"),
67-
Path.of("mercurial", "hgignore"), Path.of("mercurial", ".hgignore"),
68-
Path.of("git", "git"), Path.of("git", ".git"),
69-
Path.of("cvs_test", "cvsrepo", "CVS_dir"), Path.of("cvs_test", "cvsrepo", "CVS"),
70-
Path.of("rcs_test", "RCS_dir"), Path.of("rcs_test", "RCS"),
71-
Path.of("teamware", "SCCS_dir"), Path.of("teamware", "SCCS")
58+
Path.of("bazaar", "bzr"),
59+
Path.of("bazaar", ".bzr"),
60+
61+
Path.of("bitkeeper", "bk"),
62+
Path.of("bitkeeper", ".bk"),
63+
64+
Path.of("mercurial", "hg"),
65+
Path.of("mercurial", ".hg"),
66+
67+
Path.of("mercurial", "hgignore"),
68+
Path.of("mercurial", ".hgignore"),
69+
70+
Path.of("git", "git"),
71+
Path.of("git", ".git"),
72+
73+
Path.of("cvs_test", "cvsrepo", "CVS_dir"),
74+
Path.of("cvs_test", "cvsrepo", "CVS")
7275
));
7376

7477
private final RuntimeEnvironment env;
@@ -102,32 +105,37 @@ public void create(@NotNull final URL url) throws IOException, URISyntaxExceptio
102105

103106
private void copyDirectory(Path src, Path dest) throws IOException {
104107
try (Stream<Path> stream = Files.walk(src)) {
105-
stream.forEach(source -> {
106-
if (source.equals(src)) {
108+
stream.forEach(sourceFile -> {
109+
if (sourceFile.equals(src)) {
107110
return;
108111
}
109112
try {
110-
// possibly strip zip filesystem for the startsWith method to work
111-
var relativePath = Path.of(src.relativize(source).toString());
112-
for (var e : renameMappings.entrySet()) {
113-
if (relativePath.startsWith(e.getKey())) {
114-
if (relativePath.getNameCount() > e.getKey().getNameCount()) {
115-
relativePath = relativePath.subpath(e.getKey().getNameCount(), relativePath.getNameCount());
116-
} else {
117-
relativePath = Path.of("");
118-
}
119-
relativePath = e.getValue().resolve(relativePath);
120-
break;
121-
}
122-
}
123-
Files.copy(source, dest.resolve(relativePath.toString()), REPLACE_EXISTING);
113+
Path destRelativePath = getDestinationRelativePath(src, sourceFile);
114+
Files.copy(sourceFile, dest.resolve(destRelativePath.toString()), REPLACE_EXISTING);
124115
} catch (Exception e) {
125116
throw new RuntimeException(e);
126117
}
127118
});
128119
}
129120
}
130121

122+
private Path getDestinationRelativePath(Path sourceDirectory, Path sourceFile) {
123+
// possibly strip zip filesystem for the startsWith method to work
124+
var relativePath = Path.of(sourceDirectory.relativize(sourceFile).toString());
125+
for (var e : renameMappings.entrySet()) {
126+
if (relativePath.startsWith(e.getKey())) {
127+
if (relativePath.getNameCount() > e.getKey().getNameCount()) {
128+
relativePath = relativePath.subpath(e.getKey().getNameCount(), relativePath.getNameCount());
129+
} else {
130+
relativePath = Path.of("");
131+
}
132+
relativePath = e.getValue().resolve(relativePath);
133+
break;
134+
}
135+
}
136+
return relativePath;
137+
}
138+
131139
public void create(InputStream inputBundle) throws IOException {
132140
createEmpty();
133141
extractBundle(sourceRoot, inputBundle);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)