@@ -277,13 +277,18 @@ static Reader newLogReader(InputStream input) throws IOException {
277
277
// platform's default encoding.
278
278
return new InputStreamReader (input , "UTF-8" );
279
279
}
280
-
280
+
281
+ private String getPathRelativeToRepositoryRoot (String fullpath ) {
282
+ String repoPath = getDirectoryName () + File .separator ;
283
+ return fullpath .replace (repoPath , "" );
284
+ }
285
+
281
286
/**
282
287
* Get the name of file in given revision.
283
288
*
284
289
* @param fullpath file path
285
290
* @param changeset changeset
286
- * @return original filename
291
+ * @return original filename relative to the repository root
287
292
* @throws java.io.IOException if I/O exception occurred
288
293
*/
289
294
protected String findOriginalName (String fullpath , String changeset )
@@ -297,7 +302,7 @@ protected String findOriginalName(String fullpath, String changeset)
297
302
fullpath , changeset ));
298
303
}
299
304
300
- String file = fullpath . replace ( getDirectoryName () + File . separator , "" );
305
+ String fileInRepo = getPathRelativeToRepositoryRoot ( fullpath );
301
306
/*
302
307
* Get the list of file renames for given file to the specified
303
308
* revision.
@@ -312,13 +317,14 @@ protected String findOriginalName(String fullpath, String changeset)
312
317
"--name-status" ,
313
318
"--pretty=format:commit %h%b" ,
314
319
"--" ,
315
- fullpath
320
+ fileInRepo
316
321
};
317
322
318
323
Executor executor = new Executor (Arrays .asList (argv ), new File (getDirectoryName ()),
319
324
RuntimeEnvironment .getInstance ().getInteractiveCommandTimeout ());
320
325
int status = executor .exec ();
321
-
326
+
327
+ String originalFile = null ;
322
328
try (BufferedReader in = new BufferedReader (
323
329
new InputStreamReader (executor .getOutputStream ()))) {
324
330
String line ;
@@ -332,23 +338,26 @@ protected String findOriginalName(String fullpath, String changeset)
332
338
}
333
339
334
340
if (changeset .equals (rev )) {
341
+ if (originalFile == null ) {
342
+ originalFile = fileInRepo ;
343
+ }
335
344
break ;
336
345
}
337
346
338
347
if ((m = pattern .matcher (line )).find ()) {
339
- file = m .group (1 );
348
+ originalFile = m .group (1 );
340
349
}
341
350
}
342
351
}
343
352
344
- if (status != 0 ) {
353
+ if (status != 0 || originalFile == null ) {
345
354
LOGGER .log (Level .WARNING ,
346
355
"Failed to get original name in revision {2} for: \" {0}\" Exit code: {1}" ,
347
356
new Object []{fullpath , String .valueOf (status ), changeset });
348
357
return null ;
349
358
}
350
359
351
- return ( fullpath . substring ( 0 , getDirectoryName (). length () + 1 ) + file ) ;
360
+ return originalFile ;
352
361
}
353
362
354
363
/**
@@ -394,7 +403,7 @@ private String getFirstRevision(String fullpath) throws IOException {
394
403
*
395
404
* @param file file to annotate
396
405
* @param revision revision to annotate
397
- * @return file annotation
406
+ * @return file annotation or {@code null}
398
407
* @throws java.io.IOException if I/O exception occurred
399
408
*/
400
409
@ Override
@@ -419,16 +428,16 @@ public Annotation annotate(File file, String revision) throws IOException {
419
428
}
420
429
}
421
430
cmd .add ("--" );
422
- cmd .add (file .getName ( ));
431
+ cmd .add (getPathRelativeToRepositoryRoot ( file .getCanonicalPath () ));
423
432
424
- Executor exec = new Executor (cmd , file . getParentFile ( ),
433
+ Executor exec = new Executor (cmd , new File ( getDirectoryName () ),
425
434
RuntimeEnvironment .getInstance ().getInteractiveCommandTimeout ());
426
435
GitAnnotationParser parser = new GitAnnotationParser (file .getName ());
427
436
int status = exec .exec (true , parser );
428
437
429
438
// File might have changed its location if it was renamed.
430
439
// Try to lookup its original name and get the annotation again.
431
- if (status != 0 ) {
440
+ if (status != 0 && isHandleRenamedFiles () ) {
432
441
cmd .clear ();
433
442
ensureCommand (CMD_PROPERTY_KEY , CMD_FALLBACK );
434
443
cmd .add (RepoCommand );
@@ -439,16 +448,18 @@ public Annotation annotate(File file, String revision) throws IOException {
439
448
cmd .add (revision );
440
449
}
441
450
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 );
446
456
}
447
457
448
458
if (status != 0 ) {
449
459
LOGGER .log (Level .WARNING ,
450
460
"Failed to get annotations for: \" {0}\" Exit code: {1}" ,
451
461
new Object []{file .getAbsolutePath (), String .valueOf (status )});
462
+ return null ;
452
463
}
453
464
454
465
return parser .getAnnotation ();
0 commit comments