diff --git a/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java b/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java index 726c48f41e8d..f032b29360a9 100644 --- a/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java +++ b/plugins/git4idea/src/git4idea/history/GitHistoryUtils.java @@ -253,24 +253,39 @@ public class GitHistoryUtils { } /** - * Gets info of the given commit and checks if it was RENAME. If yes, returns the old file path, which file was renamed from. + * Gets info of the given commit and checks if it was a RENAME. + * If yes, returns the older file path, which file was renamed from. * If it's not a rename, returns null. */ @Nullable private static FilePath getFirstCommitRenamePath(Project project, VirtualFile root, String commit, FilePath filePath) throws VcsException { + // 'git show -M --name-status ' returns the information about commit and detects renames. + // NB: we can't specify the filepath, because then rename detection will work only with the '--follow' option, which we don't wanna use. final GitSimpleHandler h = new GitSimpleHandler(project, root, GitCommand.SHOW); final GitLogParser parser = new GitLogParser(HASH); h.setNoSSH(true); h.setStdoutSuppressed(true); - - h.addParameters("-M", "--follow", "--name-status", parser.getPretty(), "--encoding=UTF-8", commit); + h.addParameters("-M", "--name-status", parser.getPretty(), "--encoding=UTF-8", commit); h.endOptions(); - h.addRelativePaths(filePath); parser.setNameInOutput(true); final String output = h.run(); - final GitLogRecord record = parser.parseOneRecord(output); - if (record.getNameStatus() == 'R') { - final List paths = record.getFilePaths(root); + final List records = parser.parse(output); + + // we have information about all changed files of the commit. Extracting information about the file we need. + GitLogRecord fileRecord = null; + for (GitLogRecord record : records) { + final List paths = record.getPaths(); + if (!paths.isEmpty()) { + String path = paths.get(paths.size()-1); // if the file is renamed, it has 2 paths - we are looking for the new name. + if (path.equals(GitUtil.relativePath(root, filePath))) { + fileRecord = record; + break; + } + } + } + + if (fileRecord != null && fileRecord.getNameStatus() == 'R') { + final List paths = fileRecord.getFilePaths(root); final String message = "Rename commit should have 2 paths. Commit: " + commit; if (!LOG.assertTrue(paths.size() == 2, message + " Output: [" + output + "]")) { throw new VcsException(message); diff --git a/plugins/git4idea/src/git4idea/history/GitLogRecord.java b/plugins/git4idea/src/git4idea/history/GitLogRecord.java index 5d2f34aa6199..ccec182eb4e1 100644 --- a/plugins/git4idea/src/git4idea/history/GitLogRecord.java +++ b/plugins/git4idea/src/git4idea/history/GitLogRecord.java @@ -39,7 +39,7 @@ class GitLogRecord { private final List myPaths; private final char myNameStatus; - GitLogRecord(Map options, List paths, char nameStatus) { + GitLogRecord(@NotNull Map options, @NotNull List paths, char nameStatus) { myOptions = options; myPaths = paths; myNameStatus = nameStatus; @@ -54,7 +54,7 @@ class GitLogRecord { return myNameStatus; } - Collection getPaths() { + @NotNull List getPaths() { return myPaths; }