-
Notifications
You must be signed in to change notification settings - Fork 0
fix: exclude Docker nested bind-mount points from git clean during source sync #46
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| package project | ||
|
|
||
| import ( | ||
| "path" | ||
| "path/filepath" | ||
| "slices" | ||
| "strings" | ||
|
|
||
| "github.com/compose-spec/compose-go/v2/types" | ||
|
|
||
| "github.com/pilat/devbox/internal/app" | ||
| ) | ||
|
|
||
| // SourceCleanExcludes returns, per source name, the repo-relative paths `git clean` must skip | ||
| // because Docker mounts a different source into them as a live nested bind mount. Keys match the | ||
| // on-disk sources/<name> segment; values carry no leading slash. | ||
| func (p *Project) SourceCleanExcludes() map[string][]string { | ||
| sourcesRoot := filepath.Join(p.WorkingDir, app.SourcesDir) | ||
|
|
||
| return nestedForeignMountExcludes(p.Services, sourcesRoot) | ||
| } | ||
|
|
||
| // nestedForeignMountExcludes derives cross-source nested-mount excludes from the compose volume | ||
| // topology: a bind volume (child) of a source is mounted nested inside a same-service parent bind | ||
| // whose host source is a *different* source. The different-source discriminator keeps self-nested | ||
| // and file/config mounts out, so clean is never weakened where it should still run. | ||
| func nestedForeignMountExcludes(services types.Services, sourcesRoot string) map[string][]string { | ||
| type bindVol struct { | ||
| source string | ||
| target string | ||
| } | ||
|
|
||
| result := make(map[string][]string) | ||
|
|
||
| for _, service := range services { | ||
| binds := make([]bindVol, 0, len(service.Volumes)) | ||
| for _, v := range service.Volumes { | ||
| if v.Type != "bind" { | ||
| continue | ||
| } | ||
| binds = append(binds, bindVol{source: v.Source, target: path.Clean(v.Target)}) | ||
| } | ||
|
|
||
| for _, child := range binds { | ||
| childSrc, ok := sourceSegment(child.source, sourcesRoot) | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| var parent *bindVol | ||
| for i := range binds { | ||
| cand := &binds[i] | ||
| if !strings.HasPrefix(child.target, cand.target+"/") { | ||
| continue | ||
| } | ||
| if parent == nil || len(cand.target) > len(parent.target) { | ||
| parent = cand | ||
| } | ||
| } | ||
| if parent == nil { | ||
| continue | ||
| } | ||
|
|
||
| parentSrc, ok := sourceSegment(parent.source, sourcesRoot) | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| if childSrc == parentSrc { | ||
| continue | ||
| } | ||
|
|
||
| childRel := strings.TrimPrefix(strings.TrimPrefix(child.target, parent.target), "/") | ||
| hostMountpoint := filepath.Join(parent.source, childRel) | ||
| repoRel, err := filepath.Rel(filepath.Join(sourcesRoot, parentSrc), hostMountpoint) | ||
| if err != nil { | ||
| continue | ||
| } | ||
|
|
||
| result[parentSrc] = append(result[parentSrc], repoRel) | ||
| } | ||
| } | ||
|
|
||
| for src, excludes := range result { | ||
| slices.Sort(excludes) | ||
| result[src] = slices.Compact(excludes) | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| // sourceSegment reports the sources/<name> segment of an absolute host path, if it lives under | ||
| // sourcesRoot. It returns false for anything outside sourcesRoot (envs/, named volumes, or a | ||
| // LocalMounts-rewritten local path). | ||
| func sourceSegment(hostPath, sourcesRoot string) (string, bool) { | ||
| prefix := sourcesRoot + string(filepath.Separator) | ||
| if !strings.HasPrefix(hostPath, prefix) { | ||
| return "", false | ||
| } | ||
|
|
||
| seg, _, _ := strings.Cut(hostPath[len(prefix):], string(filepath.Separator)) | ||
| if seg == "" { | ||
| return "", false | ||
| } | ||
|
|
||
| return seg, true | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.