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

CLI: add --mode diff-ref, deprecate --diff-branch #3346

Merged
merged 1 commit into from
Oct 11, 2022
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
15 changes: 8 additions & 7 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/CliArgParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ object CliArgParser {
.text(
"test for mis-formatted code only, exits with status 1 on first failure."
)
opt[Unit]("diff")
.action((_, c) => c.copy(mode = Option(DiffFiles("master"))))
.text(
s"""Format files listed in `git diff` against master.
|Deprecated: use --mode diff instead""".stripMargin
)
opt[FileFetchMode]("mode")
.action((m, c) => c.copy(mode = Option(m)))
.text(
Expand All @@ -137,10 +131,17 @@ object CliArgParser {
|${FileFetchMode.help}
|""".stripMargin
)
opt[Unit]("diff")
.action((_, c) => c.copy(mode = Option(DiffFiles("master"))))
.text(
s"""Format files listed in `git diff` against master.
|Deprecated: use --mode diff instead""".stripMargin
)
opt[String]("diff-branch")
.action((branch, c) => c.copy(mode = Option(DiffFiles(branch))))
.text(
"If set, only format edited files in git diff against provided branch. Has no effect if mode set to `changed`."
s"""Format files listed in `git diff` against given git ref.
|Deprecated: use --mode diff-ref=<ref> instead""".stripMargin
)
opt[Unit]("build-info")
.action((_, _) => { println(buildInfo); sys.exit })
Expand Down
22 changes: 15 additions & 7 deletions scalafmt-cli/src/main/scala/org/scalafmt/cli/FileFetchMode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,31 @@ sealed trait FileFetchMode {

object FileFetchMode {

private val available: Map[String, FileFetchMode] = Map(
private val diffRefPrefix = "diff-ref="
private val availableModes = Seq(
"diff" -> DiffFiles("master"),
"changed" -> ChangedFiles,
"any" -> RecursiveSearch,
"anygit" -> GitFiles
)
private val availableModesMap: Map[String, FileFetchMode] =
availableModes.toMap

def help = available
.map { case (k, v) => s"$k: ${v.desc}" }
.mkString(" ", "\n ", "")
def help: String =
(("diff-ref=xxx", DiffFiles("xxx")) +: availableModes)
.map { case (k, v) => s"$k: ${v.desc}" }
.mkString(" ", "\n ", "")

/** The read instance is practically is not exhaustive due to the
* RecursiveSearch and GitFiles are the fallback used in the absence of other
* options
*/
implicit val read: Read[FileFetchMode] = Read.reads { x =>
available
.getOrElse(x, throw new IllegalArgumentException(s"unknown mode: $x"))
if (x.startsWith(diffRefPrefix))
DiffFiles(x.substring(diffRefPrefix.length).trim)
else
availableModesMap
.getOrElse(x, throw new IllegalArgumentException(s"unknown mode: $x"))
}

}
Expand All @@ -49,7 +56,8 @@ case object GitFiles extends FileFetchMode {
* When this is set, files passed via the cli are ignored.
*/
final case class DiffFiles(branch: String) extends FileFetchMode {
def desc: String = s"format files listed in `git diff` against $branch"
def desc: String =
s"format files listed in `git diff` against gitref `$branch`"
}

/** A call to `git status --porcelain`
Expand Down