11package systems.danger.kotlin
22
33import systems.danger.kotlin.models.git.Git
4+ import systems.danger.kotlin.models.git.GitCommit
45import systems.danger.kotlin.tools.shell.ShellExecutorFactory
56
67// extensions over [Git] object
@@ -12,16 +13,16 @@ val Git.changedLines: PullRequestChangedLines
1213 get() {
1314 if (headSha == null || baseSha == null ) return PullRequestChangedLines (0 , 0 )
1415 val shellExecutor = ShellExecutorFactory .get()
15- val commandRawOutput = shellExecutor.execute(" git diff --numstat $headSha $baseSha " )
16+ val commandRawOutput = shellExecutor.execute(" git diff --numstat $baseSha $headSha " )
1617 val additionDeletionPairs = commandRawOutput.lines()
1718 .filter { it.isNotEmpty() }
1819 .map { line ->
1920 val parts = line.split(" \\ s+" .toRegex())
2021 (parts[0 ].toIntOrNull() ? : 0 ) to (parts[1 ].toIntOrNull() ? : 0 )
2122 }
22- val additions = additionDeletionPairs.fold(0 ) { acc, (_, addition ) -> acc + addition }
23- val deletions = additionDeletionPairs.fold(0 ) { acc, (deletion, _ ) -> acc + deletion }
24- val commandRawDiffOutput = shellExecutor.execute(" git diff $headSha $baseSha " )
23+ val additions = additionDeletionPairs.fold(0 ) { acc, (addition, _ ) -> acc + addition }
24+ val deletions = additionDeletionPairs.fold(0 ) { acc, (_, deletion ) -> acc + deletion }
25+ val commandRawDiffOutput = shellExecutor.execute(" git diff $baseSha $headSha " )
2526 return PullRequestChangedLines (additions, deletions, commandRawDiffOutput)
2627 }
2728
@@ -47,13 +48,13 @@ val Git.deletions: Int
4748 * Reference to a SHA of head commit of this PR
4849 */
4950val Git .headSha: String?
50- get() = commits.firstOrNull ()?.sha
51+ get() = commits.sortChronologically().lastOrNull ()?.sha
5152
5253/* *
5354 * Reference to a SHA of base commit of this PR
5455 */
5556val Git .baseSha: String?
56- get() = commits.lastOrNull ()?.sha?.let { " $it ^1" }
57+ get() = commits.sortChronologically().firstOrNull ()?.sha?.let { " $it ^1" }
5758
5859/* *
5960 * Unified diff of this PR
@@ -74,3 +75,7 @@ data class PullRequestChangedLines(
7475 val deletions : Int ,
7576 val diff : String? = null
7677)
78+
79+ private fun List<GitCommit>.sortChronologically (): List <GitCommit > {
80+ return sortedBy { it.author.date }
81+ }
0 commit comments