Skip to content

Commit 1ea3fd8

Browse files
authored
Merge pull request #3017 from scala-steward-org/topic/use-git-trailers-option
Use Git's `--trailer` for commit message trailers
2 parents 06b28e2 + 505d323 commit 1ea3fd8

File tree

5 files changed

+19
-17
lines changed

5 files changed

+19
-17
lines changed

modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ final class HookExecutor[F[_]](implicit
7474
}
7575
commitMessage = hook
7676
.commitMessage(update)
77-
.withParagraph(s"Executed command: ${hook.command.mkString_(" ")}")
77+
.appendParagraph(s"Executed command: ${hook.command.mkString_(" ")}")
7878
maybeHookCommit <- gitAlg.commitAllIfDirty(repo, commitMessage)
7979
maybeBlameIgnoreCommit <-
8080
maybeHookCommit.flatTraverse(addToGitBlameIgnoreRevs(repo, repoDir, hook, _, commitMessage))

modules/core/src/main/scala/org/scalasteward/core/git/CommitMsg.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@ final case class CommitMsg(
2626
body: List[String] = Nil,
2727
coAuthoredBy: List[Author] = Nil
2828
) {
29-
def toNel: Nel[String] =
30-
Nel(title, body ++ trailers)
29+
def paragraphs: Nel[String] =
30+
Nel(title, body)
3131

32-
def withParagraph(paragraph: String): CommitMsg =
32+
def appendParagraph(paragraph: String): CommitMsg =
3333
copy(body = body :+ paragraph)
3434

35-
private def trailers: Option[String] = {
36-
val lines = coAuthoredBy.map(author => s"Co-authored-by: ${author.show}")
37-
Nel.fromList(lines).map(_.mkString_("\n"))
38-
}
35+
def trailers: List[(String, String)] =
36+
coAuthoredBy.map(author => ("Co-authored-by", author.show))
3937
}
4038

4139
object CommitMsg {

modules/core/src/main/scala/org/scalasteward/core/git/FileGitAlg.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ final class FileGitAlg[F[_]](config: GitCfg)(implicit
6464
fileAlg.isDirectory(repo / ".git")
6565

6666
override def commitAll(repo: File, message: CommitMsg): F[Commit] = {
67-
val messages = message.toNel.foldMap(m => List("-m", m))
68-
git_("commit" :: "--all" :: sign :: messages: _*)(repo) >>
67+
val messages = message.paragraphs.foldMap(m => List("-m", m))
68+
val trailers = message.trailers.foldMap { case (k, v) => List("--trailer", s"$k=$v") }
69+
git_("commit" :: "--all" :: sign :: messages ++ trailers: _*)(repo) >>
6970
latestSha1(repo, Branch.head).map(Commit.apply)
7071
}
7172

modules/core/src/test/scala/org/scalasteward/core/edit/scalafix/ScalafixMigrationTest.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package org.scalasteward.core.edit.scalafix
33
import io.circe.config.parser
44
import munit.FunSuite
55
import org.scalasteward.core.edit.scalafix.ScalafixMigration.{ExecutionOrder, Target}
6-
import org.scalasteward.core.util.Nel
6+
import org.scalasteward.core.git.{Author, CommitMsg}
77

88
class ScalafixMigrationTest extends FunSuite {
99
test("commitMessage") {
@@ -18,11 +18,13 @@ class ScalafixMigrationTest extends FunSuite {
1818
| authors: ["Jane Doe <[email protected]>"]
1919
|}""".stripMargin
2020
)
21-
val obtained = migration.map(_.commitMessage(Right(())).toNel)
22-
val expected = Nel.of(
23-
"Applied Scalafix rule(s) github:typelevel/cats/Cats_v2_2_0?sha=v2.2.0",
24-
"See https://github.com/typelevel/cats/blob/v2.2.0/scalafix/README.md#migration-to-cats-v220 for details",
25-
"Co-authored-by: Jane Doe <[email protected]>"
21+
val obtained = migration.map(_.commitMessage(Right(())))
22+
val expected = CommitMsg(
23+
title = "Applied Scalafix rule(s) github:typelevel/cats/Cats_v2_2_0?sha=v2.2.0",
24+
body = List(
25+
"See https://github.com/typelevel/cats/blob/v2.2.0/scalafix/README.md#migration-to-cats-v220 for details"
26+
),
27+
coAuthoredBy = List(Author("Jane Doe", "[email protected]"))
2628
)
2729
assertEquals(obtained, Right(expected))
2830
}

modules/core/src/test/scala/org/scalasteward/core/git/FileGitAlgTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ class FileGitAlgTest extends CatsEffectSuite {
108108
c1 <- ioGitAlg.containsChanges(repo)
109109
_ <- ioFileAlg.writeFile(repo / "test.txt", "hello world")
110110
c2 <- ioGitAlg.containsChanges(repo)
111-
_ <- ioGitAlg.commitAllIfDirty(repo, CommitMsg("Modify test.txt"))
111+
m2 = CommitMsg("Modify test.txt", coAuthoredBy = List(Author("name", "email")))
112+
_ <- ioGitAlg.commitAllIfDirty(repo, m2)
112113
c3 <- ioGitAlg.containsChanges(repo)
113114
_ = assertEquals((c1, c2, c3), (false, true, false))
114115
} yield ()

0 commit comments

Comments
 (0)