1
1
package systems.danger.kotlin
2
2
3
3
import systems.danger.kotlin.models.git.Git
4
+ import systems.danger.kotlin.models.git.GitCommit
4
5
import systems.danger.kotlin.tools.shell.ShellExecutorFactory
5
6
6
7
// extensions over [Git] object
@@ -12,16 +13,16 @@ val Git.changedLines: PullRequestChangedLines
12
13
get() {
13
14
if (headSha == null || baseSha == null ) return PullRequestChangedLines (0 , 0 )
14
15
val shellExecutor = ShellExecutorFactory .get()
15
- val commandRawOutput = shellExecutor.execute(" git diff --numstat $headSha $baseSha " )
16
+ val commandRawOutput = shellExecutor.execute(" git diff --numstat $baseSha $headSha " )
16
17
val additionDeletionPairs = commandRawOutput.lines()
17
18
.filter { it.isNotEmpty() }
18
19
.map { line ->
19
20
val parts = line.split(" \\ s+" .toRegex())
20
21
(parts[0 ].toIntOrNull() ? : 0 ) to (parts[1 ].toIntOrNull() ? : 0 )
21
22
}
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 " )
25
26
return PullRequestChangedLines (additions, deletions, commandRawDiffOutput)
26
27
}
27
28
@@ -47,13 +48,13 @@ val Git.deletions: Int
47
48
* Reference to a SHA of head commit of this PR
48
49
*/
49
50
val Git .headSha: String?
50
- get() = commits.firstOrNull ()?.sha
51
+ get() = commits.sortChronologically().lastOrNull ()?.sha
51
52
52
53
/* *
53
54
* Reference to a SHA of base commit of this PR
54
55
*/
55
56
val Git .baseSha: String?
56
- get() = commits.lastOrNull ()?.sha?.let { " $it ^1" }
57
+ get() = commits.sortChronologically().firstOrNull ()?.sha?.let { " $it ^1" }
57
58
58
59
/* *
59
60
* Unified diff of this PR
@@ -74,3 +75,7 @@ data class PullRequestChangedLines(
74
75
val deletions : Int ,
75
76
val diff : String? = null
76
77
)
78
+
79
+ private fun List<GitCommit>.sortChronologically (): List <GitCommit > {
80
+ return sortedBy { it.author.date }
81
+ }
0 commit comments