-
Notifications
You must be signed in to change notification settings - Fork 952
fix(agent/agentcontainers): respect ignore files #19016
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
+323
−7
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8f7bb34
fix(agent/agentcontainers): respect ignore files
DanielleMaywood 5cb9e5c
chore: run `go mod tidy`
DanielleMaywood e39edf2
chore: appease linter
DanielleMaywood a56c827
fix: test on windows
DanielleMaywood f5d16ea
chore: remove initial call
DanielleMaywood e134b47
chore: `[]string{}` -> `components`
DanielleMaywood 8e6c3f3
chore: remove duplicated test cases for `TestFilePathToParts`
DanielleMaywood d787bf6
fix: respect `.git/info/exclude` file
DanielleMaywood 0a438a8
fix: respect `~/.gitconfig`'s `core.excludesFile` option
DanielleMaywood e344d81
chore: oops, forgot my error handling
DanielleMaywood 27a735e
test: ensure we ignore nonsense dev container names
DanielleMaywood 7d8a796
chore: add some error logging
DanielleMaywood cd3be6c
chore: appease the linter
DanielleMaywood 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package ignore | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/go-git/go-git/v5/plumbing/format/config" | ||
"github.com/go-git/go-git/v5/plumbing/format/gitignore" | ||
"github.com/spf13/afero" | ||
"golang.org/x/xerrors" | ||
|
||
"cdr.dev/slog" | ||
) | ||
|
||
const ( | ||
gitconfigFile = ".gitconfig" | ||
gitignoreFile = ".gitignore" | ||
gitInfoExcludeFile = ".git/info/exclude" | ||
) | ||
|
||
func FilePathToParts(path string) []string { | ||
components := []string{} | ||
|
||
if path == "" { | ||
return components | ||
} | ||
|
||
for segment := range strings.SplitSeq(filepath.Clean(path), string(filepath.Separator)) { | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if segment != "" { | ||
components = append(components, segment) | ||
} | ||
} | ||
|
||
return components | ||
} | ||
|
||
func readIgnoreFile(fileSystem afero.Fs, path, ignore string) ([]gitignore.Pattern, error) { | ||
var ps []gitignore.Pattern | ||
|
||
data, err := afero.ReadFile(fileSystem, filepath.Join(path, ignore)) | ||
if err != nil && !errors.Is(err, os.ErrNotExist) { | ||
return nil, err | ||
} | ||
|
||
for s := range strings.SplitSeq(string(data), "\n") { | ||
DanielleMaywood marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if !strings.HasPrefix(s, "#") && len(strings.TrimSpace(s)) > 0 { | ||
ps = append(ps, gitignore.ParsePattern(s, FilePathToParts(path))) | ||
} | ||
} | ||
|
||
return ps, nil | ||
} | ||
|
||
func ReadPatterns(ctx context.Context, logger slog.Logger, fileSystem afero.Fs, path string) ([]gitignore.Pattern, error) { | ||
var ps []gitignore.Pattern | ||
|
||
subPs, err := readIgnoreFile(fileSystem, path, gitInfoExcludeFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ps = append(ps, subPs...) | ||
|
||
if err := afero.Walk(fileSystem, path, func(path string, info fs.FileInfo, err error) error { | ||
if err != nil { | ||
logger.Error(ctx, "encountered error while walking for git ignore files", | ||
slog.F("path", path), | ||
slog.Error(err)) | ||
return nil | ||
} | ||
|
||
if !info.IsDir() { | ||
return nil | ||
} | ||
|
||
subPs, err := readIgnoreFile(fileSystem, path, gitignoreFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ps = append(ps, subPs...) | ||
|
||
return nil | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
return ps, nil | ||
} | ||
|
||
func loadPatterns(fileSystem afero.Fs, path string) ([]gitignore.Pattern, error) { | ||
data, err := afero.ReadFile(fileSystem, path) | ||
if err != nil && !errors.Is(err, os.ErrNotExist) { | ||
return nil, err | ||
} | ||
|
||
decoder := config.NewDecoder(bytes.NewBuffer(data)) | ||
|
||
conf := config.New() | ||
if err := decoder.Decode(conf); err != nil { | ||
return nil, xerrors.Errorf("decode config: %w", err) | ||
} | ||
|
||
excludes := conf.Section("core").Options.Get("excludesfile") | ||
if excludes == "" { | ||
return nil, nil | ||
} | ||
|
||
return readIgnoreFile(fileSystem, "", excludes) | ||
} | ||
|
||
func LoadGlobalPatterns(fileSystem afero.Fs) ([]gitignore.Pattern, error) { | ||
home, err := os.UserHomeDir() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return loadPatterns(fileSystem, filepath.Join(home, gitconfigFile)) | ||
} |
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,38 @@ | ||
package ignore_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/v2/agent/agentcontainers/ignore" | ||
) | ||
|
||
func TestFilePathToParts(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
path string | ||
expected []string | ||
}{ | ||
{"", []string{}}, | ||
{"/", []string{}}, | ||
{"foo", []string{"foo"}}, | ||
{"/foo", []string{"foo"}}, | ||
{"./foo/bar", []string{"foo", "bar"}}, | ||
{"../foo/bar", []string{"..", "foo", "bar"}}, | ||
{"foo/bar/baz", []string{"foo", "bar", "baz"}}, | ||
{"/foo/bar/baz", []string{"foo", "bar", "baz"}}, | ||
{"foo/../bar", []string{"bar"}}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(fmt.Sprintf("`%s`", tt.path), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
parts := ignore.FilePathToParts(tt.path) | ||
require.Equal(t, tt.expected, parts) | ||
}) | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.