1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-02 23:27:32 +02:00

allow adding a submodule

This commit is contained in:
Jesse Duffield 2020-09-30 21:12:03 +10:00
parent ea307c8d94
commit d4ab607d0d
7 changed files with 92 additions and 31 deletions

View File

@ -83,13 +83,22 @@ func (c *GitCommand) SubmoduleUpdateAll() error {
func (c *GitCommand) SubmoduleDelete(submodule *models.SubmoduleConfig) error { func (c *GitCommand) SubmoduleDelete(submodule *models.SubmoduleConfig) error {
// based on https://gist.github.com/myusuf3/7f645819ded92bda6677 // based on https://gist.github.com/myusuf3/7f645819ded92bda6677
if err := c.OSCommand.RunCommand("git submodule deinit %s", submodule.Name); err != nil { if err := c.OSCommand.RunCommand("git submodule deinit --force %s", submodule.Path); err != nil {
return err return err
} }
if err := c.OSCommand.RunCommand("git rm %s", submodule.Path); err != nil { if err := c.OSCommand.RunCommand("git rm --force %s", submodule.Path); err != nil {
return err return err
} }
return os.RemoveAll(filepath.Join(c.DotGitDir, "modules", submodule.Name)) return os.RemoveAll(filepath.Join(c.DotGitDir, "modules", submodule.Path))
}
func (c *GitCommand) AddSubmodule(name string, path string, url string) error {
return c.OSCommand.RunCommand(
"git submodule add --force --name %s -- %s %s ",
c.OSCommand.Quote(name),
c.OSCommand.Quote(url),
c.OSCommand.Quote(path),
)
} }

View File

@ -1,10 +1,8 @@
package gui package gui
import ( import (
"bytes"
"log" "log"
"strings" "strings"
"text/template"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
@ -43,19 +41,7 @@ func (gui *Gui) resolveTemplate(templateStr string, promptResponses []string) (s
PromptResponses: promptResponses, PromptResponses: promptResponses,
} }
tmpl, err := template.New("template").Parse(templateStr) return utils.ResolveTemplate(templateStr, objects)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, objects); err != nil {
return "", err
}
cmdStr := buf.String()
return cmdStr, nil
} }
func (gui *Gui) handleCustomCommandKeybinding(customCommand CustomCommand) func() error { func (gui *Gui) handleCustomCommandKeybinding(customCommand CustomCommand) func() error {

View File

@ -1601,6 +1601,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Handler: gui.wrappedHandler(gui.handleResetSubmodule), Handler: gui.wrappedHandler(gui.handleResetSubmodule),
Description: gui.Tr.SLocalize("submoduleStashAndReset"), Description: gui.Tr.SLocalize("submoduleStashAndReset"),
}, },
{
ViewName: "files",
Contexts: []string{SUBMODULES_CONTEXT_KEY},
Key: gui.getKey("universal.new"),
Handler: gui.wrappedHandler(gui.handleAddSubmodule),
Description: gui.Tr.SLocalize("addSubmodule"),
},
} }
for _, viewName := range []string{"status", "branches", "files", "commits", "commitFiles", "stash", "menu"} { for _, viewName := range []string{"status", "branches", "files", "commits", "commitFiles", "stash", "menu"} {

View File

@ -3,6 +3,8 @@ package gui
import ( import (
"fmt" "fmt"
"os" "os"
"path/filepath"
"strings"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/models"
@ -71,10 +73,10 @@ func (gui *Gui) handleRemoveSubmodule() error {
return gui.ask(askOpts{ return gui.ask(askOpts{
title: gui.Tr.SLocalize("RemoveSubmodule"), title: gui.Tr.SLocalize("RemoveSubmodule"),
prompt: gui.Tr.SLocalize("RemoveSubmodulePrompt") + " '" + submodule.Name + "'?", prompt: gui.Tr.SLocalizef("RemoveSubmodulePrompt", submodule.Name),
handleConfirm: func() error { handleConfirm: func() error {
if err := gui.GitCommand.SubmoduleDelete(submodule); err != nil { if err := gui.GitCommand.SubmoduleDelete(submodule); err != nil {
return err return gui.surfaceError(err)
} }
return gui.refreshSidePanels(refreshOptions{scope: []int{SUBMODULES, FILES}}) return gui.refreshSidePanels(refreshOptions{scope: []int{SUBMODULES, FILES}})
@ -121,16 +123,30 @@ func (gui *Gui) resetSubmodule(submodule *models.SubmoduleConfig) error {
return gui.refreshSidePanels(refreshOptions{mode: ASYNC, scope: []int{FILES, SUBMODULES}}) return gui.refreshSidePanels(refreshOptions{mode: ASYNC, scope: []int{FILES, SUBMODULES}})
} }
// func (gui *Gui) handleAddsubmodule(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleAddSubmodule() error {
// return gui.prompt(gui.Tr.SLocalize("newsubmoduleName"), "", func(submoduleName string) error { return gui.prompt(gui.Tr.SLocalize("newSubmoduleUrl"), "", func(submoduleUrl string) error {
// return gui.prompt(gui.Tr.SLocalize("newsubmoduleUrl"), "", func(submoduleUrl string) error { nameSuggestion := filepath.Base(strings.TrimSuffix(submoduleUrl, filepath.Ext(submoduleUrl)))
// if err := gui.GitCommand.Addsubmodule(submoduleName, submoduleUrl); err != nil {
// return err return gui.prompt(gui.Tr.SLocalize("newSubmoduleName"), nameSuggestion, func(submoduleName string) error {
// } return gui.prompt(gui.Tr.SLocalize("newSubmodulePath"), submoduleName, func(submodulePath string) error {
// return gui.refreshSidePanels(refreshOptions{scope: []int{submoduleS}}) return gui.WithWaitingStatus(gui.Tr.SLocalize("addingSubmoduleStatus"), func() error {
// }) err := gui.GitCommand.AddSubmodule(submoduleName, submodulePath, submoduleUrl)
// }) gui.handleCredentialsPopup(err)
// }
return gui.refreshSidePanels(refreshOptions{scope: []int{SUBMODULES}})
})
// go func() {
// err := gui.GitCommand.AddSubmodule(submoduleName, submodulePath, submoduleUrl)
// gui.handleCredentialsPopup(err)
// _ = gui.refreshSidePanels(refreshOptions{scope: []int{SUBMODULES}})
// }()
return nil
})
})
})
}
// func (gui *Gui) handleEditsubmodule(g *gocui.Gui, v *gocui.View) error { // func (gui *Gui) handleEditsubmodule(g *gocui.Gui, v *gocui.View) error {
// submodule := gui.getSelectedSubmodule() // submodule := gui.getSelectedSubmodule()

View File

@ -1211,10 +1211,25 @@ func addEnglish(i18nObject *i18n.Bundle) error {
Other: "remove submodule", Other: "remove submodule",
}, &i18n.Message{ }, &i18n.Message{
ID: "RemoveSubmodulePrompt", ID: "RemoveSubmodulePrompt",
Other: "Are you sure you want to remove submodule", Other: "Are you sure you want to remove submodule '%s' and its corresponding directory? This is irreversible.",
}, &i18n.Message{ }, &i18n.Message{
ID: "resettingSubmoduleStatus", ID: "resettingSubmoduleStatus",
Other: "resetting submodule", Other: "resetting submodule",
}, &i18n.Message{
ID: "newSubmoduleName",
Other: "new submodule name:",
}, &i18n.Message{
ID: "newSubmoduleUrl",
Other: "new submodule URL:",
}, &i18n.Message{
ID: "newSubmodulePath",
Other: "new submodule path:",
}, &i18n.Message{
ID: "addSubmodule",
Other: "add new submodule",
}, &i18n.Message{
ID: "addingSubmoduleStatus",
Other: "adding submodule",
}, },
) )
} }

View File

@ -1,6 +1,8 @@
package i18n package i18n
import ( import (
"fmt"
"github.com/cloudfoundry/jibber_jabber" "github.com/cloudfoundry/jibber_jabber"
"github.com/nicksnyder/go-i18n/v2/i18n" "github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -54,6 +56,16 @@ func (l *Localizer) TemplateLocalize(ID string, TemplateData map[string]interfac
}) })
} }
func (l *Localizer) SLocalizef(ID string, args ...interface{}) string {
str := l.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: ID,
},
})
return fmt.Sprintf(str, args...)
}
// GetLanguage returns the currently selected language, e.g 'en' // GetLanguage returns the currently selected language, e.g 'en'
func (l *Localizer) GetLanguage() string { func (l *Localizer) GetLanguage() string {
return l.language return l.language

View File

@ -1,6 +1,7 @@
package utils package utils
import ( import (
"bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@ -9,6 +10,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"text/template"
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
@ -343,3 +345,17 @@ func MustConvertToInt(s string) int {
} }
return i return i
} }
func ResolveTemplate(templateStr string, object interface{}) (string, error) {
tmpl, err := template.New("template").Parse(templateStr)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, object); err != nil {
return "", err
}
return buf.String(), nil
}