Skip to content

Commit e230f5d

Browse files
committed
Fixes #2950 : don't partially canonicalize in GitRepository
1 parent 5a37bf7 commit e230f5d

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.InputStream;
3131
import java.io.InputStreamReader;
3232
import java.io.Reader;
33+
import java.nio.charset.StandardCharsets;
3334
import java.nio.file.Paths;
3435
import java.text.ParseException;
3536
import java.util.ArrayList;
@@ -38,7 +39,6 @@
3839
import java.util.LinkedList;
3940
import java.util.List;
4041
import java.util.TreeSet;
41-
import java.util.function.Supplier;
4242
import java.util.logging.Level;
4343
import java.util.logging.Logger;
4444
import java.util.regex.Matcher;
@@ -166,10 +166,11 @@ Executor getRenamedFilesExecutor(final File file, String sinceRevision) throws I
166166
cmd.add(sinceRevision + "..");
167167
}
168168

169-
if (file.getCanonicalPath().length() > getDirectoryName().length() + 1) {
169+
String canonicalPath = file.getCanonicalPath();
170+
if (canonicalPath.length() > getCanonicalDirectoryName().length() + 1) {
170171
// this is a file in the repository
171172
cmd.add("--");
172-
cmd.add(file.getCanonicalPath().substring(getDirectoryName().length() + 1));
173+
cmd.add(getPathRelativeToCanonicalRepositoryRoot(canonicalPath));
173174
}
174175

175176
return new Executor(cmd, new File(getDirectoryName()), sinceRevision != null);
@@ -250,12 +251,9 @@ boolean getHistoryGet(
250251
try {
251252
origpath = findOriginalName(fullpath, rev);
252253
} catch (IOException exp) {
253-
LOGGER.log(Level.SEVERE, exp, new Supplier<String>() {
254-
@Override
255-
public String get() {
256-
return String.format("Failed to get original revision: %s/%s (revision %s)", parent, basename, rev);
257-
}
258-
});
254+
LOGGER.log(Level.SEVERE, exp, () -> String.format(
255+
"Failed to get original revision: %s/%s (revision %s)",
256+
parent, basename, rev));
259257
return false;
260258
}
261259

@@ -283,19 +281,22 @@ public String get() {
283281
*
284282
* @param input a stream with the output from a log or blame command
285283
* @return a reader that reads the input
286-
* @throws IOException if the reader cannot be created
287284
*/
288-
static Reader newLogReader(InputStream input) throws IOException {
285+
static Reader newLogReader(InputStream input) {
289286
// Bug #17731: Git always encodes the log output using UTF-8 (unless
290287
// overridden by i18n.logoutputencoding, but let's assume that hasn't
291288
// been done for now). Create a reader that uses UTF-8 instead of the
292289
// platform's default encoding.
293-
return new InputStreamReader(input, "UTF-8");
290+
return new InputStreamReader(input, StandardCharsets.UTF_8);
294291
}
295292

296-
private String getPathRelativeToRepositoryRoot(String fullpath) {
297-
String repoPath = getDirectoryName() + File.separator;
298-
return fullpath.replace(repoPath, "");
293+
private String getPathRelativeToCanonicalRepositoryRoot(String fullpath)
294+
throws IOException {
295+
String repoPath = getCanonicalDirectoryName() + File.separator;
296+
if (fullpath.startsWith(repoPath)) {
297+
return fullpath.substring(repoPath.length());
298+
}
299+
return fullpath;
299300
}
300301

301302
/**
@@ -306,11 +307,11 @@ private String getPathRelativeToRepositoryRoot(String fullpath) {
306307
* @param changeset changeset
307308
* @return original filename relative to the repository root
308309
* @throws java.io.IOException if I/O exception occurred
309-
* @see #getPathRelativeToRepositoryRoot(String)
310+
* @see #getPathRelativeToCanonicalRepositoryRoot(String)
310311
*/
311-
protected String findOriginalName(String fullpath, String changeset)
312+
String findOriginalName(String fullpath, String changeset)
312313
throws IOException {
313-
if (fullpath == null || fullpath.isEmpty() || fullpath.length() < getDirectoryName().length()) {
314+
if (fullpath == null || fullpath.isEmpty()) {
314315
throw new IOException(String.format("Invalid file path string: %s", fullpath));
315316
}
316317

@@ -319,7 +320,7 @@ protected String findOriginalName(String fullpath, String changeset)
319320
fullpath, changeset));
320321
}
321322

322-
String fileInRepo = getPathRelativeToRepositoryRoot(fullpath);
323+
String fileInRepo = getPathRelativeToCanonicalRepositoryRoot(fullpath);
323324

324325
/*
325326
* Get the list of file renames for given file to the specified
@@ -447,7 +448,7 @@ public Annotation annotate(File file, String revision) throws IOException {
447448
}
448449
}
449450
cmd.add("--");
450-
cmd.add(getPathRelativeToRepositoryRoot(file.getCanonicalPath()));
451+
cmd.add(getPathRelativeToCanonicalRepositoryRoot(file.getCanonicalPath()));
451452

452453
Executor exec = new Executor(cmd, new File(getDirectoryName()),
453454
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,10 @@ public String getDirectoryName() {
139139
}
140140

141141
/**
142-
* Get the name of the root directory for this repository.
143-
*
144-
* @return the name of the root directory
142+
* Get the canonical {@link #getDirectoryName()} of the root directory for
143+
* this repository.
145144
*/
146-
public String getCanonicalDirectoryName() throws IOException {
145+
String getCanonicalDirectoryName() throws IOException {
147146
if (directoryNameCanonical == null) {
148147
directoryNameCanonical = new File(getDirectoryName()).getCanonicalPath();
149148
}

0 commit comments

Comments
 (0)