Skip to content

Commit a1caca6

Browse files
committed
Don't add files if template would be overwritten
1 parent 035e416 commit a1caca6

File tree

6 files changed

+34
-0
lines changed

6 files changed

+34
-0
lines changed

cmd/add.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var addCmd = &cobra.Command{
2424
}
2525

2626
type addCmdConfig struct {
27+
force bool
2728
recursive bool
2829
prompt bool
2930
options chezmoi.AddOptions
@@ -35,6 +36,7 @@ func init() {
3536
persistentFlags := addCmd.PersistentFlags()
3637
persistentFlags.BoolVarP(&config.add.options.Empty, "empty", "e", false, "add empty files")
3738
persistentFlags.BoolVar(&config.add.options.Encrypt, "encrypt", false, "encrypt files")
39+
persistentFlags.BoolVarP(&config.add.force, "force", "f", false, "overwrite source state, even if template would be lost")
3840
persistentFlags.BoolVarP(&config.add.options.Exact, "exact", "x", false, "add directories exactly")
3941
persistentFlags.BoolVarP(&config.add.prompt, "prompt", "p", false, "prompt before adding")
4042
persistentFlags.BoolVarP(&config.add.recursive, "recursive", "r", false, "recurse in to subdirectories")
@@ -75,6 +77,16 @@ func (c *Config) runAddCmd(cmd *cobra.Command, args []string) (err error) {
7577
fmt.Fprintf(c.Stderr(), "warning: skipping file ignored by .chezmoiignore: %s\n", path)
7678
return nil
7779
}
80+
if !c.add.force {
81+
entry, err := ts.Get(c.fs, path)
82+
if err != nil && !os.IsNotExist(err) {
83+
return err
84+
}
85+
if file, ok := entry.(*chezmoi.File); ok && file.Template {
86+
fmt.Fprintf(c.Stderr(), "warning: skipping file generated by template, use --force to force: %s\n", path)
87+
return nil
88+
}
89+
}
7890
if c.add.prompt {
7991
choice, err := c.prompt(fmt.Sprintf("Add %s", path), "ynqa")
8092
if err != nil {
@@ -99,6 +111,16 @@ func (c *Config) runAddCmd(cmd *cobra.Command, args []string) (err error) {
99111
fmt.Fprintf(c.Stderr(), "warning: skipping file ignored by .chezmoiignore: %s\n", path)
100112
continue
101113
}
114+
if !c.add.force {
115+
entry, err := ts.Get(c.fs, path)
116+
if err != nil && !os.IsNotExist(err) {
117+
return err
118+
}
119+
if file, ok := entry.(*chezmoi.File); ok && file.Template {
120+
fmt.Fprintf(c.Stderr(), "warning: skipping file generated by template, use --force to force: %s\n", path)
121+
continue
122+
}
123+
}
102124
if c.add.prompt {
103125
choice, err := c.prompt(fmt.Sprintf("Add %s", path), "ynqa")
104126
if err != nil {

cmd/helps.gen.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

completions/chezmoi-completion.bash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ _chezmoi_add()
277277
flags+=("--encrypt")
278278
flags+=("--exact")
279279
flags+=("-x")
280+
flags+=("--force")
281+
flags+=("-f")
280282
flags+=("--prompt")
281283
flags+=("-p")
282284
flags+=("--recursive")

completions/chezmoi.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path add' -s a -l aut
7474
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path add' -s e -l empty -d 'add empty files'
7575
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path add' -l encrypt -d 'encrypt files'
7676
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path add' -s x -l exact -d 'add directories exactly'
77+
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path add' -s f -l force -d 'overwrite source state, even if template would be lost'
7778
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path add' -s p -l prompt -d 'prompt before adding'
7879
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path add' -s r -l recursive -d 'recurse in to subdirectories'
7980
complete -c chezmoi -f -n '__fish_chezmoi_seen_subcommand_path add' -s T -l template -d 'add files as templates'

completions/chezmoi.zsh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ function _chezmoi_add {
143143
'(-e --empty)'{-e,--empty}'[add empty files]' \
144144
'--encrypt[encrypt files]' \
145145
'(-x --exact)'{-x,--exact}'[add directories exactly]' \
146+
'(-f --force)'{-f,--force}'[overwrite source state, even if template would be lost]' \
146147
'(-p --prompt)'{-p,--prompt}'[prompt before adding]' \
147148
'(-r --recursive)'{-r,--recursive}'[recurse in to subdirectories]' \
148149
'(-T --template)'{-T,--template}'[add files as templates]' \

docs/REFERENCE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ directory. The `add` command accepts additional flags:
322322

323323
Set the `empty` attribute on added files.
324324

325+
#### `-f`, `--force`
326+
327+
Add *targets*, even if doing so would cause a source template to be overwritten.
328+
325329
#### `-x`, `--exact`
326330

327331
Set the `exact` attribute on added directories.

0 commit comments

Comments
 (0)