diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-28 09:14:32 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-09-29 18:21:59 +1000 |
commit | b8da166ab1c66671d681bc2454b911993716de58 (patch) | |
tree | 6c85b73b530d2771c901024dbd1cc762ccbfa208 /pkg/commands | |
parent | ca437a65046d5af0c90c337b125af4538e625502 (diff) |
support discarding submodule changes
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/file.go | 15 | ||||
-rw-r--r-- | pkg/commands/git.go | 42 | ||||
-rw-r--r-- | pkg/commands/submodule_config.go | 7 | ||||
-rw-r--r-- | pkg/commands/submodules.go | 66 |
4 files changed, 89 insertions, 41 deletions
diff --git a/pkg/commands/file.go b/pkg/commands/file.go index 750b4e4f8..f030b4e67 100644 --- a/pkg/commands/file.go +++ b/pkg/commands/file.go @@ -19,7 +19,6 @@ type File struct { DisplayString string Type string // one of 'file', 'directory', and 'other' ShortStatus string // e.g. 'AD', ' A', 'M ', '??' - IsSubmodule bool } const RENAME_SEPARATOR = " -> " @@ -45,3 +44,17 @@ func (f *File) ID() string { func (f *File) Description() string { return f.Name } + +func (f *File) IsSubmodule(configs []*SubmoduleConfig) bool { + return f.SubmoduleConfig(configs) != nil +} + +func (f *File) SubmoduleConfig(configs []*SubmoduleConfig) *SubmoduleConfig { + for _, config := range configs { + if f.Name == config.Name { + return config + } + } + + return nil +} diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 465552b48..90fbc9890 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -1,7 +1,6 @@ package commands import ( - "bufio" "fmt" "io/ioutil" "os" @@ -272,32 +271,6 @@ func (c *GitCommand) GetConfigValue(key string) string { return strings.TrimSpace(output) } -func (c *GitCommand) GetSubmoduleNames() ([]string, error) { - file, err := os.Open(".gitmodules") - if err != nil { - if err == os.ErrNotExist { - return nil, nil - } - return nil, err - } - - submoduleNames := []string{} - - scanner := bufio.NewScanner(file) - scanner.Split(bufio.ScanLines) - for scanner.Scan() { - line := scanner.Text() - re := regexp.MustCompile(`\[submodule "(.*)"\]`) - matches := re.FindStringSubmatch(line) - - if len(matches) > 0 { - submoduleNames = append(submoduleNames, matches[1]) - } - } - - return submoduleNames, nil -} - func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*File { // check if config wants us ignoring untracked files untrackedFilesSetting := c.GetConfigValue("status.showUntrackedFiles") @@ -314,11 +287,6 @@ func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*File { statusStrings := utils.SplitLines(statusOutput) files := []*File{} - submoduleNames, err := c.GetSubmoduleNames() - if err != nil { - c.Log.Error(err) - } - for _, statusString := range statusStrings { if strings.HasPrefix(statusString, "warning") { c.Log.Warningf("warning when calling git status: %s", statusString) @@ -332,7 +300,6 @@ func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*File { hasNoStagedChanges := utils.IncludesString([]string{" ", "U", "?"}, stagedChange) hasMergeConflicts := utils.IncludesString([]string{"DD", "AA", "UU", "AU", "UA", "UD", "DU"}, change) hasInlineMergeConflicts := utils.IncludesString([]string{"UU", "AA"}, change) - isSubmodule := utils.IncludesString(submoduleNames, filename) file := &File{ Name: filename, @@ -345,7 +312,6 @@ func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*File { HasInlineMergeConflicts: hasInlineMergeConflicts, Type: c.OSCommand.FileType(filename), ShortStatus: change, - IsSubmodule: isSubmodule, } files = append(files, file) } @@ -752,11 +718,7 @@ func (c *GitCommand) DiscardAllFileChanges(file *File) error { // if the file isn't tracked, we assume you want to delete it quotedFileName := c.OSCommand.Quote(file.Name) - if file.IsSubmodule { - if err := c.OSCommand.RunCommand(fmt.Sprintf("git submodule update --checkout --force --init %s", quotedFileName)); err != nil { - return err - } - } else if file.HasStagedChanges || file.HasMergeConflicts { + if file.HasStagedChanges || file.HasMergeConflicts { if err := c.OSCommand.RunCommand("git reset -- %s", quotedFileName); err != nil { return err } @@ -816,7 +778,7 @@ func (c *GitCommand) ShowCmdStr(sha string, filterPath string) string { if filterPath != "" { filterPathArg = fmt.Sprintf(" -- %s", c.OSCommand.Quote(filterPath)) } - return fmt.Sprintf("git show --color=%s --no-renames --stat -p %s %s", c.colorArg(), sha, filterPathArg) + return fmt.Sprintf("git show --submodule --color=%s --no-renames --stat -p %s %s", c.colorArg(), sha, filterPathArg) } func (c *GitCommand) GetBranchGraphCmdStr(branchName string) string { diff --git a/pkg/commands/submodule_config.go b/pkg/commands/submodule_config.go new file mode 100644 index 000000000..e9e7ad22b --- /dev/null +++ b/pkg/commands/submodule_config.go @@ -0,0 +1,7 @@ +package commands + +type SubmoduleConfig struct { + Name string + Path string + Url string +} diff --git a/pkg/commands/submodules.go b/pkg/commands/submodules.go new file mode 100644 index 000000000..ad130544c --- /dev/null +++ b/pkg/commands/submodules.go @@ -0,0 +1,66 @@ +package commands + +import ( + "bufio" + "os" + "regexp" +) + +// .gitmodules looks like this: +// [submodule "mysubmodule"] +// path = blah/mysubmodule +// url = git@github.com:subbo.git + +func (c *GitCommand) GetSubmoduleConfigs() ([]*SubmoduleConfig, error) { + file, err := os.Open(".gitmodules") + if err != nil { + if os.IsNotExist(err) { + return nil, nil + } + return nil, err + } + + scanner := bufio.NewScanner(file) + scanner.Split(bufio.ScanLines) + + firstMatch := func(str string, regex string) (string, bool) { + re := regexp.MustCompile(regex) + matches := re.FindStringSubmatch(str) + + if len(matches) > 0 { + return matches[1], true + } else { + return "", false + } + } + + configs := []*SubmoduleConfig{} + for scanner.Scan() { + line := scanner.Text() + + if name, ok := firstMatch(line, `\[submodule "(.*)"\]`); ok { + configs = append(configs, &SubmoduleConfig{Name: name}) + continue + } + + if len(configs) > 0 { + lastConfig := configs[len(configs)-1] + + if path, ok := firstMatch(line, `\s*path\s*=\s*(.*)\s*`); ok { + lastConfig.Path = path + } else if url, ok := firstMatch(line, `\s*url\s*=\s*(.*)\s*`); ok { + lastConfig.Url = url + } + } + } + + return configs, nil +} + +func (c *GitCommand) SubmoduleStash(config *SubmoduleConfig) error { + return c.OSCommand.RunCommand("git -C %s stash --include-untracked", config.Path) +} + +func (c *GitCommand) SubmoduleReset(config *SubmoduleConfig) error { + return c.OSCommand.RunCommand("git submodule update --force %s", config.Name) +} |