Skip to content

Commit

Permalink
fix: .chezmoiremove with * when destDir is /
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs authored and twpayne committed Nov 24, 2021
1 parent 7d31ecb commit 5672e1f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions internal/chezmoi/chezmoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,11 @@ func mustTrimSuffix(s, suffix string) string {
}
return s[:len(s)-len(suffix)]
}

// ensureSuffix adds suffix to s if s is not suffixed by suffix.
func ensureSuffix(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
return s
}
return s + suffix
}
2 changes: 1 addition & 1 deletion internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
if err := s.addPatterns(removePatterns, sourceAbsPath, sourceRelPath); err != nil {
return err
}
matches, err := removePatterns.glob(s.system.UnderlyingFS(), s.destDirAbsPath.String()+"/")
matches, err := removePatterns.glob(s.system.UnderlyingFS(), ensureSuffix(s.destDirAbsPath.String(), "/"))
if err != nil {
return err
}
Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/testdata/scripts/remove.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,24 @@ chezmoi apply
! exists $HOME/.file
! exists $HOME/.dir

chhome home3/user

# test that chezmoi apply with .chezmoiremove with star works on destination dir with trailing slash
exists $HOME/.star-file
exists $HOME/.star-dir
chezmoi apply --destination=$HOME/
! exists $HOME/.star-file
! exists $HOME/.star-dir

-- home2/user/.dir/.keep --
-- home2/user/.file --
# contents of .file
-- home2/user/.local/share/chezmoi/remove_dot_file --
-- home2/user/.local/share/chezmoi/remove_dot_dir --

-- home3/user/.star-dir/.keep --
-- home3/user/.star-file --
# contents of .star-file
-- home3/user/.local/share/chezmoi/.chezmoiremove --
.*-dir/
.*-file

0 comments on commit 5672e1f

Please sign in to comment.