Skip to content

Commit

Permalink
feat(branches): force push
Browse files Browse the repository at this point in the history
  • Loading branch information
dlvhdr committed Sep 6, 2024
1 parent dba6346 commit b9498dd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
35 changes: 30 additions & 5 deletions ui/components/reposection/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
7 changes: 6 additions & 1 deletion ui/components/reposection/reposection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions ui/keys/branchKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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"),
Expand All @@ -50,6 +55,7 @@ func BranchFullHelp() []key.Binding {
BranchKeys.Checkout,
BranchKeys.FastForward,
BranchKeys.Push,
BranchKeys.ForcePush,
BranchKeys.New,
BranchKeys.Delete,
BranchKeys.ViewPRs,
Expand All @@ -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":
Expand Down

0 comments on commit b9498dd

Please sign in to comment.