Skip to content

Commit

Permalink
Fix behavior when adding an empty file without --empty
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 31, 2020
1 parent d6f80bb commit bcd0750
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 14 additions & 0 deletions cmd/add_test.go
Expand Up @@ -205,6 +205,20 @@ func TestAddCommand(t *testing.T) {
),
},
},
{
name: "add_empty_file_in_subdir",
args: []string{"/home/user/subdir/empty"},
root: map[string]interface{}{
"/home/user": &vfst.Dir{Perm: 0755},
"/home/user/.local/share/chezmoi": &vfst.Dir{Perm: 0700},
"/home/user/subdir/empty": "",
},
tests: []vfst.Test{
vfst.TestPath("/home/user/.local/share/chezmoi/subdir",
vfst.TestIsDir,
),
},
},
{
name: "add_symlink",
args: []string{"/home/user/foo"},
Expand Down
8 changes: 6 additions & 2 deletions internal/chezmoi/targetstate.go
Expand Up @@ -238,10 +238,14 @@ func (ts *TargetState) Add(fs vfs.FS, addOptions AddOptions, targetPath string,
case info.Mode().IsRegular():
if info.Size() == 0 && !addOptions.Empty {
entry, err := ts.Get(fs, targetPath)
if err != nil {
switch {
case os.IsNotExist(err):
return nil
case err == nil:
return mutator.RemoveAll(filepath.Join(ts.SourceDir, entry.SourceName()))
default:
return err
}
return mutator.RemoveAll(filepath.Join(ts.SourceDir, entry.SourceName()))
}
contents, err := fs.ReadFile(targetPath)
if err != nil {
Expand Down

0 comments on commit bcd0750

Please sign in to comment.