-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/twpayne/chezmoi/v2/internal/chezmoi" | ||
) | ||
|
||
type mergeAllCmdConfig struct { | ||
init bool | ||
recursive bool | ||
} | ||
|
||
func (c *Config) newMergeAllCmd() *cobra.Command { | ||
mergeAllCmd := &cobra.Command{ | ||
Use: "merge-all", | ||
Short: "Perform a three-way merge for each modified file", | ||
Long: mustLongHelp("merge-all"), | ||
Example: example("merge-all"), | ||
RunE: c.makeRunEWithSourceState(c.runMergeAllCmd), | ||
Annotations: map[string]string{ | ||
modifiesSourceDirectory: "true", | ||
requiresSourceDirectory: "true", | ||
}, | ||
} | ||
|
||
flags := mergeAllCmd.Flags() | ||
flags.BoolVar(&c.mergeAll.init, "init", c.mergeAll.init, "Recreate config file from template") | ||
flags.BoolVarP(&c.mergeAll.recursive, "recursive", "r", c.mergeAll.recursive, "Recurse into subdirectories") | ||
|
||
return mergeAllCmd | ||
} | ||
|
||
func (c *Config) runMergeAllCmd(cmd *cobra.Command, args []string, sourceState *chezmoi.SourceState) error { | ||
var targetRelPaths []chezmoi.RelPath | ||
dryRunSystem := chezmoi.NewDryRunSystem(c.destSystem) | ||
preApplyFunc := func(targetRelPath chezmoi.RelPath, targetEntryState, lastWrittenEntryState, actualEntryState *chezmoi.EntryState) error { | ||
if !targetEntryState.Equivalent(actualEntryState) { | ||
targetRelPaths = append(targetRelPaths, targetRelPath) | ||
} | ||
return chezmoi.Skip | ||
} | ||
if err := c.applyArgs(cmd.Context(), dryRunSystem, c.DestDirAbsPath, args, applyArgsOptions{ | ||
include: chezmoi.NewEntryTypeSet(chezmoi.EntryTypeFiles), | ||
init: c.mergeAll.init, | ||
recursive: c.mergeAll.recursive, | ||
umask: c.Umask, | ||
preApplyFunc: preApplyFunc, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
for _, targetRelPath := range targetRelPaths { | ||
sourceStateEntry := sourceState.MustEntry(targetRelPath) | ||
if err := c.doMerge(targetRelPath, sourceStateEntry); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[windows] skip 'UNIX only' | ||
|
||
# test that chezmoi merge-all does not run the merge command if nothing is modified | ||
chezmoi merge-all | ||
! stdout . | ||
|
||
# test that chezmoi merge-all runs the merge command if a file is modified | ||
edit $HOME/.file | ||
chezmoi merge-all | ||
stdout ^${HOME@R}/\.file\s+${CHEZMOISOURCEDIR@R}/dot_file\.tmpl\s+${WORK@R}/.*/\.file$ | ||
|
||
-- home/user/.file -- | ||
# contents of .file | ||
-- home/user/.config/chezmoi/chezmoi.toml -- | ||
[merge] | ||
command = "echo" | ||
-- home/user/.local/share/chezmoi/dot_file.tmpl -- | ||
{{ "# contents of .file" }} |