/
updatecmd.go
104 lines (94 loc) · 3.07 KB
/
updatecmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cmd
import (
"errors"
"github.com/go-git/go-git/v5"
"github.com/spf13/cobra"
"github.com/twpayne/chezmoi/v2/internal/chezmoi"
)
type updateCmdConfig struct {
Command string `json:"command" mapstructure:"command" yaml:"command"`
Args []string `json:"args" mapstructure:"args" yaml:"args"`
Apply bool `json:"apply" mapstructure:"apply" yaml:"apply"`
RecurseSubmodules bool `json:"recurseSubmodules" mapstructure:"recurseSubmodules" yaml:"recurseSubmodules"`
filter *chezmoi.EntryTypeFilter
init bool
recursive bool
}
func (c *Config) newUpdateCmd() *cobra.Command {
updateCmd := &cobra.Command{
Use: "update",
Short: "Pull and apply any changes",
Long: mustLongHelp("update"),
Example: example("update"),
Args: cobra.NoArgs,
RunE: c.runUpdateCmd,
Annotations: newAnnotations(
modifiesDestinationDirectory,
persistentStateModeReadWrite,
requiresSourceDirectory,
requiresWorkingTree,
runsCommands,
),
}
updateCmd.Flags().BoolVarP(&c.Update.Apply, "apply", "a", c.Update.Apply, "Apply after pulling")
updateCmd.Flags().VarP(c.Update.filter.Exclude, "exclude", "x", "Exclude entry types")
updateCmd.Flags().VarP(c.Update.filter.Include, "include", "i", "Include entry types")
updateCmd.Flags().BoolVar(&c.Update.init, "init", c.Update.init, "Recreate config file from template")
updateCmd.Flags().
BoolVar(&c.Update.RecurseSubmodules, "recurse-submodules", c.Update.RecurseSubmodules, "Recursively update submodules")
updateCmd.Flags().BoolVarP(&c.Update.recursive, "recursive", "r", c.Update.recursive, "Recurse into subdirectories")
return updateCmd
}
func (c *Config) runUpdateCmd(cmd *cobra.Command, args []string) error {
switch {
case c.Update.Command != "":
if err := c.run(c.WorkingTreeAbsPath, c.Update.Command, c.Update.Args); err != nil {
return err
}
case c.UseBuiltinGit.Value(c.useBuiltinGitAutoFunc):
rawWorkingTreeAbsPath, err := c.baseSystem.RawPath(c.WorkingTreeAbsPath)
if err != nil {
return err
}
repo, err := git.PlainOpen(rawWorkingTreeAbsPath.String())
if err != nil {
return err
}
wt, err := repo.Worktree()
if err != nil {
return err
}
if err := wt.Pull(&git.PullOptions{
RecurseSubmodules: git.DefaultSubmoduleRecursionDepth,
}); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return err
}
default:
gitArgs := []string{
"pull",
"--autostash",
"--rebase",
}
if c.Update.RecurseSubmodules {
gitArgs = append(gitArgs,
"--recurse-submodules",
)
}
if err := c.run(c.WorkingTreeAbsPath, c.Git.Command, gitArgs); err != nil {
return err
}
}
if c.Update.Apply {
if err := c.applyArgs(cmd.Context(), c.destSystem, c.DestDirAbsPath, args, applyArgsOptions{
cmd: cmd,
filter: c.Update.filter,
init: c.Update.init,
recursive: c.Update.recursive,
umask: c.Umask,
preApplyFunc: c.defaultPreApplyFunc,
}); err != nil {
return err
}
}
return nil
}