1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-28 16:02:01 +03:00

Support for deleting a worktree

This commit is contained in:
Joel Baranick
2022-09-01 17:58:36 -07:00
committed by Jesse Duffield
parent f8ba899b87
commit afc4aedd4f
4 changed files with 84 additions and 36 deletions

View File

@ -37,6 +37,7 @@ type GitCommand struct {
Tag *git_commands.TagCommands Tag *git_commands.TagCommands
WorkingTree *git_commands.WorkingTreeCommands WorkingTree *git_commands.WorkingTreeCommands
Bisect *git_commands.BisectCommands Bisect *git_commands.BisectCommands
Worktree *git_commands.WorktreeCommands
Loaders Loaders Loaders Loaders
} }
@ -128,6 +129,7 @@ func NewGitCommandAux(
}) })
patchCommands := git_commands.NewPatchCommands(gitCommon, rebaseCommands, commitCommands, statusCommands, stashCommands, patchBuilder) patchCommands := git_commands.NewPatchCommands(gitCommon, rebaseCommands, commitCommands, statusCommands, stashCommands, patchBuilder)
bisectCommands := git_commands.NewBisectCommands(gitCommon) bisectCommands := git_commands.NewBisectCommands(gitCommon)
worktreeCommands := git_commands.NewWorktreeCommands(gitCommon)
branchLoader := git_commands.NewBranchLoader(cmn, cmd, branchCommands.CurrentBranchInfo, configCommands) branchLoader := git_commands.NewBranchLoader(cmn, cmd, branchCommands.CurrentBranchInfo, configCommands)
commitFileLoader := git_commands.NewCommitFileLoader(cmn, cmd) commitFileLoader := git_commands.NewCommitFileLoader(cmn, cmd)
@ -156,6 +158,7 @@ func NewGitCommandAux(
Tag: tagCommands, Tag: tagCommands,
Bisect: bisectCommands, Bisect: bisectCommands,
WorkingTree: workingTreeCommands, WorkingTree: workingTreeCommands,
Worktree: worktreeCommands,
Loaders: Loaders{ Loaders: Loaders{
BranchLoader: branchLoader, BranchLoader: branchLoader,
CommitFileLoader: commitFileLoader, CommitFileLoader: commitFileLoader,

View File

@ -0,0 +1,17 @@
package git_commands
type WorktreeCommands struct {
*GitCommon
}
func NewWorktreeCommands(gitCommon *GitCommon) *WorktreeCommands {
return &WorktreeCommands{
GitCommon: gitCommon,
}
}
func (self *WorktreeCommands) Delete(worktreePath string, force bool) error {
cmdArgs := NewGitCmd("worktree").Arg("remove").ArgIf(force, "-f").Arg(worktreePath).ToArgv()
return self.cmd.New(cmdArgs).Run()
}

View File

@ -8,6 +8,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
) )
type WorktreesController struct { type WorktreesController struct {
@ -33,27 +34,16 @@ func (self *WorktreesController) GetKeybindings(opts types.KeybindingsOpts) []*t
Handler: self.checkSelected(self.enter), Handler: self.checkSelected(self.enter),
Description: self.c.Tr.EnterWorktree, Description: self.c.Tr.EnterWorktree,
}, },
//{ {
// Key: opts.GetKey(opts.Config.Universal.Remove), Key: opts.GetKey(opts.Config.Universal.Remove),
// Handler: self.withSelectedTag(self.delete), Handler: self.checkSelected(self.delete),
// Description: self.c.Tr.LcDeleteTag, Description: self.c.Tr.DeleteWorktree,
//}, },
//{
// Key: opts.GetKey(opts.Config.Branches.PushTag),
// Handler: self.withSelectedTag(self.push),
// Description: self.c.Tr.LcPushTag,
//},
//{ //{
// Key: opts.GetKey(opts.Config.Universal.New), // Key: opts.GetKey(opts.Config.Universal.New),
// Handler: self.create, // Handler: self.create,
// Description: self.c.Tr.LcCreateTag, // Description: self.c.Tr.LcCreateTag,
//}, //},
//{
// Key: opts.GetKey(opts.Config.Commits.ViewResetOptions),
// Handler: self.withSelectedTag(self.createResetMenu),
// Description: self.c.Tr.LcViewResetOptions,
// OpensMenu: true,
//},
} }
return bindings return bindings
@ -95,26 +85,50 @@ func (self *WorktreesController) GetOnRenderToMain() func() error {
// return gui.dispatchSwitchToRepo(submodule.Path, true) // return gui.dispatchSwitchToRepo(submodule.Path, true)
//} //}
// func (self *WorktreesController) delete(tag *models.Tag) error { func (self *WorktreesController) delete(worktree *models.Worktree) error {
// prompt := utils.ResolvePlaceholderString( if worktree.Main {
// self.c.Tr.DeleteTagPrompt, return self.c.ErrorMsg(self.c.Tr.CantDeleteMainWorktree)
// map[string]string{ }
// "tagName": tag.Name,
// }, if worktree.Current {
// ) return self.c.ErrorMsg(self.c.Tr.CantDeleteCurrentWorktree)
// }
// return self.c.Confirm(types.ConfirmOpts{
// Title: self.c.Tr.DeleteTagTitle, return self.deleteWithForce(worktree, false)
// Prompt: prompt, }
// HandleConfirm: func() error {
// self.c.LogAction(self.c.Tr.Actions.DeleteTag) func (self *WorktreesController) deleteWithForce(worktree *models.Worktree, force bool) error {
// if err := self.git.Tag.Delete(tag.Name); err != nil { title := self.c.Tr.DeleteWorktreeTitle
// return self.c.Error(err) var templateStr string
// } if force {
// return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.COMMITS, types.TAGS}}) templateStr = self.c.Tr.ForceDeleteWorktreePrompt
// }, } else {
// }) templateStr = self.c.Tr.DeleteWorktreePrompt
// } }
message := utils.ResolvePlaceholderString(
templateStr,
map[string]string{
"worktreeName": worktree.Name,
},
)
return self.c.Confirm(types.ConfirmOpts{
Title: title,
Prompt: message,
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.DeleteWorktree)
if err := self.c.Git().Worktree.Delete(worktree.Path, force); err != nil {
errMessage := err.Error()
if !force {
return self.deleteWithForce(worktree, true)
}
return self.c.ErrorMsg(errMessage)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.WORKTREES}})
},
})
}
// //
// func (self *WorktreesController) push(tag *models.Tag) error { // func (self *WorktreesController) push(tag *models.Tag) error {
// title := utils.ResolvePlaceholderString( // title := utils.ResolvePlaceholderString(

View File

@ -543,6 +543,12 @@ type TranslationSet struct {
ExitSearchMode string ExitSearchMode string
ExitTextFilterMode string ExitTextFilterMode string
EnterWorktree string EnterWorktree string
DeleteWorktree string
DeleteWorktreeTitle string
DeleteWorktreePrompt string
ForceDeleteWorktreePrompt string
CantDeleteCurrentWorktree string
CantDeleteMainWorktree string
Actions Actions Actions Actions
Bisect Bisect Bisect Bisect
} }
@ -673,6 +679,7 @@ type Actions struct {
ResetBisect string ResetBisect string
BisectSkip string BisectSkip string
BisectMark string BisectMark string
DeleteWorktree string
} }
const englishIntroPopupMessage = ` const englishIntroPopupMessage = `
@ -1243,6 +1250,12 @@ func EnglishTranslationSet() TranslationSet {
SearchPrefix: "Search: ", SearchPrefix: "Search: ",
FilterPrefix: "Filter: ", FilterPrefix: "Filter: ",
EnterWorktree: "Enter worktree", EnterWorktree: "Enter worktree",
DeleteWorktree: "Delete worktree",
DeleteWorktreeTitle: "Delete worktree",
DeleteWorktreePrompt: "Are you sure you want to delete worktree '{{.worktreeName}}'?",
ForceDeleteWorktreePrompt: "'{{.worktreeName}}' is not fully merged. Are you sure you want to delete it?",
CantDeleteCurrentWorktree: "You cannot delete the current worktree!",
CantDeleteMainWorktree: "You cannot delete the main worktree!",
Actions: Actions{ Actions: Actions{
// TODO: combine this with the original keybinding descriptions (those are all in lowercase atm) // TODO: combine this with the original keybinding descriptions (those are all in lowercase atm)
CheckoutCommit: "Checkout commit", CheckoutCommit: "Checkout commit",
@ -1350,6 +1363,7 @@ func EnglishTranslationSet() TranslationSet {
ResetBisect: "Reset bisect", ResetBisect: "Reset bisect",
BisectSkip: "Bisect skip", BisectSkip: "Bisect skip",
BisectMark: "Bisect mark", BisectMark: "Bisect mark",
DeleteWorktree: "Delete worktree",
}, },
Bisect: Bisect{ Bisect: Bisect{
Mark: "Mark current commit (%s) as %s", Mark: "Mark current commit (%s) as %s",