Skip to content

Commit bc055df

Browse files
committed
fix: Ignore templates in exact_ dirs in re-add command
1 parent 4b67af9 commit bc055df

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

internal/cmd/readdcmd.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
"io/fs"
78
"runtime"
@@ -11,6 +12,7 @@ import (
1112
"github.com/spf13/cobra"
1213

1314
"chezmoi.io/chezmoi/internal/chezmoi"
15+
"chezmoi.io/chezmoi/internal/chezmoiset"
1416
)
1517

1618
type reAddCmdConfig struct {
@@ -257,23 +259,31 @@ func (c *Config) processExactDirs(
257259

258260
// Read the target directory contents
259261
dirEntries, err := c.destSystem.ReadDir(targetDirAbsPath)
260-
if err != nil {
262+
switch {
263+
case errors.Is(err, fs.ErrNotExist):
261264
// If directory doesn't exist in target, skip it
262265
continue
266+
case err != nil:
267+
return err
263268
}
264269

265270
// Build sets of files in source and target
266271
sourceFiles := make(map[string]chezmoi.SourceStateEntry)
272+
ignoredFiles := chezmoiset.New[chezmoi.RelPath]()
267273
targetFiles := make(map[string]fs.DirEntry)
268274

269275
// Collect files from source state that are in this exact directory
270276
// Only consider actual files, not removes or other entry types
271277
for entryRelPath, entry := range sourceStateEntries {
272278
// Check if this entry is a direct child of the exact directory
273279
if entryRelPath.Dir() == targetRelPath {
274-
// Only count actual files in source, not remove entries
275-
if _, ok := entry.(*chezmoi.SourceStateFile); ok {
276-
sourceFiles[entryRelPath.Base()] = entry
280+
// Only count actual files in source, not remove entries or templates
281+
if sourceStateFile, ok := entry.(*chezmoi.SourceStateFile); ok {
282+
if sourceStateFile.Attr().Template {
283+
ignoredFiles.Add(entryRelPath)
284+
} else {
285+
sourceFiles[entryRelPath.Base()] = entry
286+
}
277287
}
278288
}
279289
}
@@ -292,8 +302,9 @@ func (c *Config) processExactDirs(
292302
for name, dirEntry := range targetFiles {
293303
entryRelPath := targetRelPath.JoinString(name)
294304

295-
// Skip files that were already processed in the file re-add loop
296-
if processedFiles[entryRelPath] {
305+
// Skip files that were already processed in the file re-add loop or
306+
// are ignored
307+
if processedFiles[entryRelPath] || ignoredFiles.Contains(entryRelPath) {
297308
continue
298309
}
299310

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# test that chezmoi re-add does not affect templates
2+
exec chezmoi apply
3+
edit $HOME/dir/a
4+
edit $HOME/dir/b
5+
exec chezmoi re-add
6+
cmp $CHEZMOISOURCEDIR/exact_dir/a golden/edited-a
7+
cmp $CHEZMOISOURCEDIR/exact_dir/b.tmpl golden/b.tmpl
8+
9+
-- golden/b.tmpl --
10+
# contents of b
11+
-- golden/edited-a --
12+
# contents of a
13+
# edited
14+
-- home/user/.local/share/chezmoi/exact_dir/a --
15+
# contents of a
16+
-- home/user/.local/share/chezmoi/exact_dir/b.tmpl --
17+
# contents of b

0 commit comments

Comments
 (0)