forked from gastownhall/beads
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvc.go
More file actions
222 lines (190 loc) · 6.34 KB
/
vc.go
File metadata and controls
222 lines (190 loc) · 6.34 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"io"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/ui"
)
var vcCmd = &cobra.Command{
Use: "vc",
GroupID: "sync",
Short: "Version control operations",
Long: `Version control operations for the beads database.
These commands provide git-like version control for your issue data, including branching, merging, and
viewing history.
Note: 'bd history', 'bd diff', and 'bd branch' also work for quick access.
This subcommand provides additional operations like merge and commit.`,
}
var vcMergeStrategy string
var vcMergeCmd = &cobra.Command{
Use: "merge <branch>",
Short: "Merge a branch into the current branch",
Long: `Merge the specified branch into the current branch.
If there are merge conflicts, they will be reported. You can resolve
conflicts with --strategy.
Examples:
bd vc merge feature-xyz # Merge feature-xyz into current branch
bd vc merge feature-xyz --strategy ours # Merge, preferring our changes on conflict
bd vc merge feature-xyz --strategy theirs # Merge, preferring their changes on conflict`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := rootCtx
branchName := args[0]
// Perform merge
conflicts, err := store.Merge(ctx, branchName)
if err != nil {
FatalErrorRespectJSON("failed to merge branch: %v", err)
}
// Handle conflicts
if len(conflicts) > 0 {
if vcMergeStrategy != "" {
// Auto-resolve conflicts with specified strategy
for _, conflict := range conflicts {
table := conflict.Field // Field contains table name from GetConflicts
if table == "" {
table = "issues" // Default to issues table
}
if err := store.ResolveConflicts(ctx, table, vcMergeStrategy); err != nil {
FatalErrorRespectJSON("failed to resolve conflicts: %v", err)
}
}
if jsonOutput {
outputJSON(map[string]interface{}{
"merged": branchName,
"conflicts": len(conflicts),
"resolved_with": vcMergeStrategy,
})
return
}
fmt.Printf("Merged %s with %d conflicts resolved using '%s' strategy\n",
ui.RenderAccent(branchName), len(conflicts), vcMergeStrategy)
return
}
// Report conflicts without auto-resolution
if jsonOutput {
outputJSON(map[string]interface{}{
"merged": branchName,
"conflicts": conflicts,
})
return
}
fmt.Printf("\n%s Merge completed with conflicts:\n\n", ui.RenderAccent("!!"))
for _, conflict := range conflicts {
fmt.Printf(" - %s\n", conflict.Field)
}
fmt.Printf("\nResolve conflicts with: bd vc merge %s --strategy [ours|theirs]\n\n", branchName)
return
}
if jsonOutput {
outputJSON(map[string]interface{}{
"merged": branchName,
"conflicts": 0,
})
return
}
fmt.Printf("Successfully merged %s\n", ui.RenderAccent(branchName))
},
}
var vcCommitMessage string
var vcCommitStdin bool
var vcCommitCmd = &cobra.Command{
Use: "commit",
Short: "Create a commit with all staged changes",
Long: `Create a new Dolt commit with all current changes.
Examples:
bd vc commit -m "Added new feature issues"
bd vc commit --message "Fixed priority on several issues"
echo "Multi-line message" | bd vc commit --stdin`,
Run: func(cmd *cobra.Command, args []string) {
ctx := rootCtx
if vcCommitStdin {
if vcCommitMessage != "" {
FatalErrorRespectJSON("cannot specify both --stdin and -m/--message")
}
b, err := io.ReadAll(os.Stdin)
if err != nil {
FatalErrorRespectJSON("failed to read commit message from stdin: %v", err)
}
vcCommitMessage = strings.TrimRight(string(b), "\n")
}
if vcCommitMessage == "" {
FatalErrorRespectJSON("commit message is required (use -m, --message, or --stdin)")
}
// We are explicitly creating a Dolt commit; avoid redundant auto-commit in PersistentPostRun.
// Use CommitPending which calls CommitWithConfig internally — this stages ALL
// tables including config. The interface method Commit() intentionally skips
// config to prevent sweeping stale changes during auto-commits (GH#2455), but
// an explicit `bd vc commit` is a user action that should commit everything
// the user sees in `bd vc status`.
//
// Note: CommitPending generates its own descriptive commit message rather than
// using vcCommitMessage. The user's message is displayed in the output.
commandDidExplicitDoltCommit = true
committed, err := store.CommitPending(ctx, getActorWithGit())
if err != nil {
FatalErrorRespectJSON("failed to commit: %v", err)
}
if !committed {
if jsonOutput {
outputJSON(map[string]interface{}{"committed": false, "message": "nothing to commit"})
} else {
fmt.Println("Nothing to commit")
}
return
}
// Get the new commit hash
hash, err := store.GetCurrentCommit(ctx)
if err != nil {
hash = "(unknown)"
}
if jsonOutput {
outputJSON(map[string]interface{}{
"committed": true,
"hash": hash,
"message": vcCommitMessage,
})
return
}
fmt.Printf("Created commit %s\n", ui.RenderMuted(hash[:8]))
},
}
var vcStatusCmd = &cobra.Command{
Use: "status",
Short: "Show current branch and uncommitted changes",
Long: `Show the current branch, commit hash, and any uncommitted changes.
Examples:
bd vc status`,
Run: func(cmd *cobra.Command, args []string) {
ctx := rootCtx
currentBranch, err := store.CurrentBranch(ctx)
if err != nil {
FatalErrorRespectJSON("failed to get current branch: %v", err)
}
currentCommit, err := store.GetCurrentCommit(ctx)
if err != nil {
currentCommit = "(unknown)"
}
if jsonOutput {
outputJSON(map[string]interface{}{
"branch": currentBranch,
"commit": currentCommit,
})
return
}
fmt.Printf("\n%s Version Control Status\n\n", ui.RenderAccent("📊"))
fmt.Printf(" Branch: %s\n", ui.StatusInProgressStyle.Render(currentBranch))
fmt.Printf(" Commit: %s\n", ui.RenderMuted(currentCommit[:8]))
fmt.Println()
},
}
func init() {
vcMergeCmd.Flags().StringVar(&vcMergeStrategy, "strategy", "", "Conflict resolution strategy: 'ours' or 'theirs'")
vcCommitCmd.Flags().StringVarP(&vcCommitMessage, "message", "m", "", "Commit message")
vcCommitCmd.Flags().BoolVar(&vcCommitStdin, "stdin", false, "Read commit message from stdin")
vcCmd.AddCommand(vcMergeCmd)
vcCmd.AddCommand(vcCommitCmd)
vcCmd.AddCommand(vcStatusCmd)
rootCmd.AddCommand(vcCmd)
}