pushWorkflowFiles commits and pushes the workflow files to the repository
(ctx context.Context, workflowName string, files []string, refOverride string, verbose bool)
| 380 | |
| 381 | // pushWorkflowFiles commits and pushes the workflow files to the repository |
| 382 | func pushWorkflowFiles(ctx context.Context, workflowName string, files []string, refOverride string, verbose bool) error { |
| 383 | if ctx == nil { |
| 384 | return errors.New("context is required") |
| 385 | } |
| 386 | runPushLog.Printf("Pushing %d files for workflow: %s", len(files), workflowName) |
| 387 | runPushLog.Printf("Files to push: %v", files) |
| 388 | |
| 389 | if verbose { |
| 390 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("Staging %d files for commit", len(files)))) |
| 391 | for _, file := range files { |
| 392 | fmt.Fprintf(os.Stderr, " - %s\n", file) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // Stage all files |
| 397 | gitArgs := append([]string{"add"}, files...) |
| 398 | runPushLog.Printf("Executing git command: git %v", gitArgs) |
| 399 | cmd := exec.CommandContext(ctx, "git", gitArgs...) |
| 400 | if output, err := cmd.CombinedOutput(); err != nil { |
| 401 | runPushLog.Printf("Failed to stage files: %v, output: %s", err, string(output)) |
| 402 | return fmt.Errorf("failed to stage files: %w\nOutput: %s", err, string(output)) |
| 403 | } |
| 404 | runPushLog.Printf("Successfully staged %d files", len(files)) |
| 405 | |
| 406 | if verbose { |
| 407 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Files staged successfully")) |
| 408 | } |
| 409 | |
| 410 | // Check if there are any staged files in git (after we've staged our files) |
| 411 | runPushLog.Printf("Checking staged files with git diff --cached --name-only") |
| 412 | statusCmd := exec.CommandContext(ctx, "git", "diff", "--cached", "--name-only") |
| 413 | statusOutput, err := statusCmd.CombinedOutput() |
| 414 | if err != nil { |
| 415 | runPushLog.Printf("Failed to check git status: %v, output: %s", err, string(statusOutput)) |
| 416 | return fmt.Errorf("failed to check git status: %w\nOutput: %s", err, string(statusOutput)) |
| 417 | } |
| 418 | runPushLog.Printf("Git status output: %s", string(statusOutput)) |
| 419 | |
| 420 | // Check if there are no staged changes (nothing to commit) |
| 421 | if strings.TrimSpace(string(statusOutput)) == "" { |
| 422 | runPushLog.Printf("No staged changes detected") |
| 423 | if verbose { |
| 424 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("No changes to commit")) |
| 425 | } |
| 426 | runPushLog.Print("No changes to commit") |
| 427 | return nil |
| 428 | } |
| 429 | |
| 430 | // Now that we know there are changes to commit, check that current branch matches --ref value if specified |
| 431 | // This happens after we've determined there are actual changes, so we don't fail unnecessarily |
| 432 | if refOverride != "" { |
| 433 | runPushLog.Printf("Checking if current branch matches --ref value: %s", refOverride) |
| 434 | currentBranch, err := getCurrentBranch() |
| 435 | if err != nil { |
| 436 | runPushLog.Printf("Failed to determine current branch: %v", err) |
| 437 | return fmt.Errorf("failed to determine current branch: %w", err) |
| 438 | } |
| 439 | runPushLog.Printf("Current branch: %s", currentBranch) |