devflow-core 1.4.0

Agent-agnostic development workflow state machine
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
//! Stage-transition hooks.
//!
//! Branching, docs, changelog, and version bumps are no longer workflow stages
//! (as they were in v0.x). They are *hooks* that fire at specific stage
//! transitions. [`hooks_for_transition`] maps a `(from, to)` stage move to the
//! hooks that should run, and [`Hook::run`] executes one.

use crate::config::GitFlowConfig;
use crate::git::GitFlow;
use crate::stage::Stage;
use crate::version;
use std::path::{Path, PathBuf};
use std::process::Command;
use tracing::{info, warn};

/// A side-effecting action that fires at a stage transition.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Hook {
    /// Create the `feature/phase-NN` branch from develop.
    BranchCreate,
    /// Delete the merged feature branch after Ship.
    BranchCleanup,
    /// Regenerate and commit docs.
    DocsUpdate,
    /// Merge the phase feature branch into develop before release bookkeeping.
    Merge,
    /// Append a CHANGELOG entry.
    ChangelogAppend,
    /// Compute and write the next version, then tag it.
    VersionBump,
}

/// Context passed to every hook.
#[derive(Debug, Clone)]
pub struct HookContext {
    /// Phase the workflow is on.
    pub phase: u32,
    /// Project root.
    pub project_root: PathBuf,
    /// Stage the workflow is entering.
    pub stage: Stage,
    /// Git-flow branch model.
    pub git_flow: GitFlowConfig,
    /// The version `VersionBump` actually tagged, set once it runs (GAP-7).
    /// `ChangelogAppend` reads this instead of re-deriving the version from
    /// disk, so the changelog heading and the git tag never desync — in
    /// particular when there is no version file and `version::read_version`
    /// would otherwise error and fall back to the `unreleased` literal.
    pub shipped_version: Option<String>,
}

/// Errors produced by hooks.
#[derive(Debug, thiserror::Error)]
pub enum HookError {
    /// A git-flow operation failed.
    #[error(transparent)]
    Git(#[from] crate::git::GitError),
    /// A version operation failed.
    #[error(transparent)]
    Version(#[from] version::VersionError),
    /// Filesystem operation failed.
    #[error("hook I/O failed: {0}")]
    Io(#[from] std::io::Error),
}

impl Hook {
    /// Run this hook against the given context.
    pub fn run(&self, ctx: &mut HookContext) -> Result<(), HookError> {
        match self {
            Hook::BranchCreate => branch_create(ctx),
            Hook::BranchCleanup => branch_cleanup(ctx),
            Hook::DocsUpdate => docs_update(ctx),
            Hook::Merge => merge_feature(ctx),
            Hook::ChangelogAppend => changelog_append(ctx),
            Hook::VersionBump => version_bump(ctx),
        }
    }
}

/// Which hooks fire when moving `from` → `to`.
///
/// - Validate → Ship: docs are finalized before shipping.
/// - Ship → (done): merge + version bump + changelog + branch cleanup.
/// - everything else: none.
///
/// `ChangelogAppend` deliberately does NOT run here (WR-04, 17-12): a
/// changelog heading naming a release is only true once `VersionBump` has
/// actually cut the tag, and `VersionBump` runs in [`hooks_after_ship`],
/// strictly after this transition.
pub fn hooks_for_transition(from: Stage, to: Stage) -> Vec<Hook> {
    match (from, to) {
        (Stage::Validate, Stage::Ship) => vec![Hook::DocsUpdate],
        _ => Vec::new(),
    }
}

/// Hooks that fire after Ship completes (the workflow's terminal transition).
///
/// `ChangelogAppend` runs strictly after `VersionBump` (WR-04, 17-12) — the
/// entry must describe the version `VersionBump` actually wrote and tagged,
/// never a version computed independently of it. It runs before
/// `BranchCleanup` so a changelog failure still stops short of deleting the
/// feature branch (`run_checkout_hooks`' terminal-batch fail-fast breaks on
/// the first error in this batch).
pub fn hooks_after_ship() -> Vec<Hook> {
    vec![
        Hook::Merge,
        Hook::VersionBump,
        Hook::ChangelogAppend,
        Hook::BranchCleanup,
    ]
}

fn branch_create(ctx: &HookContext) -> Result<(), HookError> {
    let git = GitFlow::new(&ctx.project_root);
    let branch = git.feature_start(ctx.phase)?;
    info!("BranchCreate: created {branch}");
    Ok(())
}

fn branch_cleanup(ctx: &HookContext) -> Result<(), HookError> {
    let git = GitFlow::new(&ctx.project_root);
    let branch = format!("{}phase-{:02}", ctx.git_flow.feature_prefix, ctx.phase);
    if git.branch_exists(&branch) {
        // Non-force cleanup is intentional: never discard unmerged work.
        match git.delete_branch(&branch, false) {
            Ok(()) => info!("BranchCleanup: deleted {branch}"),
            Err(err) => {
                let message = err.to_string();
                if message.contains("not fully merged") || message.contains("not yet merged") {
                    warn!(
                        "BranchCleanup: feature branch {branch} is not merged yet — left in place"
                    );
                } else {
                    warn!("BranchCleanup: could not delete {branch}: {err}");
                }
            }
        }
    }
    Ok(())
}

fn merge_feature(ctx: &HookContext) -> Result<(), HookError> {
    let git = GitFlow::new(&ctx.project_root);
    let branch = format!("{}phase-{:02}", ctx.git_flow.feature_prefix, ctx.phase);
    if !git.branch_exists(&branch) {
        return Err(crate::git::GitError::Command(format!(
            "feature branch `{branch}` is missing; refusing to report an unproven merge"
        ))
        .into());
    }
    if git.is_merged_into_develop(ctx.phase) {
        info!("Merge: {branch} is already merged; nothing to merge");
        crate::events::emit(
            &ctx.project_root,
            ctx.phase,
            "merge_result",
            serde_json::json!({"merged": false, "branch": branch}),
        );
        return Ok(());
    }

    git.merge_feature_into_develop(ctx.phase)?;
    info!("Merge: merged {branch} into develop");
    crate::events::emit(
        &ctx.project_root,
        ctx.phase,
        "merge_result",
        serde_json::json!({"merged": true, "branch": branch}),
    );
    Ok(())
}

fn docs_update(ctx: &HookContext) -> Result<(), HookError> {
    let output = Command::new("sh")
        .arg("-c")
        .arg("cargo doc --no-deps 2>&1")
        .current_dir(&ctx.project_root)
        .output();
    match output {
        Ok(out) if out.status.success() => {
            // Commit any doc changes; ignore "nothing to commit".
            let git = GitFlow::new(&ctx.project_root);
            if let Err(err) = git.commit_all("docs: update generated docs") {
                warn!("DocsUpdate: commit failed: {err}");
            } else {
                info!("DocsUpdate: docs regenerated and committed");
            }
        }
        Ok(_) => warn!("DocsUpdate: cargo doc reported a failure; skipping commit"),
        Err(err) => warn!("DocsUpdate: could not run cargo doc: {err}"),
    }
    Ok(())
}

fn changelog_append(ctx: &mut HookContext) -> Result<(), HookError> {
    // Prefer the version VersionBump (which runs immediately before this
    // hook in hooks_after_ship()) actually tagged, handed through
    // batch-scoped context state (GAP-7) — this is the only source that's
    // correct with no version file present. Fall back to
    // version::read_version (deliberately NOT version::compute_version,
    // which recomputes MINOR from the live git tag count that VersionBump's
    // own tag just incremented, yielding a version one higher than the tag
    // actually cut — WR-04, 17-12), then to the `unreleased` literal.
    let version = ctx.shipped_version.clone().unwrap_or_else(|| {
        version::read_version(&ctx.project_root)
            .map(|v| v.to_string())
            .unwrap_or_else(|_| "unreleased".to_string())
    });
    let path = ctx.project_root.join("CHANGELOG.md");
    let existing = std::fs::read_to_string(&path).unwrap_or_default();
    let updated = crate::ship::prepend_changelog(&existing, &version, &today());
    std::fs::write(&path, updated)?;
    // Commit the write. Round 2's WR-04 finding: this hook used to write and
    // never commit, and docs_update — the only committing hook — ran first
    // in the old (Validate→Ship) batch order, so the entry was left dirty
    // and lost when Merge/BranchCleanup ran. Scoped to CHANGELOG.md (not
    // commit_all) so this hook never sweeps in unrelated dirty state. A
    // failed commit propagates as an error so the terminal batch's fail-fast
    // stops BranchCleanup from running against an uncommitted entry.
    let git = GitFlow::new(&ctx.project_root);
    git.commit_path(
        "CHANGELOG.md",
        &format!("docs: add changelog entry for {version}"),
    )?;
    info!("ChangelogAppend: wrote and committed entry for {version}");
    Ok(())
}

fn version_bump(ctx: &mut HookContext) -> Result<(), HookError> {
    let version = version::compute_version(&ctx.project_root)?;
    let git = GitFlow::new(&ctx.project_root);
    // Write the computed version into the version file when one exists, and
    // commit that write before tagging (17-12: previously left uncommitted,
    // so the tag named a version the tagged commit itself didn't contain,
    // and the working tree stayed dirty through the rest of the terminal
    // batch — the same "write without committing" defect WR-04 named for
    // ChangelogAppend, just not called out there).
    if has_version_file(&ctx.project_root) {
        let path = version::write_version(&ctx.project_root, &version)?;
        if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
            git.commit_path(name, &format!("chore: bump version to {version}"))?;
        }
        info!("VersionBump: wrote {version} to {}", path.display());
    } else {
        warn!("VersionBump: no supported version file; tagging only");
    }
    let tag = format!("v{version}");
    git.tag(&tag)?;
    // Hand the tagged version to ChangelogAppend via batch-scoped context
    // state (GAP-7) — on both branches above, since both tag. Without this,
    // ChangelogAppend re-derives the version from disk and, with no version
    // file, falls back to the `unreleased` literal while the tag names a
    // real version.
    ctx.shipped_version = Some(version.to_string());
    info!("VersionBump: tagged {tag}");
    Ok(())
}

/// Today's date as YYYY-MM-DD (best-effort via the `date` command).
fn today() -> String {
    Command::new("date")
        .arg("+%Y-%m-%d")
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| "unreleased".to_string())
}

/// Whether a project has a version file, used by callers to decide if a version
/// bump is meaningful.
pub fn has_version_file(project_root: &Path) -> bool {
    version::detect_version_file(project_root).is_some()
}

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

    fn git(root: &Path, args: &[&str]) {
        let ok = Command::new("git")
            .args(args)
            .current_dir(root)
            .output()
            .unwrap()
            .status
            .success();
        assert!(ok, "git {args:?} failed");
    }

    fn init_repo(root: &Path) {
        init_repo_with_options(root, true);
    }

    /// Same as [`init_repo`], but lets a test choose whether a version file
    /// gets written. `init_repo` unconditionally wrote `Cargo.toml`, which
    /// made `version_bump`'s no-version-file `else` branch unreachable from
    /// the batch tests (GAP-7). `init_repo` delegates here with `true`, so
    /// its observable effect for every existing test is unchanged byte for
    /// byte.
    fn init_repo_with_options(root: &Path, write_version_file: bool) {
        git(root, &["init", "-q"]);
        git(root, &["config", "user.email", "test@example.com"]);
        git(root, &["config", "user.name", "Test"]);
        git(root, &["config", "commit.gpgsign", "false"]);
        git(root, &["config", "tag.gpgsign", "false"]);
        git(root, &["config", "core.hooksPath", "/dev/null"]);
        if write_version_file {
            std::fs::write(root.join("Cargo.toml"), "[package]\nversion = \"2.0.0\"\n").unwrap();
        } else {
            std::fs::write(root.join("README.md"), "no version file in this repo\n").unwrap();
        }
        git(root, &["add", "."]);
        git(root, &["commit", "-q", "-m", "init"]);
        git(root, &["branch", "-M", "main"]);
        git(root, &["checkout", "-q", "-b", "develop"]);
    }

    fn ctx(root: &Path, stage: Stage) -> HookContext {
        HookContext {
            phase: 11,
            project_root: root.to_path_buf(),
            stage,
            git_flow: GitFlowConfig::default(),
            shipped_version: None,
        }
    }

    #[test]
    fn transition_map_finalizes_docs_only_before_ship() {
        // WR-04 (17-12): ChangelogAppend no longer fires here — a changelog
        // heading naming a release can't be true before VersionBump (which
        // runs in hooks_after_ship, strictly after this transition) cuts the
        // tag it describes.
        assert_eq!(
            hooks_for_transition(Stage::Validate, Stage::Ship),
            vec![Hook::DocsUpdate]
        );
        assert!(hooks_for_transition(Stage::Define, Stage::Plan).is_empty());
        assert!(hooks_for_transition(Stage::Code, Stage::Validate).is_empty());
    }

    #[test]
    fn validate_to_ship_hooks_do_not_touch_changelog() {
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        let mut context = ctx(dir.path(), Stage::Ship);

        for hook in hooks_for_transition(Stage::Validate, Stage::Ship) {
            hook.run(&mut context).unwrap();
        }

        assert!(!dir.path().join("CHANGELOG.md").exists());
    }

    #[test]
    fn after_ship_runs_version_changelog_then_cleanup() {
        // WR-04 (17-12): ChangelogAppend strictly after VersionBump (so it
        // can read back the version VersionBump just tagged), and before
        // BranchCleanup (so a changelog failure still stops short of
        // deleting the feature branch).
        assert_eq!(
            hooks_after_ship(),
            vec![
                Hook::Merge,
                Hook::VersionBump,
                Hook::ChangelogAppend,
                Hook::BranchCleanup,
            ]
        );
    }

    #[test]
    fn branch_create_makes_feature_branch() {
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        Hook::BranchCreate
            .run(&mut ctx(dir.path(), Stage::Define))
            .unwrap();
        assert!(GitFlow::new(dir.path()).branch_exists("feature/phase-11"));
    }

    #[test]
    fn changelog_append_writes_entry() {
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        Hook::ChangelogAppend
            .run(&mut ctx(dir.path(), Stage::Ship))
            .unwrap();
        let changelog = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();
        assert!(changelog.contains("# Changelog"));
    }

    #[test]
    fn changelog_append_commits_its_own_write() {
        // WR-04 (Round 2, 17-12): changelog_append must not leave its write
        // uncommitted — that's what let the entry get orphaned when
        // BranchCleanup ran before it.
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        Hook::ChangelogAppend
            .run(&mut ctx(dir.path(), Stage::Ship))
            .unwrap();

        let status = git_output(dir.path(), &["status", "--porcelain"]);
        assert!(status.is_empty(), "expected clean tree, got: {status}");

        let committed_files = git_output(dir.path(), &["log", "-1", "--name-only"]);
        assert!(
            committed_files.contains("CHANGELOG.md"),
            "expected CHANGELOG.md in the latest commit, got: {committed_files}"
        );
    }

    #[test]
    fn version_bump_tags_repo() {
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        // Hybrid SemVer: major 2 (Cargo.toml), minor 0 (no tags), patch from
        // the commit count since the last tag — one `init` commit → v2.0.1.
        let expected = format!("v{}", version::compute_version(dir.path()).unwrap());
        Hook::VersionBump
            .run(&mut ctx(dir.path(), Stage::Ship))
            .unwrap();
        let tags = Command::new("git")
            .arg("tag")
            .current_dir(dir.path())
            .output()
            .unwrap();
        assert!(String::from_utf8_lossy(&tags.stdout).contains(&expected));
    }

    #[test]
    fn terminal_hooks_version_post_merge_develop() {
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        git(dir.path(), &["checkout", "-q", "-b", "feature/phase-11"]);
        std::fs::write(dir.path().join("feature.txt"), "phase work\n").unwrap();
        git(dir.path(), &["add", "feature.txt"]);
        git(dir.path(), &["commit", "-q", "-m", "phase work"]);

        let feature_tip = git_output(dir.path(), &["rev-parse", "feature/phase-11"]);
        let pre_merge_count = git_output(dir.path(), &["rev-list", "--count", "HEAD"]);

        let mut context = ctx(dir.path(), Stage::Ship);
        for hook in hooks_after_ship() {
            hook.run(&mut context).unwrap();
        }

        git(
            dir.path(),
            &["merge-base", "--is-ancestor", &feature_tip, "develop"],
        );
        let post_merge_count = git_output(dir.path(), &["rev-list", "--count", "develop"]);
        assert_ne!(pre_merge_count, post_merge_count);

        // Exactly one tag was created, and it names the version VersionBump
        // actually wrote to the version file (not a raw rev-list count,
        // which would now also include VersionBump's own commit and
        // ChangelogAppend's — both introduced by 17-12).
        let all_tags = git_output(dir.path(), &["tag"]);
        assert_eq!(all_tags.lines().count(), 1, "expected exactly one tag");
        let tag = all_tags.trim().to_string();
        let version_file_version = version::read_version(dir.path()).unwrap().to_string();
        assert_eq!(tag, format!("v{version_file_version}"));

        // The tag no longer points at develop's tip — ChangelogAppend's
        // commit (17-12) lands after it.
        let develop_tip = git_output(dir.path(), &["rev-parse", "develop"]);
        let tag_commit = git_output(dir.path(), &["rev-parse", &format!("{tag}^{{commit}}")]);
        assert_ne!(develop_tip, tag_commit);
    }

    #[test]
    fn after_ship_batch_changelog_tag_and_version_file_agree_and_tree_is_clean() {
        // Full regression for WR-04 (17-12): drives the whole hooks_after_ship
        // batch and asserts three-way agreement between the changelog
        // heading, the created git tag, and the version file's version —
        // plus the Round 2 WR-04 commit requirement (clean tree, CHANGELOG.md
        // present in a commit). Must fail against pre-17-12 main: the old
        // batch order never ran ChangelogAppend here at all (it fired at
        // Validate→Ship, before any tag existed), so CHANGELOG.md would not
        // exist after running only hooks_after_ship().
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        // Merge fires events::emit, which creates .devflow/ — gitignored in
        // every real project (WR-11); mirror that here so the clean-tree
        // assertion below checks hook writes, not test-fixture telemetry.
        std::fs::write(dir.path().join(".gitignore"), ".devflow/\n").unwrap();
        git(dir.path(), &["add", ".gitignore"]);
        git(dir.path(), &["commit", "-q", "-m", "add gitignore"]);
        git(dir.path(), &["checkout", "-q", "-b", "feature/phase-11"]);
        std::fs::write(dir.path().join("feature.txt"), "phase work\n").unwrap();
        git(dir.path(), &["add", "feature.txt"]);
        git(dir.path(), &["commit", "-q", "-m", "phase work"]);

        let mut context = ctx(dir.path(), Stage::Ship);
        for hook in hooks_after_ship() {
            hook.run(&mut context).unwrap();
        }

        // Exactly one tag was created by this batch (init_repo creates none).
        let all_tags = git_output(dir.path(), &["tag"]);
        assert_eq!(all_tags.lines().count(), 1, "expected exactly one tag");
        let tag = all_tags.trim().to_string();

        let changelog = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();
        let changelog_version = changelog
            .lines()
            .find(|l| l.starts_with("## "))
            .and_then(|l| l.trim_start_matches("## ").split(' ').next())
            .unwrap()
            .to_string();

        let version_file_version = version::read_version(dir.path()).unwrap().to_string();

        assert_eq!(
            tag,
            format!("v{changelog_version}"),
            "tag must match the changelog heading version"
        );
        assert_eq!(
            changelog_version, version_file_version,
            "changelog heading must match the version file's version"
        );

        // Round 2 WR-04: the changelog write must be committed, and the
        // working tree must be clean after the full batch.
        let status = git_output(dir.path(), &["status", "--porcelain"]);
        assert!(status.is_empty(), "expected clean tree, got: {status}");
        let committed_files = git_output(dir.path(), &["log", "-1", "--name-only"]);
        assert!(
            committed_files.contains("CHANGELOG.md"),
            "expected CHANGELOG.md in the latest commit, got: {committed_files}"
        );
    }

    #[test]
    fn after_ship_batch_with_no_version_file_keeps_tag_and_changelog_in_sync() {
        // GAP-7: with no version file, version_bump takes the `else` branch
        // (warns, tags only) and still tags v{compute_version()}.
        // changelog_append then calls version::read_version, which errors
        // with no version file present, and falls back to the literal
        // "unreleased" -- desyncing the tag from the changelog heading.
        // init_repo unconditionally writes Cargo.toml, so this branch is
        // unreachable from the other batch tests; init_repo_with_options
        // reaches it without changing init_repo's own behavior.
        let dir = tempfile::tempdir().unwrap();
        init_repo_with_options(dir.path(), false);
        // Mirror the existing batch test's .gitignore / feature-branch setup
        // so the run is comparable.
        std::fs::write(dir.path().join(".gitignore"), ".devflow/\n").unwrap();
        git(dir.path(), &["add", ".gitignore"]);
        git(dir.path(), &["commit", "-q", "-m", "add gitignore"]);
        git(dir.path(), &["checkout", "-q", "-b", "feature/phase-11"]);
        std::fs::write(dir.path().join("feature.txt"), "phase work\n").unwrap();
        git(dir.path(), &["add", "feature.txt"]);
        git(dir.path(), &["commit", "-q", "-m", "phase work"]);

        let mut context = ctx(dir.path(), Stage::Ship);
        for hook in hooks_after_ship() {
            hook.run(&mut context).unwrap();
        }

        let all_tags = git_output(dir.path(), &["tag"]);
        assert_eq!(all_tags.lines().count(), 1, "expected exactly one tag");
        let tag = all_tags.trim().to_string();
        let tag_version = tag
            .strip_prefix('v')
            .expect("tag should be prefixed with v")
            .to_string();

        let changelog = std::fs::read_to_string(dir.path().join("CHANGELOG.md")).unwrap();
        let changelog_version = changelog
            .lines()
            .find(|l| l.starts_with("## "))
            .and_then(|l| l.trim_start_matches("## ").split(' ').next())
            .unwrap()
            .to_string();

        assert_ne!(
            changelog_version, "unreleased",
            "changelog heading must name the tagged version, not fall back to the literal"
        );
        assert_eq!(
            changelog_version, tag_version,
            "changelog heading must match the git tag ({tag}) even with no version file"
        );
    }

    #[test]
    fn merge_succeeds_while_feature_branch_is_checked_out_in_linked_worktree() {
        let dir = tempfile::tempdir().unwrap();
        let repo = dir.path().join("repo");
        let worktree = dir.path().join("phase-worktree");
        std::fs::create_dir_all(&repo).unwrap();
        init_repo(&repo);
        git(
            &repo,
            &[
                "worktree",
                "add",
                "-q",
                "-b",
                "feature/phase-11",
                worktree.to_str().unwrap(),
                "develop",
            ],
        );
        std::fs::write(worktree.join("feature.txt"), "phase work\n").unwrap();
        git(&worktree, &["add", "feature.txt"]);
        git(&worktree, &["commit", "-q", "-m", "phase work"]);

        Hook::Merge.run(&mut ctx(&repo, Stage::Ship)).unwrap();

        git(
            &repo,
            &["merge-base", "--is-ancestor", "feature/phase-11", "develop"],
        );
        assert!(GitFlow::new(&repo).branch_exists("feature/phase-11"));
    }

    #[test]
    fn branch_cleanup_is_fail_soft_when_branch_absent() {
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        // No feature branch exists — cleanup must still succeed.
        Hook::BranchCleanup
            .run(&mut ctx(dir.path(), Stage::Ship))
            .unwrap();
    }

    #[test]
    fn merge_fails_closed_when_branch_absent() {
        let dir = tempfile::tempdir().unwrap();
        init_repo(dir.path());
        // Branch absence cannot prove that phase work reached develop.
        let error = Hook::Merge
            .run(&mut ctx(dir.path(), Stage::Ship))
            .unwrap_err();
        assert!(error.to_string().contains("unproven merge"));
    }

    fn git_output(root: &Path, args: &[&str]) -> String {
        let output = Command::new("git")
            .args(args)
            .current_dir(root)
            .output()
            .unwrap();
        assert!(output.status.success(), "git {args:?} failed");
        String::from_utf8_lossy(&output.stdout).trim().to_string()
    }
}