Skip to content
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

Fix working with raw git diff #297

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}

Expand All @@ -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
Expand All @@ -74,3 +75,7 @@ data class PullRequestChangedLines(
val deletions: Int,
val diff: String? = null
)

private fun List<GitCommit>.sortChronologically(): List<GitCommit> {
return sortedBy { it.author.date }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -32,16 +32,24 @@ internal class GitKtxTest {
commits = listOf(
GitCommit(
sha = "commit1",
author = GitCommitAuthor("John Doe", "[email protected]", "now"),
committer = GitCommitAuthor("John Doe", "[email protected]", "now"),
author = GitCommitAuthor("John Doe", "[email protected]", "2024-11-28T13:41:53Z"),
committer = GitCommitAuthor("John Doe", "[email protected]", "2024-12-04T09:15:23Z"),
message = "Random message",
parents = listOf(),
url = ""
),
GitCommit(
sha = "commit2",
author = GitCommitAuthor("John Doe", "[email protected]", "2024-11-28T13:54:45Z"),
committer = GitCommitAuthor("John Doe", "[email protected]", "2024-12-04T09:15:23Z"),
message = "Random message",
parents = listOf(),
url = ""
)
)
)

private val expectedResult = PullRequestChangedLines(22, 8, diffCommandOutput)
private val expectedResult = PullRequestChangedLines(8, 22, diffCommandOutput)

@Before
fun setup() {
Expand Down Expand Up @@ -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)
}
}
Loading