Skip to content

Commit

Permalink
fix: Fix status for onchange scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Oct 10, 2021
1 parent 597f586 commit ebc0149
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
2 changes: 1 addition & 1 deletion internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func (s *SourceState) Apply(targetSystem, destSystem System, persistentState Per
return err
}

switch skip, err := targetStateEntry.SkipApply(persistentState); {
switch skip, err := targetStateEntry.SkipApply(persistentState, targetAbsPath); {
case err != nil:
return err
case skip:
Expand Down
61 changes: 40 additions & 21 deletions internal/chezmoi/targetstateentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type TargetStateEntry interface {
Apply(system System, persistentState PersistentState, actualStateEntry ActualStateEntry) (bool, error)
EntryState(umask fs.FileMode) (*EntryState, error)
Evaluate() error
SkipApply(persistentState PersistentState) (bool, error)
SkipApply(persistentState PersistentState, targetAbsPath AbsPath) (bool, error)
}

// A TargetStateDir represents the state of a directory in the target state.
Expand Down Expand Up @@ -86,7 +86,7 @@ func (t *TargetStateDir) Evaluate() error {
}

// SkipApply implements TargetState.SkipApply.
func (t *TargetStateDir) SkipApply(persistentState PersistentState) (bool, error) {
func (t *TargetStateDir) SkipApply(persistentState PersistentState, targetAbsPath AbsPath) (bool, error) {
return false, nil
}

Expand Down Expand Up @@ -162,7 +162,7 @@ func (t *TargetStateFile) Perm(umask fs.FileMode) fs.FileMode {
}

// SkipApply implements TargetState.SkipApply.
func (t *TargetStateFile) SkipApply(persistentState PersistentState) (bool, error) {
func (t *TargetStateFile) SkipApply(persistentState PersistentState, targetAbsPath AbsPath) (bool, error) {
return false, nil
}

Expand All @@ -187,7 +187,7 @@ func (t *TargetStateRemove) Evaluate() error {
}

// SkipApply implements TargetState.SkipApply.
func (t *TargetStateRemove) SkipApply(persistentState PersistentState) (bool, error) {
func (t *TargetStateRemove) SkipApply(persistentState PersistentState, targetAbsPath AbsPath) (bool, error) {
return false, nil
}

Expand Down Expand Up @@ -270,23 +270,42 @@ func (t *TargetStateScript) Evaluate() error {
}

// SkipApply implements TargetState.SkipApply.
func (t *TargetStateScript) SkipApply(persistentState PersistentState) (bool, error) {
if t.condition != ScriptConditionOnce {
return false, nil
}
contentsSHA256, err := t.ContentsSHA256()
if err != nil {
return false, err
}
key := []byte(hex.EncodeToString(contentsSHA256))
switch scriptState, err := persistentState.Get(scriptStateBucket, key); {
case err != nil:
return false, err
case scriptState != nil:
return true, nil
default:
func (t *TargetStateScript) SkipApply(persistentState PersistentState, targetAbsPath AbsPath) (bool, error) {
switch t.condition {
case ScriptConditionAlways:
return false, nil
case ScriptConditionOnce:
contentsSHA256, err := t.ContentsSHA256()
if err != nil {
return false, err
}
scriptStateKey := []byte(hex.EncodeToString(contentsSHA256))
switch scriptState, err := persistentState.Get(scriptStateBucket, scriptStateKey); {
case err != nil:
return false, err
case scriptState != nil:
return true, nil
}
case ScriptConditionOnChange:
entryStateKey := []byte(targetAbsPath.String())
switch entryStateBytes, err := persistentState.Get(EntryStateBucket, entryStateKey); {
case err != nil:
return false, err
case entryStateBytes != nil:
var entryState EntryState
if err := stateFormat.Unmarshal(entryStateBytes, &entryState); err != nil {
return false, err
}
contentsSHA256, err := t.ContentsSHA256()
if err != nil {
return false, err
}
if bytes.Equal(entryState.ContentsSHA256.Bytes(), contentsSHA256) {
return true, nil
}
}
}
return false, nil
}

// Apply updates actualStateEntry to match t.
Expand Down Expand Up @@ -349,7 +368,7 @@ func (t *TargetStateSymlink) Evaluate() error {
}

// SkipApply implements TargetState.SkipApply.
func (t *TargetStateSymlink) SkipApply(persistentState PersistentState) (bool, error) {
func (t *TargetStateSymlink) SkipApply(persistentState PersistentState, targetAbsPath AbsPath) (bool, error) {
return false, nil
}

Expand All @@ -370,6 +389,6 @@ func (t *targetStateRenameDir) Evaluate() error {
}

// SkipApply implements TargetState.SkipApply.
func (t *targetStateRenameDir) SkipApply(persistentState PersistentState) (bool, error) {
func (t *targetStateRenameDir) SkipApply(persistentState PersistentState, targetAbsPath AbsPath) (bool, error) {
return false, nil
}
12 changes: 11 additions & 1 deletion internal/cmd/testdata/scripts/scriptonchange_unix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ cmp stdout golden/script-one-state.json
chezmoi apply
! stdout .

# test that chezmoi apply runs onchange scripts when their contents are changed
# test that chezmoi status does not print that it will run onchange scripts when their contents are not changed
chezmoi status
! stdout .

# test that chezmoi status does print that it will run onchange scripts when their contents are changed
cp golden/script-two.sh $CHEZMOISOURCEDIR/run_onchange_script.sh
chezmoi status
cmp stdout golden/status

# test that chezmoi apply runs onchange scripts when their contents are changed
chezmoi apply
stdout two
chezmoi state get --bucket=entryState --key=$HOME/script.sh
Expand Down Expand Up @@ -44,3 +52,5 @@ echo one
#!/bin/sh

echo two
-- golden/status --
R script.sh

0 comments on commit ebc0149

Please sign in to comment.