Skip to content

Commit

Permalink
Don't add ignored files, fixes #124
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 16, 2019
1 parent 53c20a0 commit 8cc9fdd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/lib/chezmoi"
Expand Down Expand Up @@ -55,6 +56,7 @@ func (c *Config) runAddCommand(fs vfs.FS, args []string) error {
default:
return err
}
destDirPrefix := ts.DestDir + "/"
for _, arg := range args {
path, err := filepath.Abs(arg)
if err != nil {
Expand All @@ -65,11 +67,17 @@ func (c *Config) runAddCommand(fs vfs.FS, args []string) error {
if err != nil {
return err
}
if ts.TargetIgnore.Match(strings.TrimPrefix(path, destDirPrefix)) {
return nil
}
return ts.Add(fs, c.add.options, path, info, mutator)
}); err != nil {
return err
}
} else {
if ts.TargetIgnore.Match(strings.TrimPrefix(path, destDirPrefix)) {
continue
}
if err := ts.Add(fs, c.add.options, path, nil, mutator); err != nil {
return err
}
Expand Down
48 changes: 48 additions & 0 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,54 @@ func TestAddCommand(t *testing.T) {
),
},
},
{
name: "dont_add_ignored_file",
args: []string{"/home/user/foo"},
root: map[string]interface{}{
"/home/user": &vfst.Dir{Perm: 0755},
"/home/user/.chezmoi": &vfst.Dir{
Perm: 0700,
Entries: map[string]interface{}{
".chezmoiignore": "foo\n",
},
},
"/home/user/foo": "bar",
},
tests: []vfst.Test{
vfst.TestPath("/home/user/.chezmoi/foo",
vfst.TestDoesNotExist,
),
},
},
{
name: "dont_add_ignored_file_recursive",
args: []string{"/home/user/foo"},
add: addCommandConfig{
recursive: true,
},
root: map[string]interface{}{
"/home/user": &vfst.Dir{Perm: 0755},
"/home/user/.chezmoi": &vfst.Dir{
Perm: 0700,
Entries: map[string]interface{}{
"exact_foo/.chezmoiignore": "bar/qux\n",
},
},
"/home/user/foo/bar": map[string]interface{}{
"baz": "baz",
"qux": "quz",
},
},
tests: []vfst.Test{
vfst.TestPath("/home/user/.chezmoi/exact_foo/bar/baz",
vfst.TestModeIsRegular,
vfst.TestContentsString("baz"),
),
vfst.TestPath("/home/user/.chezmoi/exact_foo/bar/qux",
vfst.TestDoesNotExist,
),
},
},
} {
t.Run(tc.name, func(t *testing.T) {
c := &Config{
Expand Down

0 comments on commit 8cc9fdd

Please sign in to comment.