Git Diff: get modified lines
Answered
I am looking to perform a git diff from the current branch to a specified branch (e.g. develop), but I want a list of lines that have been added or modified. I thought that stealing the Git4Idea GitCompareWithBranchAction would help. But this finds a Collection<Change> which, as far as I can tell, only has the before and after view of the entire file, but not a list of the modified lines.
The diff editor clearly indicates modified lines, so there must be a way to get this info. Any ideas on how to get it?
Please sign in to leave a comment.
IDE uses 'Change' to load file contents before/after (see Change#getBeforeRevision/getAfterRevision and ContentRevision#getContent / ByteBackedContentRevision#getContentAsBytes) and compares them itself (see com.intellij.diff.comparison#ComparisonManager).
As an alternative, you can use GitLineHandler to execute git command (ex: "git diff develop"), that will produce these changed lines, and then parse its output (either manually, or using 'com.intellij.openapi.diff.impl.patch.PatchReader').