-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlink.go
More file actions
61 lines (50 loc) · 1.19 KB
/
link.go
File metadata and controls
61 lines (50 loc) · 1.19 KB
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
// cmd/link.go
package cmd
import (
"fmt"
"os"
"strconv"
"github.com/boneskull/gh-stack/internal/config"
"github.com/boneskull/gh-stack/internal/git"
"github.com/boneskull/gh-stack/internal/style"
"github.com/spf13/cobra"
)
var linkCmd = &cobra.Command{
Use: "link <pr-number>",
Short: "Associate a PR with the current branch",
Long: `Associate an existing GitHub PR number with the current branch.`,
Args: cobra.ExactArgs(1),
RunE: runLink,
}
func init() {
rootCmd.AddCommand(linkCmd)
}
func runLink(cmd *cobra.Command, args []string) error {
prNumber, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("invalid PR number: %s", args[0])
}
cwd, err := os.Getwd()
if err != nil {
return err
}
g := git.New(cwd)
cfg, err := config.New(g)
if err != nil {
return err
}
branch, err := g.CurrentBranch()
if err != nil {
return err
}
// Verify branch is tracked
if _, err := cfg.GetParent(branch); err != nil {
return fmt.Errorf("branch %q is not tracked", branch)
}
if err := cfg.SetPR(branch, prNumber); err != nil {
return err
}
s := style.New()
fmt.Printf("%s Linked PR #%d to branch %s\n", s.SuccessIcon(), prNumber, s.Branch(branch))
return nil
}