diff --git a/src/main/scala/com/github/sbt/git/GitPlugin.scala b/src/main/scala/com/github/sbt/git/GitPlugin.scala index 690aff2..a8043ce 100644 --- a/src/main/scala/com/github/sbt/git/GitPlugin.scala +++ b/src/main/scala/com/github/sbt/git/GitPlugin.scala @@ -112,8 +112,8 @@ object SbtGit { // Build settings. import GitKeys._ def buildSettings = Seq( - useConsoleForROGit := false, - gitReader := new DefaultReadableGit(baseDirectory.value, if (useConsoleForROGit.value) Some(new ConsoleGitReadableOnly(ConsoleGitRunner, file("."), sLog.value)) else None), + useConsoleForROGit := isGitLinkedRepo(baseDirectory.value), + gitReader := new DefaultReadableGit(baseDirectory.value, if (useConsoleForROGit.value) Some(new ConsoleGitReadableOnly(ConsoleGitRunner, baseDirectory.value, sLog.value)) else None), gitRunner := ConsoleGitRunner, gitHeadCommit := gitReader.value.withGit(_.headCommitSha), gitHeadMessage := gitReader.value.withGit(_.headCommitMessage), @@ -126,6 +126,21 @@ object SbtGit { ThisBuild / gitUncommittedChanges := gitReader.value.withGit(_.hasUncommittedChanges), scmInfo := parseScmInfo(gitReader.value.withGit(_.remoteOrigin)) ) + + private def isGitLinkedRepo(dir: File): Boolean = + isGitWorktreeDir(Option(System.getenv("GIT_DIR")).fold(dir)(file)) + + @scala.annotation.tailrec + private def isGitWorktreeDir(dir: File): Boolean = { + val maybeGit = dir / ".git" + // In a linked worktree, .git is a file that contains the path to the main worktree. + if (maybeGit.exists()) maybeGit.isFile + else Option(dir.getParentFile) match { + case Some(parent) => isGitWorktreeDir(parent) + case None => false + } + } + private[sbt] def parseScmInfo(remoteOrigin: String): Option[ScmInfo] = { val user = """(?:[^@\/]+@)?""" val domain = """([^\/]+)""" diff --git a/src/sbt-test/git-versioning/worktree/.project/project/plugins.sbt b/src/sbt-test/git-versioning/worktree/.project/project/plugins.sbt new file mode 100644 index 0000000..3417043 --- /dev/null +++ b/src/sbt-test/git-versioning/worktree/.project/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.github.sbt" % "sbt-git" % sys.props("project.version")) \ No newline at end of file diff --git a/src/sbt-test/git-versioning/worktree/init.sh b/src/sbt-test/git-versioning/worktree/init.sh new file mode 100755 index 0000000..5af95d5 --- /dev/null +++ b/src/sbt-test/git-versioning/worktree/init.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +set -eux + +mkdir -p main + +cd main +git init +git commit --allow-empty -m "Initial commit" +git worktree add ../project +cd - + +cp -r .project/project project diff --git a/src/sbt-test/git-versioning/worktree/test b/src/sbt-test/git-versioning/worktree/test new file mode 100644 index 0000000..5d71fd5 --- /dev/null +++ b/src/sbt-test/git-versioning/worktree/test @@ -0,0 +1,3 @@ +$ exec ./init.sh +> reload plugins +> show gitHeadMessage \ No newline at end of file