Skip to content

Commit

Permalink
fix: Don't traverse into ignored directories when adding files
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 31, 2024
1 parent ed0186f commit 2e4236c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -40,7 +40,7 @@ require (
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a
github.com/twpayne/go-pinentry/v3 v3.0.1
github.com/twpayne/go-shell v0.4.0
github.com/twpayne/go-vfs/v5 v5.0.3
github.com/twpayne/go-vfs/v5 v5.0.4
github.com/twpayne/go-xdg/v6 v6.1.2
github.com/ulikunitz/xz v0.5.11
github.com/zalando/go-keyring v0.2.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -423,8 +423,8 @@ github.com/twpayne/go-shell v0.4.0 h1:RAAMbjEj7mcwDdwC7SiFHGUKR+WDAURU6mnyd3r2p2
github.com/twpayne/go-shell v0.4.0/go.mod h1:MP3aUA0TQ3IGoJc15ahjb+7A7wZH4NeGrvLZ/aFQsHc=
github.com/twpayne/go-vfs/v4 v4.3.0 h1:rTqFzzOQ/6ESKTSiwVubHlCBedJDOhQyVSnw8rQNZhU=
github.com/twpayne/go-vfs/v4 v4.3.0/go.mod h1:tq2UVhnUepesc0lSnPJH/jQ8HruGhzwZe2r5kDFpEIw=
github.com/twpayne/go-vfs/v5 v5.0.3 h1:9jeacSMvwh8sSLCuS4Ay0QmyAv1ToG1ITeQn6+Zsq1k=
github.com/twpayne/go-vfs/v5 v5.0.3/go.mod h1:zTPFJUbgsEMFNSWnWQlLq9wh4AN83edZzx3VXbxrS1w=
github.com/twpayne/go-vfs/v5 v5.0.4 h1:/ne3h+rW7f5YOyOFguz+3ztfUwzOLR0Vts3y0mMAitg=
github.com/twpayne/go-vfs/v5 v5.0.4/go.mod h1:zTPFJUbgsEMFNSWnWQlLq9wh4AN83edZzx3VXbxrS1w=
github.com/twpayne/go-xdg/v6 v6.1.2 h1:KbfCsAP4bBR5+dzfTIh/M9onOPCSqlYsIER79IKwt+s=
github.com/twpayne/go-xdg/v6 v6.1.2/go.mod h1:BFHclQaEPLq3jRRYjf1PdFzUEvAfPeLjNymIO/7/7o4=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/addcmd.go
Expand Up @@ -164,8 +164,9 @@ func (c *Config) defaultReplaceFunc(

func (c *Config) runAddCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
destAbsPathInfos, err := c.destAbsPathInfos(sourceState, args, destAbsPathInfosOptions{
follow: c.Mode == chezmoi.ModeSymlink || c.Add.follow,
recursive: c.Add.recursive,
follow: c.Mode == chezmoi.ModeSymlink || c.Add.follow,
onIgnoreFunc: c.defaultOnIgnoreFunc,
recursive: c.Add.recursive,
})
if err != nil {
return err
Expand Down
22 changes: 21 additions & 1 deletion internal/cmd/config.go
Expand Up @@ -1071,6 +1071,7 @@ func (c *Config) defaultSourceDir(fileSystem vfs.Stater, bds *xdg.BaseDirectoryS
type destAbsPathInfosOptions struct {
follow bool
ignoreNotExist bool
onIgnoreFunc func(chezmoi.RelPath)
recursive bool
}

Expand All @@ -1089,9 +1090,14 @@ func (c *Config) destAbsPathInfos(
if err != nil {
return nil, err
}
if _, err := c.targetRelPath(destAbsPath); err != nil {
targetRelPath, err := c.targetRelPath(destAbsPath)
if err != nil {
return nil, err
}
if sourceState.Ignore(targetRelPath) {
options.onIgnoreFunc(targetRelPath)
continue
}
if options.recursive {
walkFunc := func(destAbsPath chezmoi.AbsPath, fileInfo fs.FileInfo, err error) error {
switch {
Expand All @@ -1100,12 +1106,26 @@ func (c *Config) destAbsPathInfos(
case err != nil:
return err
}

targetRelPath, err := c.targetRelPath(destAbsPath)
if err != nil {
return err
}
if sourceState.Ignore(targetRelPath) {
options.onIgnoreFunc(targetRelPath)
if fileInfo.IsDir() {
return fs.SkipDir
}
return nil
}

if options.follow && fileInfo.Mode().Type() == fs.ModeSymlink {
fileInfo, err = c.destSystem.Stat(destAbsPath)
if err != nil {
return err
}
}

return sourceState.AddDestAbsPathInfos(destAbsPathInfos, c.destSystem, destAbsPath, fileInfo)
}
if err := chezmoi.Walk(c.destSystem, destAbsPath, walkFunc); err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/cmd/mackupcmd_darwin.go
Expand Up @@ -97,6 +97,7 @@ func (c *Config) runMackupAddCmd(cmd *cobra.Command, args []string, sourceState
destAbsPathInfos, err := c.destAbsPathInfos(sourceState, addArgs, destAbsPathInfosOptions{
follow: c.Add.follow,
ignoreNotExist: true,
onIgnoreFunc: c.defaultOnIgnoreFunc,
recursive: c.Add.recursive,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/testdata/scripts/add.txtar
Expand Up @@ -59,7 +59,7 @@ chhome home3/user
# test that chezmoi add respects .chezmoiignore
exec chezmoi add $HOME${/}.dir
exists $CHEZMOISOURCEDIR/dot_dir/file
stderr 'warning: ignoring'
stderr 'warning: ignoring .dir/ignore'
! exists $CHEZMOISOURCEDIR/dot_dir/ignore

chhome home4/user
Expand Down
14 changes: 14 additions & 0 deletions internal/cmd/testdata/scripts/issue3630.txtar
@@ -0,0 +1,14 @@
# test that chezmoi add does not traverse into ignored directories
chmod 000 $HOME/.dir/private
exec chezmoi add $HOME${/}.dir
stderr 'warning: ignoring .dir/private'
cmp $CHEZMOISOURCEDIR/dot_dir/public/file golden/file

-- golden/file --
# contents of .dir/public/file
-- home/user/.dir/private/file --
# contents of .dir/private/file
-- home/user/.dir/public/file --
# contents of .dir/public/file
-- home/user/.local/share/chezmoi/.chezmoiignore --
.dir/private

0 comments on commit 2e4236c

Please sign in to comment.