Skip to content

Commit

Permalink
Make modify scripts always overwrite the target without prompting
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jul 12, 2021
1 parent 4b428e8 commit 38d163d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions internal/chezmoi/entrystate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type EntryState struct {
Mode fs.FileMode `json:"mode,omitempty" toml:"mode,omitempty" yaml:"mode,omitempty"`
ContentsSHA256 HexBytes `json:"contentsSHA256,omitempty" toml:"contentsSHA256,omitempty" yaml:"contentsSHA256,omitempty"` //nolint:tagliatelle
contents []byte
overwrite bool
}

// Contents returns s's contents, if available.
Expand Down Expand Up @@ -54,3 +55,8 @@ func (s *EntryState) Equivalent(other *EntryState) bool {
return s.Equal(other)
}
}

// Overwrite returns true if s should be overwritten by default.
func (s *EntryState) Overwrite() bool {
return s.overwrite
}
1 change: 1 addition & 0 deletions internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,7 @@ func (s *SourceState) newModifyTargetStateEntryFunc(sourceRelPath SourceRelPath,
}
return &TargetStateFile{
lazyContents: newLazyContentsFunc(contentsFunc),
overwrite: true,
perm: fileAttr.perm() &^ s.umask,
}, nil
}
Expand Down
6 changes: 4 additions & 2 deletions internal/chezmoi/targetstateentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ type TargetStateDir struct {
// A TargetStateFile represents the state of a file in the target state.
type TargetStateFile struct {
*lazyContents
empty bool
perm fs.FileMode
empty bool
overwrite bool
perm fs.FileMode
}

// A TargetStateRemove represents the absence of an entry in the target state.
Expand Down Expand Up @@ -145,6 +146,7 @@ func (t *TargetStateFile) EntryState(umask fs.FileMode) (*EntryState, error) {
Mode: t.perm &^ umask,
ContentsSHA256: HexBytes(contentsSHA256),
contents: contents,
overwrite: t.overwrite,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ func (c *Config) defaultConfigFile(fileSystem vfs.Stater, bds *xdg.BaseDirectory

func (c *Config) defaultPreApplyFunc(targetRelPath chezmoi.RelPath, targetEntryState, lastWrittenEntryState, actualEntryState *chezmoi.EntryState) error {
switch {
case targetEntryState.Overwrite():
return nil
case targetEntryState.Type == chezmoi.EntryStateTypeScript:
return nil
case c.force:
Expand Down
20 changes: 20 additions & 0 deletions internal/cmd/testdata/scripts/modify_unix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ cmpmod 666 $HOME/.file
chezmoi apply $HOME${/}.file
cmpmod 700 $HOME/.file

chhome home3/user

# test that chezmoi apply always overwrites modified files without --force
chezmoi add $HOME${/}.modify
chezmoi apply
edit $HOME${/}.modify
rm $CHEZMOISOURCEDIR/dot_modify
cp home/user/.local/share/chezmoi/modify_dot_modify $CHEZMOISOURCEDIR
chezmoi apply
cmp $HOME${/}.modify golden/.edited-and-modified

-- golden/.edited-and-modified --
beginning
modified-middle
end
# edited
-- golden/.modified --
beginning
modified-middle
Expand Down Expand Up @@ -76,3 +92,7 @@ exit 1
#!/bin/sh

cat
-- home3/user/.modify --
beginning
middle
end

0 comments on commit 38d163d

Please sign in to comment.