Skip to content

Commit

Permalink
Merge branch 'twpayne:master' into lookPathAt
Browse files Browse the repository at this point in the history
  • Loading branch information
arran4 committed Aug 3, 2023
2 parents d352cef + 1e8a649 commit a598a7c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
11 changes: 8 additions & 3 deletions assets/chezmoi.io/docs/reference/commands/re-add.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# `re-add`
# `re-add` [*target*...]

Re-add all modified files in the target state. chezmoi will not overwrite
templates, and all entries that are not files are ignored.
Re-add modified files in the target state, preserving any `encrypted_`
attributes. chezmoi will not overwrite templates, and all entries that are not
files are ignored.

If no *target*s are specified then all modified files are re-added. If one or
more *target*s are given then only those targets are re-added.

!!! hint

Expand All @@ -11,4 +15,5 @@ templates, and all entries that are not files are ignored.

```console
$ chezmoi re-add
$ chezmoi re-add ~/.bashrc
```
31 changes: 24 additions & 7 deletions internal/cmd/readdcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io/fs"
"path/filepath"
"sort"

"github.com/spf13/cobra"
Expand All @@ -22,7 +23,7 @@ func (c *Config) newReAddCmd() *cobra.Command {
Long: mustLongHelp("re-add"),
Example: example("re-add"),
ValidArgsFunction: c.targetValidArgs,
Args: cobra.NoArgs,
Args: cobra.ArbitraryArgs,
RunE: c.makeRunEWithSourceState(c.runReAddCmd),
Annotations: newAnnotations(
modifiesSourceDirectory,
Expand All @@ -47,13 +48,29 @@ func (c *Config) runReAddCmd(
) error {
var targetRelPaths chezmoi.RelPaths
sourceStateEntries := make(map[chezmoi.RelPath]chezmoi.SourceStateEntry)
_ = sourceState.ForEach(
func(targetRelPath chezmoi.RelPath, sourceStateEntry chezmoi.SourceStateEntry) error {
if len(args) == 0 {
_ = sourceState.ForEach(
func(targetRelPath chezmoi.RelPath, sourceStateEntry chezmoi.SourceStateEntry) error {
targetRelPaths = append(targetRelPaths, targetRelPath)
sourceStateEntries[targetRelPath] = sourceStateEntry
return nil
},
)
} else {
for _, arg := range args {
arg = filepath.Clean(arg)
destAbsPath, err := chezmoi.NewAbsPathFromExtPath(arg, c.homeDirAbsPath)
if err != nil {
return err
}
targetRelPath, err := c.targetRelPath(destAbsPath)
if err != nil {
return err
}
targetRelPaths = append(targetRelPaths, targetRelPath)
sourceStateEntries[targetRelPath] = sourceStateEntry
return nil
},
)
sourceStateEntries[targetRelPath] = sourceState.Get(targetRelPath)
}
}
sort.Sort(targetRelPaths)

TARGET_REL_PATH:
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/testdata/scripts/re-add.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,12 @@ grep '# edited' $CHEZMOISOURCEDIR/dot_dir/file
grep '# edited' $CHEZMOISOURCEDIR/dot_dir/exact_subdir/file
exec chezmoi diff
! stdout .

# test that chezmoi re-add adds only specified targets
edit $HOME/.file
edit $HOME/.dir/file
edit $HOME/.dir/subdir/file
exec chezmoi re-add ~/.dir/file
grep -count=1 '# edited' $CHEZMOISOURCEDIR/dot_file
grep -count=2 '# edited' $CHEZMOISOURCEDIR/dot_dir/file
grep -count=1 '# edited' $CHEZMOISOURCEDIR/dot_dir/exact_subdir/file

0 comments on commit a598a7c

Please sign in to comment.