Repository navigation
Closed
Labels
Description
NOTE: Likely this would be a good candidate for v0.12.0 as it's a possible 💥 breaking change for some users.
The problem
If KeepGitDir is true, the .git directory is preserved when a git repo is used as a Dagger Directory. If it's false, then the .git directory is omitted.
Today, there is an inconsistency in the default for KeepGitDir:
- In the Dagger API,
KeepGitDiris set tofalseby default. - When a git repo ref is provided as an argument of type Directory in the dagger CLI, then it behaves as ifKeepGitDir is set to true by default.
The proposal
- I'd like to see the default to be
trueeverywhere since I find myself needing the.gitdir often, haven't had any issues with it being there by default, and have had cryptic errors when it was missing but required subsequent steps. In any case, the split between CLI and API should be made consistent.
Examples and additional context
In the Dagger API, KeepGitDir is set to false by default.
dag.git("https://github.com/dagger/dagger").branch("main").tree() // keepGitDir false by default
// dag.git("https://github.com/dagger/dagger", {keepGitDir: true}).branch("main").tree()dagger call -m github.com/jpadams/testgit \
git-ls --repoStr https://github.com/dagger/dagger stdout
total 336K
drwxr-xr-x 19 root root 4.0K Jun 27 19:08 .
drwxr-xr-x 1 root root 4.0K Jun 27 19:16 ..
drwxr-xr-x 3 root root 4.0K Jun 27 19:08 .changes
-rw-r--r-- 2 root root 962 Jun 27 19:08 .changie.yaml
drwxr-xr-x 4 root root 4.0K Jun 27 19:08 .github
-rw-r--r-- 2 root root 561 Jun 27 19:08 .gitignore
-rw-r--r-- 2 root root 157 Jun 27 19:08 .gitmodules
-rw-r--r-- 2 root root 1.5K Jun 27 19:08 .golangci.yml
...
When a git repo ref is provided as an argument of type Directory in the dagger CLI, then it behaves as ifKeepGitDir is set to true by default.
dagger call -m github.com/jpadams/testgit \
git-ls --repoDir https://github.com/dagger/dagger\#main stdout
total 340K
drwxr-xr-x 20 root root 4.0K Jun 27 19:19 .
drwxr-xr-x 1 root root 4.0K Jun 27 19:20 ..
drwxr-xr-x 3 root root 4.0K Jun 27 19:19 .changes
-rw-r--r-- 2 root root 962 Jun 27 19:19 .changie.yaml
drwxr-xr-x 9 root root 4.0K Jun 27 19:19 .git
drwxr-xr-x 4 root root 4.0K Jun 27 19:19 .github
-rw-r--r-- 2 root root 561 Jun 27 19:19 .gitignore
-rw-r--r-- 2 root root 157 Jun 27 19:19 .gitmodules
-rw-r--r-- 2 root root 1.5K Jun 27 19:19 .golangci.yml
...
What if I don't like this default true behavior?
If users NEED the .git dir to be removed, they can remove it via the API with either
dag.git("https://github.com/dagger/dagger", {keepGitDir: false}).branch("main").tree()or
myTree = myTree.withoutDirectory(".git")Code references
Reacted by Robert Underwood and Ali AKCA
Activity
jpadams commentedon Jun 30, 2024
Example of such an error I just hit. Glad to have Traces :)

jedevc commentedon Jul 2, 2024
There is a small issue with applying this change - we can't change
KeepGitDir's default totrue.The problem is that Go can't see the difference between a struct field explicitly set to
false, and one that's unset. So we can't distinguish betweendagger.GitOpts{KeepGitDir: false}anddagger.GitOpts{}.One easy workaround would be to create a new option -
DiscardGitDir, and the default is nowfalse.Or maybe, we could apply a special case in the Go SDK, and invert the condition there, to something like
NoKeepGitDir, since I think this is a Go-specific issue?@helderco am I missing another option that might be neater? Any preferred way forwards? I think I'm leaning towards the go-specific fix, since this is just a limitation of go.
helderco commentedon Jul 2, 2024
Yes, the problem is in the Go SDK (client). I actually tried hacking that codegen to allow flipping the default on
skipEntrypointinWithExec, but the problem is you have to turn that into a pointer, so you can't just useSkipEntrypoint: falseanymore.I do have an idea that just popped in my mind, I'll have to explore it.helderco commentedon Jul 2, 2024
Any solution I can think of requires changing the DX for calling optional arguments, like using pointers or the functional approach. If I were to change it I'd choose the builder pattern instead:
This way handling the pointers can be hidden from the user so it's easier to use.
helderco commentedon Jul 3, 2024
Just to be clear, what we've been doing so far in this sort of situation is to create a new option in the API where the name suggests the non-empty value (like
DiscardGitDir), and deprecate the current value (KeepGitDir).The difference with what @jedevc is suggesting is that rather than doing it in the API, we just patch the Go SDK. I could support that, but only if it applies to all options in the same condition. For example, we can check if the default for the boolean option is different from the empty value (i.e.,
true), and prefixNoso thatGitOpt{NoKeepGitDir: true}would actually sendkeepGitDir: falseto the API.Since the name of the option changes though, it's a breaking change anyway, while in the other SDKs it isn't.
The best fix for the future would be to change to the builder pattern like I said, but that's a very big change to ask users to make.
jedevc commentedon Jul 8, 2024
Moving this out of the milestone - and into the next release where we can make breaking changes.
jpadams commentedon Jul 8, 2024
I’d vote to keep things consistent across SDKs @helderco
Are there important cache implications? @sipsma
sipsma commentedon Jul 8, 2024
This came up a long time ago and at the time it did indeed seem like it created more cache invalidations since the .git dir is not always identical even when the actual files in the repo itself are identical. This doesn't matter for pulling the git repo itself, but can matter for operations done with the directory created by the git op (copy, mount+exec, etc.)
KeepGitDirstill exists, just a matter of deciding on the tradeoffs between "max cacheability by default" and "max usability by default".jedevc commentedon Sep 4, 2024
This still seems to be true - simple script to confirm:
So, yup. Some of these seem like differences in timestamps from
.git/logs(potentially fixable), the others seem like genuine differences in the pack files (I have no idea how to fix this).However! For local use at least this shouldn't hugely impact cache usage. The slow cache key (content hash of the directory) is different, but the fast cache key is the same (it's the commit ID). So if we have a
WithExecconsuming the directory, then as long as the commit ID stays the same, we should continue to allow caching just as before (even though technically the directory contents are different).jpadams commentedon Sep 21, 2024
related to #8520