1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-24 05:36:19 +02:00
lazygit/pkg/gui/controllers/helpers/refs_helper.go

191 lines
5.8 KiB
Go
Raw Normal View History

2022-02-06 15:54:26 +11:00
package helpers
import (
"fmt"
"strings"
2022-03-19 19:12:58 +11:00
"github.com/jesseduffield/generics/slices"
Use first class task objects instead of global counter The global counter approach is easy to understand but it's brittle and depends on implicit behaviour that is not very discoverable. With a global counter, if any goroutine accidentally decrements the counter twice, we'll think lazygit is idle when it's actually busy. Likewise if a goroutine accidentally increments the counter twice we'll think lazygit is busy when it's actually idle. With the new approach we have a map of tasks where each task can either be busy or not. We create a new task and add it to the map when we spawn a worker goroutine (among other things) and we remove it once the task is done. The task can also be paused and continued for situations where we switch back and forth between running a program and asking for user input. In order for this to work with `git push` (and other commands that require credentials) we need to obtain the task from gocui when we create the worker goroutine, and then pass it along to the commands package to pause/continue the task as required. This is MUCH more discoverable than the old approach which just decremented and incremented the global counter from within the commands package, but it's at the cost of expanding some function signatures (arguably a good thing). Likewise, whenever you want to call WithWaitingStatus or WithLoaderPanel the callback will now have access to the task for pausing/ continuing. We only need to actually make use of this functionality in a couple of places so it's a high price to pay, but I don't know if I want to introduce a WithWaitingStatusTask and WithLoaderPanelTask function (open to suggestions).
2023-07-09 11:32:27 +10:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
2022-02-06 15:54:26 +11:00
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
2022-01-30 20:03:08 +11:00
"github.com/jesseduffield/lazygit/pkg/utils"
)
2022-01-31 22:11:34 +11:00
type IRefsHelper interface {
CheckoutRef(ref string, options types.CheckoutRefOptions) error
2022-02-06 15:54:26 +11:00
GetCheckedOutRef() *models.Branch
2022-01-31 22:11:34 +11:00
CreateGitResetMenu(ref string) error
ResetToRef(ref string, strength string, envVars []string) error
NewBranch(from string, fromDescription string, suggestedBranchname string) error
}
2022-01-31 22:11:34 +11:00
type RefsHelper struct {
2023-03-23 12:53:18 +11:00
c *HelperCommon
}
2022-01-30 10:23:39 +11:00
func NewRefsHelper(
c *HelperCommon,
2022-01-30 10:23:39 +11:00
) *RefsHelper {
return &RefsHelper{
2023-03-23 12:53:18 +11:00
c: c,
}
}
2022-01-31 22:11:34 +11:00
var _ IRefsHelper = &RefsHelper{}
2022-01-30 10:23:39 +11:00
func (self *RefsHelper) CheckoutRef(ref string, options types.CheckoutRefOptions) error {
waitingStatus := options.WaitingStatus
if waitingStatus == "" {
waitingStatus = self.c.Tr.CheckingOutStatus
}
cmdOptions := git_commands.CheckoutOptions{Force: false, EnvVars: options.EnvVars}
onSuccess := func() {
2023-03-23 12:53:18 +11:00
self.c.Contexts().Branches.SetSelectedLineIdx(0)
self.c.Contexts().ReflogCommits.SetSelectedLineIdx(0)
self.c.Contexts().LocalCommits.SetSelectedLineIdx(0)
// loading a heap of commits is slow so we limit them whenever doing a reset
2023-03-23 12:53:18 +11:00
self.c.Contexts().LocalCommits.SetLimitCommits(true)
}
return self.c.WithWaitingStatus(waitingStatus, func(gocui.Task) error {
2023-03-23 12:53:18 +11:00
if err := self.c.Git().Branch.Checkout(ref, cmdOptions); err != nil {
// note, this will only work for english-language git commands. If we force git to use english, and the error isn't this one, then the user will receive an english command they may not understand. I'm not sure what the best solution to this is. Running the command once in english and a second time in the native language is one option
if options.OnRefNotFound != nil && strings.Contains(err.Error(), "did not match any file(s) known to git") {
return options.OnRefNotFound(ref)
}
if strings.Contains(err.Error(), "Please commit your changes or stash them before you switch branch") {
// offer to autostash changes
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.AutoStashTitle,
Prompt: self.c.Tr.AutoStashPrompt,
HandleConfirm: func() error {
if err := self.c.Git().Stash.Push(self.c.Tr.StashPrefix + ref); err != nil {
return self.c.Error(err)
}
2023-03-23 12:53:18 +11:00
if err := self.c.Git().Branch.Checkout(ref, cmdOptions); err != nil {
return self.c.Error(err)
}
onSuccess()
2023-03-23 12:53:18 +11:00
if err := self.c.Git().Stash.Pop(0); err != nil {
if err := self.c.Refresh(types.RefreshOptions{Mode: types.BLOCK_UI}); err != nil {
return err
}
return self.c.Error(err)
}
return self.c.Refresh(types.RefreshOptions{Mode: types.BLOCK_UI})
},
})
}
if err := self.c.Error(err); err != nil {
return err
}
}
onSuccess()
return self.c.Refresh(types.RefreshOptions{Mode: types.BLOCK_UI})
})
}
2022-02-06 15:54:26 +11:00
func (self *RefsHelper) GetCheckedOutRef() *models.Branch {
2023-03-23 12:53:18 +11:00
if len(self.c.Model().Branches) == 0 {
2022-02-06 15:54:26 +11:00
return nil
}
2023-03-23 12:53:18 +11:00
return self.c.Model().Branches[0]
2022-02-06 15:54:26 +11:00
}
2022-01-30 10:23:39 +11:00
func (self *RefsHelper) ResetToRef(ref string, strength string, envVars []string) error {
2023-03-23 12:53:18 +11:00
if err := self.c.Git().Commit.ResetToCommit(ref, strength, envVars); err != nil {
return self.c.Error(err)
}
2023-03-23 12:53:18 +11:00
self.c.Contexts().LocalCommits.SetSelectedLineIdx(0)
self.c.Contexts().ReflogCommits.SetSelectedLineIdx(0)
// loading a heap of commits is slow so we limit them whenever doing a reset
2023-03-23 12:53:18 +11:00
self.c.Contexts().LocalCommits.SetLimitCommits(true)
if err := self.c.Refresh(types.RefreshOptions{Scope: []types.RefreshableView{types.FILES, types.BRANCHES, types.REFLOG, types.COMMITS}}); err != nil {
return err
}
return nil
}
2022-01-30 10:23:39 +11:00
func (self *RefsHelper) CreateGitResetMenu(ref string) error {
type strengthWithKey struct {
strength string
label string
key types.Key
}
strengths := []strengthWithKey{
// not i18'ing because it's git terminology
{strength: "soft", label: "Soft reset", key: 's'},
{strength: "mixed", label: "Mixed reset", key: 'm'},
{strength: "hard", label: "Hard reset", key: 'h'},
}
menuItems := slices.Map(strengths, func(row strengthWithKey) *types.MenuItem {
2022-03-19 19:12:58 +11:00
return &types.MenuItem{
LabelColumns: []string{
row.label,
style.FgRed.Sprintf("reset --%s %s", row.strength, ref),
},
OnPress: func() error {
self.c.LogAction("Reset")
return self.ResetToRef(ref, row.strength, []string{})
},
Key: row.key,
}
2022-03-19 19:12:58 +11:00
})
2022-01-29 19:09:20 +11:00
return self.c.Menu(types.CreateMenuOptions{
Title: fmt.Sprintf("%s %s", self.c.Tr.ResetTo, ref),
Items: menuItems,
})
}
2022-01-30 20:03:08 +11:00
func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggestedBranchName string) error {
message := utils.ResolvePlaceholderString(
self.c.Tr.NewBranchNameBranchOff,
map[string]string{
"branchName": fromFormattedName,
},
)
return self.c.Prompt(types.PromptOpts{
Title: message,
InitialContent: suggestedBranchName,
HandleConfirm: func(response string) error {
self.c.LogAction(self.c.Tr.Actions.CreateBranch)
2023-03-23 12:53:18 +11:00
if err := self.c.Git().Branch.New(sanitizedBranchName(response), from); err != nil {
2022-01-30 20:03:08 +11:00
return err
}
2023-03-23 12:53:18 +11:00
if self.c.CurrentContext() != self.c.Contexts().Branches {
if err := self.c.PushContext(self.c.Contexts().Branches); err != nil {
2022-01-30 20:03:08 +11:00
return err
}
}
2023-03-23 12:53:18 +11:00
self.c.Contexts().LocalCommits.SetSelectedLineIdx(0)
self.c.Contexts().Branches.SetSelectedLineIdx(0)
2022-01-30 20:03:08 +11:00
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
},
})
}
2022-01-31 22:11:34 +11:00
// sanitizedBranchName will remove all spaces in favor of a dash "-" to meet
// git's branch naming requirement.
func sanitizedBranchName(input string) string {
return strings.Replace(input, " ", "-", -1)
}