Skip to content

Commit

Permalink
[patch] squishing one merge related all contribution history bug - mo…
Browse files Browse the repository at this point in the history
…re remain
  • Loading branch information
robertfmurdock committed Aug 6, 2024
1 parent e8c4376 commit a24fc36
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ private fun List<CommitRef>.foldInBranches(log: List<CommitRef>): List<CommitRef
return reversed()
.fold<CommitRef, List<CommitRef>>(emptyList()) { acc, commit ->
if (commit.parents.size > 1) {
acc + branchCommits(
commit.parents[1],
offMainCommits - acc.toSet(),
) + listOf(commit)
acc + (
branchCommits(
commit.parents[1],
offMainCommits - acc.toSet(),
) + listOf(commit)
)
} else {
acc + listOf(commit)
}
Expand All @@ -61,9 +63,8 @@ private fun List<Pair<TagRef?, Set<String>>>.withCommitsInOriginalOrder(
tag to log.filter { commit -> commitIds.contains(commit.id) }
}

fun branchCommits(id: String?, offMainCommits: List<CommitRef>): List<CommitRef> = (0..offMainCommits.size)
.fold(id to emptyList<CommitRef>()) { (id, foundCommits), _ ->
val newCommit = offMainCommits.find { it.id == id }
newCommit?.parents?.first() to (foundCommits + newCommit)
.filterNotNull()
fun branchCommits(id: String, offMainCommits: List<CommitRef>): List<CommitRef> = (0..offMainCommits.size)
.fold(listOf(id) to emptyList<CommitRef>()) { (ids, foundCommits), _ ->
val newCommits = offMainCommits.filter { ids.contains(it.id) }
newCommits.flatMap { it.parents } to (foundCommits + newCommits)
}.second
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,69 @@ interface AllContributionTestSpec : SetupWithOverrides {
)
}

@Test
fun `will handle merge commits on merged branches correctly`() {
setupWithDefaults()
val grgit = initializeGitRepo(
projectDirectoryPath = projectDir.absolutePath,
addFileNames = addFileNames,
listOf("first"),
)
val firstCommit = grgit.head()

val firstRelease = grgit.addTag("release")
grgit.branch.add { it.name = "branch2" }
grgit.checkout { it.branch = "branch2" }
val secondCommit = grgit.addCommitWithMessage("second")

grgit.branch.add { it.name = "branch1" }
grgit.checkout { it.branch = "branch1" }
grgit.addCommitWithMessage("third")

grgit.checkout { it.branch = "branch2" }
grgit.addCommitWithMessage("fourth")
grgit.checkout { it.branch = "branch1" }
grgit.addCommitWithMessage("fifth")

grgit.merge {
it.head = "branch2"
it.setMode("no-commit")
}
grgit.addCommitWithMessage("merge1")

grgit.checkout { it.branch = "main" }
grgit.addCommitWithMessage("sixth")
grgit.merge {
it.head = "branch1"
it.setMode("no-commit")
}
val merge2Commit = grgit.addCommitWithMessage("merge2")
val thirdRelease = grgit.addTag("release3")

val allOutput = runAllContributionData()
val expectedAuthors = listOf(
"[email protected]",
"[email protected]",
)
assertEquals(
listOf(
toContribution(
lastCommit = merge2Commit,
firstCommit = secondCommit,
expectedCommitCount = 7,
tag = thirdRelease,
expectedAuthors = expectedAuthors,
),
toContribution(
lastCommit = firstCommit,
tag = firstRelease,
expectedAuthors = expectedAuthors,
),
),
parseAll(allOutput),
)
}

@Test
fun `when merging multiple times from same branch, commits are only counted once`() {
setupWithDefaults()
Expand Down

0 comments on commit a24fc36

Please sign in to comment.