Milestone 3
This commit is contained in:
@ -3,12 +3,10 @@ package commands
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.dws.rip/DWS/onyx/internal/core"
|
||||
"git.dws.rip/DWS/onyx/internal/git"
|
||||
"git.dws.rip/DWS/onyx/internal/models"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -95,14 +93,11 @@ func executeSave(repo *core.OnyxRepository, message string) error {
|
||||
return fmt.Errorf("failed to get ephemeral commit object: %w", err)
|
||||
}
|
||||
|
||||
// 3. Load workstream collection
|
||||
workstreams, err := loadWorkstreams(repo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load workstreams: %w", err)
|
||||
}
|
||||
// 3. Create workstream manager
|
||||
wsManager := core.NewWorkstreamManager(repo)
|
||||
|
||||
// 4. Get the current workstream
|
||||
currentWorkstream, err := workstreams.GetCurrentWorkstream()
|
||||
currentWorkstream, err := wsManager.GetCurrentWorkstream()
|
||||
if err != nil {
|
||||
return fmt.Errorf("no active workstream. Use 'onx new' to create one: %w", err)
|
||||
}
|
||||
@ -116,16 +111,21 @@ func executeSave(repo *core.OnyxRepository, message string) error {
|
||||
}
|
||||
parentSHA = latestCommit.SHA
|
||||
} else {
|
||||
// For the first commit in the workstream, use the base branch HEAD
|
||||
baseBranch := currentWorkstream.BaseBranch
|
||||
if baseBranch == "" {
|
||||
baseBranch = "main"
|
||||
}
|
||||
// Try to get the base branch reference
|
||||
branchRef := fmt.Sprintf("refs/heads/%s", baseBranch)
|
||||
sha, err := gitBackend.GetRef(branchRef)
|
||||
if err == nil {
|
||||
parentSHA = sha
|
||||
// For the first commit in the workstream, use the base commit
|
||||
baseCommitSHA := currentWorkstream.Metadata["base_commit"]
|
||||
if baseCommitSHA == "" {
|
||||
// Fallback to getting the base branch HEAD
|
||||
baseBranch := currentWorkstream.BaseBranch
|
||||
if baseBranch == "" {
|
||||
baseBranch = "main"
|
||||
}
|
||||
branchRef := fmt.Sprintf("refs/heads/%s", baseBranch)
|
||||
sha, err := gitBackend.GetRef(branchRef)
|
||||
if err == nil {
|
||||
parentSHA = sha
|
||||
}
|
||||
} else {
|
||||
parentSHA = baseCommitSHA
|
||||
}
|
||||
}
|
||||
|
||||
@ -136,29 +136,9 @@ func executeSave(repo *core.OnyxRepository, message string) error {
|
||||
return fmt.Errorf("failed to create commit: %w", err)
|
||||
}
|
||||
|
||||
// 7. Determine next branch number
|
||||
nextNumber := currentWorkstream.GetCommitCount() + 1
|
||||
|
||||
// 8. Create branch ref (e.g., refs/onyx/workstreams/feature-name/commit-1)
|
||||
branchRef := fmt.Sprintf("refs/onyx/workstreams/%s/commit-%d", currentWorkstream.Name, nextNumber)
|
||||
if err := gitBackend.UpdateRef(branchRef, commitSHA); err != nil {
|
||||
return fmt.Errorf("failed to create branch ref: %w", err)
|
||||
}
|
||||
|
||||
// 9. Add commit to workstream
|
||||
workstreamCommit := models.NewWorkstreamCommit(
|
||||
commitSHA,
|
||||
message,
|
||||
"User",
|
||||
parentSHA,
|
||||
currentWorkstream.BaseBranch,
|
||||
branchRef,
|
||||
)
|
||||
currentWorkstream.AddCommit(workstreamCommit)
|
||||
|
||||
// 10. Save updated workstreams
|
||||
if err := saveWorkstreams(repo, workstreams); err != nil {
|
||||
return fmt.Errorf("failed to save workstreams: %w", err)
|
||||
// 7. Add commit to workstream using the manager
|
||||
if err := wsManager.AddCommitToWorkstream(commitSHA, message); err != nil {
|
||||
return fmt.Errorf("failed to add commit to workstream: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@ -173,39 +153,6 @@ func getEphemeralCommit(gitBackend *git.GitBackend) (string, error) {
|
||||
return sha, nil
|
||||
}
|
||||
|
||||
// loadWorkstreams loads the workstream collection from .onx/workstreams.json
|
||||
func loadWorkstreams(repo *core.OnyxRepository) (*models.WorkstreamCollection, error) {
|
||||
workstreamsPath := filepath.Join(repo.GetOnyxPath(), "workstreams.json")
|
||||
|
||||
data, err := os.ReadFile(workstreamsPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read workstreams file: %w", err)
|
||||
}
|
||||
|
||||
workstreams, err := models.DeserializeWorkstreamCollection(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to deserialize workstreams: %w", err)
|
||||
}
|
||||
|
||||
return workstreams, nil
|
||||
}
|
||||
|
||||
// saveWorkstreams saves the workstream collection to .onx/workstreams.json
|
||||
func saveWorkstreams(repo *core.OnyxRepository, workstreams *models.WorkstreamCollection) error {
|
||||
workstreamsPath := filepath.Join(repo.GetOnyxPath(), "workstreams.json")
|
||||
|
||||
data, err := workstreams.Serialize()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to serialize workstreams: %w", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(workstreamsPath, data, 0644); err != nil {
|
||||
return fmt.Errorf("failed to write workstreams file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateCommitMessage validates the commit message
|
||||
func validateCommitMessage(message string) error {
|
||||
// Check if message is empty
|
||||
|
Reference in New Issue
Block a user