diff --git a/pkg/cmd/config.go b/pkg/cmd/config.go index 7a82735c8ec..4bc2c2e2c03 100644 --- a/pkg/cmd/config.go +++ b/pkg/cmd/config.go @@ -54,6 +54,11 @@ import ( "github.com/twpayne/chezmoi/v2/pkg/shell" ) +// defaultSentinel is a string value used to indicate that the default value +// should be used. It is a string unlikely to be an actual value set by the +// user. +const defaultSentinel = "\x00" + const ( logComponentKey = "component" logComponentValueEncryption = "encryption" @@ -1129,7 +1134,7 @@ func (c *Config) diffFile( if err := unifiedEncoder.Encode(diffPatch); err != nil { return err } - return c.pageOutputString(builder.String(), c.Diff.Pager) + return c.pageDiffOutput(builder.String()) } // editor returns the path to the user's editor and any extra arguments. @@ -1725,10 +1730,16 @@ func (c *Config) persistentPostRunRootE(cmd *cobra.Command, args []string) error return nil } -// pageOutputString writes output using cmdPager as the pager command. -func (c *Config) pageOutputString(output, cmdPager string) error { - pager := firstNonEmptyString(cmdPager, c.Pager) - if c.noPager || pager == "" { +// pageDiffOutput pages the diff output to stdout. +func (c *Config) pageDiffOutput(output string) error { + pager := c.Diff.Pager + switch { + case c.noPager: + pager = "" + case pager == defaultSentinel: + pager = c.Pager // Use default pager. + } + if pager == "" { return c.writeOutputString(output) } @@ -2624,6 +2635,7 @@ func newConfigFile(bds *xdg.BaseDirectorySpecification) ConfigFile { }, Diff: diffCmdConfig{ Exclude: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesNone), + Pager: defaultSentinel, ScriptContents: true, include: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesAll), }, diff --git a/pkg/cmd/diffcmd.go b/pkg/cmd/diffcmd.go index 7c5c99bee00..8c04629a3db 100644 --- a/pkg/cmd/diffcmd.go +++ b/pkg/cmd/diffcmd.go @@ -80,7 +80,7 @@ func (c *Config) runDiffCmd(cmd *cobra.Command, args []string) (err error) { }); err != nil { return } - if err = c.pageOutputString(builder.String(), c.Diff.Pager); err != nil { + if err = c.pageDiffOutput(builder.String()); err != nil { return } if closer, ok := diffSystem.(interface { diff --git a/pkg/cmd/testdata/scripts/issue3113.txtar b/pkg/cmd/testdata/scripts/issue3113.txtar new file mode 100644 index 00000000000..0c563d07ce1 --- /dev/null +++ b/pkg/cmd/testdata/scripts/issue3113.txtar @@ -0,0 +1,44 @@ +[windows] skip 'UNIX only' + +chmod 777 bin/custom-pager +chmod 777 bin/default-pager + +# test that chezmoi diff uses the default pager by default +exec chezmoi diff +stdout default-pager + +chhome home2/user + +# test that setting diff.pager to a custom pager uses that pager +exec chezmoi diff +stdout custom-pager + +chhome home3/user + +# test that setting diff.pager to the empty string disables the pager +exec chezmoi diff +stdout diff + +-- bin/custom-pager -- +#!/bin/sh + +echo custom-pager +-- bin/default-pager -- +#!/bin/sh + +echo default-pager +-- home/user/.config/chezmoi/chezmoi.yaml -- +pager: default-pager +-- home/user/.local/share/chezmoi/dot_file -- +# contents of .file +-- home2/user/.config/chezmoi/chezmoi.yaml -- +diff: + pager: custom-pager +-- home2/user/.local/share/chezmoi/dot_file -- +# contents of .file +-- home3/user/.config/chezmoi/chezmoi.yaml -- +pager: default-pager +diff: + pager: '' +-- home3/user/.local/share/chezmoi/dot_file -- +# contents of .file