1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/commands/submodules.go

115 lines
3.1 KiB
Go
Raw Normal View History

2020-09-28 01:14:32 +02:00
package commands
import (
"bufio"
"os"
2020-09-30 01:06:11 +02:00
"path/filepath"
2020-09-28 01:14:32 +02:00
"regexp"
2020-09-29 10:45:00 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
2020-09-28 01:14:32 +02:00
)
// .gitmodules looks like this:
// [submodule "mysubmodule"]
// path = blah/mysubmodule
// url = git@github.com:subbo.git
2020-09-29 10:45:00 +02:00
func (c *GitCommand) GetSubmoduleConfigs() ([]*models.SubmoduleConfig, error) {
2020-09-28 01:14:32 +02:00
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
}
}
2020-09-29 10:45:00 +02:00
configs := []*models.SubmoduleConfig{}
2020-09-28 01:14:32 +02:00
for scanner.Scan() {
line := scanner.Text()
if name, ok := firstMatch(line, `\[submodule "(.*)"\]`); ok {
2020-09-29 10:45:00 +02:00
configs = append(configs, &models.SubmoduleConfig{Name: name})
2020-09-28 01:14:32 +02:00
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
}
2020-09-30 01:06:11 +02:00
func (c *GitCommand) SubmoduleStash(submodule *models.SubmoduleConfig) error {
2020-09-29 00:47:14 +02:00
// if the path does not exist then it hasn't yet been initialized so we'll swallow the error
// because the intention here is to have no dirty worktree state
2020-09-30 01:06:11 +02:00
if _, err := os.Stat(submodule.Path); os.IsNotExist(err) {
c.Log.Infof("submodule path %s does not exist, returning", submodule.Path)
2020-09-29 00:47:14 +02:00
return nil
}
2020-09-30 01:06:11 +02:00
return c.OSCommand.RunCommand("git -C %s stash --include-untracked", submodule.Path)
2020-09-28 01:14:32 +02:00
}
2020-09-30 01:06:11 +02:00
func (c *GitCommand) SubmoduleReset(submodule *models.SubmoduleConfig) error {
return c.OSCommand.RunCommand("git submodule update --init --force %s", submodule.Name)
2020-09-28 01:14:32 +02:00
}
2020-09-29 00:47:14 +02:00
func (c *GitCommand) SubmoduleUpdateAll() error {
2020-09-30 01:06:11 +02:00
// not doing an --init here because the user probably doesn't want that
2020-09-29 00:47:14 +02:00
return c.OSCommand.RunCommand("git submodule update --force")
}
2020-09-30 01:06:11 +02:00
func (c *GitCommand) SubmoduleDelete(submodule *models.SubmoduleConfig) error {
// based on https://gist.github.com/myusuf3/7f645819ded92bda6677
2020-09-30 13:12:03 +02:00
if err := c.OSCommand.RunCommand("git submodule deinit --force %s", submodule.Path); err != nil {
2020-09-30 01:06:11 +02:00
return err
}
2020-09-30 13:12:03 +02:00
if err := c.OSCommand.RunCommand("git rm --force %s", submodule.Path); err != nil {
2020-09-30 01:06:11 +02:00
return err
}
2020-09-30 13:12:03 +02:00
return os.RemoveAll(filepath.Join(c.DotGitDir, "modules", submodule.Path))
}
2020-09-30 14:05:34 +02:00
func (c *GitCommand) SubmoduleAdd(name string, path string, url string) error {
2020-09-30 13:12:03 +02:00
return c.OSCommand.RunCommand(
"git submodule add --force --name %s -- %s %s ",
c.OSCommand.Quote(name),
c.OSCommand.Quote(url),
c.OSCommand.Quote(path),
)
2020-09-30 01:06:11 +02:00
}
2020-09-30 14:05:34 +02:00
2020-09-30 23:19:53 +02:00
func (c *GitCommand) SubmoduleUpdateUrl(name string, path string, newUrl string) error {
// the set-url command is only for later git versions so we're doing it manually here
if err := c.OSCommand.RunCommand("git config --file .gitmodules submodule.%s.url %s", name, newUrl); err != nil {
return err
}
return c.OSCommand.RunCommand("git submodule sync -- %s", path)
2020-09-30 14:05:34 +02:00
}