-
Notifications
You must be signed in to change notification settings - Fork 103
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
Automatic worktree detection #243
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this intentionally There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likely not an oversight, but apparently this was made in an attempt to work with worktree as well: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't even know what a worktree is, so I'm like 🤷 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah fair enough. They are pretty neat. Essentially they allow you to checkout a branch to a different directory without having to have a completely separate clone. So it is almost always going to be at the local root project base directory (or higher), just like a normal |
||
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 = """([^\/]+)""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice if we could check if jgit v7 is used and if so make
useConsoleForROGit
false, no matter if we are in a linked repo or not (because jgit 7 supports worktrees, read). Like:I looked into jgit but they do not publish the version in a variable nor method... So I don't see a way of doing this easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Maven Central we can find the latest jgit v6 and first v7 version published. The diff is quite big, most of the things we don't need to look at.
So I provide my own html rendered diff here from this command:
You can see in v7 the class
org.eclipse.jgit.lib.Constants
added theGITDIR_FILE
constant which is directly related to git worktree.Would it be able to make use of reflection and check if the
Contants
class contains the fieldGITDIR_FILE
and if so, we can treat the jgit version on the classpath as one that supports git worktree?Like:
and in the
jGitWithWorktreeSupportDetected
method put the reflection code?What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stepping back a bit, instead of making the automatic detection more complicated, would it make sense to make this more of a machine-wide setting that someone could always opt out of JGit (or opt into JGit)? Also in general, I don't really get the motivation behind JGit. Is the assumption that someone could be working on an opensource project but somehow do not have
git
installed on their PATH?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also sbt-pgp. In sbt/sbt-pgp#146, I defaulted to using command line
gpg
and I think the plugin became significantly more reliable because it relied less on the never-ending emulation of gpg via Bouncy Castle.