From b9498dd56adc92436e53aa48d31e685bd242449c Mon Sep 17 00:00:00 2001 From: Dolev Hadar Date: Fri, 6 Sep 2024 15:14:59 +0300 Subject: [PATCH] feat(branches): force push --- ui/components/reposection/commands.go | 35 ++++++++++++++++++++---- ui/components/reposection/reposection.go | 7 ++++- ui/keys/branchKeys.go | 8 ++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/ui/components/reposection/commands.go b/ui/components/reposection/commands.go index 94de9e91..98451f43 100644 --- a/ui/components/reposection/commands.go +++ b/ui/components/reposection/commands.go @@ -76,24 +76,49 @@ func (m *Model) fastForward() (tea.Cmd, error) { }), nil } -func (m *Model) push() (tea.Cmd, error) { +type pushOptions struct { + force bool +} + +func (m *Model) push(opts pushOptions) (tea.Cmd, error) { b := m.getCurrBranch() taskId := fmt.Sprintf("push_%s_%d", b.Data.Name, time.Now().Unix()) + withForceText := func() string { + if opts.force { + return " with force" + } + return "" + } task := context.Task{ Id: taskId, - StartText: fmt.Sprintf("Pushing branch %s", b.Data.Name), - FinishedText: fmt.Sprintf("Branch %s has been pushed", b.Data.Name), + StartText: fmt.Sprintf("Pushing branch %s%s", b.Data.Name, withForceText()), + FinishedText: fmt.Sprintf("Branch %s has been pushed%s", b.Data.Name, withForceText()), State: context.TaskStart, Error: nil, } startCmd := m.Ctx.StartTask(task) return tea.Batch(startCmd, func() tea.Msg { var err error + args := []string{} + if opts.force { + args = append(args, "--force") + } if len(b.Data.Remotes) == 0 { - err = gitm.Push(*m.Ctx.RepoPath, "origin", b.Data.Name, gitm.PushOptions{CommandOptions: gitm.CommandOptions{Args: []string{"--set-upstream"}}}) + args = append(args, "--set-upstream") + err = gitm.Push( + *m.Ctx.RepoPath, + "origin", + b.Data.Name, + gitm.PushOptions{CommandOptions: gitm.CommandOptions{Args: args}}, + ) } else { - err = gitm.Push(*m.Ctx.RepoPath, b.Data.Remotes[0], b.Data.Name) + err = gitm.Push( + *m.Ctx.RepoPath, + b.Data.Remotes[0], + b.Data.Name, + gitm.PushOptions{CommandOptions: gitm.CommandOptions{Args: args}}, + ) } if err != nil { return constants.TaskFinishedMsg{TaskId: taskId, Err: err} diff --git a/ui/components/reposection/reposection.go b/ui/components/reposection/reposection.go index f0cc97b1..d783ac92 100644 --- a/ui/components/reposection/reposection.go +++ b/ui/components/reposection/reposection.go @@ -142,7 +142,12 @@ func (m *Model) Update(msg tea.Msg) (section.Section, tea.Cmd) { } case key.Matches(msg, keys.BranchKeys.Push): - cmd, err = m.push() + cmd, err = m.push(pushOptions{force: false}) + if err != nil { + m.Ctx.Error = err + } + case key.Matches(msg, keys.BranchKeys.ForcePush): + cmd, err = m.push(pushOptions{force: true}) if err != nil { m.Ctx.Error = err } diff --git a/ui/keys/branchKeys.go b/ui/keys/branchKeys.go index bfe244a3..e160da96 100644 --- a/ui/keys/branchKeys.go +++ b/ui/keys/branchKeys.go @@ -14,6 +14,7 @@ type BranchKeyMap struct { New key.Binding FastForward key.Binding Push key.Binding + ForcePush key.Binding Delete key.Binding ViewPRs key.Binding } @@ -35,6 +36,10 @@ var BranchKeys = BranchKeyMap{ key.WithKeys("P"), key.WithHelp("P", "push"), ), + ForcePush: key.NewBinding( + key.WithKeys("F"), + key.WithHelp("F", "force-push"), + ), Delete: key.NewBinding( key.WithKeys("d", "backspace"), key.WithHelp("d/backspace", "delete"), @@ -50,6 +55,7 @@ func BranchFullHelp() []key.Binding { BranchKeys.Checkout, BranchKeys.FastForward, BranchKeys.Push, + BranchKeys.ForcePush, BranchKeys.New, BranchKeys.Delete, BranchKeys.ViewPRs, @@ -73,6 +79,8 @@ func rebindBranchKeys(keys []config.Keybinding) error { key = &BranchKeys.Delete case "push": key = &BranchKeys.Push + case "forcePush": + key = &BranchKeys.ForcePush case "fastForward": key = &BranchKeys.FastForward case "checkout":