@@ -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 ();
0 commit comments