Skip to content

Commit

Permalink
Merge pull request #194 from raboof/fix-invalid-.git-capture
Browse files Browse the repository at this point in the history
Fix invalid .git capture
  • Loading branch information
raboof authored Oct 7, 2021
2 parents 457d8e7 + c5c539d commit c392e14
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
3 changes: 2 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ git.baseVersion := "1.0"

libraryDependencies ++= Seq(
"org.eclipse.jgit" % "org.eclipse.jgit" % "5.11.0.202103091610-r",
"com.michaelpollmeier" % "versionsort" % "1.0.0"
"com.michaelpollmeier" % "versionsort" % "1.0.0",
"org.scalameta" %% "munit" % "0.7.29" % Test
)

scriptedLaunchOpts += s"-Dproject.version=${version.value}"
44 changes: 23 additions & 21 deletions src/main/scala/com/typesafe/sbt/SbtGit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,30 +121,32 @@ object SbtGit {
gitCurrentTags := gitReader.value.withGit(_.currentTags),
gitCurrentBranch := Option(gitReader.value.withGit(_.branch)).getOrElse(""),
gitUncommittedChanges in ThisBuild := gitReader.value.withGit(_.hasUncommittedChanges),
scmInfo := {
val user = """(?:[^@\/]+@)?"""
val domain = """([^\/]+)"""
val gitPath = """(.*)(?:\.git)?\/?"""
val unauthenticated = raw"""(?:git|https?|ftps?)\:\/\/$domain\/$gitPath""".r
val ssh = raw"""ssh\:\/\/$user$domain\/$gitPath""".r
val headlessSSH = raw"""$user$domain:$gitPath""".r

def buildScmInfo(domain: String, repo: String) = Option(
ScmInfo(
url(s"https://$domain/$repo"),
s"scm:git:https://$domain/$repo.git",
Some(s"scm:git:git@$domain:$repo.git")
)
scmInfo := parseScmInfo(gitReader.value.withGit(_.remoteOrigin))
)
private[sbt] def parseScmInfo(remoteOrigin: String): Option[ScmInfo] = {
val user = """(?:[^@\/]+@)?"""
val domain = """([^\/]+)"""
val gitPath = """(.*?)(?:\.git)?\/?$"""
val unauthenticated = raw"""(?:git|https?|ftps?)\:\/\/$domain\/$gitPath""".r
val ssh = raw"""ssh\:\/\/$user$domain\/$gitPath""".r
val headlessSSH = raw"""$user$domain:$gitPath""".r

def buildScmInfo(domain: String, repo: String): Option[ScmInfo] = Option(
ScmInfo(
url(s"https://$domain/$repo"),
s"scm:git:https://$domain/$repo.git",
Some(s"scm:git:git@$domain:$repo.git")
)
)

gitReader.value.withGit(_.remoteOrigin) match {
case unauthenticated(domain, repo) => buildScmInfo(domain,repo)
case ssh(domain, repo) => buildScmInfo(domain,repo)
case headlessSSH(domain, repo) => buildScmInfo(domain,repo)
case _ => None
}
remoteOrigin match {
case unauthenticated(domain, repo) => buildScmInfo(domain,repo)
case ssh(domain, repo) => buildScmInfo(domain,repo)
case headlessSSH(domain, repo) => buildScmInfo(domain,repo)
case _ => None
}
)
}

val projectSettings = Seq(
// Input task to run git commands directly.
commands += GitCommand.command
Expand Down
25 changes: 25 additions & 0 deletions src/test/scala/com/typesafe/sbt/SbtGitSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.typesafe.sbt

import sbt.ScmInfo
import sbt.url

class SbtGitSuite extends munit.FunSuite {
val expectedScmInfo = Some(ScmInfo(
browseUrl = url("https://github.com/akka/akka"),
connection = "scm:git:https://github.com/akka/akka.git",
devConnection = Some("scm:git:git@github.com:akka/akka.git")
))

test("a git URL with the .git postfix") {
assertEquals(SbtGit.parseScmInfo("git@github.com:akka/akka.git"), expectedScmInfo)
}
test("a git URL without the .git postfix") {
assertEquals(SbtGit.parseScmInfo("git@github.com:akka/akka"), expectedScmInfo)
}
test("a https URL with the .git postfix") {
assertEquals(SbtGit.parseScmInfo("https://github.com/akka/akka.git"), expectedScmInfo)
}
test("a https URL without the .git postfix") {
assertEquals(SbtGit.parseScmInfo("https://github.com/akka/akka"), expectedScmInfo)
}
}

0 comments on commit c392e14

Please sign in to comment.