diff --git a/src/main/kotlin/nl/avisi/structurizr/site/generatr/GenerateSiteCommand.kt b/src/main/kotlin/nl/avisi/structurizr/site/generatr/GenerateSiteCommand.kt index 2d7cfddf..5103d43f 100644 --- a/src/main/kotlin/nl/avisi/structurizr/site/generatr/GenerateSiteCommand.kt +++ b/src/main/kotlin/nl/avisi/structurizr/site/generatr/GenerateSiteCommand.kt @@ -101,7 +101,7 @@ class GenerateSiteCommand : Subcommand( println("Bad Branch $branch: $errorMessage") false } - } + }.sortedWith(branchComparator(defaultBranch)) println("The following branches contain a valid Structurizr workspace: $branchesToGenerate") @@ -139,3 +139,9 @@ class GenerateSiteCommand : Subcommand( ) } } + +fun branchComparator(defaultBranch: String) = Comparator { a, b -> + if (a == defaultBranch) -1 + else if (b == defaultBranch) 1 + else a.compareTo(b) +} diff --git a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/BranchComparatorTest.kt b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/BranchComparatorTest.kt new file mode 100644 index 00000000..4c0aa111 --- /dev/null +++ b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/BranchComparatorTest.kt @@ -0,0 +1,24 @@ +package nl.avisi.structurizr.site.generatr.site + +import assertk.assertThat +import assertk.assertions.containsExactly +import nl.avisi.structurizr.site.generatr.branchComparator +import org.junit.jupiter.api.DynamicTest +import org.junit.jupiter.api.TestFactory + +class BranchComparatorTest { + @TestFactory + fun `default branch first then by string`() = listOf( + listOf("a", "b", "main"), + listOf("a", "main", "b"), + listOf("b", "a", "main"), + listOf("b", "main", "a"), + listOf("main", "a", "b"), + listOf("main", "b", "a") + ).map { branches -> + DynamicTest.dynamicTest(branches.toString()) { + assertThat(branches.sortedWith(branchComparator("main"))) + .containsExactly("main", "a", "b") + } + } +}