Skip to content

Commit 455d807

Browse files
crossVladimir Kotal
authored andcommitted
Perforce filenames 2746 (#2762)
Add a mapping to translate "special" characters in filenames before passing those paths to perforce commands. fixes #2746
1 parent 9ce52a2 commit 455d807

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/PerforceHistoryParser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2019, Chris Ross <[email protected]>.
2223
*/
2324

2425
package org.opengrok.indexer.history;
@@ -35,6 +36,7 @@
3536
import java.util.regex.Pattern;
3637

3738
import org.opengrok.indexer.util.Executor;
39+
import static org.opengrok.indexer.history.PerforceRepository.protectPerforceFilename;
3840

3941
/**
4042
* Parse source history for a Perforce Repository
@@ -87,7 +89,7 @@ public static History getRevisions(File file, String rev) throws IOException {
8789
cmd.add("p4");
8890
cmd.add("filelog");
8991
cmd.add("-lti");
90-
cmd.add(file.getName() + PerforceRepository.getRevisionCmd(rev));
92+
cmd.add(protectPerforceFilename(file.getName()) + PerforceRepository.getRevisionCmd(rev));
9193
Executor executor = new Executor(cmd, file.getCanonicalFile().getParentFile());
9294
executor.exec();
9395

opengrok-indexer/src/main/java/org/opengrok/indexer/history/PerforceRepository.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/*
2121
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
23+
* Portions Copyright (c) 2019, Chris Ross <[email protected]>.
2324
*/
2425
package org.opengrok.indexer.history;
2526

@@ -61,14 +62,31 @@ public PerforceRepository() {
6162
ignoredFiles.add(".p4config");
6263
}
6364

65+
static String protectPerforceFilename(String name) {
66+
/* For each of the [four] special characters, replace them with */
67+
/* the recognized escape sequence for perforce. */
68+
/* NOTE: Must replace '%' first, or that translation would */
69+
/* affect the output of the others. */
70+
String t = name.replaceAll("%", "%25");
71+
t = t.replaceAll("#", "%23");
72+
t = t.replaceAll("\\*", "%2A");
73+
t = t.replaceAll("@", "%40");
74+
if (name != t) {
75+
LOGGER.log(Level.FINEST,
76+
"protectPerforceFilename: replaced ''{0}'' with ''{1}''",
77+
new Object[]{name, t});
78+
}
79+
return t;
80+
}
81+
6482
@Override
6583
public Annotation annotate(File file, String rev) throws IOException {
6684
ArrayList<String> cmd = new ArrayList<>();
6785
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
6886
cmd.add(RepoCommand);
6987
cmd.add("annotate");
7088
cmd.add("-qci");
71-
cmd.add(file.getPath() + getRevisionCmd(rev));
89+
cmd.add(protectPerforceFilename(file.getPath()) + getRevisionCmd(rev));
7290

7391
Executor executor = new Executor(cmd, file.getParentFile(),
7492
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
@@ -87,7 +105,7 @@ boolean getHistoryGet(
87105
cmd.add(RepoCommand);
88106
cmd.add("print");
89107
cmd.add("-q");
90-
cmd.add(basename + getRevisionCmd(rev));
108+
cmd.add(protectPerforceFilename(basename) + getRevisionCmd(rev));
91109
Executor executor = new Executor(cmd, new File(parent));
92110
// TODO: properly evaluate Perforce return code
93111
executor.exec();
@@ -140,7 +158,7 @@ public static boolean isInP4Depot(File file, boolean interactive) {
140158
boolean status = false;
141159
if (testRepo.isWorking()) {
142160
ArrayList<String> cmd = new ArrayList<>();
143-
String name = file.getName();
161+
String name = protectPerforceFilename(file.getName());
144162
File dir = file.getParentFile();
145163
if (file.isDirectory()) {
146164
dir = file;

opengrok-indexer/src/test/java/org/opengrok/indexer/history/PerforceRepositoryTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/*
2121
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
23+
* Portions Copyright (c) 2019, Chris Ross <[email protected]>.
2324
*/
2425
package org.opengrok.indexer.history;
2526

@@ -35,9 +36,12 @@
3536
import java.io.InputStream;
3637
import java.util.ArrayList;
3738
import java.util.List;
39+
import java.util.AbstractMap.SimpleImmutableEntry;
3840

3941
import static org.junit.Assert.assertNotNull;
42+
import static org.junit.Assert.assertEquals;
4043
import org.opengrok.indexer.configuration.RuntimeEnvironment;
44+
import static org.opengrok.indexer.history.PerforceRepository.protectPerforceFilename;
4145

4246
/**
4347
* Do basic testing of the Perforce support
@@ -105,4 +109,23 @@ public void testHistoryAndAnnotations() throws Exception {
105109
}
106110
}
107111
}
112+
113+
@Test
114+
public void testProtectFilename() throws Exception {
115+
ArrayList<SimpleImmutableEntry<String,String>> testmap = new ArrayList<>();
116+
testmap.add(new SimpleImmutableEntry<>("Testfile 34", "Testfile 34"));
117+
testmap.add(new SimpleImmutableEntry<>("Test%52", "Test%2552"));
118+
testmap.add(new SimpleImmutableEntry<>("Test*4+2", "Test%2A4+2"));
119+
testmap.add(new SimpleImmutableEntry<>("Test@", "Test%40"));
120+
testmap.add(new SimpleImmutableEntry<>("@seventeen", "%40seventeen"));
121+
testmap.add(new SimpleImmutableEntry<>("upNdown(", "upNdown("));
122+
testmap.add(new SimpleImmutableEntry<>("tst#99", "tst%2399"));
123+
testmap.add(new SimpleImmutableEntry<>("#File*Three%trig", "%23File%2AThree%25trig"));
124+
testmap.add(new SimpleImmutableEntry<>("Two%and5#3#4", "Two%25and5%233%234"));
125+
126+
for (SimpleImmutableEntry<String,String> ent : testmap) {
127+
String prot = protectPerforceFilename(ent.getKey());
128+
assertEquals("Improper protected filename, "+prot+" != "+ent.getValue(), ent.getValue(), prot);
129+
}
130+
}
108131
}

0 commit comments

Comments
 (0)