-
Notifications
You must be signed in to change notification settings - Fork 18
/
branch_rename.go
83 lines (67 loc) · 1.92 KB
/
branch_rename.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"fmt"
"strings"
"github.com/charmbracelet/log"
"go.abhg.dev/gs/internal/must"
"go.abhg.dev/gs/internal/text"
"go.abhg.dev/gs/internal/ui"
)
type branchRenameCmd struct {
OldName string `arg:"" optional:"" help:"Old name of the branch"`
NewName string `arg:"" optional:"" help:"New name of the branch"`
}
func (*branchRenameCmd) Help() string {
return text.Dedent(`
The following modes are supported:
# Rename <old> to <new>
gs branch rename <old> <new>
# Rename current branch to <new>
gs branch rename <new>
# Rename current branch interactively
gs branch rename
For branches renamed with 'git branch -m',
use 'gs branch track' and 'gs branch untrack'
to update the branch tracking.
`)
}
func (cmd *branchRenameCmd) Run(ctx context.Context, log *log.Logger, view ui.View) (err error) {
repo, _, svc, err := openRepo(ctx, log, view)
if err != nil {
return err
}
oldName, newName := cmd.OldName, cmd.NewName
// For "gs branch rename <new>",
// we'll actually get oldName = <new> and newName = "".
if oldName != "" && newName == "" {
oldName, newName = "", oldName
}
if oldName == "" {
oldName, err = repo.CurrentBranch(ctx)
if err != nil {
return fmt.Errorf("get current branch: %w", err)
}
}
if newName == "" {
prompt := ui.NewInput().
WithValue(&newName).
WithTitle("New branch name").
WithDescription(fmt.Sprintf("Renaming branch: %v", oldName)).
WithValidate(func(s string) error {
if strings.TrimSpace(s) == "" {
return fmt.Errorf("branch name cannot be empty")
}
return nil
})
if err := ui.Run(view, prompt); err != nil {
return fmt.Errorf("prompt: %w", err)
}
}
must.NotBeBlankf(oldName, "old branch name must be set")
must.NotBeBlankf(newName, "new branch name must be set")
if err := svc.RenameBranch(ctx, oldName, newName); err != nil {
return fmt.Errorf("rename branch: %w", err)
}
return nil
}