76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package commands
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.dws.rip/DWS/onyx/internal/core"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// NewNewCmd creates the new command
|
|
func NewNewCmd() *cobra.Command {
|
|
var baseBranch string
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "new <name>",
|
|
Short: "Create a new workstream",
|
|
Long: `Create a new workstream for a feature or task.
|
|
|
|
A workstream is a logical unit of work that can contain multiple commits.
|
|
It's similar to creating a new branch in Git, but with better support for
|
|
stacked diffs and atomic operations.
|
|
|
|
The workstream will be based on the specified base branch (default: main).`,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return runNew(args[0], baseBranch)
|
|
},
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&baseBranch, "base", "b", "main", "Base branch for the workstream")
|
|
|
|
return cmd
|
|
}
|
|
|
|
// runNew executes the new command
|
|
func runNew(name, baseBranch string) error {
|
|
// Get current directory
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get current directory: %w", err)
|
|
}
|
|
|
|
// Check if this is an Onyx repository
|
|
if !core.IsOnyxRepo(cwd) {
|
|
return fmt.Errorf("not an Onyx repository. Run 'onx init' first")
|
|
}
|
|
|
|
// Open the repository
|
|
repo, err := core.Open(cwd)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open repository: %w", err)
|
|
}
|
|
defer repo.Close()
|
|
|
|
// Create workstream manager
|
|
wsManager := core.NewWorkstreamManager(repo)
|
|
|
|
// Use ExecuteWithTransaction to capture state_before and state_after
|
|
err = core.ExecuteWithTransaction(repo, "new", fmt.Sprintf("Created workstream: %s", name), func() error {
|
|
return wsManager.CreateWorkstream(name, baseBranch)
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Printf("Created workstream '%s' based on '%s'\n", name, baseBranch)
|
|
fmt.Printf("\nYou can now:\n")
|
|
fmt.Printf(" - Make changes to your files\n")
|
|
fmt.Printf(" - Save your work with 'onx save -m \"message\"'\n")
|
|
fmt.Printf(" - Switch to another workstream with 'onx switch <name>'\n")
|
|
|
|
return nil
|
|
}
|