Skip to content

Commit

Permalink
fix: Respect setting diff.pager to the empty string as disabling the …
Browse files Browse the repository at this point in the history
…pager
  • Loading branch information
twpayne committed Jul 28, 2023
1 parent abd9e77 commit cf70d35
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
22 changes: 17 additions & 5 deletions pkg/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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),
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/diffcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
44 changes: 44 additions & 0 deletions pkg/cmd/testdata/scripts/issue3113.txtar
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cf70d35

Please sign in to comment.