From 2b9f72b82124b12a6582734ffc087bec30766546 Mon Sep 17 00:00:00 2001 From: Grigoriy Bykov Date: Thu, 12 Dec 2024 00:58:49 +0300 Subject: [PATCH] Fix working with raw git diff - Fix getting `baseSha` and `headSha`. Now I get `baseSha` from first commit, and `headSha` is got from the last commit. Need to sort commits by author date to be ordered chronologically - Reverted `baseSha` and `headSha` when computing `git diff` - Due to swapping `baseSha` and `headSha` also swapped additions and deletions in computation lambdas Closes #250 --- .../kotlin/systems/danger/kotlin/KtxGit.kt | 17 +++++++----- .../systems/danger/kotlin/KtxGitTest.kt | 26 ++++++++++++++++--- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/KtxGit.kt b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/KtxGit.kt index 7861c56e..db94ad4c 100644 --- a/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/KtxGit.kt +++ b/danger-kotlin-library/src/main/kotlin/systems/danger/kotlin/KtxGit.kt @@ -1,6 +1,7 @@ package systems.danger.kotlin import systems.danger.kotlin.models.git.Git +import systems.danger.kotlin.models.git.GitCommit import systems.danger.kotlin.tools.shell.ShellExecutorFactory // extensions over [Git] object @@ -12,16 +13,16 @@ val Git.changedLines: PullRequestChangedLines get() { if (headSha == null || baseSha == null) return PullRequestChangedLines(0, 0) val shellExecutor = ShellExecutorFactory.get() - val commandRawOutput = shellExecutor.execute("git diff --numstat $headSha $baseSha") + val commandRawOutput = shellExecutor.execute("git diff --numstat $baseSha $headSha") val additionDeletionPairs = commandRawOutput.lines() .filter { it.isNotEmpty() } .map { line -> val parts = line.split("\\s+".toRegex()) (parts[0].toIntOrNull() ?: 0) to (parts[1].toIntOrNull() ?: 0) } - val additions = additionDeletionPairs.fold(0) { acc, (_, addition) -> acc + addition } - val deletions = additionDeletionPairs.fold(0) { acc, (deletion, _) -> acc + deletion } - val commandRawDiffOutput = shellExecutor.execute("git diff $headSha $baseSha") + val additions = additionDeletionPairs.fold(0) { acc, (addition, _) -> acc + addition } + val deletions = additionDeletionPairs.fold(0) { acc, (_, deletion) -> acc + deletion } + val commandRawDiffOutput = shellExecutor.execute("git diff $baseSha $headSha") return PullRequestChangedLines(additions, deletions, commandRawDiffOutput) } @@ -47,13 +48,13 @@ val Git.deletions: Int * Reference to a SHA of head commit of this PR */ val Git.headSha: String? - get() = commits.firstOrNull()?.sha + get() = commits.sortChronologically().lastOrNull()?.sha /** * Reference to a SHA of base commit of this PR */ val Git.baseSha: String? - get() = commits.lastOrNull()?.sha?.let { "$it^1" } + get() = commits.sortChronologically().firstOrNull()?.sha?.let { "$it^1" } /** * Unified diff of this PR @@ -74,3 +75,7 @@ data class PullRequestChangedLines( val deletions: Int, val diff: String? = null ) + +private fun List.sortChronologically(): List { + return sortedBy { it.author.date } +} \ No newline at end of file diff --git a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/KtxGitTest.kt b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/KtxGitTest.kt index 9bc60ba8..d4b3fcb2 100644 --- a/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/KtxGitTest.kt +++ b/danger-kotlin-library/src/test/kotlin/systems/danger/kotlin/KtxGitTest.kt @@ -2,7 +2,7 @@ package systems.danger.kotlin import io.mockk.every import io.mockk.mockk -import junit.framework.Assert.assertEquals +import org.junit.Assert.assertEquals import org.junit.Before import org.junit.Test import systems.danger.kotlin.models.git.Git @@ -32,8 +32,16 @@ internal class GitKtxTest { commits = listOf( GitCommit( sha = "commit1", - author = GitCommitAuthor("John Doe", "john@doe.com", "now"), - committer = GitCommitAuthor("John Doe", "john@doe.com", "now"), + author = GitCommitAuthor("John Doe", "john@doe.com", "2024-11-28T13:41:53Z"), + committer = GitCommitAuthor("John Doe", "john@doe.com", "2024-12-04T09:15:23Z"), + message = "Random message", + parents = listOf(), + url = "" + ), + GitCommit( + sha = "commit2", + author = GitCommitAuthor("John Doe", "john@doe.com", "2024-11-28T13:54:45Z"), + committer = GitCommitAuthor("John Doe", "john@doe.com", "2024-12-04T09:15:23Z"), message = "Random message", parents = listOf(), url = "" @@ -41,7 +49,7 @@ internal class GitKtxTest { ) ) - private val expectedResult = PullRequestChangedLines(22, 8, diffCommandOutput) + private val expectedResult = PullRequestChangedLines(8, 22, diffCommandOutput) @Before fun setup() { @@ -75,4 +83,14 @@ internal class GitKtxTest { val gitWOCommits = basicGit.copy(commits = emptyList()) assertEquals(PullRequestChangedLines(0, 0), gitWOCommits.changedLines) } + + @Test + fun testBaseShaIsCorrect() { + assertEquals("commit1^1", basicGit.baseSha) + } + + @Test + fun testHeadShaIsCorrect() { + assertEquals("commit2", basicGit.headSha) + } } \ No newline at end of file