From ecf5b2a505bf8ac694af918fb6bf2356c739b692 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Sun, 19 Jun 2022 21:15:48 +0000 Subject: [PATCH 1/2] feat: Make add command add empty files, remove --empty flag --- assets/chezmoi.io/docs/reference/commands/add.md | 4 ---- pkg/chezmoi/sourcestate.go | 8 +++----- pkg/chezmoi/sourcestate_test.go | 1 - pkg/cmd/addcmd.go | 3 --- pkg/cmd/addcmd_test.go | 14 -------------- pkg/cmd/readdcmd.go | 1 - pkg/cmd/testdata/scripts/add.txt | 8 ++------ 7 files changed, 5 insertions(+), 34 deletions(-) diff --git a/assets/chezmoi.io/docs/reference/commands/add.md b/assets/chezmoi.io/docs/reference/commands/add.md index a6f65088ae8..06c569dcaaa 100644 --- a/assets/chezmoi.io/docs/reference/commands/add.md +++ b/assets/chezmoi.io/docs/reference/commands/add.md @@ -12,10 +12,6 @@ names as a template string. Longer substitutions occur before shorter ones. This implies the `--template` option. ``` -## `-e`, `--empty` - -Set the `empty` attribute on added files. - ## `--encrypt` Encrypt files using the defined encryption method. diff --git a/pkg/chezmoi/sourcestate.go b/pkg/chezmoi/sourcestate.go index 7767458e0ba..b99829b629c 100644 --- a/pkg/chezmoi/sourcestate.go +++ b/pkg/chezmoi/sourcestate.go @@ -269,7 +269,6 @@ type ReplaceFunc func(targetRelPath RelPath, newSourceStateEntry, oldSourceState type AddOptions struct { AutoTemplate bool // Automatically create templates, if possible. Create bool // Add create_ entries instead of normal entries. - Empty bool // Add the empty_ attribute to added files. Encrypt bool // Encrypt files. EncryptedSuffix string // Suffix for encrypted files. Exact bool // Add the exact_ attribute to added directories. @@ -1731,7 +1730,6 @@ func (s *SourceState) newSourceStateFileEntryFromFile( ) (SourceStateEntry, error) { fileAttr := FileAttr{ TargetName: fileInfo.Name(), - Empty: options.Empty, Encrypted: options.Encrypt, Executable: isExecutable(fileInfo), Private: isPrivate(fileInfo), @@ -1754,8 +1752,8 @@ func (s *SourceState) newSourceStateFileEntryFromFile( fileAttr.Template = true } } - if len(contents) == 0 && !options.Empty { - return nil, nil + if len(contents) == 0 { + fileAttr.Empty = true } if options.Encrypt { contents, err = s.encryption.Encrypt(contents) @@ -1772,7 +1770,7 @@ func (s *SourceState) newSourceStateFileEntryFromFile( lazyContents: lazyContents, targetStateEntry: &TargetStateFile{ lazyContents: lazyContents, - empty: options.Empty, + empty: len(contents) == 0, perm: 0o666 &^ s.umask, }, }, nil diff --git a/pkg/chezmoi/sourcestate_test.go b/pkg/chezmoi/sourcestate_test.go index 84fea56a6e6..12d03ed14dd 100644 --- a/pkg/chezmoi/sourcestate_test.go +++ b/pkg/chezmoi/sourcestate_test.go @@ -223,7 +223,6 @@ func TestSourceStateAdd(t *testing.T) { NewAbsPath("/home/user/.empty"), }, addOptions: AddOptions{ - Empty: true, Include: NewEntryTypeSet(EntryTypesAll), }, tests: []interface{}{ diff --git a/pkg/cmd/addcmd.go b/pkg/cmd/addcmd.go index 6665dfab659..e3c2c2bb3ff 100644 --- a/pkg/cmd/addcmd.go +++ b/pkg/cmd/addcmd.go @@ -12,7 +12,6 @@ type addCmdConfig struct { TemplateSymlinks bool `mapstructure:"templateSymlinks"` autoTemplate bool create bool - empty bool encrypt bool exact bool exclude *chezmoi.EntryTypeSet @@ -43,7 +42,6 @@ func (c *Config) newAddCmd() *cobra.Command { flags := addCmd.Flags() flags.BoolVarP(&c.Add.autoTemplate, "autotemplate", "a", c.Add.autoTemplate, "Generate the template when adding files as templates") //nolint:lll flags.BoolVar(&c.Add.create, "create", c.Add.create, "Add files that should exist, irrespective of their contents") - flags.BoolVarP(&c.Add.empty, "empty", "e", c.Add.empty, "Add empty files") flags.BoolVar(&c.Add.encrypt, "encrypt", c.Add.encrypt, "Encrypt files") flags.BoolVar(&c.Add.exact, "exact", c.Add.exact, "Add directories exactly") flags.VarP(c.Add.exclude, "exclude", "x", "Exclude entry types") @@ -144,7 +142,6 @@ func (c *Config) runAddCmd(cmd *cobra.Command, args []string, sourceState *chezm return sourceState.Add(c.sourceSystem, c.persistentState, c.destSystem, destAbsPathInfos, &chezmoi.AddOptions{ AutoTemplate: c.Add.autoTemplate, Create: c.Add.create, - Empty: c.Add.empty, Encrypt: c.Add.encrypt, EncryptedSuffix: c.encryption.EncryptedSuffix(), Exact: c.Add.exact, diff --git a/pkg/cmd/addcmd_test.go b/pkg/cmd/addcmd_test.go index e76d3d7f89a..10055312e11 100644 --- a/pkg/cmd/addcmd_test.go +++ b/pkg/cmd/addcmd_test.go @@ -156,20 +156,6 @@ func TestAddCmd(t *testing.T) { }, }, args: []string{"~/.empty"}, - tests: []interface{}{ - vfst.TestPath("/home/user/.local/share/chezmoi/empty_dot_empty", - vfst.TestDoesNotExist, - ), - }, - }, - { - name: "empty_with_--empty", - root: map[string]interface{}{ - "/home/user": map[string]interface{}{ - ".empty": "", - }, - }, - args: []string{"--empty", "~/.empty"}, tests: []interface{}{ vfst.TestPath("/home/user/.local/share/chezmoi/empty_dot_empty", vfst.TestModeIsRegular, diff --git a/pkg/cmd/readdcmd.go b/pkg/cmd/readdcmd.go index d7ac2daafab..e78f95a2ea1 100644 --- a/pkg/cmd/readdcmd.go +++ b/pkg/cmd/readdcmd.go @@ -96,7 +96,6 @@ func (c *Config) runReAddCmd(cmd *cobra.Command, args []string, sourceState *che destAbsPath: destAbsPathInfo, } if err := sourceState.Add(c.sourceSystem, c.persistentState, c.destSystem, destAbsPathInfos, &chezmoi.AddOptions{ - Empty: sourceStateFile.Attr.Empty, Encrypt: sourceStateFile.Attr.Encrypted, EncryptedSuffix: c.encryption.EncryptedSuffix(), Include: c.reAdd.include.Sub(c.reAdd.exclude), diff --git a/pkg/cmd/testdata/scripts/add.txt b/pkg/cmd/testdata/scripts/add.txt index 334f4c42072..69040b5f298 100644 --- a/pkg/cmd/testdata/scripts/add.txt +++ b/pkg/cmd/testdata/scripts/add.txt @@ -17,13 +17,9 @@ cmp $CHEZMOISOURCEDIR/dot_dir/file golden/dot_dir/file chezmoi add --exact $HOME${/}.dir/subdir cmp $CHEZMOISOURCEDIR/dot_dir/exact_subdir/file golden/dot_dir/exact_subdir/file -# test adding an empty file without --empty +# test adding an empty file chezmoi add $HOME${/}.empty -! exists $CHEZMOISOURCEDIR/dot_empty - -# test adding an empty file with --empty -chezmoi add --empty $HOME${/}.empty -cmp $CHEZMOISOURCEDIR/empty_dot_empty golden/empty_dot_empty +exists $CHEZMOISOURCEDIR/empty_dot_empty # test adding an executable file chezmoi add $HOME${/}.executable From ea32611299d2d650e4bcd9f78e8ab38dce66a514 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Sun, 19 Jun 2022 21:18:31 +0000 Subject: [PATCH 2/2] chore: Remove stray markers --- assets/chezmoi.io/docs/reference/commands/add.md | 1 - 1 file changed, 1 deletion(-) diff --git a/assets/chezmoi.io/docs/reference/commands/add.md b/assets/chezmoi.io/docs/reference/commands/add.md index 06c569dcaaa..f2459dfbd52 100644 --- a/assets/chezmoi.io/docs/reference/commands/add.md +++ b/assets/chezmoi.io/docs/reference/commands/add.md @@ -10,7 +10,6 @@ Automatically generate a template by replacing strings that match variable values from the `data` section of the config file with their respective config names as a template string. Longer substitutions occur before shorter ones. This implies the `--template` option. -``` ## `--encrypt`