Skip to content

Commit 18747b0

Browse files
authored
Merge pull request #297 from grine4ka/fix-raw-git-diff
Fix working with raw git diff
2 parents ab04ec7 + e1dbcbe commit 18747b0

File tree

2 files changed

+33
-10
lines changed
  • danger-kotlin-library/src

2 files changed

+33
-10
lines changed

danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/KtxGit.kt

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package systems.danger.kotlin
22

33
import systems.danger.kotlin.models.git.Git
4+
import systems.danger.kotlin.models.git.GitCommit
45
import 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
*/
4950
val 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
*/
5556
val 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+
}

danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/KtxGitTest.kt

+22-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package systems.danger.kotlin
22

33
import io.mockk.every
44
import io.mockk.mockk
5-
import junit.framework.Assert.assertEquals
5+
import org.junit.Assert.assertEquals
66
import org.junit.Before
77
import org.junit.Test
88
import systems.danger.kotlin.models.git.Git
@@ -32,16 +32,24 @@ internal class GitKtxTest {
3232
commits = listOf(
3333
GitCommit(
3434
sha = "commit1",
35-
author = GitCommitAuthor("John Doe", "[email protected]", "now"),
36-
committer = GitCommitAuthor("John Doe", "[email protected]", "now"),
35+
author = GitCommitAuthor("John Doe", "[email protected]", "2024-11-28T13:41:53Z"),
36+
committer = GitCommitAuthor("John Doe", "[email protected]", "2024-12-04T09:15:23Z"),
37+
message = "Random message",
38+
parents = listOf(),
39+
url = ""
40+
),
41+
GitCommit(
42+
sha = "commit2",
43+
author = GitCommitAuthor("John Doe", "[email protected]", "2024-11-28T13:54:45Z"),
44+
committer = GitCommitAuthor("John Doe", "[email protected]", "2024-12-04T09:15:23Z"),
3745
message = "Random message",
3846
parents = listOf(),
3947
url = ""
4048
)
4149
)
4250
)
4351

44-
private val expectedResult = PullRequestChangedLines(22, 8, diffCommandOutput)
52+
private val expectedResult = PullRequestChangedLines(8, 22, diffCommandOutput)
4553

4654
@Before
4755
fun setup() {
@@ -75,4 +83,14 @@ internal class GitKtxTest {
7583
val gitWOCommits = basicGit.copy(commits = emptyList())
7684
assertEquals(PullRequestChangedLines(0, 0), gitWOCommits.changedLines)
7785
}
86+
87+
@Test
88+
fun testBaseShaIsCorrect() {
89+
assertEquals("commit1^1", basicGit.baseSha)
90+
}
91+
92+
@Test
93+
fun testHeadShaIsCorrect() {
94+
assertEquals("commit2", basicGit.headSha)
95+
}
7896
}

0 commit comments

Comments
 (0)