Skip to content

feat: lift git logic out of buildkit into dagop - #9980

Merged
jedevc merged 11 commits into
dagger:mainfrom
jedevc:refactor-sources-dagop
Apr 23, 2025
Merged

jedevc merged 11 commits into
dagger:mainfrom
jedevc:refactor-sources-dagop

Conversation

@jedevc

@jedevc jedevc commented Mar 26, 2025 •

Copy link
Copy Markdown
Contributor

Fixes:

This PR lifts the implementation for dag.Git entirely out of buildkit, and into dagq; (using DagOp #9395). While in the area, I also modernized and refactored a lot of the cruft out - there's a lot of logic that handles ancient backwards compatibility case that we really just don't need to worry about. Potentially I've missed something that is actually required, but I'd prefer to accidentally remove it (and add it back later), than to keep holding on to legacy code that we don't understand the purpose of.

Finally, we update the API with a couple small low-hanging features:

  • GitRef.ref to get the fully resolved ref, like refs/tags/xyz. This is useful so you can actually see what you're cloning - and we needed to use it internally, so it was easy to expose.
  • GitRef.tree now takes a depth arg, which is very easy to add since we now own the entire backend.

I've also forked gitutil out of buildkit, we needed quite a few changes for this to work nicely in dagger. Some of these maybe could still be upstreamed at some point.

@jedevc
jedevc force-pushed the refactor-sources-dagop branch from da9b927 to eec4125 Compare March 31, 2025 09:57
@jedevc
jedevc force-pushed the refactor-sources-dagop branch 15 times, most recently from e0f1c1c to a85e630 Compare April 11, 2025 13:51
Comment thread core/git.go
Comment on lines +216 to +219
if repo.URL.Host == "bitbucket.org" {
// NOTE: bitbucket.org is picky, and needs *this* username
username = "x-token-auth"
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @grouville, this is a simplification of part of #8778 - it turns out, we don't need the whole credential helper here?

Anyways, just wanted to check that this bit seemed good to you, all our tests seem to pass correctly 🎉

Comment thread core/git.go
Comment on lines +647 to +689
// TODO: this feels completely out-of-sync from how we do the rest
// of the clone - caching will not be as great here
_, err = checkoutGit.Run(ctx, "submodule", "update", "--init", "--recursive", "--depth=1")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to the reviewer: the TODOs in this PR are mostly intentional, they don't need to be done here, since they're not a regression from the existing behavior.

However, just marking them for future improvement.

Comment thread core/schema/git.go Outdated
Comment on lines +415 to +421
// also include what auth methods are used, currently we can't
// handle a cache hit where the result has a different auth
// method than the caller used (i.e. a git repo is pulled w/
// a token but hits cache for a dir where a ssh sock was used)
strconv.FormatBool(ref.Repo.AuthToken.Self != nil),
strconv.FormatBool(ref.Repo.AuthHeader.Self != nil),
strconv.FormatBool(ref.Repo.SSHAuthSocket != nil),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sipsma so I think this is right.

Instead of having this be on tree, I've moved it up to the ref selection - so it'll apply to every operation on a GitRef.

The reasoning being, we should have a content hash here, which means we should easily get the following behavior:

  • Cache the results of dag().git().ref() per session (which effectively means we only resolve each remote + ref combo once per session, similar to buildkit today).
  • And then when it's not cached between sessions, get a "content hashed" call, that includes the ref + commit + remote + etc. So we don't need to invoke the actual op multiple times when this is shared.

But would really love a bit of a closer inspection, just to make sure I'm definitely thinking about this right.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this moved back to be in tree IIUC, curious if something came up that motivated the move back (or if I'm not following something)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh yeah, I moved this back 🤔 I'm now slightly struggling to remember exactly why.

I think it was related to accidentally edge merging way earlier than we should be 🤔 but yes, ended up deciding to lift it up for now, just to entirely avoid that problem.

Comment thread dagql/cachekey.go

// CachePerSession is a CacheKeyFunc that scopes the cache key to the session by mixing in the session ID to the original digest of the operation.
// It should be used when the operation should be run for each session, but not more than once for a given session.
func CachePerSession[P Typed, A any](

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just thought this would be worth having? It wasn't useful until #9682, but with this, there should be a way to scope cache to just a session (so if two parallel sessions are running, that we won't share results).

Then this'll become more useful when we have true global function caching.

@jedevc jedevc changed the title chore: lift git logic out of buildkit into dagop feat: lift git logic out of buildkit into dagop Apr 11, 2025
Comment thread util/gitutil/cli.go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to start carving out a util directory - happy to reconsider and put it somewhere else, but IMO, it's just not clear what component should own this.

@jedevc
jedevc force-pushed the refactor-sources-dagop branch from a85e630 to b55b0fc Compare April 11, 2025 14:16
Comment thread core/git.go Outdated
Comment on lines +516 to +524
svcs, err := ref.Repo.Query.Services(ctx)
if err != nil {
return fmt.Errorf("failed to get services: %w", err)
}
detach, _, err := svcs.StartBindings(ctx, ref.Repo.Services)
if err != nil {
return err
}
defer detach()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, pretty happy with this - cc @vito

This lets us only spin up services when we're doing the operations that need them - if we don't do a git fetch, then we never need to access them.

@jedevc
jedevc requested review from sipsma and vito April 11, 2025 14:18
@jedevc
jedevc marked this pull request as ready for review April 11, 2025 14:18
@jedevc
jedevc requested a review from a team as a code owner April 11, 2025 14:18
@jedevc
jedevc force-pushed the refactor-sources-dagop branch 2 times, most recently from 8b3441c to a40b064 Compare April 22, 2025 11:06
@jedevc
jedevc requested a review from sipsma April 22, 2025 11:13
@jedevc
jedevc force-pushed the refactor-sources-dagop branch from a40b064 to fe49377 Compare April 22, 2025 11:31
jedevc added 11 commits April 23, 2025 10:37
Add some long-awaited enhancements to the CLI helper:
- Integrate each `git` command with dagger telemetry for improved tracing
- Skip all the weird retry logic, this isn't really needed (and if it
  is, we should implement it *way* better)
- Add dagger-specific `GIT_ASKPASS=echo`

And improve the URL parsing:
- Don't store the `Remote` directly, but instead compute it on the fly:
  this allows for modifying properties, and getting the result out.

Signed-off-by: Justin Chadwell <[email protected]>
Adds several features:
- Allow specifying the depth to get a `GitRef.tree` at (defaults to 1)
- Get the fully resolved ref string for a `GitRef`

Signed-off-by: Justin Chadwell <[email protected]>
There was technically only *one* way to use this left - if you created a
container, bound a service, and then built a dockerfile in that
container.

However, there are no tests for this. We've discussed deprecating
`Container.build` for `Directory.dockerBuild` which has no such
functionality. So I think it should be safe to remove, and assume that
no one was using this.

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
- Preserves shallow cloning behaviour
- Ensure correct caching behavior for checkouts (we were accidentally
  early-merging refs in some cases)

Signed-off-by: Justin Chadwell <[email protected]>
This is automatically added by all clients on connection - sipsma
correctly pointed out we don't need this duplication.

Signed-off-by: Justin Chadwell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐞 Unexpected behavior when referencing a git fork as a Directory arg

3 participants