-
Notifications
You must be signed in to change notification settings - Fork 356
feat: add sparse checkout support #3903
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
Open
mcncl
wants to merge
3
commits into
main
Choose a base branch
from
SUP-6409/sparse_checkout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+662
−2
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Contributor
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. Given the size of checkout.go and the fact that this code change is quite isolated, I recommend opening a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| package job | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/buildkite/agent/v3/internal/osutil" | ||
| "github.com/buildkite/agent/v3/internal/shell" | ||
| ) | ||
|
|
||
| func cleanGitSparseCheckoutPaths(paths []string) []string { | ||
| cleaned := make([]string, 0, len(paths)) | ||
| for _, path := range paths { | ||
| path = strings.TrimSpace(path) | ||
| if path != "" { | ||
| cleaned = append(cleaned, path) | ||
| } | ||
| } | ||
| return cleaned | ||
| } | ||
|
|
||
| func parseGitVersion(output string) (major, minor int, ok bool) { | ||
| if _, err := fmt.Sscanf(output, "git version %d.%d", &major, &minor); err != nil { | ||
| return 0, 0, false | ||
| } | ||
| return major, minor, true | ||
| } | ||
|
|
||
| func gitVersionAtLeast(ctx context.Context, sh *shell.Shell, major, minor int) (bool, error) { | ||
| output, err := sh.Command("git", "--version").RunAndCaptureStdout(ctx) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| gitMajor, gitMinor, ok := parseGitVersion(strings.TrimSpace(output)) | ||
| if !ok { | ||
| return false, fmt.Errorf("parsing git version from %q", strings.TrimSpace(output)) | ||
| } | ||
|
|
||
| if gitMajor != major { | ||
| return gitMajor > major, nil | ||
| } | ||
| return gitMinor >= minor, nil | ||
| } | ||
|
|
||
| // sparseCheckoutMayBeConfigured does a cheap filesystem check for marker files | ||
| // that indicate sparse checkout (or the worktree-config extension that | ||
| // `sparse-checkout` enables) might already be in effect, so we can avoid | ||
| // shelling out to `git config` on every checkout. It resolves the .git dir | ||
| // directly to handle the worktree/submodule case where .git is a file | ||
| // containing `gitdir: <path>`. | ||
| func sparseCheckoutMayBeConfigured(sh *shell.Shell) bool { | ||
| gitDir := filepath.Join(sh.Getwd(), ".git") | ||
| if data, err := os.ReadFile(gitDir); err == nil && bytes.HasPrefix(data, []byte("gitdir:")) { | ||
| gitDirValue := strings.TrimSpace(string(bytes.TrimPrefix(data, []byte("gitdir:")))) | ||
| if !filepath.IsAbs(gitDirValue) { | ||
| gitDirValue = filepath.Join(sh.Getwd(), gitDirValue) | ||
| } | ||
| gitDir = gitDirValue | ||
| } | ||
|
|
||
| return osutil.FileExists(filepath.Join(gitDir, "info", "sparse-checkout")) || | ||
| osutil.FileExists(filepath.Join(gitDir, "config.worktree")) | ||
| } | ||
|
|
||
| func (e *Executor) disableSparseCheckoutIfConfigured(ctx context.Context) { | ||
| if !sparseCheckoutMayBeConfigured(e.shell) { | ||
| return | ||
| } | ||
|
|
||
| sparseOutput, err := e.shell.Command("git", "config", "--get", "core.sparseCheckout").RunAndCaptureStdout(ctx, shell.ShowStderr(false)) | ||
| if err != nil || strings.TrimSpace(sparseOutput) != "true" { | ||
| return | ||
| } | ||
|
|
||
| e.shell.Commentf("Disabling sparse checkout from previous build") | ||
| if err := e.shell.Command("git", "sparse-checkout", "disable").Run(ctx); err != nil { | ||
| e.shell.Warningf("Failed to disable sparse checkout: %v", err) | ||
| } | ||
|
|
||
| // `sparse-checkout disable` leaves extensions.worktreeConfig set, which | ||
| // can cause problems for subsequent git operations. Only unset it if no | ||
| // other worktree-scoped config remains, to avoid clobbering user config. | ||
| worktreeConfig, err := e.shell.Command("git", "config", "--worktree", "--list").RunAndCaptureStdout(ctx, shell.ShowStderr(false)) | ||
| if err == nil && strings.TrimSpace(worktreeConfig) == "" { | ||
| _ = e.shell.Command("git", "config", "--unset", "extensions.worktreeConfig").Run(ctx) | ||
| } | ||
| } | ||
|
|
||
| // setupSparseCheckout configures (or disables) git sparse checkout for the | ||
| // current working tree. It returns true if sparse checkout was successfully | ||
| // applied for this build, so callers can adjust later behaviour (e.g. skip | ||
| // submodule init, which requires the full tree). | ||
| func (e *Executor) setupSparseCheckout(ctx context.Context) (bool, error) { | ||
| paths := cleanGitSparseCheckoutPaths(e.GitSparseCheckoutPaths) | ||
| if len(paths) == 0 { | ||
| e.disableSparseCheckoutIfConfigured(ctx) | ||
| return false, nil | ||
| } | ||
|
|
||
| ok, err := gitVersionAtLeast(ctx, e.shell, 2, 26) | ||
| if err != nil { | ||
| e.shell.Warningf("Sparse checkout requires git >= 2.26; falling back to full checkout (%v)", err) | ||
| e.disableSparseCheckoutIfConfigured(ctx) | ||
| return false, nil | ||
| } | ||
| if !ok { | ||
| e.shell.Warningf("Sparse checkout requires git >= 2.26; falling back to full checkout") | ||
| e.disableSparseCheckoutIfConfigured(ctx) | ||
| return false, nil | ||
| } | ||
|
|
||
| e.shell.Commentf("Setting up sparse checkout for paths: %s", strings.Join(paths, ",")) | ||
| args := append([]string{"sparse-checkout", "set", "--cone"}, paths...) | ||
| if err := e.shell.Command("git", args...).Run(ctx); err != nil { | ||
| return false, fmt.Errorf("setting sparse checkout paths: %w", err) | ||
| } | ||
|
|
||
| return true, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should this
setEnvbe conditional, (ie.if len(r.conf.AgentConfiguration.GitSparseCheckoutPaths) > 0 { setEnv("BUILDKITE_GIT_SPARSE_CHECKOUT_PATHS", strings.Join(r.conf.AgentConfiguration.GitSparseCheckoutPaths, ",")) })?