Skip to content

Commit

Permalink
Feedback from PR, fix more Scala3 syntax hints
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyAutrey committed Oct 8, 2024
1 parent 3de9c10 commit f13720e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
25 changes: 15 additions & 10 deletions src/main/scala/com/github/sbt/git/ConsoleGitRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@ package com.github.sbt.git
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(Terminal.console.isAnsiSupported) 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.toString + "$ " + full.mkString(" "))
val code = Process(full, cwd, colorSupport :_*) ! gitLogger
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 @@ -38,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
20 changes: 8 additions & 12 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,12 +30,10 @@ final class JGit(val repo: Repository) extends GitReadonlyInterface {
def branch: String = repo.getBranch

private def branchesRef: Seq[Ref] = {
import scala.jdk.CollectionConverters._
porcelain.branchList.call.asScala.toSeq
}

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

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

def currentTags: Seq[String] = {
import scala.jdk.CollectionConverters._
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 scala.jdk.CollectionConverters._
import org.eclipse.jgit.api.ListBranchCommand.ListMode
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 scala.jdk.CollectionConverters._
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

0 comments on commit f13720e

Please sign in to comment.