Skip to content

Commit

Permalink
Merge pull request #1200 from twpayne/fix-1194
Browse files Browse the repository at this point in the history
Update last written state when target and actual states are equivalent
  • Loading branch information
twpayne committed May 10, 2021
2 parents 37e2f09 + 037a372 commit 2256a83
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,19 @@ func (s *SourceState) Apply(targetSystem, destSystem System, persistentState Per
}
}

// If the target entry state matches the actual entry state, but not the
// last written entry state then silently update the last written entry
// state. This handles the case where the user makes identical edits to
// the source and target states: instead of reporting a diff with
// respect to the last written state, we record the effect of the last
// apply as the last written state.
if targetEntryState.Equivalent(actualEntryState) && !lastWrittenEntryState.Equivalent(actualEntryState) {
if err := persistentStateSet(persistentState, EntryStateBucket, []byte(targetAbsPath), targetEntryState); err != nil {
return err
}
lastWrittenEntryState = targetEntryState
}

if err := options.PreApplyFunc(targetRelPath, targetEntryState, lastWrittenEntryState, actualEntryState); err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions testdata/scripts/edgecases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ exists $CHEZMOICONFIGDIR/chezmoistate.boltdb
exists $CHEZMOISOURCEDIR/dot_file
chezmoi state dump
stdout 634a4dd193c7b3b926d2e08026aa81a416fd41cec52854863b974af422495663 # sha256sum of "# contents of .file\n"

# test that chezmoi apply updates the state if the target and destination states match, even if the last written state does not
edit $CHEZMOISOURCEDIR/dot_file
edit $HOME/.file
cmp $HOME/.file $CHEZMOISOURCEDIR/dot_file
chezmoi apply --dry-run $HOME${/}.file
chezmoi state dump
! stdout 2e9dd6a2a8c15b20d4b0882d4c0fb8c7eea4e8ece46818090b387132f9f84c34 # sha256sum of "# contents of .file\n# edited\n"
chezmoi apply $HOME${/}.file
chezmoi state dump
stdout 2e9dd6a2a8c15b20d4b0882d4c0fb8c7eea4e8ece46818090b387132f9f84c34 # sha256sum of "# contents of .file\n# edited\n"

0 comments on commit 2256a83

Please sign in to comment.