-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#2196] Use git blame line info for aggregate blame author modified and date info #2232
Open
logical-1985516
wants to merge
24
commits into
reposense:master
Choose a base branch
from
logical-1985516:2196-use-GitBlameLineInfo-for-aggregateBlameAuthorModifiedAndDateInfo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
0f8de06
Replace string wrangling with blameLine
logical-1985516 8c74f33
Fix lineNumber argument for blameLine and commitDate
logical-1985516 cd69a96
Improve variable name, add comments and replace magic numbers
logical-1985516 f8ddc62
Replace commitHash in blameLine with "" to return all blame lines
logical-1985516 72b1f89
Edit commitHash to now be returned from blameLineInfo for abstraction
logical-1985516 92ccb77
Add variable commitDateInMs for better readability
logical-1985516 c0c78a6
Rename method to be in seconds and change to commit-time in blameLine
logical-1985516 5073ecf
Remove unused constant COMMIT_TIME_OFFSET
logical-1985516 310856e
Remove unused constants
logical-1985516 5b6caf0
Change milliseconds to seconds
logical-1985516 b869ba1
Merge branch 'master' into 2196-use-GitBlameLineInfo-for-aggregateBla…
logical-1985516 22473ba
Add and use blameFile that returns a list of GitBlameLineInfo
logical-1985516 ed17dc8
Add timeOption in processGitBlameResultLine and clean up statements
logical-1985516 c8115e2
Merge branch '2196-use-GitBlameLineInfo-for-aggregateBlameAuthorModif…
logical-1985516 37e8f6b
Merge branch 'master' into 2196-use-GitBlameLineInfo-for-aggregateBla…
logical-1985516 c6e4015
Merge branch 'master' into 2196-use-GitBlameLineInfo-for-aggregateBla…
logical-1985516 eab3489
Merge branch 'master' into 2196-use-GitBlameLineInfo-for-aggregateBla…
gok99 cecb1b2
Change processGitBlameResultLine to take in String[] for compatibility
logical-1985516 96eaf57
Update blameFile to perform blame; abstract out processing in blameFile
logical-1985516 82bb6f7
Merge branch '2196-use-GitBlameLineInfo-for-aggregateBlameAuthorModif…
logical-1985516 d1a3cf8
Merge branch 'master' into 2196-use-GitBlameLineInfo-for-aggregateBla…
logical-1985516 2ab32d3
Use getGitBlameFileResult in FileInfoAnalyzer for abstraction
logical-1985516 f843539
Merge branch '2196-use-GitBlameLineInfo-for-aggregateBlameAuthorModif…
logical-1985516 972f0e2
Merge branch 'master' into 2196-use-GitBlameLineInfo-for-aggregateBla…
gok99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ | |
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import reposense.git.model.GitBlameLineInfo; | ||
import reposense.util.StringsUtil; | ||
|
@@ -16,6 +19,8 @@ | |
public class GitBlame { | ||
public static final String IGNORE_COMMIT_LIST_FILE_NAME = ".git-blame-ignore-revs"; | ||
|
||
private static final int BLAME_LINE_INFO_ROW_COUNT = 5; | ||
|
||
private static final String COMMIT_HASH_REGEX = "(^[0-9a-f]{40} .*)"; | ||
private static final String AUTHOR_NAME_REGEX = "(^author .*)"; | ||
private static final String AUTHOR_EMAIL_REGEX = "(^author-mail .*)"; | ||
|
@@ -28,8 +33,9 @@ public class GitBlame { | |
|
||
private static final int AUTHOR_NAME_OFFSET = "author ".length(); | ||
private static final int AUTHOR_EMAIL_OFFSET = "author-mail ".length(); | ||
private static final int FULL_COMMIT_HASH_LENGTH = 40; | ||
private static final int AUTHOR_TIME_OFFSET = "author-time ".length(); | ||
private static final int COMMIT_TIME_OFFSET = "committer-time ".length(); | ||
private static final int FULL_COMMIT_HASH_LENGTH = 40; | ||
|
||
/** | ||
* Returns the raw git blame result for the {@code fileDirectory}, performed at the {@code root} directory. | ||
|
@@ -57,6 +63,17 @@ public static String blameWithPreviousAuthors(String root, String fileDirectory) | |
return StringsUtil.filterText(runCommand(rootPath, blameCommandWithFindingPreviousAuthors), COMBINATION_REGEX); | ||
} | ||
|
||
/** | ||
* Returns the processed git blame result for the {@code fileDirectory} performed at the {@code root} directory, | ||
* with reference to {@code withPreviousAuthors}. | ||
*/ | ||
public static List<GitBlameLineInfo> blameFile(String root, String fileDirectory, boolean withPreviousAuthors) { | ||
String blameResults = withPreviousAuthors | ||
? blameWithPreviousAuthors(root, fileDirectory) | ||
: blame(root, fileDirectory); | ||
return processGitBlameResultLines(blameResults); | ||
} | ||
|
||
/** | ||
* Returns the git blame result for {@code lineNumber} of {@code fileDirectory} at {@code commitHash}. | ||
*/ | ||
|
@@ -68,21 +85,41 @@ public static GitBlameLineInfo blameLine(String root, String commitHash, String | |
|
||
String blameResult = StringsUtil.filterText(runCommand(rootPath, blameCommand), | ||
COMBINATION_WITH_COMMIT_TIME_REGEX); | ||
String[] blameResultLines = StringsUtil.NEWLINE.split(blameResult); | ||
return processGitBlameResultLine(blameResultLines, "committer"); | ||
} | ||
|
||
return processGitBlameResultLine(blameResult); | ||
/** | ||
* Returns the processed result of {@code blameResults}. | ||
*/ | ||
private static List<GitBlameLineInfo> processGitBlameResultLines(String blameResults) { | ||
String[] blameResultsLines = StringsUtil.NEWLINE.split(blameResults); | ||
List<GitBlameLineInfo> blameFileResult = new ArrayList<>(); | ||
for (int lineCount = 0; lineCount < blameResultsLines.length; lineCount += BLAME_LINE_INFO_ROW_COUNT) { | ||
String[] blameResultLines = Arrays | ||
.copyOfRange(blameResultsLines, lineCount, lineCount + BLAME_LINE_INFO_ROW_COUNT - 1); | ||
GitBlameLineInfo blameLineInfo = processGitBlameResultLine(blameResultLines, "author"); | ||
blameFileResult.add(blameLineInfo); | ||
} | ||
return blameFileResult; | ||
} | ||
|
||
/** | ||
* Returns the processed result of {@code blameResult}. | ||
* Returns the processed result of {@code blameResultLines}, with reference to {@code timeOption}. | ||
*/ | ||
private static GitBlameLineInfo processGitBlameResultLine(String blameResult) { | ||
String[] blameResultLines = StringsUtil.NEWLINE.split(blameResult); | ||
private static GitBlameLineInfo processGitBlameResultLine(String[] blameResultLines, String timeOption) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to use a boolean for timeOption since it doesn't seem like there are likely to be new options in git in the future. |
||
assert timeOption.equals("author") || timeOption.equals("committer"); | ||
|
||
String commitHash = blameResultLines[0].substring(0, FULL_COMMIT_HASH_LENGTH); | ||
String authorName = blameResultLines[1].substring(AUTHOR_NAME_OFFSET); | ||
String authorEmail = blameResultLines[2].substring(AUTHOR_EMAIL_OFFSET).replaceAll("[<>]", ""); | ||
long timestampMilliseconds = Long.parseLong(blameResultLines[5].substring(COMMIT_TIME_OFFSET)); | ||
|
||
return new GitBlameLineInfo(commitHash, authorName, authorEmail, timestampMilliseconds); | ||
long timestampInSeconds; | ||
if (timeOption.equals("author")) { | ||
timestampInSeconds = Long.parseLong(blameResultLines[3].substring(AUTHOR_TIME_OFFSET)); | ||
} else { | ||
timestampInSeconds = Long.parseLong(blameResultLines[5].substring(COMMIT_TIME_OFFSET)); | ||
} | ||
|
||
return new GitBlameLineInfo(commitHash, authorName, authorEmail, timestampInSeconds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add / update tests for functions / flags you've added here?