@@ -24,6 +24,7 @@ var addCmd = &cobra.Command{
2424}
2525
2626type 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 {
0 commit comments