fix: Pass /dev/null to diff command for missing files in apply --interactive

This commit is contained in:
dualfroz committed 2026-10-11 10:21:53 +02:00
1 parent ee28f348f2
commit 4e184024be
5 files changed
+38 -8

No files matched your search

+5 -5
View File
@@ -115,7 +115,7 @@ func (s *ExternalDiffSystem) Mkdir(name AbsPath, perm fs.FileMode) error {
if err := os.MkdirAll(targetAbsPath.String(), perm); err != nil { if err := os.MkdirAll(targetAbsPath.String(), perm); err != nil {
return err return err
} }
if err := s.RunDiffCommand(devNullAbsPath, targetAbsPath); err != nil { if err := s.RunDiffCommand(DevNullAbsPath, targetAbsPath); err != nil {
return err return err
} }
} }
@@ -151,7 +151,7 @@ func (s *ExternalDiffSystem) Remove(name AbsPath) error {
case err != nil: case err != nil:
return err return err
case s.filter.IncludeFileInfo(fileInfo): case s.filter.IncludeFileInfo(fileInfo):
if err := s.RunDiffCommand(name, devNullAbsPath); err != nil { if err := s.RunDiffCommand(name, DevNullAbsPath); err != nil {
return err return err
} }
} }
@@ -168,7 +168,7 @@ func (s *ExternalDiffSystem) RemoveAll(name AbsPath) error {
case err != nil: case err != nil:
return err return err
case s.filter.IncludeFileInfo(fileInfo): case s.filter.IncludeFileInfo(fileInfo):
if err := s.RunDiffCommand(name, devNullAbsPath); err != nil { if err := s.RunDiffCommand(name, DevNullAbsPath); err != nil {
return err return err
} }
} }
@@ -304,7 +304,7 @@ func (s *ExternalDiffSystem) RunScript(scriptName RelPath, dir AbsPath, data []b
if err := os.WriteFile(targetAbsPath.String(), toData, 0o700); err != nil { if err := os.WriteFile(targetAbsPath.String(), toData, 0o700); err != nil {
return err return err
} }
if err := s.RunDiffCommand(devNullAbsPath, targetAbsPath); err != nil { if err := s.RunDiffCommand(DevNullAbsPath, targetAbsPath); err != nil {
return err return err
} }
} }
@@ -340,7 +340,7 @@ func (s *ExternalDiffSystem) WriteFile(filename AbsPath, data []byte, perm fs.Fi
fromAbsPath := filename fromAbsPath := filename
switch fileInfo, err := os.Lstat(fromAbsPath.String()); { switch fileInfo, err := os.Lstat(fromAbsPath.String()); {
case errors.Is(err, fs.ErrNotExist): case errors.Is(err, fs.ErrNotExist):
fromAbsPath = devNullAbsPath fromAbsPath = DevNullAbsPath
case err != nil: case err != nil:
return err return err
case s.textConvFunc != nil: case s.textConvFunc != nil:
+1 -1
View File
@@ -7,7 +7,7 @@ import (
"strings" "strings"
) )
var devNullAbsPath = NewAbsPath("/dev/null") var DevNullAbsPath = NewAbsPath("/dev/null")
// NewAbsPathFromExtPath returns a new AbsPath by converting extPath to use // NewAbsPathFromExtPath returns a new AbsPath by converting extPath to use
// slashes, performing tilde expansion, and making the path absolute. // slashes, performing tilde expansion, and making the path absolute.
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"strings" "strings"
) )
var devNullAbsPath = NewAbsPath("NUL:") var DevNullAbsPath = NewAbsPath("NUL:")
// NewAbsPathFromExtPath returns a new AbsPath by converting extPath to use // NewAbsPathFromExtPath returns a new AbsPath by converting extPath to use
// slashes, performing tilde expansion, making the path absolute, and converting // slashes, performing tilde expansion, making the path absolute, and converting
+5 -1
View File
@@ -1197,9 +1197,13 @@ func (c *Config) defaultPreApplyFunc(
case err != nil: case err != nil:
return err return err
case choice == "diff": case choice == "diff":
destAbsPath := c.DestDirAbsPath.Join(targetRelPath)
if actualEntryState.Type == chezmoi.EntryStateTypeRemove {
destAbsPath = chezmoi.DevNullAbsPath
}
if err := c.diffFile( if err := c.diffFile(
targetRelPath, targetRelPath,
c.DestDirAbsPath.Join(targetRelPath), actualContents, actualEntryState.Mode, destAbsPath, actualContents, actualEntryState.Mode,
chezmoi.EmptyAbsPath, targetContents, targetEntryState.Mode, chezmoi.EmptyAbsPath, targetContents, targetEntryState.Mode,
); err != nil { ); err != nil {
return err return err
+26
View File
@@ -1,5 +1,6 @@
[windows] skip 'UNIX only' [windows] skip 'UNIX only'
chmod 755 bin/diff-exists
chmod 755 bin/diff-pager chmod 755 bin/diff-pager
# test that chezmoi diff invokes diff.command when configured # test that chezmoi diff invokes diff.command when configured
@@ -59,10 +60,30 @@ cp golden/dot_file $CHEZMOISOURCEDIR
exec chezmoi diff exec chezmoi diff
stdout diff-pager stdout diff-pager
chhome home7/user
stdin golden/diff-no
exec chezmoi apply --interactive --no-tty
stdout \s/dev/null\s+${WORK@R}/.*/\.file$
! exists $HOME/.file
-- bin/diff-exists --
#!/bin/sh
for arg in "$@"; do
if [ ! -e "$arg" ]; then
echo "$arg: no such file" >&2
exit 2
fi
done
echo "$@"
-- bin/diff-pager -- -- bin/diff-pager --
#!/bin/sh #!/bin/sh
echo diff-pager echo diff-pager
-- golden/diff-no --
diff
no
-- golden/diff-umask-002.diff -- -- golden/diff-umask-002.diff --
diff --git a/.file b/.file diff --git a/.file b/.file
index bd729e8ee3cc005444c67dc77eed60016886b5e0..b508963510528ab709627ec448026a10a64c72ef 100664 index bd729e8ee3cc005444c67dc77eed60016886b5e0..b508963510528ab709627ec448026a10a64c72ef 100664
@@ -118,3 +139,8 @@ index bd729e8ee3cc005444c67dc77eed60016886b5e0..b508963510528ab709627ec448026a10
[diff] [diff]
pager = "diff-pager" pager = "diff-pager"
-- home6/user/.local/share/chezmoi/.keep -- -- home6/user/.local/share/chezmoi/.keep --
-- home7/user/.config/chezmoi/chezmoi.toml --
[diff]
command = "diff-exists"
-- home7/user/.local/share/chezmoi/dot_file --
# contents of .file