sr-cli 4.1.0

CLI binary for sr
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
mod commands;

use std::path::Path;
use std::process::ExitCode;

use clap::{CommandFactory, Parser, Subcommand};
use sr_core::changelog::DefaultChangelogFormatter;
use sr_core::commit::ConfiguredCommitParser;
use sr_core::config::{Config, DEFAULT_CONFIG_FILE, LEGACY_CONFIG_FILE, VersioningMode};
use sr_core::error::ReleaseError;
use sr_core::github::GitHubProvider;
use sr_core::native_git::NativeGitRepository;
use sr_core::release::{ReleaseStrategy, TrunkReleaseStrategy};

#[derive(Parser)]
#[command(name = "sr", about = "Release engineering CLI", version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Execute a release (trunk flow: tag + GitHub release)
    Release {
        /// Target a specific package in a monorepo
        #[arg(long, short)]
        package: Option<String>,

        /// Release channel (e.g. canary, rc, stable) — overrides config fields
        #[arg(long, short)]
        channel: Option<String>,

        /// Preview what would happen without making changes
        #[arg(long)]
        dry_run: bool,

        /// Glob patterns for artifact files to upload to the release (repeatable)
        #[arg(long = "artifacts")]
        artifacts: Vec<String>,

        /// Re-release the current tag (use when a previous release partially failed)
        #[arg(long)]
        force: bool,

        /// Additional file globs to stage in the release commit (repeatable, e.g. Cargo.lock)
        #[arg(long = "stage-files")]
        stage_files: Vec<String>,

        /// Pre-release identifier (e.g. alpha, beta, rc). Produces versions like 1.2.0-alpha.1
        #[arg(long)]
        prerelease: Option<String>,

        /// Sign tags with GPG/SSH (git tag -s)
        #[arg(long)]
        sign_tags: bool,

        /// Create GitHub release as a draft (requires manual publishing)
        #[arg(long)]
        draft: bool,
    },

    /// Show repo status: unreleased commits, next version, changelog preview, open PRs
    Status {
        /// Target a specific package in a monorepo
        #[arg(long, short)]
        package: Option<String>,

        /// Output format
        #[arg(long, default_value = "human")]
        format: PlanFormat,
    },

    /// Validate and display resolved configuration
    Config {
        /// Show the fully resolved config with defaults applied
        #[arg(long)]
        resolved: bool,
    },

    /// Create a default configuration file
    Init {
        /// Overwrite the config file if it already exists
        #[arg(long)]
        force: bool,

        /// Merge new default fields into existing config without overwriting customizations
        #[arg(long, conflicts_with = "force")]
        merge: bool,
    },

    /// Generate shell completions
    Completions {
        /// Shell to generate completions for
        shell: clap_complete::Shell,
    },

    /// MCP server
    Mcp {
        #[command(subcommand)]
        command: McpCommands,
    },

    /// Update sr to the latest version
    Update,

    /// Show migration guide from sr 3.x to 4.x
    Migrate,
}

#[derive(Subcommand)]
enum McpCommands {
    /// Start MCP server over stdio
    Serve,
    /// Create .mcp.json in the current project for agentspec discovery
    Init,
}

#[derive(Clone, clap::ValueEnum)]
enum PlanFormat {
    Human,
    Json,
}

use sr_core::release::NoopVcsProvider;

fn build_local_strategy(
    config: Config,
    force: bool,
) -> anyhow::Result<
    TrunkReleaseStrategy<
        NativeGitRepository,
        NoopVcsProvider,
        ConfiguredCommitParser,
        DefaultChangelogFormatter,
    >,
> {
    let git = NativeGitRepository::open(Path::new("."))?;
    let parser =
        ConfiguredCommitParser::new(config.commit.types.clone(), config.commit.pattern.clone());
    let types = config.commit.types.clone();
    let breaking_section = config.commit.breaking_section.clone();
    let misc_section = config.commit.misc_section.clone();
    let formatter = DefaultChangelogFormatter::new(
        config.release.changelog.template.clone(),
        types,
        breaking_section,
        misc_section,
    );
    Ok(TrunkReleaseStrategy {
        git,
        vcs: NoopVcsProvider,
        parser,
        formatter,
        config,
        force,
    })
}

fn build_full_strategy(
    config: Config,
    force: bool,
) -> anyhow::Result<
    TrunkReleaseStrategy<
        NativeGitRepository,
        GitHubProvider,
        ConfiguredCommitParser,
        DefaultChangelogFormatter,
    >,
> {
    let git = NativeGitRepository::open(Path::new("."))?;
    let (hostname, owner, repo) = git.parse_remote_full()?;

    let token = std::env::var("GH_TOKEN")
        .or_else(|_| std::env::var("GITHUB_TOKEN"))
        .map_err(|_| anyhow::anyhow!("neither GH_TOKEN nor GITHUB_TOKEN is set"))?;

    let git = git.with_http_auth(hostname.clone(), token.clone());
    let vcs = GitHubProvider::new(owner, repo, hostname, token);
    let parser =
        ConfiguredCommitParser::new(config.commit.types.clone(), config.commit.pattern.clone());
    let types = config.commit.types.clone();
    let breaking_section = config.commit.breaking_section.clone();
    let misc_section = config.commit.misc_section.clone();
    let formatter = DefaultChangelogFormatter::new(
        config.release.changelog.template.clone(),
        types,
        breaking_section,
        misc_section,
    );

    Ok(TrunkReleaseStrategy {
        git,
        vcs,
        parser,
        formatter,
        config,
        force,
    })
}

fn is_no_release_error(err: &anyhow::Error) -> bool {
    if let Some(re) = err.downcast_ref::<ReleaseError>() {
        matches!(
            re,
            ReleaseError::NoCommits { .. } | ReleaseError::NoBump { .. }
        )
    } else {
        false
    }
}

fn load_config_for_package(package: Option<&str>) -> anyhow::Result<Config> {
    let config_path = resolve_config_path();
    let mut config = Config::load(&config_path)?;

    if config.release.versioning == VersioningMode::Fixed && !config.packages.is_empty() {
        if let Some(name) = package {
            anyhow::bail!(
                "--package '{name}' is not supported with `versioning: fixed` — \
                 all packages are released together"
            );
        }
        return Ok(config.resolve_fixed());
    }

    match package {
        Some(name) => {
            let pkg = config.find_package(name)?;
            Ok(config.resolve_package(pkg))
        }
        None => {
            if config.release.version_files.is_empty() {
                config.release.version_files =
                    sr_core::version_files::detect_version_files(Path::new("."));
            }
            Ok(config)
        }
    }
}

fn resolve_config_path() -> std::path::PathBuf {
    match Config::find_config(Path::new(".")) {
        Some((path, is_legacy)) => {
            if is_legacy {
                eprintln!(
                    "warning: {} is deprecated, rename to {} (legacy support will be removed in a future release)",
                    LEGACY_CONFIG_FILE, DEFAULT_CONFIG_FILE,
                );
            }
            path
        }
        None => std::path::PathBuf::from(DEFAULT_CONFIG_FILE),
    }
}

fn self_update() -> anyhow::Result<()> {
    eprintln!("current version: {}", env!("CARGO_PKG_VERSION"));

    match agentspec_update::self_update("urmzd/sr", env!("CARGO_PKG_VERSION"), "sr")? {
        agentspec_update::UpdateResult::AlreadyUpToDate => {
            eprintln!("already up to date");
        }
        agentspec_update::UpdateResult::Updated { from, to } => {
            eprintln!("updated: {from}{to}");
        }
    }

    Ok(())
}

fn print_migration_guide() {
    let guide = r#"
sr 4.x Migration Guide
======================

sr is now an AI-managed release tool. Most commands from 3.x (commit, pr,
review, worktree, rebase, branch, ask, explain) have been removed. The CLI
focuses on release engineering; AI-powered workflows (commits, PRs, reviews)
are handled by your AI assistant via the MCP server (`sr mcp serve`).

Remaining commands: release, status, config, init, mcp, completions, update


1. Remove git hooks
-------------------

sr 3.x installed git hooks (commit-msg, pre-commit) that called `sr hook ...`.
These no longer exist. Remove them:

    rm .git/hooks/commit-msg .git/hooks/pre-commit

If you use a hook manager (husky, lefthook, pre-commit), remove any sr entries:

    # .husky/commit-msg  — delete the file or remove the `sr hook commit-msg` line
    # .pre-commit-config.yaml — remove sr-related hooks

Also remove the old `hooks` section from sr.yaml if it referenced git hook names:

    # 3.x — REMOVE this
    hooks:
      commit-msg: [sr hook commit-msg]
      pre-commit: [sr hook pre-commit]


2. Update sr.yaml
-----------------

The flat 3.x config is now grouped by concern. Run `sr init --merge` to add
new fields automatically, or restructure manually:

    # 3.x (flat — no longer valid)
    branches: [main]
    tag_prefix: "v"
    commit_pattern: '...'
    types: [...]
    build_command: "cargo build --release"
    pre_release_command: "cargo test"

    # 4.x (grouped)
    commit:
      pattern: '...'
      types: [...]

    release:
      branches: [main]
      tag_prefix: "v"
      version_files: [Cargo.toml]

    hooks:
      pre_release: ["cargo test"]
      post_release: ["./notify.sh"]

Key changes:
  - `branches`, `tag_prefix`           → nested under `release:`
  - `commit_pattern`, `types`          → nested under `commit:`
  - `build_command`                    → REMOVED (use hooks.pre_release)
  - `pre_release_command`              → hooks.pre_release
  - `post_release_command`             → hooks.post_release
  - `lifecycle`                        → REMOVED (use hooks)
  - `hooks:` with git hook names       → REMOVED (use lifecycle hooks above)
  - `breaking_section`                 → commit.breaking_section

Quick migration:

    sr init --merge   # adds new fields, preserves your customizations
    sr config         # review the result
    sr status         # verify everything resolves correctly


3. Commands removed or moved
----------------------------

  3.x command       What to do now
  ─────────────     ──────────────────────────────────────────────────
  sr ask            Use your AI assistant (Claude Code, Copilot, etc.)
  sr explain        Use your AI assistant with `git show <rev>`
  sr commit         Use your AI assistant or `sr mcp serve` for MCP
  sr rebase         Use your AI assistant or git rebase directly
  sr branch         Use your AI assistant or git worktree directly
  sr worktree       Use your AI assistant or git worktree directly
  sr pr             Use your AI assistant with gh CLI
  sr review         Use your AI assistant with gh CLI
  sr plan           `sr status`
  sr version        `sr status` or `sr status --format json | jq .next_version`
  sr changelog      Generated automatically by `sr release`


4. CI/CD migration
------------------

Update scripts that called removed commands:

    # 3.x
    VERSION=$(sr version --short)
    sr changelog --write

    # 4.x
    VERSION=$(sr status --format json | jq -r '.next_version')
    # changelog is written automatically by `sr release`

For more details, see: docs/migration-3x-4x.md
"#;
    print!("{guide}");
}

async fn run() -> anyhow::Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Init { force, merge } => {
            let path = Path::new(DEFAULT_CONFIG_FILE);

            if path.exists() && !force && !merge {
                anyhow::bail!(
                    "{DEFAULT_CONFIG_FILE} already exists (use --force to overwrite, or --merge to add new fields)"
                );
            }

            let detected = sr_core::version_files::detect_version_files(Path::new("."));
            if !detected.is_empty() {
                for f in &detected {
                    eprintln!("detected version file: {f}");
                }
            }

            if merge && path.exists() {
                let existing = std::fs::read_to_string(path)?;
                let merged = sr_core::config::merge_config_yaml(&existing)?;
                std::fs::write(path, merged)?;
                eprintln!("merged new defaults into {DEFAULT_CONFIG_FILE}");
            } else {
                let template = sr_core::config::default_config_template(&detected);
                std::fs::write(path, template)?;
                eprintln!("wrote {DEFAULT_CONFIG_FILE}");
            }

            Ok(())
        }

        Commands::Config { resolved } => {
            let config_path = resolve_config_path();
            let config = Config::load(&config_path)?;
            if resolved {
                let yaml = serde_yaml_ng::to_string(&config)?;
                print!("{yaml}");
            } else if config_path.exists() {
                let raw = std::fs::read_to_string(&config_path)?;
                print!("{raw}");
            } else {
                eprintln!("no config file found; showing defaults");
                let yaml = serde_yaml_ng::to_string(&config)?;
                print!("{yaml}");
            }
            Ok(())
        }

        Commands::Status { package, format } => {
            let config = load_config_for_package(package.as_deref())?;
            let tag_prefix = config.release.tag_prefix.clone();

            let git = NativeGitRepository::open(Path::new("."))?;
            let branch_output = std::process::Command::new("git")
                .args(["branch", "--show-current"])
                .output()?;
            let branch = String::from_utf8_lossy(&branch_output.stdout)
                .trim()
                .to_string();

            let formatter = DefaultChangelogFormatter::new(
                config.release.changelog.template.clone(),
                config.commit.types.clone(),
                config.commit.breaking_section.clone(),
                config.commit.misc_section.clone(),
            );
            let strategy = build_local_strategy(config, false)?;
            let plan_result = strategy.plan();

            match format {
                PlanFormat::Json => match plan_result {
                    Ok(plan) => {
                        let repo_url = git
                            .parse_remote_full()
                            .ok()
                            .map(|(h, o, r)| format!("https://{h}/{o}/{r}"));
                        let today = sr_core::release::today_string();
                        let entry = sr_core::changelog::ChangelogEntry {
                            version: plan.next_version.to_string(),
                            date: today,
                            commits: plan.commits.clone(),
                            compare_url: None,
                            repo_url,
                        };
                        let changelog =
                            sr_core::changelog::ChangelogFormatter::format(&formatter, &[entry])?;
                        #[derive(serde::Serialize)]
                        struct StatusOutput<'a> {
                            branch: String,
                            #[serde(flatten)]
                            plan: &'a sr_core::release::ReleasePlan,
                            changelog: String,
                        }
                        let output = StatusOutput {
                            branch,
                            plan: &plan,
                            changelog,
                        };
                        println!("{}", serde_json::to_string_pretty(&output)?);
                    }
                    Err(e) => {
                        let msg = if matches!(
                            &e,
                            ReleaseError::NoCommits { .. } | ReleaseError::NoBump { .. }
                        ) {
                            "no unreleased changes"
                        } else {
                            "error"
                        };
                        println!("{{\"branch\":\"{branch}\",\"status\":\"{msg}\"}}");
                    }
                },
                PlanFormat::Human => {
                    println!("  Branch: {branch}");
                    match plan_result {
                        Ok(plan) => {
                            let current_tag = plan
                                .current_version
                                .as_ref()
                                .map(|v| format!("{tag_prefix}{v}"))
                                .unwrap_or_else(|| "(initial)".to_string());
                            println!("  Current: {current_tag}");
                            println!("  Next: {} ({})", plan.tag_name, plan.bump);
                            println!("  Commits: {}", plan.commits.len());
                            for commit in &plan.commits {
                                let scope = commit
                                    .scope
                                    .as_deref()
                                    .map(|s| format!("({s})"))
                                    .unwrap_or_default();
                                let breaking = if commit.breaking { " BREAKING" } else { "" };
                                println!(
                                    "    {}{scope}: {}{breaking}",
                                    commit.r#type, commit.description
                                );
                            }
                        }
                        Err(e) => match &e {
                            ReleaseError::NoCommits { .. } | ReleaseError::NoBump { .. } => {
                                println!("  No unreleased changes");
                            }
                            _ => println!("  Release: error — {e}"),
                        },
                    }
                    if let Ok((hostname, owner, repo_name)) = git.parse_remote_full()
                        && let Ok(token) =
                            std::env::var("GH_TOKEN").or_else(|_| std::env::var("GITHUB_TOKEN"))
                    {
                        let github = GitHubProvider::new(owner, repo_name, hostname, token);
                        if let Ok((ready, draft)) = github.count_open_prs() {
                            println!(
                                "  Open PRs: {} ({} ready, {} draft)",
                                ready + draft,
                                ready,
                                draft
                            );
                        }
                    }
                }
            }
            Ok(())
        }

        Commands::Completions { shell } => {
            let mut cmd = Cli::command();
            clap_complete::generate(shell, &mut cmd, "sr", &mut std::io::stdout());
            Ok(())
        }

        Commands::Release {
            package,
            channel,
            dry_run,
            artifacts,
            force,
            stage_files,
            prerelease,
            sign_tags,
            draft,
        } => {
            let mut config = load_config_for_package(package.as_deref())?;

            let channel_name = channel.or_else(|| config.release.default_channel.clone());
            if let Some(name) = &channel_name {
                config = config
                    .resolve_channel(name)
                    .map_err(|e| anyhow::anyhow!("{e}"))?;
            }
            config.release.artifacts.extend(artifacts);
            config.release.stage_files.extend(stage_files);
            if prerelease.is_some() {
                config.release.prerelease = prerelease;
            }
            if sign_tags {
                config.release.sign_tags = true;
            }
            if draft {
                config.release.draft = true;
            }

            let plan = match build_full_strategy(config.clone(), force) {
                Ok(strategy) => {
                    let plan = strategy.plan()?;
                    strategy.execute(&plan, dry_run)?;
                    plan
                }
                Err(e) => {
                    if dry_run {
                        eprintln!("warning: {e} (continuing dry-run without GitHub)");
                        let strategy = build_local_strategy(config, force)?;
                        let plan = strategy.plan()?;
                        strategy.execute(&plan, dry_run)?;
                        plan
                    } else {
                        return Err(e);
                    }
                }
            };
            #[derive(serde::Serialize)]
            struct ReleaseOutput {
                version: String,
                previous_version: String,
                tag: String,
                bump: String,
                floating_tag: String,
                commit_count: usize,
            }
            let output = ReleaseOutput {
                version: plan.next_version.to_string(),
                previous_version: plan
                    .current_version
                    .as_ref()
                    .map(|v| v.to_string())
                    .unwrap_or_default(),
                tag: plan.tag_name.clone(),
                bump: plan.bump.to_string(),
                floating_tag: plan.floating_tag_name.as_deref().unwrap_or("").to_string(),
                commit_count: plan.commits.len(),
            };
            println!("{}", serde_json::to_string(&output)?);
            Ok(())
        }

        Commands::Mcp { command } => match command {
            McpCommands::Serve => commands::mcp::run().await,
            McpCommands::Init => commands::mcp::config(),
        },
        Commands::Update => self_update(),
        Commands::Migrate => {
            print_migration_guide();
            Ok(())
        }
    }
}

#[tokio::main]
async fn main() -> ExitCode {
    match run().await {
        Ok(()) => ExitCode::from(0),
        Err(e) => {
            if is_no_release_error(&e) {
                eprintln!("{e:#}");
                ExitCode::from(2)
            } else {
                eprintln!("error: {e:#}");
                ExitCode::from(1)
            }
        }
    }
}