-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate.go
More file actions
112 lines (91 loc) · 2.65 KB
/
create.go
File metadata and controls
112 lines (91 loc) · 2.65 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
112
// cmd/create.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/spf13/cobra"
)
var createCmd = &cobra.Command{
Use: "create <name>",
Short: "Create a new branch stacked on the current branch",
Long: `Create a new branch stacked on the current branch and optionally commit staged changes.`,
Args: cobra.ExactArgs(1),
RunE: runCreate,
}
var (
createMessageFlag string
createEmptyFlag bool
)
func init() {
createCmd.Flags().StringVarP(&createMessageFlag, "message", "m", "", "commit message for staged changes")
createCmd.Flags().BoolVar(&createEmptyFlag, "empty", false, "create branch without committing staged changes")
rootCmd.AddCommand(createCmd)
}
func runCreate(cmd *cobra.Command, args []string) error {
branchName := 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
}
// Get current branch
currentBranch, err := g.CurrentBranch()
if err != nil {
return err
}
// Validate current branch is trunk or tracked
trunk, err := cfg.GetTrunk()
if err != nil {
return err
}
if currentBranch != trunk {
if _, parentErr := cfg.GetParent(currentBranch); parentErr != nil {
return fmt.Errorf("current branch %q is not tracked; use 'gh stack adopt' first", currentBranch)
}
}
// Check if branch already exists
if g.BranchExists(branchName) {
return fmt.Errorf("branch %q already exists", branchName)
}
// Check for staged changes
hasStaged, err := g.HasStagedChanges()
if err != nil {
return err
}
if hasStaged && !createEmptyFlag {
if createMessageFlag == "" {
return errors.New("staged changes found but no commit message provided; use -m or --empty")
}
}
// Create and checkout the new branch
if err := g.CreateAndCheckout(branchName); err != nil {
return err
}
s := style.New()
// Commit staged changes if any
if hasStaged && !createEmptyFlag && createMessageFlag != "" {
if err := g.Commit(createMessageFlag); err != nil {
return err
}
fmt.Printf("%s Committed staged changes: %s\n", s.SuccessIcon(), createMessageFlag)
}
// Set parent
if err := cfg.SetParent(branchName, currentBranch); err != nil {
return err
}
// Store fork point (where this branch diverges from parent)
forkPoint, fpErr := g.GetMergeBase(branchName, currentBranch)
if fpErr == nil {
_ = cfg.SetForkPoint(branchName, forkPoint) //nolint:errcheck // best effort
}
fmt.Printf("%s Created branch %s stacked on %s\n", s.SuccessIcon(), s.Branch(branchName), s.Branch(currentBranch))
return nil
}