gitgrip 0.13.0

Multi-repo workflow tool - manage multiple git repositories as one
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
//! Release command implementation
//!
//! Automates the full release workflow: version bump, changelog, build,
//! branch, commit, push, PR, CI wait, merge, and GitHub release creation.

use std::path::PathBuf;
use std::process::Command;

use chrono::Local;
use serde::Serialize;

use crate::cli::output::Output;
use crate::core::manifest::Manifest;
use crate::core::repo::{filter_repos, RepoInfo};
use crate::platform::{get_platform_adapter, ReleaseResult};

/// Options for the release command
pub struct ReleaseOptions<'a> {
    pub workspace_root: &'a PathBuf,
    pub manifest: &'a Manifest,
    pub version: &'a str,
    pub notes: Option<&'a str>,
    pub dry_run: bool,
    pub skip_pr: bool,
    pub target_repo: Option<&'a str>,
    pub json: bool,
    pub quiet: bool,
    pub timeout: u64,
}

/// JSON output for release steps
#[derive(Serialize)]
struct ReleaseOutputJson {
    version: String,
    steps: Vec<StepResultJson>,
}

#[derive(Serialize)]
struct StepResultJson {
    name: String,
    status: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    files: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    number: Option<u64>,
}

impl StepResultJson {
    fn ok(name: &str) -> Self {
        Self {
            name: name.to_string(),
            status: "ok".to_string(),
            files: None,
            url: None,
            number: None,
        }
    }

    fn skipped(name: &str) -> Self {
        Self {
            name: name.to_string(),
            status: "skipped".to_string(),
            files: None,
            url: None,
            number: None,
        }
    }
}

/// Normalize version string — strip leading 'v' for internal use, ensure tag has 'v' prefix
pub fn normalize_version(version: &str) -> anyhow::Result<(String, String)> {
    let bare = version.strip_prefix('v').unwrap_or(version);

    // Basic semver validation: must have at least X.Y.Z
    let parts: Vec<&str> = bare.split('.').collect();
    if parts.len() < 2 {
        anyhow::bail!(
            "Invalid version '{}'. Expected format: X.Y.Z (e.g. 0.12.4)",
            version
        );
    }
    for part in &parts[..2] {
        if part.parse::<u64>().is_err() {
            anyhow::bail!(
                "Invalid version '{}'. Version components must be numeric.",
                version
            );
        }
    }

    let tag = format!("v{}", bare);
    Ok((bare.to_string(), tag))
}

/// Auto-detect version files in repo directories
pub fn detect_version_files(
    workspace_root: &PathBuf,
    repos: &[RepoInfo],
) -> Vec<(String, PathBuf)> {
    let mut files = Vec::new();

    for repo in repos {
        if repo.reference {
            continue;
        }

        let cargo_toml = repo.absolute_path.join("Cargo.toml");
        if cargo_toml.exists() {
            files.push((repo.name.clone(), cargo_toml));
        }

        let package_json = repo.absolute_path.join("package.json");
        if package_json.exists() {
            files.push((repo.name.clone(), package_json));
        }
    }

    // Also check workspace root
    let root_cargo = workspace_root.join("Cargo.toml");
    if root_cargo.exists() && !files.iter().any(|(_, p)| p == &root_cargo) {
        files.push(("workspace".to_string(), root_cargo));
    }

    let root_package = workspace_root.join("package.json");
    if root_package.exists() && !files.iter().any(|(_, p)| p == &root_package) {
        files.push(("workspace".to_string(), root_package));
    }

    files
}

/// Bump version in a Cargo.toml file
pub fn bump_cargo_toml(path: &PathBuf, new_version: &str, dry_run: bool) -> anyhow::Result<bool> {
    let content = std::fs::read_to_string(path)?;

    // Match the first `version = "..."` in the [package] section
    let re = regex::Regex::new(r#"(?m)^(version\s*=\s*")([^"]+)(")"#)?;
    if !re.is_match(&content) {
        return Ok(false);
    }

    let old_version = re
        .captures(&content)
        .and_then(|c| c.get(2))
        .map(|m| m.as_str().to_string())
        .unwrap_or_default();

    if old_version == new_version {
        return Ok(false); // Already at this version
    }

    let new_content = re
        .replace(&content, format!("${{1}}{}${{3}}", new_version))
        .to_string();

    if !dry_run {
        std::fs::write(path, &new_content)?;
    }

    Ok(true)
}

/// Bump version in a package.json file
pub fn bump_package_json(path: &PathBuf, new_version: &str, dry_run: bool) -> anyhow::Result<bool> {
    let content = std::fs::read_to_string(path)?;

    // Match "version": "..."
    let re = regex::Regex::new(r#"("version"\s*:\s*")([^"]+)(")"#)?;
    if !re.is_match(&content) {
        return Ok(false);
    }

    let old_version = re
        .captures(&content)
        .and_then(|c| c.get(2))
        .map(|m| m.as_str().to_string())
        .unwrap_or_default();

    if old_version == new_version {
        return Ok(false);
    }

    let new_content = re
        .replace(&content, format!("${{1}}{}${{3}}", new_version))
        .to_string();

    if !dry_run {
        std::fs::write(path, &new_content)?;
    }

    Ok(true)
}

/// Bump version in a file using a custom pattern from manifest config
pub fn bump_custom_file(
    path: &PathBuf,
    pattern: &str,
    new_version: &str,
    dry_run: bool,
) -> anyhow::Result<bool> {
    let content = std::fs::read_to_string(path)?;

    // The pattern uses {version} as a placeholder — escape regex-special chars
    // and replace {version} with a capture group
    let escaped = regex::escape(pattern);
    let regex_pattern = escaped.replace(r"\{version\}", r#"([^\s"']+)"#);
    let re = regex::Regex::new(&regex_pattern)?;

    if !re.is_match(&content) {
        return Ok(false);
    }

    let replacement = pattern.replace("{version}", new_version);
    let new_content = re.replace(&content, replacement.as_str()).to_string();

    if new_content == content {
        return Ok(false);
    }

    if !dry_run {
        std::fs::write(path, &new_content)?;
    }

    Ok(true)
}

/// Update CHANGELOG.md with a new version section
pub fn update_changelog(
    path: &PathBuf,
    version_tag: &str,
    notes: Option<&str>,
    dry_run: bool,
) -> anyhow::Result<bool> {
    if !path.exists() {
        return Ok(false);
    }

    let content = std::fs::read_to_string(path)?;
    let date = Local::now().format("%Y-%m-%d").to_string();

    let new_section = if let Some(notes) = notes {
        format!("## [{}] - {}\n\n{}\n\n", version_tag, date, notes)
    } else {
        format!("## [{}] - {}\n\n", version_tag, date)
    };

    // Insert after the first heading line (# Changelog or similar)
    let new_content = if let Some(pos) = content.find('\n') {
        // Check if there's a blank line after the heading
        let insert_pos = if content[pos + 1..].starts_with('\n') {
            pos + 2
        } else {
            pos + 1
        };
        format!(
            "{}\n{}{}",
            &content[..pos],
            new_section,
            &content[insert_pos..]
        )
    } else {
        format!("{}\n\n{}", content, new_section)
    };

    if !dry_run {
        std::fs::write(path, &new_content)?;
    }

    Ok(true)
}

/// Find the target repo for GitHub release creation
fn find_release_target<'a>(
    repos: &'a [RepoInfo],
    target_name: Option<&str>,
) -> anyhow::Result<&'a RepoInfo> {
    if let Some(name) = target_name {
        repos
            .iter()
            .find(|r| r.name == name)
            .ok_or_else(|| anyhow::anyhow!("Repository '{}' not found in manifest", name))
    } else {
        // Auto-detect: first non-reference repo
        repos
            .iter()
            .find(|r| !r.reference)
            .ok_or_else(|| anyhow::anyhow!("No non-reference repos found for release target"))
    }
}

/// Run the release command
pub async fn run_release(opts: ReleaseOptions<'_>) -> anyhow::Result<()> {
    let (bare_version, version_tag) = normalize_version(opts.version)?;
    let repos = filter_repos(opts.manifest, opts.workspace_root, None, None, false);

    let mut steps: Vec<StepResultJson> = Vec::new();

    if !opts.json {
        if opts.dry_run {
            Output::header(&format!("Release {} (dry run)", version_tag));
        } else {
            Output::header(&format!("Releasing {}", version_tag));
        }
        println!();
    }

    // ── Step 1: Bump version files ──────────────────────────────
    if !opts.json && !opts.quiet {
        Output::info(&format!("Step 1: Bumping version to {}", bare_version));
    }

    let release_config = opts
        .manifest
        .workspace
        .as_ref()
        .and_then(|w| w.release.as_ref());

    let mut bumped_files: Vec<String> = Vec::new();

    if let Some(config) = release_config.and_then(|r| r.version_files.as_ref()) {
        // Use manifest-configured version files
        for vf in config {
            let path = opts.workspace_root.join(&vf.path);
            if !path.exists() {
                if !opts.quiet {
                    Output::warning(&format!("Version file not found: {}", vf.path));
                }
                continue;
            }

            let changed = bump_custom_file(&path, &vf.pattern, &bare_version, opts.dry_run)?;
            if changed {
                bumped_files.push(vf.path.clone());
                if !opts.json && !opts.quiet {
                    Output::success(&format!("  Updated {}", vf.path));
                }
            }
        }
    } else {
        // Auto-detect version files
        let detected = detect_version_files(opts.workspace_root, &repos);
        for (repo_name, path) in &detected {
            let file_name = path.file_name().unwrap_or_default().to_string_lossy();
            let relative = path
                .strip_prefix(opts.workspace_root)
                .unwrap_or(path)
                .to_string_lossy()
                .to_string();

            let changed = if file_name == "Cargo.toml" {
                bump_cargo_toml(path, &bare_version, opts.dry_run)?
            } else if file_name == "package.json" {
                bump_package_json(path, &bare_version, opts.dry_run)?
            } else {
                false
            };

            if changed {
                bumped_files.push(relative.clone());
                if !opts.json && !opts.quiet {
                    Output::success(&format!("  Updated {} ({})", relative, repo_name));
                }
            }
        }
    }

    if bumped_files.is_empty() && !opts.quiet {
        Output::warning("  No version files were updated");
    }

    let mut step = StepResultJson::ok("bump-version");
    step.files = Some(bumped_files.clone());
    steps.push(step);

    // Update Cargo.lock if any Cargo.toml was bumped
    if !opts.dry_run {
        for file in &bumped_files {
            if file.ends_with("Cargo.toml") {
                let cargo_dir = opts.workspace_root.join(file);
                let cargo_dir = cargo_dir.parent().unwrap_or(opts.workspace_root);
                if cargo_dir.join("Cargo.lock").exists() {
                    let status = Command::new("cargo")
                        .arg("generate-lockfile")
                        .current_dir(cargo_dir)
                        .status();
                    if let Ok(s) = status {
                        if s.success() && !opts.json && !opts.quiet {
                            Output::success(&format!(
                                "  Updated Cargo.lock in {}",
                                cargo_dir.display()
                            ));
                        }
                    }
                }
            }
        }
    }

    // ── Step 2: Update CHANGELOG ────────────────────────────────
    if !opts.json && !opts.quiet {
        Output::info("Step 2: Updating CHANGELOG");
    }

    let changelog_path = release_config
        .and_then(|r| r.changelog.as_ref())
        .map(|p| opts.workspace_root.join(p))
        .unwrap_or_else(|| opts.workspace_root.join("CHANGELOG.md"));

    let changelog_updated =
        update_changelog(&changelog_path, &version_tag, opts.notes, opts.dry_run)?;

    if changelog_updated {
        let relative = changelog_path
            .strip_prefix(opts.workspace_root)
            .unwrap_or(&changelog_path)
            .to_string_lossy()
            .to_string();
        if !opts.json && !opts.quiet {
            Output::success(&format!("  Updated {}", relative));
        }
        steps.push(StepResultJson::ok("changelog"));
    } else {
        if !opts.json && !opts.quiet {
            Output::info("  No CHANGELOG.md found, skipping");
        }
        steps.push(StepResultJson::skipped("changelog"));
    }

    // ── Step 3: Build ───────────────────────────────────────────
    if !opts.json && !opts.quiet {
        Output::info("Step 3: Building");
    }

    let mut built_any = false;
    for repo in &repos {
        let build_cmd = repo.agent.as_ref().and_then(|a| a.build.as_deref());
        let Some(cmd) = build_cmd else {
            continue;
        };

        if opts.dry_run {
            if !opts.json && !opts.quiet {
                Output::info(&format!("  Would run in {}: {}", repo.name, cmd));
            }
            built_any = true;
            continue;
        }

        if !opts.json && !opts.quiet {
            Output::info(&format!("  Building {} ({})", repo.name, cmd));
        }

        let status = Command::new("sh")
            .arg("-c")
            .arg(cmd)
            .current_dir(&repo.absolute_path)
            .status()?;

        if !status.success() {
            anyhow::bail!(
                "Build failed for '{}' (exit code: {:?})",
                repo.name,
                status.code()
            );
        }

        built_any = true;
        if !opts.json && !opts.quiet {
            Output::success(&format!("  {} built successfully", repo.name));
        }
    }

    if built_any {
        steps.push(StepResultJson::ok("build"));
    } else {
        if !opts.json && !opts.quiet {
            Output::info("  No agent.build configured, skipping");
        }
        steps.push(StepResultJson::skipped("build"));
    }

    // ── Steps 4-9: PR workflow (skip if --skip-pr) ──────────────
    if opts.skip_pr {
        if !opts.json && !opts.quiet {
            Output::info("Skipping PR workflow (--skip-pr)");
        }
        steps.push(StepResultJson::skipped("branch"));
        steps.push(StepResultJson::skipped("pr"));
        steps.push(StepResultJson::skipped("ci"));
        steps.push(StepResultJson::skipped("merge"));
    } else {
        // Step 4: Create branch
        if !opts.json && !opts.quiet {
            Output::info(&format!("Step 4: Creating branch release/{}", version_tag));
        }

        if opts.dry_run {
            if !opts.json && !opts.quiet {
                Output::info(&format!(
                    "  Would create branch release/{} across repos",
                    version_tag
                ));
            }
            steps.push(StepResultJson::ok("branch"));
        } else {
            crate::cli::commands::branch::run_branch(
                crate::cli::commands::branch::BranchOptions {
                    workspace_root: opts.workspace_root,
                    manifest: opts.manifest,
                    name: Some(&format!("release/{}", version_tag)),
                    delete: false,
                    move_commits: false,
                    repos_filter: None,
                    group_filter: None,
                    json: opts.json,
                },
            )?;
            steps.push(StepResultJson::ok("branch"));
        }

        // Step 5: Stage all changes
        if !opts.json && !opts.quiet {
            Output::info("Step 5: Staging and committing changes");
        }

        if opts.dry_run {
            if !opts.json && !opts.quiet {
                Output::info(&format!(
                    "  Would commit: \"chore: release {}\"",
                    version_tag
                ));
            }
        } else {
            // Stage all changes
            crate::cli::commands::add::run_add(
                opts.workspace_root,
                opts.manifest,
                &[".".to_string()],
            )?;

            // Commit
            crate::cli::commands::commit::run_commit(
                opts.workspace_root,
                opts.manifest,
                &format!("chore: release {}", version_tag),
                false,
                opts.json,
            )?;
        }

        // Step 6: Push
        if !opts.json && !opts.quiet {
            Output::info("Step 6: Pushing to remote");
        }

        if opts.dry_run {
            if !opts.json && !opts.quiet {
                Output::info("  Would push with upstream tracking");
            }
        } else {
            crate::cli::commands::push::run_push(
                opts.workspace_root,
                opts.manifest,
                true,  // set_upstream
                false, // force
                opts.quiet,
                opts.json,
            )?;
        }

        // Step 7: Create PR
        if !opts.json && !opts.quiet {
            Output::info("Step 7: Creating pull request");
        }

        if opts.dry_run {
            if !opts.json && !opts.quiet {
                Output::info(&format!(
                    "  Would create PR: \"chore: release {}\"",
                    version_tag
                ));
            }
            steps.push(StepResultJson::ok("pr"));
        } else {
            crate::cli::commands::pr::run_pr_create(
                opts.workspace_root,
                opts.manifest,
                Some(&format!("chore: release {}", version_tag)),
                opts.notes,
                false, // draft
                false, // push (already pushed)
                false, // dry_run
                opts.json,
            )
            .await?;
            steps.push(StepResultJson::ok("pr"));
        }

        // Step 8: Wait for CI + Merge
        if !opts.json && !opts.quiet {
            Output::info("Step 8: Waiting for CI and merging");
        }

        if opts.dry_run {
            if !opts.json && !opts.quiet {
                Output::info(&format!(
                    "  Would wait {}s for CI, then merge",
                    opts.timeout
                ));
            }
            steps.push(StepResultJson::ok("ci"));
            steps.push(StepResultJson::ok("merge"));
        } else {
            crate::cli::commands::pr::run_pr_merge(
                opts.workspace_root,
                opts.manifest,
                None,  // method (default)
                false, // force
                false, // update
                false, // auto
                opts.json,
                true, // wait
                opts.timeout,
            )
            .await?;
            steps.push(StepResultJson::ok("ci"));
            steps.push(StepResultJson::ok("merge"));
        }

        // Step 9: Sync after merge
        if !opts.json && !opts.quiet {
            Output::info("Step 9: Syncing after merge");
        }

        if !opts.dry_run {
            // Checkout default branch
            let default_branch = repos
                .first()
                .map(|r| r.default_branch.as_str())
                .unwrap_or("main");
            crate::cli::commands::checkout::run_checkout(
                opts.workspace_root,
                opts.manifest,
                default_branch,
                false,
            )?;

            // Sync
            crate::cli::commands::sync::run_sync(
                opts.workspace_root,
                opts.manifest,
                false, // force
                opts.quiet,
                None,  // group
                false, // sequential
                false, // reset_refs
                opts.json,
                false, // no_hooks
            )
            .await?;
        }
    }

    // ── Step 10: Create GitHub release ──────────────────────────
    if !opts.json && !opts.quiet {
        Output::info("Step 10: Creating GitHub release");
    }

    let target_repo = find_release_target(&repos, opts.target_repo)?;

    if opts.dry_run {
        if !opts.json && !opts.quiet {
            Output::info(&format!(
                "  Would create release {} on {}/{} ({})",
                version_tag, target_repo.owner, target_repo.repo, target_repo.name
            ));
        }
        let mut step = StepResultJson::ok("release");
        step.url = Some(format!(
            "https://github.com/{}/{}/releases/tag/{}",
            target_repo.owner, target_repo.repo, version_tag
        ));
        steps.push(step);
    } else {
        let platform = get_platform_adapter(
            target_repo.platform_type,
            target_repo.platform_base_url.as_deref(),
        );

        let default_branch = &target_repo.default_branch;

        let result: ReleaseResult = platform
            .create_release(
                &target_repo.owner,
                &target_repo.repo,
                &version_tag,
                &version_tag,
                opts.notes,
                default_branch,
                false, // draft
                false, // prerelease
            )
            .await
            .map_err(|e| anyhow::anyhow!("Failed to create release: {}", e))?;

        if !opts.json && !opts.quiet {
            Output::success(&format!("  Created release: {}", result.url));
        }

        let mut step = StepResultJson::ok("release");
        step.url = Some(result.url);
        steps.push(step);
    }

    // ── Step 11: Post-release hooks ─────────────────────────────
    if let Some(hooks) = release_config.and_then(|r| r.post_release.as_ref()) {
        if !opts.json && !opts.quiet {
            Output::info("Step 11: Running post-release hooks");
        }

        for hook in hooks {
            let cmd = hook.command.replace("{version}", &bare_version);
            let display_name = hook.name.as_deref().unwrap_or(&cmd);

            if opts.dry_run {
                if !opts.json && !opts.quiet {
                    Output::info(&format!("  Would run: {}", display_name));
                }
                continue;
            }

            if !opts.json && !opts.quiet {
                Output::info(&format!("  Running: {}", display_name));
            }

            let working_dir = hook
                .cwd
                .as_ref()
                .map(|p| opts.workspace_root.join(p))
                .unwrap_or_else(|| opts.workspace_root.clone());

            let status = Command::new("sh")
                .arg("-c")
                .arg(&cmd)
                .current_dir(&working_dir)
                .status()?;

            if !status.success() {
                anyhow::bail!(
                    "Post-release hook '{}' failed (exit code: {:?})",
                    display_name,
                    status.code()
                );
            }

            if !opts.json && !opts.quiet {
                Output::success(&format!("  {} completed", display_name));
            }
        }
        steps.push(StepResultJson::ok("post-release"));
    }

    // ── Output ──────────────────────────────────────────────────
    if opts.json {
        let output = ReleaseOutputJson {
            version: bare_version,
            steps,
        };
        println!("{}", serde_json::to_string_pretty(&output)?);
    } else if !opts.quiet {
        println!();
        if opts.dry_run {
            Output::success(&format!("Dry run complete for {}", version_tag));
        } else {
            Output::success(&format!("Released {}", version_tag));
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_normalize_version_with_v_prefix() {
        let (bare, tag) = normalize_version("v1.2.3").unwrap();
        assert_eq!(bare, "1.2.3");
        assert_eq!(tag, "v1.2.3");
    }

    #[test]
    fn test_normalize_version_without_prefix() {
        let (bare, tag) = normalize_version("1.2.3").unwrap();
        assert_eq!(bare, "1.2.3");
        assert_eq!(tag, "v1.2.3");
    }

    #[test]
    fn test_normalize_version_two_parts() {
        let (bare, tag) = normalize_version("1.0").unwrap();
        assert_eq!(bare, "1.0");
        assert_eq!(tag, "v1.0");
    }

    #[test]
    fn test_normalize_version_prerelease() {
        let (bare, tag) = normalize_version("v1.2.3-rc.1").unwrap();
        assert_eq!(bare, "1.2.3-rc.1");
        assert_eq!(tag, "v1.2.3-rc.1");
    }

    #[test]
    fn test_normalize_version_invalid() {
        assert!(normalize_version("abc").is_err());
        assert!(normalize_version("x.y.z").is_err());
    }

    #[test]
    fn test_bump_cargo_toml() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("Cargo.toml");
        std::fs::write(
            &path,
            r#"[package]
name = "test"
version = "0.1.0"
edition = "2021"
"#,
        )
        .unwrap();

        let changed = bump_cargo_toml(&path, "0.2.0", false).unwrap();
        assert!(changed);

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains(r#"version = "0.2.0""#));
        assert!(!content.contains(r#"version = "0.1.0""#));
    }

    #[test]
    fn test_bump_cargo_toml_dry_run() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("Cargo.toml");
        std::fs::write(
            &path,
            r#"[package]
name = "test"
version = "0.1.0"
"#,
        )
        .unwrap();

        let changed = bump_cargo_toml(&path, "0.2.0", true).unwrap();
        assert!(changed);

        // File should NOT be modified in dry run
        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains(r#"version = "0.1.0""#));
    }

    #[test]
    fn test_bump_cargo_toml_same_version() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("Cargo.toml");
        std::fs::write(
            &path,
            r#"[package]
name = "test"
version = "0.2.0"
"#,
        )
        .unwrap();

        let changed = bump_cargo_toml(&path, "0.2.0", false).unwrap();
        assert!(!changed);
    }

    #[test]
    fn test_bump_package_json() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("package.json");
        std::fs::write(
            &path,
            r#"{
  "name": "test",
  "version": "1.0.0"
}"#,
        )
        .unwrap();

        let changed = bump_package_json(&path, "1.1.0", false).unwrap();
        assert!(changed);

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains(r#""version": "1.1.0""#));
    }

    #[test]
    fn test_bump_custom_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("version.txt");
        std::fs::write(&path, "APP_VERSION=1.0.0\n").unwrap();

        let changed = bump_custom_file(&path, "APP_VERSION={version}", "2.0.0", false).unwrap();
        assert!(changed);

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains("APP_VERSION=2.0.0"));
    }

    #[test]
    fn test_update_changelog() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("CHANGELOG.md");
        std::fs::write(
            &path,
            "# Changelog\n\n## [v0.1.0] - 2025-01-01\n\n- Initial release\n",
        )
        .unwrap();

        let updated = update_changelog(&path, "v0.2.0", Some("New features"), false).unwrap();
        assert!(updated);

        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains("## [v0.2.0]"));
        assert!(content.contains("New features"));
        // Old entry should still be there
        assert!(content.contains("## [v0.1.0]"));
    }

    #[test]
    fn test_update_changelog_no_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("CHANGELOG.md");

        let updated = update_changelog(&path, "v0.1.0", None, false).unwrap();
        assert!(!updated);
    }
}