Skip to content

Commit

Permalink
feat: support relative paths in ignore patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
s0up4200 committed Jan 14, 2025
1 parent 4c0b565 commit 947e42b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Path patterns to ignore (separated by comma). Patterns can be absolute or relati

#### `ignore-from-file`

Read absolute path regex patterns to ignore from file
Read path patterns to ignore from file. Patterns can be absolute or relative to the current working directory.

#### `max-cores`

Expand Down
12 changes: 9 additions & 3 deletions gdu.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ is not so huge.

**-h**, **\--help**\[=false\] help for gdu

**-i**, **\--ignore-dirs**=\[/proc,/dev,/sys,/run\] Paths to ignore (separated by comma). Supports both absolute and relative paths.
**-i**, **\--ignore-dirs**=\[/proc,/dev,/sys,/run\]
Paths to ignore (separated by comma).
Supports both absolute and relative paths.

**-I**, **\--ignore-dirs-pattern** Path patterns to ignore (separated by comma). Supports both absolute and relative path patterns.
**-I**, **\--ignore-dirs-pattern**
Path patterns to ignore (separated by comma).
Supports both absolute and relative path patterns.

**-X**, **\--ignore-from** Read absolute path patterns to ignore from file
**-X**, **\--ignore-from**
Read path patterns to ignore from file.
Supports both absolute and relative path patterns.

**-l**, **\--log-file**=\"/dev/null\" Path to a logfile

Expand Down
11 changes: 11 additions & 0 deletions internal/common/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ func CreateIgnorePattern(paths []string) (*regexp.Regexp, error) {
if _, err = regexp.Compile(path); err != nil {
return nil, err
}
if !filepath.IsAbs(path) {
absPath, err := filepath.Abs(path)
if err == nil {
paths = append(paths, absPath)
}
} else {
relPath, err := filepath.Rel("/", path)
if err == nil {
paths = append(paths, relPath)
}
}
paths[i] = "(" + path + ")"
}

Expand Down
39 changes: 39 additions & 0 deletions internal/common/ignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,42 @@ func TestIgnoreByAbsPathWithRelativeMatch(t *testing.T) {
assert.True(t, shouldBeIgnored("abc", "test_dir/abc"))
assert.False(t, shouldBeIgnored("xxx", "test_dir/xxx"))
}

func TestIgnoreByRelativePattern(t *testing.T) {
ui := &common.UI{}
err := ui.SetIgnoreDirPatterns([]string{"test_dir/[abc]+"})
assert.Nil(t, err)
shouldBeIgnored := ui.CreateIgnoreFunc()

assert.True(t, shouldBeIgnored("abc", "test_dir/abc"))
absPath, err := filepath.Abs("test_dir/abc")
assert.Nil(t, err)
assert.True(t, shouldBeIgnored("abc", absPath))
assert.False(t, shouldBeIgnored("xxx", "test_dir/xxx"))
}

func TestIgnoreFromFileWithRelativePaths(t *testing.T) {
file, err := os.OpenFile("ignore", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
panic(err)
}
defer file.Close()

if _, err := file.WriteString("test_dir/aaa\n"); err != nil {
panic(err)
}
if _, err := file.WriteString("node_modules/[^/]+\n"); err != nil {
panic(err)
}

ui := &common.UI{}
err = ui.SetIgnoreFromFile("ignore")
assert.Nil(t, err)
shouldBeIgnored := ui.CreateIgnoreFunc()

assert.True(t, shouldBeIgnored("aaa", "test_dir/aaa"))
absPath, err := filepath.Abs("test_dir/aaa")
assert.Nil(t, err)
assert.True(t, shouldBeIgnored("aaa", absPath))
assert.False(t, shouldBeIgnored("xxx", "test_dir/xxx"))
}

0 comments on commit 947e42b

Please sign in to comment.