Skip to content

Commit 5497485

Browse files
author
Vladimir Kotal
committed
rework finding original names
1 parent d56fc23 commit 5497485

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

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

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,18 @@ static Reader newLogReader(InputStream input) throws IOException {
277277
// platform's default encoding.
278278
return new InputStreamReader(input, "UTF-8");
279279
}
280-
280+
281+
private String getPathRelativeToRepositoryRoot(String fullpath) {
282+
String repoPath = getDirectoryName() + File.separator;
283+
return fullpath.replace(repoPath, "");
284+
}
285+
281286
/**
282287
* Get the name of file in given revision.
283288
*
284289
* @param fullpath file path
285290
* @param changeset changeset
286-
* @return original filename
291+
* @return original filename relative to the repository root
287292
* @throws java.io.IOException if I/O exception occurred
288293
*/
289294
protected String findOriginalName(String fullpath, String changeset)
@@ -297,7 +302,7 @@ protected String findOriginalName(String fullpath, String changeset)
297302
fullpath, changeset));
298303
}
299304

300-
String file = fullpath.replace(getDirectoryName() + File.separator, "");
305+
String fileInRepo = getPathRelativeToRepositoryRoot(fullpath);
301306
/*
302307
* Get the list of file renames for given file to the specified
303308
* revision.
@@ -312,13 +317,14 @@ protected String findOriginalName(String fullpath, String changeset)
312317
"--name-status",
313318
"--pretty=format:commit %h%b",
314319
"--",
315-
fullpath
320+
fileInRepo
316321
};
317322

318323
Executor executor = new Executor(Arrays.asList(argv), new File(getDirectoryName()),
319324
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
320325
int status = executor.exec();
321-
326+
327+
String originalFile = null;
322328
try (BufferedReader in = new BufferedReader(
323329
new InputStreamReader(executor.getOutputStream()))) {
324330
String line;
@@ -332,23 +338,26 @@ protected String findOriginalName(String fullpath, String changeset)
332338
}
333339

334340
if (changeset.equals(rev)) {
341+
if (originalFile == null) {
342+
originalFile = fileInRepo;
343+
}
335344
break;
336345
}
337346

338347
if ((m = pattern.matcher(line)).find()) {
339-
file = m.group(1);
348+
originalFile = m.group(1);
340349
}
341350
}
342351
}
343352

344-
if (status != 0) {
353+
if (status != 0 || originalFile == null) {
345354
LOGGER.log(Level.WARNING,
346355
"Failed to get original name in revision {2} for: \"{0}\" Exit code: {1}",
347356
new Object[]{fullpath, String.valueOf(status), changeset});
348357
return null;
349358
}
350359

351-
return (fullpath.substring(0, getDirectoryName().length() + 1) + file);
360+
return originalFile;
352361
}
353362

354363
/**
@@ -394,7 +403,7 @@ private String getFirstRevision(String fullpath) throws IOException {
394403
*
395404
* @param file file to annotate
396405
* @param revision revision to annotate
397-
* @return file annotation
406+
* @return file annotation or {@code null}
398407
* @throws java.io.IOException if I/O exception occurred
399408
*/
400409
@Override
@@ -419,16 +428,16 @@ public Annotation annotate(File file, String revision) throws IOException {
419428
}
420429
}
421430
cmd.add("--");
422-
cmd.add(file.getName());
431+
cmd.add(getPathRelativeToRepositoryRoot(file.getCanonicalPath()));
423432

424-
Executor exec = new Executor(cmd, file.getParentFile(),
433+
Executor exec = new Executor(cmd, new File(getDirectoryName()),
425434
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
426435
GitAnnotationParser parser = new GitAnnotationParser(file.getName());
427436
int status = exec.exec(true, parser);
428437

429438
// File might have changed its location if it was renamed.
430439
// Try to lookup its original name and get the annotation again.
431-
if (status != 0) {
440+
if (status != 0 && isHandleRenamedFiles()) {
432441
cmd.clear();
433442
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
434443
cmd.add(RepoCommand);
@@ -439,16 +448,18 @@ public Annotation annotate(File file, String revision) throws IOException {
439448
cmd.add(revision);
440449
}
441450
cmd.add("--");
442-
cmd.add(findOriginalName(file.getAbsolutePath(), revision));
443-
File directory = new File(getDirectoryName());
444-
exec = new Executor(cmd, directory);
445-
status = exec.exec();
451+
cmd.add(findOriginalName(file.getCanonicalPath(), revision));
452+
exec = new Executor(cmd, new File(getDirectoryName()),
453+
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
454+
parser = new GitAnnotationParser(file.getName());
455+
status = exec.exec(true, parser);
446456
}
447457

448458
if (status != 0) {
449459
LOGGER.log(Level.WARNING,
450460
"Failed to get annotations for: \"{0}\" Exit code: {1}",
451461
new Object[]{file.getAbsolutePath(), String.valueOf(status)});
462+
return null;
452463
}
453464

454465
return parser.getAnnotation();

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,29 @@ public void testRenamedFiles() throws Exception {
186186
{"moved2/renamed2.c", "ce4c98ec", "renamed.c"},
187187
{"moved2/renamed2.c", "bb74b7e8", "renamed.c"}
188188
};
189+
189190
File root = new File(repository.getSourceRoot(), "git");
190191
GitRepository gitrepo
191192
= (GitRepository) RepositoryFactory.getRepository(root);
193+
gitrepo.setHandleRenamedFiles(true);
194+
192195
int i = 0;
193196
for (String[] test : tests) {
194-
String file = root.getCanonicalPath() + File.separator + test[0];
197+
String file = Paths.get(root.getCanonicalPath(), test[0]).toString();
195198
String changeset = test[1];
196-
String expected = root.getCanonicalPath() + File.separator + test[2];
199+
String expectedName = test[2];
197200
try {
198201
String originalName = gitrepo.findOriginalName(file, changeset);
199-
Assert.assertEquals(expected, originalName);
202+
Assert.assertEquals(expectedName, originalName);
200203
} catch (IOException ex) {
201204
Assert.fail(String.format("Looking for original name of {} in {} shouldn't fail", file, changeset));
202205
}
203206
i++;
204207
}
205208
}
206209

207-
private void testAnnotationOfRenamedFile(GitRepository gitrepo, File file, Set<String> revSet) throws Exception {
208-
Annotation annotation = gitrepo.annotate(file, null);
210+
private void testAnnotationOfFile(GitRepository gitrepo, File file, String revision, Set<String> revSet) throws Exception {
211+
Annotation annotation = gitrepo.annotate(file, revision);
209212

210213
assertNotNull(annotation);
211214
assertEquals(revSet, annotation.getRevisions());
@@ -222,7 +225,7 @@ public void testAnnotationOfRenamedFileWithHandlingOff() throws Exception {
222225
= (GitRepository) RepositoryFactory.getRepository(root);
223226
gitrepo.setHandleRenamedFiles(false);
224227
File renamedFile = Paths.get(root.getAbsolutePath(),"moved2", "renamed2.c").toFile();
225-
testAnnotationOfRenamedFile(gitrepo, renamedFile, revSet);
228+
testAnnotationOfFile(gitrepo, renamedFile, null, revSet);
226229
}
227230

228231
@Test
@@ -236,7 +239,21 @@ public void testAnnotationOfRenamedFileWithHandlingOn() throws Exception {
236239
= (GitRepository) RepositoryFactory.getRepository(root);
237240
gitrepo.setHandleRenamedFiles(true);
238241
File renamedFile = Paths.get(root.getAbsolutePath(),"moved2", "renamed2.c").toFile();
239-
testAnnotationOfRenamedFile(gitrepo, renamedFile, revSet);
242+
testAnnotationOfFile(gitrepo, renamedFile, null, revSet);
243+
}
244+
245+
@Test
246+
public void testAnnotationOfRenamedFilePastWithHandlingOn() throws Exception {
247+
String[] revisions = {"1086eaf5", "ce4c98ec"};
248+
Set<String> revSet = new HashSet<>();
249+
Collections.addAll(revSet, revisions);
250+
251+
File root = new File(repository.getSourceRoot(), "git");
252+
GitRepository gitrepo
253+
= (GitRepository) RepositoryFactory.getRepository(root);
254+
gitrepo.setHandleRenamedFiles(true);
255+
File renamedFile = Paths.get(root.getAbsolutePath(),"moved2", "renamed2.c").toFile();
256+
testAnnotationOfFile(gitrepo, renamedFile, "1086eaf5", revSet);
240257
}
241258

242259
@Test(expected = IOException.class)
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)