forked from boneskull/gh-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadopt.go
More file actions
111 lines (91 loc) · 2.51 KB
/
adopt.go
File metadata and controls
111 lines (91 loc) · 2.51 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// cmd/adopt.go
package cmd
import (
"errors"
"fmt"
"os"
"github.com/boneskull/gh-stack/internal/config"
"github.com/boneskull/gh-stack/internal/git"
"github.com/boneskull/gh-stack/internal/style"
"github.com/boneskull/gh-stack/internal/tree"
"github.com/spf13/cobra"
)
var adoptCmd = &cobra.Command{
Use: "adopt <parent>",
Short: "Start tracking an existing branch",
Long: `Start tracking an existing branch by setting its parent.
By default, adopts the current branch. Use --branch to specify a different branch.`,
Args: cobra.ExactArgs(1),
RunE: runAdopt,
}
var adoptBranchFlag string
func init() {
adoptCmd.Flags().StringVar(&adoptBranchFlag, "branch", "", "branch to adopt (default: current branch)")
rootCmd.AddCommand(adoptCmd)
}
func runAdopt(cmd *cobra.Command, args []string) error {
cwd, err := os.Getwd()
if err != nil {
return err
}
g := git.New(cwd)
cfg, err := config.New(g)
if err != nil {
return err
}
// Parent is the required positional argument
parent := args[0]
// Determine branch to adopt (from flag or current branch)
var branchName string
if adoptBranchFlag != "" {
branchName = adoptBranchFlag
} else {
branchName, err = g.CurrentBranch()
if err != nil {
return err
}
}
// Validate branch exists
if !g.BranchExists(branchName) {
return fmt.Errorf("branch %q does not exist", branchName)
}
// Check if already tracked
if _, getParentErr := cfg.GetParent(branchName); getParentErr == nil {
return fmt.Errorf("branch %q is already tracked", branchName)
}
// Validate parent is trunk or tracked
trunk, err := cfg.GetTrunk()
if err != nil {
return err
}
if parent != trunk {
if _, parentErr := cfg.GetParent(parent); parentErr != nil {
return fmt.Errorf("parent %q is not tracked", parent)
}
}
// Check for cycles (branch can't be ancestor of parent)
root, err := tree.Build(cfg)
if err != nil {
return err
}
parentNode := tree.FindNode(root, parent)
if parentNode != nil {
for _, ancestor := range tree.GetAncestors(parentNode) {
if ancestor.Name == branchName {
return errors.New("cannot adopt: would create a cycle")
}
}
}
// Set parent
if err := cfg.SetParent(branchName, parent); err != nil {
return err
}
// Store fork point
forkPoint, fpErr := g.GetMergeBase(branchName, parent)
if fpErr == nil {
_ = cfg.SetForkPoint(branchName, forkPoint) //nolint:errcheck // best effort
}
s := style.New()
fmt.Printf("%s Adopted branch %s with parent %s\n", s.SuccessIcon(), s.Branch(branchName), s.Branch(parent))
return nil
}