Repository navigation
fix(workspaceindex): skip git-ignored and build-cache dirs in Scan only #1117
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
FabioLeitao
wants to merge
4
commits into
Twigpine:main
Choose a base branch
from
FabioLeitao:fix/1108-scan-ignored-dirs
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.
+488
−5
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6e57f7f
fix(workspaceindex): skip git-ignored and build-cache dirs in Scan only
FabioLeitao a420af8
fix(workspaceindex): skip a rename's original-path field when parsing…
FabioLeitao 2dc4049
fix(workspaceindex): disable repo fsmonitor and force untracked-files…
FabioLeitao 63566da
Merge upstream main (Go 1.26.9) into fix/1108-scan-ignored-dirs
FabioLeitao 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package workspaceindex | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "os" | ||
| "os/exec" | ||
| "path" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| // gitIgnoredPathsTimeout bounds how long Scan waits on git before giving up and falling | ||
| // back to the non-git behavior. Scan also runs for the per-turn workspace seed, so a slow or | ||
| // degenerate tree (network filesystem, huge monorepo) must not stall a turn. | ||
| const gitIgnoredPathsTimeout = 5 * time.Second | ||
|
|
||
| // gitIgnoredPaths asks git which paths under root it considers ignored, honoring every | ||
| // source git itself resolves (nested .gitignore files, .git/info/exclude, | ||
| // core.excludesFile). Returns a set of paths relative to root: whole ignored directories | ||
| // collapsed to one entry plus individually ignored files, using forward slashes and no | ||
| // trailing slash (the `rel` form Scan computes). | ||
| // | ||
| // Returns (nil, false) when root is not inside a git work tree, git is missing or older | ||
| // than the floor below, or a command fails or times out. Scan then behaves as it does | ||
| // outside git. A scan must never fail or hang because of this lookup. | ||
| // | ||
| // Git version floor: 2.16. `status --ignored=<mode>` landed in 2.16 and `--porcelain=v1` | ||
| // in 2.11; an older git rejects the arguments, which lands on the (nil, false) path. | ||
| // GIT_OPTIONAL_LOCKS (2.15) is an environment variable, so an older git ignores it. | ||
| // | ||
| // Why `status --ignored=matching` and not `ls-files --others --ignored --directory`: | ||
| // ls-files stops descending at the first untracked directory, so an ignored directory or | ||
| // file nested inside one (svc/build-out/, app/cache.db) is never reported. status descends, | ||
| // collapses a fully ignored directory to a single entry, and lists partially ignored files | ||
| // individually. `-uno` is not usable as a speedup: with it, status reports no ignored | ||
| // entries at all. | ||
| func gitIgnoredPaths(root string) (map[string]bool, bool) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), gitIgnoredPathsTimeout) | ||
| defer cancel() | ||
|
|
||
| // status reports paths relative to the repository top level even when run from a | ||
| // subdirectory, while Scan's `rel` is relative to root. show-prefix is root's own | ||
| // location inside the repo ("" at the top level, "svc/" for <repo>/svc), and also the | ||
| // cheapest way to learn whether this is a git work tree at all. | ||
| prefixOut, err := gitCommand(ctx, root, "rev-parse", "--show-prefix").Output() | ||
| if err != nil { | ||
| return nil, false | ||
| } | ||
| prefix := strings.TrimSpace(string(prefixOut)) | ||
|
|
||
| // -z: NUL-terminated and unquoted. Without it git C-quotes non-ASCII bytes | ||
| // (core.quotePath), e.g. "caf\303\251/", which would never match the raw `rel`. | ||
| // The trailing "." limits the report to root when root is a repository subdirectory. | ||
| // --untracked-files=normal: status.showUntrackedFiles=no makes git reject | ||
| // --ignored=matching; without an explicit override Scan falls back to the fixed list. | ||
| out, err := gitCommand(ctx, root, "status", "--porcelain=v1", "-z", "--untracked-files=normal", "--ignored=matching", ".").Output() | ||
| if err != nil { | ||
| return nil, false | ||
| } | ||
|
|
||
| return parseIgnoredStatus(out, prefix), true | ||
| } | ||
|
|
||
| // parseIgnoredStatus extracts the ignored paths from `git status --porcelain=v1 -z` output, | ||
| // made relative to prefix (root's location inside the repository). | ||
| // | ||
| // Each record is "XY <path>". A rename or copy (R or C in either status column) is followed | ||
| // by one more NUL field holding the original path with no XY prefix. That field is consumed, | ||
| // not inspected: an original name that itself begins with "!! " would otherwise read as an | ||
| // ignored entry and hide a real file from Scan. | ||
| func parseIgnoredStatus(out []byte, prefix string) map[string]bool { | ||
| paths := make(map[string]bool) | ||
| skipOrigPath := false | ||
| for entry := range bytes.SplitSeq(out, []byte{0}) { | ||
| if skipOrigPath { | ||
| skipOrigPath = false | ||
| continue | ||
| } | ||
| if len(entry) < 3 { | ||
| continue | ||
| } | ||
| if x, y := entry[0], entry[1]; x == 'R' || x == 'C' || y == 'R' || y == 'C' { | ||
| skipOrigPath = true | ||
| continue | ||
| } | ||
| // Ignored entries are "!! <path>"; untracked "??" and changed entries are skipped. | ||
| rest, ok := bytes.CutPrefix(entry, []byte("!! ")) | ||
| if !ok { | ||
| continue | ||
| } | ||
| rel, ok := strings.CutPrefix(string(rest), prefix) | ||
| if !ok { | ||
| continue | ||
| } | ||
| rel = path.Clean(strings.TrimSuffix(rel, "/")) | ||
| if rel == "." || rel == "" { | ||
| continue | ||
| } | ||
| paths[rel] = true | ||
| } | ||
| return paths | ||
| } | ||
|
|
||
| // gitCommand builds a git invocation with a fixed argv (no shell) rooted at dir. | ||
| // | ||
| // GIT_OPTIONAL_LOCKS=0 stops `git status` from taking .git/index.lock to refresh the | ||
| // index stat cache. Scan runs on every turn, often while the user or the agent is running | ||
| // git in the same repository; an opportunistic lock there can make a concurrent | ||
| // `git commit` or `git add` fail with "index.lock exists". | ||
| func gitCommand(ctx context.Context, dir string, args ...string) *exec.Cmd { | ||
| // #nosec G204 -- fixed argv, no shell; dir is the caller's own absolute path. | ||
| // core.fsmonitor in .git/config can point at a command git status runs; disable it so | ||
| // Scan never executes foreign hooks from the repository under scan. | ||
| cmd := exec.CommandContext(ctx, "git", append([]string{"-C", dir, "-c", "core.fsmonitor=false"}, args...)...) | ||
| cmd.Env = append(os.Environ(), "GIT_OPTIONAL_LOCKS=0") | ||
| return cmd | ||
| } | ||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Remove inherited Git repository-location variables from the child environment.
gitCommandpassesos.Environ()to the child process unchanged. Git hooks and some wrapper shells setGIT_DIR,GIT_WORK_TREE,GIT_INDEX_FILE, orGIT_COMMON_DIR. Zero can run inside such an environment, for example when an agent runs it from a hook. In that case-C dirchanges only the working directory.GIT_DIRandGIT_WORK_TREEstill choose the repository.rev-parse --show-prefixandstatusthen describe a different repository, and Scan skips files using that repository's ignore list. Remove these variables before you appendGIT_OPTIONAL_LOCKS=0.Proposed fix
Add a regression test that sets
GIT_DIRto another repository and confirms that Scan still usesroot's own ignore rules.🤖 Prompt for AI Agents
Source: Learnings