Skip to content

Commit c405776

Browse files
committed
Merge remote-tracking branch 'upstream/master' into add-dockerfile2
2 parents a307a3b + e1d0c55 commit c405776

File tree

13 files changed

+175
-54
lines changed

13 files changed

+175
-54
lines changed

distribution/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.opengrok</groupId>
99
<artifactId>opengrok-top</artifactId>
10-
<version>1.2.7</version>
10+
<version>1.2.8</version>
1111
</parent>
1212

1313
<artifactId>opengrok-dist</artifactId>
@@ -24,20 +24,20 @@
2424
<dependency>
2525
<groupId>org.opengrok</groupId>
2626
<artifactId>opengrok</artifactId>
27-
<version>1.2.7</version>
27+
<version>1.2.8</version>
2828
</dependency>
2929

3030
<dependency>
3131
<groupId>org.opengrok</groupId>
3232
<artifactId>opengrok-web</artifactId>
33-
<version>1.2.7</version>
33+
<version>1.2.8</version>
3434
<type>war</type>
3535
</dependency>
3636

3737
<dependency>
3838
<groupId>org.opengrok</groupId>
3939
<artifactId>opengrok-tools</artifactId>
40-
<version>1.2.7</version>
40+
<version>1.2.8</version>
4141
<type>pom</type>
4242
</dependency>
4343
</dependencies>

opengrok-indexer/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
2828
<parent>
2929
<groupId>org.opengrok</groupId>
3030
<artifactId>opengrok-top</artifactId>
31-
<version>1.2.7</version>
31+
<version>1.2.8</version>
3232
</parent>
3333

3434
<artifactId>opengrok</artifactId>
35-
<version>1.2.7</version>
35+
<version>1.2.8</version>
3636
<packaging>jar</packaging>
3737

3838
<name>OpenGrok Indexer</name>

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

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -590,20 +590,20 @@ boolean hasFileBasedTags() {
590590
return true;
591591
}
592592

593-
private TagEntry buildTagEntry(File directory, String tags, boolean interactive) throws HistoryException, IOException {
593+
private TagEntry buildTagEntry(File directory, String tag, boolean interactive) {
594594
ArrayList<String> argv = new ArrayList<>();
595595
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
596596
argv.add(RepoCommand);
597597
argv.add("log");
598-
argv.add("--format=commit:%H" + System.getProperty("line.separator")
599-
+ "Date:%at");
600-
argv.add("-r");
601-
argv.add(tags + "^.." + tags);
598+
argv.add("--format=commit:%H%nDate:%at");
599+
argv.add("-n");
600+
argv.add("1");
601+
argv.add(tag);
602602

603603
Executor executor = new Executor(argv, directory, interactive ?
604604
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout() :
605605
RuntimeEnvironment.getInstance().getCommandTimeout());
606-
GitTagParser parser = new GitTagParser(tags);
606+
GitTagParser parser = new GitTagParser(tag);
607607
executor.exec(true, parser);
608608
return parser.getEntries().first();
609609
}
@@ -645,21 +645,11 @@ protected void buildTagList(File directory, boolean interactive) {
645645
this.tagList = null;
646646
}
647647

648-
try {
649-
// Now get hash & date for each tag
650-
for (String tags : tagsList) {
651-
TagEntry tagEntry = buildTagEntry(directory, tags, interactive);
652-
// Reverse the order of the list
653-
this.tagList.add(tagEntry);
654-
}
655-
} catch (HistoryException e) {
656-
LOGGER.log(Level.WARNING,
657-
"Failed to parse tag list: {0}", e.getMessage());
658-
this.tagList = null;
659-
} catch (IOException e) {
660-
LOGGER.log(Level.WARNING,
661-
"Failed to read tag list: {0}", e.getMessage());
662-
this.tagList = null;
648+
// Now get hash & date for each tag.
649+
for (String tag : tagsList) {
650+
TagEntry tagEntry = buildTagEntry(directory, tag, interactive);
651+
// Reverse the order of the list
652+
this.tagList.add(tagEntry);
663653
}
664654
}
665655

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

Lines changed: 63 additions & 3 deletions
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;
@@ -31,27 +32,37 @@
3132
import java.util.Calendar;
3233
import java.util.Date;
3334
import java.util.List;
35+
import java.util.logging.Level;
36+
import java.util.logging.Logger;
3437
import java.util.regex.Matcher;
3538
import java.util.regex.Pattern;
3639

40+
import org.opengrok.indexer.logger.LoggerFactory;
3741
import org.opengrok.indexer.util.Executor;
42+
import static org.opengrok.indexer.history.PerforceRepository.protectPerforceFilename;
3843

3944
/**
4045
* Parse source history for a Perforce Repository
4146
*
4247
* @author Emilio Monti - [email protected]
4348
*/
4449
public class PerforceHistoryParser {
50+
private static final Logger LOGGER = LoggerFactory.getLogger(PerforceHistoryParser.class);
51+
52+
History parse(File file, Repository repos) throws HistoryException {
53+
return this.parse(file, null, repos);
54+
}
4555

4656
/**
4757
* Parse the history for the specified file.
4858
*
4959
* @param file the file to parse history for
60+
* @param sinceRevision the revision before the start of desired history
5061
* @param repos Pointer to the {@code PerforceRepository}
5162
* @return object representing the file's history
5263
* @throws HistoryException if a problem occurs while executing p4 command
5364
*/
54-
History parse(File file, Repository repos) throws HistoryException {
65+
History parse(File file, String sinceRevision, Repository repos) throws HistoryException {
5566
History history;
5667

5768
if (!PerforceRepository.isInP4Depot(file, false)) {
@@ -60,9 +71,16 @@ History parse(File file, Repository repos) throws HistoryException {
6071

6172
try {
6273
if (file.isDirectory()) {
74+
/* TODO: Do I need to think about revisions here? */
6375
history = parseDirectory(file);
6476
} else {
65-
history = getRevisions(file, null);
77+
if (sinceRevision == null || "".equals(sinceRevision)) {
78+
/* Get all revisions */
79+
history = getRevisions(file, null);
80+
} else {
81+
/* Get revisions between specified and head */
82+
history = getRevisionsSince(file, sinceRevision);
83+
}
6684
}
6785
} catch (IOException ioe) {
6886
throw new HistoryException(ioe);
@@ -82,12 +100,54 @@ private History parseDirectory(File file) throws IOException {
82100
return parseChanges(executor.getOutputReader());
83101
}
84102

103+
/**
104+
* Retrieve the history of a given file.
105+
*
106+
* @param file the file to parse history for
107+
* @param rev the revision at which to end history
108+
* @return object representing the file's history
109+
*/
85110
public static History getRevisions(File file, String rev) throws IOException {
86111
ArrayList<String> cmd = new ArrayList<String>();
87112
cmd.add("p4");
88113
cmd.add("filelog");
89114
cmd.add("-lti");
90-
cmd.add(file.getName() + PerforceRepository.getRevisionCmd(rev));
115+
cmd.add(protectPerforceFilename(file.getName()) + PerforceRepository.getRevisionCmd(rev));
116+
Executor executor = new Executor(cmd, file.getCanonicalFile().getParentFile());
117+
executor.exec();
118+
119+
return parseFileLog(executor.getOutputReader());
120+
}
121+
122+
/**
123+
* Retrieve the history of a given file, beginning after the specified
124+
* revision.
125+
*
126+
* @param file the file to parse history for
127+
* @param rev the revision before the start of desired history
128+
* @return object representing the file's history
129+
*/
130+
public static History getRevisionsSince(File file, String rev) throws IOException {
131+
ArrayList<String> cmd = new ArrayList<String>();
132+
cmd.add("p4");
133+
cmd.add("filelog");
134+
cmd.add("-lti");
135+
/* Okay. This is a little cheeky. getRevisionCmd(String,String) gives
136+
* a range spec that _includes_ the first revision. But, we don't want
137+
* that in this case here. So, presume that "rev" is always an integer
138+
* for perforce, add one to it, then convert back into a string to
139+
* pass into getRevisionCmd as a starting revision. */
140+
try {
141+
Integer irev = Integer.parseInt(rev);
142+
irev += 1;
143+
rev = irev.toString();
144+
} catch (NumberFormatException e) {
145+
LOGGER.log(Level.WARNING,
146+
"Unable to increment revision {}, NumberFormatException",
147+
new Object[]{rev});
148+
/* Move along with rev unchanged... */
149+
}
150+
cmd.add(file.getName() + PerforceRepository.getRevisionCmd(rev, "now"));
91151
Executor executor = new Executor(cmd, file.getCanonicalFile().getParentFile());
92152
executor.exec();
93153

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

Lines changed: 55 additions & 7 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.replace("%", "%25");
71+
t = t.replace("#", "%23");
72+
t = t.replace("*", "%2A");
73+
t = t.replace("@", "%40");
74+
if (!name.equals(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();
@@ -136,11 +154,11 @@ public boolean fileHasAnnotation(File file) {
136154
* @param interactive interactive mode flag
137155
* @return true if the given file is in the depot, false otherwise
138156
*/
139-
public static boolean isInP4Depot(File file, boolean interactive) {
157+
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;
@@ -201,6 +219,12 @@ History getHistory(File file) throws HistoryException {
201219
return new PerforceHistoryParser().parse(file, this);
202220
}
203221

222+
@Override
223+
History getHistory(File file, String sinceRevision)
224+
throws HistoryException {
225+
return new PerforceHistoryParser().parse(file, sinceRevision, this);
226+
}
227+
204228
@Override
205229
String determineParent(boolean interactive) throws IOException {
206230
return null;
@@ -211,16 +235,40 @@ String determineBranch(boolean interactive) {
211235
return null;
212236
}
213237
/**
214-
* Parse internal rev number and returns it in format suitable for P4 command-line.
238+
* Parse internal rev number and return it in a format suitable for P4 command-line.
215239
* @param rev Internal rev number.
216240
* @return rev number formatted for P4 command-line.
217241
*/
218-
public static String getRevisionCmd(String rev) {
219-
if(rev == null || "".equals(rev)) {
242+
static String getRevisionCmd(String rev) {
243+
if (rev == null || "".equals(rev)) {
220244
return "";
221245
}
222246
return "@" + rev;
223247
}
248+
/**
249+
* Parse rev numbers and return it as a range in a format suitable for P4 command-line.
250+
* @param first First revision number.
251+
* @param last Last revision number.
252+
* @return rev number formatted for P4 command-line.
253+
*/
254+
static String getRevisionCmd(String first, String last) {
255+
if ((first == null || "".equals(first)) &&
256+
((last == null) || "".equals(last))) {
257+
return "";
258+
}
259+
String ret = "@";
260+
if (first == null || "".equals(first)) {
261+
ret += "0,";
262+
} else {
263+
ret += first + ",";
264+
}
265+
if (last == null || "".equals(last)) {
266+
ret += "now";
267+
} else {
268+
ret += last;
269+
}
270+
return ret;
271+
}
224272

225273
@Override
226274
String determineCurrentVersion(boolean interactive) throws IOException {

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)