@@ -2,6 +2,7 @@ package cmd
22
33import (
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
1618type 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
0 commit comments