Skip to content

Commit

Permalink
Merge pull request #256 from BillyAutrey/fix-sbt-deprecation-warnings
Browse files Browse the repository at this point in the history
Add support for Scala 3 crossbuilds, raise min version to 1.5.8
  • Loading branch information
eed3si9n authored Oct 8, 2024
2 parents f01ae86 + f13720e commit 8fe8f36
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Build and test
run: |
gpg --import test-key.gpg
sbt -v clean ^test ^scripted
sbt -v clean test scripted
rm -rf "$HOME/.ivy2/local" || true
find $HOME/Library/Caches/Coursier/v1 -name "ivydata-*.properties" -delete || true
find $HOME/.ivy2/cache -name "ivydata-*.properties" -delete || true
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
*~
target/
.idea/
.bsp/
/bin/
/.settings/
/.cache
/.classpath
/.project
metals.sbt
12 changes: 11 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ startYear := Some(2011)
homepage := scmInfo.value map (_.browseUrl)
scmInfo := Some(ScmInfo(url("https://github.com/sbt/sbt-git"), "scm:git:[email protected]:sbt/sbt-git.git"))

crossSbtVersions := List("1.3.13")
lazy val scala212 = "2.12.20"
lazy val scala3 = "3.3.4"

crossScalaVersions := Seq(scala212, scala3)

enablePlugins(GitVersioning, SbtPlugin)
git.baseVersion := "1.0"
Expand All @@ -19,4 +22,11 @@ libraryDependencies ++= Seq(
"org.scalameta" %% "munit" % "1.0.2" % Test
)

(pluginCrossBuild / sbtVersion) := {
scalaBinaryVersion.value match {
case "2.12" => "1.5.8"
case _ => "2.0.0-M2"
}
}

scriptedLaunchOpts += s"-Dproject.version=${version.value}"
34 changes: 20 additions & 14 deletions src/main/scala/com/github/sbt/git/ConsoleGitRunner.scala
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
package com.github.sbt.git

import sbt._
import Keys._
import sys.process.{ Process, ProcessLogger }
import sbt.*
import sbt.internal.util.Terminal

import scala.util.Try
import sys.process.{Process, ProcessLogger}

/** A mechanism of running git that simply shells out to the console. */
object ConsoleGitRunner extends GitRunner {
// TODO - Something less lame here.
def isWindowsShell = {
val ostype = System.getenv("OSTYPE")
val isCygwin = ostype != null && ostype.toLowerCase.contains("cygwin")
val isWindows = System.getProperty("os.name", "").toLowerCase.contains("windows")
isWindows && !isCygwin
}
def isWindowsShell: Boolean = {
val ostype = System.getenv("OSTYPE")
val isCygwin = ostype != null && ostype.toLowerCase.contains("cygwin")
val isWindows = System.getProperty("os.name", "").toLowerCase.contains("windows")
isWindows && !isCygwin
}
private lazy val cmd = if(isWindowsShell) Seq("cmd", "/c", "git") else Seq("git")

// in order to enable colors we trick git into thinking we're a pager, because it already knows we're not a tty
val colorSupport: Seq[(String, String)] =
if(ConsoleLogger.formatEnabled) Seq("GIT_PAGER_IN_USE" -> "1")
else Seq.empty
Try{
if(Terminal.console.isAnsiSupported)
Seq("GIT_PAGER_IN_USE" -> "1")
else
Seq.empty
}.getOrElse(Seq.empty)

override def apply(args: String*)(cwd: File, log: Logger = ConsoleLogger()): String = {
val gitLogger = new GitLogger(log)
IO.createDirectory(cwd)
val full = cmd ++ args
log.debug(cwd + "$ " + full.mkString(" "))
val code = Process(full, cwd, colorSupport :_*) ! gitLogger
log.debug(cwd.toString + "$ " + full.mkString(" "))
val code = Process(full, cwd, colorSupport *) ! gitLogger
val result = gitLogger.flush(code)
if(code != 0)
throw new MessageOnlyException("Nonzero exit code (" + code + ") running git.")
Expand All @@ -37,7 +43,7 @@ object ConsoleGitRunner extends GitRunner {
// reduce log level for git process
private class GitLogger(log: Logger) extends ProcessLogger {
import scala.collection.mutable.ListBuffer
import Level.{ Debug, Info, Warn, Error, Value => LogLevel }
import Level.{ Debug, Info, Error, Value as LogLevel }

private val msgs: ListBuffer[(LogLevel, String)] = new ListBuffer()

Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/com/github/sbt/git/GitPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.sbt.git

import sbt._
import Keys._
import sys.process.Process

/** This plugin has all the basic 'git' functionality for other plugins. */
object SbtGit {
Expand Down Expand Up @@ -170,7 +169,7 @@ object SbtGit {
def useJGit: Setting[_] = ThisBuild / gitRunner := JGitRunner

/** Setting to use console git for readable ops, to allow working with git worktrees */
def useReadableConsoleGit: Setting[_] = useConsoleForROGit in ThisBuild := true
def useReadableConsoleGit: Setting[_] = ThisBuild / useConsoleForROGit := true

/** Adapts the project prompt to show the current project name *and* the current git branch. */
def showCurrentGitBranch: Setting[_] =
Expand Down
26 changes: 11 additions & 15 deletions src/main/scala/com/github/sbt/git/JGit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.github.sbt.git

import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import org.eclipse.jgit.api.{Git => PGit}
import org.eclipse.jgit.api.Git as PGit
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
Expand All @@ -11,6 +11,7 @@ import org.eclipse.jgit.lib.ObjectId
import org.eclipse.jgit.lib.Ref
import org.eclipse.jgit.revwalk.{RevCommit, RevWalk}

import scala.jdk.CollectionConverters.*
import scala.util.Try


Expand All @@ -29,13 +30,11 @@ final class JGit(val repo: Repository) extends GitReadonlyInterface {
def branch: String = repo.getBranch

private def branchesRef: Seq[Ref] = {
import collection.JavaConverters._
porcelain.branchList.call.asScala
porcelain.branchList.call.asScala.toSeq
}

def tags: Seq[Ref] = {
import collection.JavaConverters._
porcelain.tagList.call().asScala
porcelain.tagList.call().asScala.toSeq
}

def checkoutBranch(branch: String): Unit = {
Expand All @@ -59,7 +58,6 @@ final class JGit(val repo: Repository) extends GitReadonlyInterface {
headCommit map (_.name)

def currentTags: Seq[String] = {
import collection.JavaConverters._
for {
hash <- headCommit.map(_.name).toSeq
unpeeledTag <- tags
Expand All @@ -71,7 +69,7 @@ final class JGit(val repo: Repository) extends GitReadonlyInterface {
}


def tagHash(tag: Ref) = {
def tagHash(tag: Ref): String = {
// Annotated (signed) and plain tags work differently,
// plain ones have the null PeeledObjectId
val peeled = repo.getRefDatabase.peel(tag)
Expand All @@ -88,22 +86,20 @@ final class JGit(val repo: Repository) extends GitReadonlyInterface {
Try(Option(porcelain
.describe()
.setTags(true)
.setMatch(patterns:_*)
.setMatch(patterns *)
.call())).getOrElse(None)

override def hasUncommittedChanges: Boolean = porcelain.status.call.hasUncommittedChanges

override def branches: Seq[String] = branchesRef.filter(_.getName.startsWith("refs/heads")).map(_.getName.drop(11))

override def remoteBranches: Seq[String] = {
import collection.JavaConverters._
import org.eclipse.jgit.api.ListBranchCommand.ListMode
porcelain.branchList.setListMode(ListMode.REMOTE).call.asScala.filter(_.getName.startsWith("refs/remotes")).map(_.getName.drop(13))
porcelain.branchList.setListMode(ListMode.REMOTE).call.asScala.filter(_.getName.startsWith("refs/remotes")).map(_.getName.drop(13)).toSeq
}

override def remoteOrigin: String = {
// same functionality as Process("git ls-remote --get-url origin").lines_!.head
import collection.JavaConverters._
porcelain.remoteList().call.asScala
.filter(_.getName == "origin")
.flatMap(_.getURIs.asScala)
Expand All @@ -130,12 +126,12 @@ final class JGit(val repo: Repository) extends GitReadonlyInterface {
object JGit {

/** Creates a new git instance from a base directory. */
def apply(base: File) =
try (new JGit({
def apply(base: File): JGit =
try new JGit({
new FileRepositoryBuilder().findGitDir(base).build
})) catch {
}) catch {
// This is thrown if we never find the git base directory. In that instance, we'll assume root is the base dir.
case e: IllegalArgumentException =>
case _: IllegalArgumentException =>
val defaultGitDir = new File(base, ".git")
new JGit({ new FileRepositoryBuilder().setGitDir(defaultGitDir).build()})
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/com/github/sbt/git/NullLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import sbt.Level
import sbt.ControlEvent

object NullLogger extends sbt.BasicLogger {
override def control(event: ControlEvent.Value, message: String): Unit = ()
override def log(level: Level.Value, message: String): Unit = ()
override def control(event: ControlEvent.Value, message: => String): Unit = ()
override def log(level: Level.Value, message: => String): Unit = ()
override def logAll(events: Seq[LogEvent]): Unit = ()
override def success(message: String): Unit = ()
override def trace(t: Throwable): Unit = ()
}
override def success(message: => String): Unit = ()
override def trace(t: => Throwable): Unit = ()
}

0 comments on commit 8fe8f36

Please sign in to comment.