Skip to content

Commit

Permalink
feat: Improve inconsistent state error with removes
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 17, 2021
1 parent bb4ce3c commit e2899b8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
10 changes: 8 additions & 2 deletions internal/chezmoi/sourcestate.go
Expand Up @@ -411,7 +411,10 @@ DESTABSPATH:
return nil
}
sourceRelPath := sourceStateEntry.SourceRelPath()
sourceRoot.Set(sourceRelPath.RelPath(), &SourceStateRemove{})
sourceRoot.Set(sourceRelPath.RelPath(), &SourceStateRemove{
sourceRelPath: sourceRelPath,
targetRelPath: targetRelPath,
})
update := sourceUpdate{
destAbsPath: s.destDirAbsPath.Join(targetRelPath),
entryState: &EntryState{
Expand Down Expand Up @@ -784,6 +787,7 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
for _, match := range matches {
targetRelPath := targetParentRelPath.JoinString(match)
sourceStateEntry := &SourceStateRemove{
sourceRelPath: sourceRelPath,
targetRelPath: targetRelPath,
}
allSourceStateEntries[targetRelPath] = append(allSourceStateEntries[targetRelPath], sourceStateEntry)
Expand Down Expand Up @@ -892,7 +896,8 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
continue
}

switch sourceStateDir, ok := sourceStateEntries[0].(*SourceStateDir); {
sourceStateDir, ok := sourceStateEntries[0].(*SourceStateDir)
switch {
case !ok:
continue
case !sourceStateDir.Attr.Exact:
Expand All @@ -914,6 +919,7 @@ func (s *SourceState) Read(ctx context.Context, options *ReadOptions) error {
continue
}
allSourceStateEntries[destEntryRelPath] = append(allSourceStateEntries[destEntryRelPath], &SourceStateRemove{
sourceRelPath: sourceStateDir.sourceRelPath,
targetRelPath: destEntryRelPath,
})
}
Expand Down
3 changes: 3 additions & 0 deletions internal/chezmoi/sourcestate_test.go
Expand Up @@ -1141,6 +1141,7 @@ func TestSourceStateRead(t *testing.T) {
},
},
NewRelPath("dir/file2"): &SourceStateRemove{
sourceRelPath: NewSourceRelDirPath("exact_dir"),
targetRelPath: NewRelPath("dir/file2"),
},
}),
Expand All @@ -1162,6 +1163,7 @@ func TestSourceStateRead(t *testing.T) {
expectedSourceState: NewSourceState(
withEntries(map[RelPath]SourceStateEntry{
NewRelPath("file"): &SourceStateRemove{
sourceRelPath: NewSourceRelPath(".chezmoiremove"),
targetRelPath: NewRelPath("file"),
},
}),
Expand All @@ -1182,6 +1184,7 @@ func TestSourceStateRead(t *testing.T) {
expectedSourceState: NewSourceState(
withEntries(map[RelPath]SourceStateEntry{
NewRelPath("file1"): &SourceStateRemove{
sourceRelPath: NewSourceRelPath(".chezmoiremove"),
targetRelPath: NewRelPath("file1"),
},
}),
Expand Down
3 changes: 2 additions & 1 deletion internal/chezmoi/sourcestateentry.go
Expand Up @@ -39,6 +39,7 @@ type SourceStateFile struct {

// A SourceStateRemove represents that an entry should be removed.
type SourceStateRemove struct {
sourceRelPath SourceRelPath
targetRelPath RelPath
}

Expand Down Expand Up @@ -146,7 +147,7 @@ func (s *SourceStateRemove) Order() ScriptOrder {

// Origin returns s's origin.
func (s *SourceStateRemove) Origin() string {
return ""
return s.sourceRelPath.String()
}

// SourceRelPath returns s's source relative path.
Expand Down
23 changes: 23 additions & 0 deletions internal/cmd/testdata/scripts/edgecases.txt
Expand Up @@ -35,7 +35,30 @@ mkdir $HOME/.dir
chezmoi add $HOME${/}.dir/
! exists $CHEZMOISOURCEDIR/dot_dir/dot_dir

chhome home3/user

# test that chezmoi reports an inconsistent state error when a file should be both removed and present
! chezmoi apply
stderr 'chezmoi: \.file: inconsistent state \(\.chezmoiremove, dot_file\)'

skip 'FIXME make the following test pass'
chhome home4/user

# test that chezmoi reports an inconsistent state error when a file should be both removed and present, even if the file is not already present
! chezmoi apply
stderr 'chezmoi: \.file: inconsistent state \(\.chezmoiremove, dot_file\)'

-- golden/.file --
# contents of .file
-- home2/user/.local/share/chezmoi/dot_file --
# contents of .file
-- home3/user/.file --
# contents of .file
-- home3/user/.local/share/chezmoi/.chezmoiremove --
.file
-- home3/user/.local/share/chezmoi/dot_file --
# contents of .file
-- home4/user/.local/share/chezmoi/.chezmoiremove --
.file
-- home4/user/.local/share/chezmoi/dot_file --
# contents of .file

0 comments on commit e2899b8

Please sign in to comment.