Skip to content

Commit

Permalink
fix: Fix panic when merging unmanaged file
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Feb 22, 2024
1 parent 8262e43 commit 1abcfa5
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 29 deletions.
4 changes: 1 addition & 3 deletions internal/cmd/catcmd.go
Expand Up @@ -27,9 +27,7 @@ func (c *Config) newCatCmd() *cobra.Command {
}

func (c *Config) runCatCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
targetRelPaths, err := c.targetRelPaths(sourceState, args, targetRelPathsOptions{
mustBeManaged: true,
})
targetRelPaths, err := c.targetRelPaths(sourceState, args, nil)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/chattrcmd.go
Expand Up @@ -150,9 +150,8 @@ func (c *Config) runChattrCmd(cmd *cobra.Command, args []string, sourceState *ch
return err
}

targetRelPaths, err := c.targetRelPaths(sourceState, args[1:], targetRelPathsOptions{
mustBeManaged: true,
recursive: c.chattr.recursive,
targetRelPaths, err := c.targetRelPaths(sourceState, args[1:], &targetRelPathsOptions{
recursive: c.chattr.recursive,
})
if err != nil {
return err
Expand Down
17 changes: 7 additions & 10 deletions internal/cmd/config.go
Expand Up @@ -631,9 +631,8 @@ func (c *Config) applyArgs(
return err
}
default:
targetRelPaths, err = c.targetRelPaths(sourceState, args, targetRelPathsOptions{
mustBeManaged: true,
recursive: options.recursive,
targetRelPaths, err = c.targetRelPaths(sourceState, args, &targetRelPathsOptions{
recursive: options.recursive,
})
if err != nil {
return err
Expand Down Expand Up @@ -2437,9 +2436,8 @@ func (c *Config) setEnvironmentVariables() error {
// sourceAbsPaths returns the source absolute paths for each target path in
// args.
func (c *Config) sourceAbsPaths(sourceState *chezmoi.SourceState, args []string) ([]chezmoi.AbsPath, error) {
targetRelPaths, err := c.targetRelPaths(sourceState, args, targetRelPathsOptions{
targetRelPaths, err := c.targetRelPaths(sourceState, args, &targetRelPathsOptions{
mustBeInSourceState: true,
mustBeManaged: true,
})
if err != nil {
return nil, err
Expand All @@ -2462,7 +2460,6 @@ func (c *Config) targetRelPath(absPath chezmoi.AbsPath) (chezmoi.RelPath, error)

type targetRelPathsOptions struct {
mustBeInSourceState bool
mustBeManaged bool
recursive bool
}

Expand All @@ -2471,7 +2468,7 @@ type targetRelPathsOptions struct {
func (c *Config) targetRelPaths(
sourceState *chezmoi.SourceState,
args []string,
options targetRelPathsOptions,
options *targetRelPathsOptions,
) (chezmoi.RelPaths, error) {
targetRelPaths := make(chezmoi.RelPaths, 0, len(args))
for _, arg := range args {
Expand All @@ -2484,16 +2481,16 @@ func (c *Config) targetRelPaths(
return nil, err
}
sourceStateEntry := sourceState.Get(targetRelPath)
if options.mustBeManaged && sourceStateEntry == nil {
if sourceStateEntry == nil {
return nil, fmt.Errorf("%s: not managed", arg)
}
if options.mustBeInSourceState {
if options != nil && options.mustBeInSourceState {
if _, ok := sourceStateEntry.(*chezmoi.SourceStateRemove); ok {
return nil, fmt.Errorf("%s: not in source state", arg)
}
}
targetRelPaths = append(targetRelPaths, targetRelPath)
if options.recursive {
if options != nil && options.recursive {
parentRelPath := targetRelPath
// FIXME we should not call s.TargetRelPaths() here - risk of
// accidentally quadratic
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/editcmd.go
Expand Up @@ -76,9 +76,8 @@ func (c *Config) runEditCmd(cmd *cobra.Command, args []string) error {
return err
}

targetRelPaths, err := c.targetRelPaths(sourceState, args, targetRelPathsOptions{
targetRelPaths, err := c.targetRelPaths(sourceState, args, &targetRelPathsOptions{
mustBeInSourceState: true,
mustBeManaged: true,
})
if err != nil {
return err
Expand Down
4 changes: 1 addition & 3 deletions internal/cmd/forgetcmd.go
Expand Up @@ -28,9 +28,7 @@ func (c *Config) newForgetCmd() *cobra.Command {
}

func (c *Config) runForgetCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
targetRelPaths, err := c.targetRelPaths(sourceState, args, targetRelPathsOptions{
mustBeManaged: true,
})
targetRelPaths, err := c.targetRelPaths(sourceState, args, nil)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/mergecmd.go
Expand Up @@ -37,9 +37,8 @@ func (c *Config) newMergeCmd() *cobra.Command {
}

func (c *Config) runMergeCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
targetRelPaths, err := c.targetRelPaths(sourceState, args, targetRelPathsOptions{
targetRelPaths, err := c.targetRelPaths(sourceState, args, &targetRelPathsOptions{
mustBeInSourceState: true,
mustBeManaged: false,
recursive: true,
})
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/readdcmd.go
Expand Up @@ -52,9 +52,8 @@ func (c *Config) runReAddCmd(cmd *cobra.Command, args []string, sourceState *che
)
} else {
var err error
targetRelPaths, err = c.targetRelPaths(sourceState, args, targetRelPathsOptions{
mustBeManaged: true,
recursive: c.reAdd.recursive,
targetRelPaths, err = c.targetRelPaths(sourceState, args, &targetRelPathsOptions{
recursive: c.reAdd.recursive,
})
if err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/removecmd.go
Expand Up @@ -37,9 +37,8 @@ func (c *Config) newRemoveCmd() *cobra.Command {
}

func (c *Config) runRemoveCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error {
targetRelPaths, err := c.targetRelPaths(sourceState, args, targetRelPathsOptions{
mustBeManaged: true,
recursive: c.remove.recursive,
targetRelPaths, err := c.targetRelPaths(sourceState, args, &targetRelPathsOptions{
recursive: c.remove.recursive,
})
if err != nil {
return err
Expand Down

0 comments on commit 1abcfa5

Please sign in to comment.