Skip to content

✨ KeepGitDir should be set to true everywhere by default as it's often needed and can cause cryptic errors #7777

Closed

Description

@jpadams

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, KeepGitDir is set to false by 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 true everywhere since I find myself needing the .git dir 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()

Used in this function:

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.

Used in this function:

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

https://github.com/search?q=repo%3Adagger%2Fdagger%20KeepGitDir&type=code

Activity

  1. added this to the v0.12.0 milestone on Jun 27, 2024
  2. jpadams commented on Jun 30, 2024

    @jpadams
    ContributorAuthor

    Example of such an error I just hit. Glad to have Traces :)
    image

  3. jedevc commented on Jul 2, 2024

    @jedevc
    Contributor

    There is a small issue with applying this change - we can't change KeepGitDir's default to true.

    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 between dagger.GitOpts{KeepGitDir: false} and dagger.GitOpts{}.


    One easy workaround would be to create a new option - DiscardGitDir, and the default is now false.

    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.

  4. helderco commented on Jul 2, 2024

    @helderco
    Contributor

    Yes, the problem is in the Go SDK (client). I actually tried hacking that codegen to allow flipping the default on skipEntrypoint in WithExec, but the problem is you have to turn that into a pointer, so you can't just use SkipEntrypoint: false anymore. I do have an idea that just popped in my mind, I'll have to explore it.

  5. helderco commented on Jul 2, 2024

    @helderco
    Contributor

    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:

    dag.Git("https://github.com/dagger/dagger", GitOpts().KeepGitDir(false)).Branch("main").Tree()

    This way handling the pointers can be hidden from the user so it's easier to use.

  6. helderco commented on Jul 3, 2024

    @helderco
    Contributor

    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 prefix No so that GitOpt{NoKeepGitDir: true} would actually send keepGitDir: false to 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.

  7. modified the milestones: v0.12.0, v0.13.0 on Jul 8, 2024
  8. jedevc commented on Jul 8, 2024

    @jedevc
    Contributor

    Moving this out of the milestone - and into the next release where we can make breaking changes.

  9. jpadams commented on Jul 8, 2024

    @jpadams
    ContributorAuthor

    I’d vote to keep things consistent across SDKs @helderco

    Are there important cache implications? @sipsma

  10. sipsma commented on Jul 8, 2024

    @sipsma
    Contributor

    Are there important cache implications?

    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.)

    1. Worth double-checking we still see this. So much has changed that it's not guaranteed to still be true.
      • Also, in the case where it is still true, worth checking if there's easy tweaks to git options to reduce/eliminate it
    2. This isn't a deal-breaker even if true provided the option to disable KeepGitDir still exists, just a matter of deciding on the tradeoffs between "max cacheability by default" and "max usability by default".
      • At first glance, I think usability should typically win here provided the cache invalidations aren't utterly devastating, which I kind of doubt they are.
  11. jedevc commented on Sep 4, 2024

    @jedevc
    Contributor

    Worth double-checking we still see this. So much has changed that it's not guaranteed to still be true.

    This still seems to be true - simple script to confirm:

    $ cd `mktemp -d`
    $ git clone [email protected]:dagger/dagger.git dagger1
    $ git clone [email protected]:dagger/dagger.git dagger2
    $ find dagger1 -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum
    958518649d87ad395d81a02b3bb47728a3e6e1e3  -
    $ find dagger2 -type f -print0 | sort -z | xargs -0 sha1sum | sha1sum
    3f013769c36d64036d38bea31416d0c179e63eed  -
    $ diff -r dagger dagger2
    Binary files dagger1/.git/index and dagger2/.git/index differ
    diff -r dagger1.git/logs/HEAD dagger2/.git/logs/HEAD
    1c1
    < 0000000000000000000000000000000000000000 778ece45fc121e25ff29735777a98687d940d458 Justin Chadwell <[email protected]> 1725443337 +0100	clone: from github.com:dagger/dagger.git
    ---
    > 0000000000000000000000000000000000000000 778ece45fc121e25ff29735777a98687d940d458 Justin Chadwell <[email protected]> 1725443365 +0100	clone: from github.com:dagger/dagger.git
    diff -r dagger1/.git/logs/refs/heads/main dagger2/.git/logs/refs/heads/main
    1c1
    < 0000000000000000000000000000000000000000 778ece45fc121e25ff29735777a98687d940d458 Justin Chadwell <[email protected]> 1725443337 +0100	clone: from github.com:dagger/dagger.git
    ---
    > 0000000000000000000000000000000000000000 778ece45fc121e25ff29735777a98687d940d458 Justin Chadwell <[email protected]> 1725443365 +0100	clone: from github.com:dagger/dagger.git
    diff -r dagger1/.git/logs/refs/remotes/origin/HEAD dagger2/.git/logs/refs/remotes/origin/HEAD
    1c1
    < 0000000000000000000000000000000000000000 778ece45fc121e25ff29735777a98687d940d458 Justin Chadwell <[email protected]> 1725443337 +0100	clone: from github.com:dagger/dagger.git
    ---
    > 0000000000000000000000000000000000000000 778ece45fc121e25ff29735777a98687d940d458 Justin Chadwell <[email protected]> 1725443365 +0100	clone: from github.com:dagger/dagger.git
    Only in dagger1/.git/objects/pack: pack-6270f89e8c084a22c58caafd81661d5e590a370a.idx
    Only in dagger1/.git/objects/pack: pack-6270f89e8c084a22c58caafd81661d5e590a370a.pack
    Only in dagger1/.git/objects/pack: pack-6270f89e8c084a22c58caafd81661d5e590a370a.rev
    Only in dagger2/.git/objects/pack: pack-c07a8a40b3d8dadc397e37b6f8c7dade89d2acb1.idx
    Only in dagger2/.git/objects/pack: pack-c07a8a40b3d8dadc397e37b6f8c7dade89d2acb1.pack
    Only in dagger2/.git/objects/pack: pack-c07a8a40b3d8dadc397e37b6f8c7dade89d2acb1.rev
    

    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 WithExec consuming 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).

  12. removed this from the v0.13.0 milestone on Sep 10, 2024
  13. jpadams commented on Sep 21, 2024

    @jpadams
    ContributorAuthor

    related to #8520

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions