grubble 5.3.0

Automatic semantic versioning based on conventional commits, optimized for AI-generated commit messages
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
use clap::Parser;
use std::process;

mod analyser;
mod changelog;
mod config;
mod error;
mod git;
mod output;
mod strategy;
mod versioner;

use analyser::{analyse_commits, BumpType};
use config::Config;
use error::{BumperError, BumperResult};
use output::Output;
use strategy::load_strategy;

#[derive(Debug)]
enum ExitCode {
    Ok,
    NoBump,
}

#[derive(Parser, Debug)]
#[command(name = "grubble")]
#[command(
    about = "Automatic semantic versioning based on conventional commits",
    long_about = "Grubble - Automatic Semantic Versioning

Grubble analyzes conventional commits since the last version tag and automatically
bumps the semantic version accordingly.

Version Bump Rules:
  - feat:        -> minor bump
  - fix:         -> patch bump
  - feat!: / !:  -> major bump (breaking change)

Common Usage:
  grubble                    # Analyze and bump version
  grubble --push --tag       # Bump and push to remote with tag
  grubble --dry-run          # Check if bump is needed (exit 0 if yes, 1 if no)
  grubble --bump-type        # Output bump type (major/minor/patch/none)
  grubble --preset rust      # Use Rust Cargo.toml versioning
  grubble --changelog        # Generate CHANGELOG.md"
)]
struct Args {
    /// Push changes to remote
    #[arg(short, long)]
    push: bool,

    /// Force push changes to remote (uses --force-with-lease).
    /// Useful when pushing a workflow-owned branch that may need to
    /// overwrite a remote branch with a different history (e.g., when
    /// re-creating a release branch from a newer main commit).
    /// Requires --git-branch to be set.
    #[arg(long, requires = "git_branch")]
    force_push: bool,

    /// Push to this branch instead of HEAD (e.g., release/v0.35.0).
    /// When set, uses `git push --set-upstream origin <branch>`.
    /// The branch is created locally if it doesn't exist.
    #[arg(long, default_value = "")]
    git_branch: String,

    /// Suppress commit list output
    #[arg(short, long)]
    quiet: bool,

    /// Create git tag for the version
    #[arg(short, long)]
    tag: bool,

    /// Include release notes in the git tag annotation
    #[arg(short = 'r', long)]
    release_notes: bool,

    /// Output only the new version string (dry run, no changes)
    #[arg(long)]
    raw: bool,

    /// Versioning strategy (node, rust, git)
    #[arg(long)]
    preset: Option<String>,

    /// Prefix for git tags (default: v)
    #[arg(long)]
    tag_prefix: Option<String>,

    /// Prefix for commit messages
    #[arg(long)]
    commit_prefix: Option<String>,

    /// Comma-separated list of files to update (for node/rust preset)
    #[arg(long)]
    package_files: Option<String>,

    /// Git user name for commits
    #[arg(long)]
    git_user_name: Option<String>,

    /// Git user email for commits
    #[arg(long)]
    git_user_email: Option<String>,

    /// Update major version tag (e.g., v4 -> v4.x.x)
    #[arg(long)]
    update_major_tag: bool,

    /// Update minor version tag (e.g., v4.1 -> v4.1.x)
    #[arg(long)]
    update_minor_tag: bool,

    /// Generate and maintain a CHANGELOG.md file
    #[arg(long)]
    changelog: bool,

    /// Output the bump type (major, minor, patch, or none) and exit
    #[arg(long)]
    bump_type: bool,

    /// Check if version bump is needed (exit 0 if bump needed, exit 1 if no bump)
    /// Does not modify any files or create commits/tags
    #[arg(long)]
    dry_run: bool,

    /// Output format. Only valid with --bump-type, --raw, or --release-from-pr.
    #[arg(long, value_enum, default_value_t = Output::Text)]
    output: Output,

    /// Resolve a merged release PR to a tag spec for post-merge release
    /// automation. Given a PR number, fetches the PR via the GitHub API
    /// and emits the version, tag name, major tag name, merge commit SHA,
    /// and PR body. Requires `GH_TOKEN` or `GITHUB_TOKEN` in the env.
    #[arg(long, value_name = "PR_NUMBER", conflicts_with_all = ["bump_type", "raw", "dry_run"])]
    release_from_pr: Option<u32>,

    /// Read the latest entry from CHANGELOG.md and print it to stdout.
    /// Pure read-only operation — no files are modified.
    #[arg(long)]
    changelog_entry: bool,
}

fn log(msg: &str, is_raw: bool) {
    if !is_raw {
        println!("{}", msg);
    }
}

fn emit_raw(version: &versioner::Version, preset: &str, output: Output) {
    if output == Output::Json {
        let json = serde_json::json!({
            "version": version.to_string(),
            "preset": preset,
        });
        println!(
            "{}",
            serde_json::to_string_pretty(&json).expect("failed to serialize raw JSON output")
        );
    } else {
        println!("{}", version);
    }
}

fn run() -> BumperResult<ExitCode> {
    let args = Args::parse();

    let is_bump_type = args.bump_type;
    let is_dry_run = args.dry_run;
    let is_raw = args.raw;
    let output = args.output;

    // Handle --release-from-pr: resolves a merged release PR to a tag spec.
    // This is used by the version workflow after a release PR is merged.
    if let Some(pr_number) = args.release_from_pr {
        run_release(pr_number, output)?;
        return Ok(ExitCode::Ok);
    }

    // --output json is only valid with --bump-type, --raw, or --release-from-pr
    if output == Output::Json && !is_bump_type && !is_raw {
        return Err(BumperError::InvalidConfig(
            "--output json is only valid with --bump-type, --raw, or --release-from-pr".to_string(),
        ));
    }

    // Handle --bump-type mode
    if is_bump_type {
        run_bump_type(&args, output)?;
        return Ok(ExitCode::Ok);
    }

    // Handle --changelog-entry mode: read-only, no side effects
    if args.changelog_entry {
        let entry = changelog::read_latest_changelog_entry()?;
        if !entry.is_empty() {
            println!("{}", entry);
        }
        return Ok(ExitCode::Ok);
    }

    // Load config from file
    let mut config = Config::load();

    // Override with CLI arguments
    if let Some(preset) = args.preset {
        config.preset = preset;
    }
    if args.package_files.is_none() {
        config.package_files = match config.preset.as_str() {
            "rust" => vec!["Cargo.toml".to_string()],
            "node" => vec!["package.json".to_string()],
            "git" => vec![],
            _ => vec!["package.json".to_string()],
        };
    }
    if let Some(tag_prefix) = args.tag_prefix {
        config.tag_prefix = tag_prefix;
    }
    if let Some(commit_prefix) = args.commit_prefix {
        config.commit_prefix = commit_prefix;
    }
    if let Some(package_files) = args.package_files {
        config.package_files = package_files.split(',').map(|s| s.to_string()).collect();
    }
    if args.push {
        config.push = true;
    }
    if args.force_push {
        config.force_push = true;
    }
    if args.tag {
        config.tag = true;
    }
    if args.release_notes {
        config.release_notes = true;
    }
    if let Some(git_user_name) = args.git_user_name {
        config.git_user_name = git_user_name;
    }
    if let Some(git_user_email) = args.git_user_email {
        config.git_user_email = git_user_email;
    }
    if args.update_major_tag {
        config.update_major_tag = true;
    }
    if args.update_minor_tag {
        config.update_minor_tag = true;
    }
    if args.changelog {
        config.changelog = true;
    }

    // --force-push requires --git-branch (manual check because --git-branch
    // has a default value, so clap's `requires` attribute cannot enforce it)
    if config.force_push && args.git_branch.is_empty() {
        return Err(BumperError::InvalidConfig(
            "--force-push requires --git-branch to be set. \
             Use --git-branch <NAME> to specify the named branch to force-push."
                .to_string(),
        ));
    }

    let quiet = args.quiet;

    // Force settings for raw mode
    if is_raw {
        config.raw = true;
        config.push = false;
        config.tag = false;
    }

    // Force settings for dry-run mode
    if is_dry_run {
        config.raw = true;
        config.push = false;
        config.tag = false;
        config.changelog = false;
        config.release_notes = false;
    }

    if config.release_notes && !config.tag {
        log(
            "Warning: --release-notes requires --tag to be effective.",
            is_raw,
        );
    }

    // Set git config for commits
    git::set_git_config(&config.git_user_name, &config.git_user_email)?;

    let strategy = load_strategy(&config);

    let mut current_version = strategy.get_current_version()?;
    log(&format!("Current version: {}", current_version), is_raw);

    let last_tag = git::get_last_tag()?;
    log(
        &format!("Last tag: {}", last_tag.as_deref().unwrap_or("none")),
        is_raw,
    );

    let last_tag_version = git::get_last_tag_version(&config)?;

    // Validate the file/tag relationship before any work that could mask the state.
    // Read-only modes are exempt: --raw and --dry-run are for inspection.
    if !config.raw {
        if let Some(tag_ver) = last_tag_version {
            if config.preset != "git" {
                if current_version > tag_ver {
                    let file_path = config
                        .package_files
                        .first()
                        .map(String::as_str)
                        .unwrap_or("(unknown)");
                    return Err(BumperError::InvalidConfig(format!(
                        "package version {} (in {}) is ahead of latest tag v{}. \
                         Refusing to bump.\n\n\
                         To fix, align the file and tag. Either:\n  - revert {} to match the latest tag, or\n  - create the missing tag: git tag v{} && git push origin v{}",
                        current_version,
                        file_path,
                        tag_ver,
                        file_path,
                        current_version,
                        current_version
                    )));
                }
                if current_version < tag_ver {
                    log(
                        &format!(
                            "Package version {} is behind latest tag version {}, syncing...",
                            current_version, tag_ver
                        ),
                        is_raw,
                    );
                    let updated_files = strategy.update_files(&tag_ver)?;
                    if !updated_files.is_empty() {
                        git::commit_changes(
                            &format!("v{}", tag_ver),
                            &updated_files,
                            "chore: sync package version",
                        )?;
                        log(&format!("Synced package to version {}", tag_ver), is_raw);
                    }
                    current_version = tag_ver;
                }
            }
        }
    }

    let commits = git::get_commits_since_tag(last_tag.as_deref())?;

    if !quiet {
        log("Commits to analyse:", is_raw);
        for commit in &commits {
            log(&format!("  - {}", commit), is_raw);
        }
    }

    let release_notes_message = if config.release_notes && !commits.is_empty() {
        Some(
            commits
                .iter()
                .map(|c| format!("- {}", c))
                .collect::<Vec<_>>()
                .join("\n"),
        )
    } else {
        None
    };

    if commits.is_empty() {
        log("No commits since last tag.", is_raw);
        if is_raw {
            emit_raw(&current_version, &config.preset, output);
        }
        return Ok(ExitCode::NoBump);
    }

    let analysis = analyse_commits(&commits, &config);
    log(
        &format!("Version bump: {}", analysis.bump.as_str().to_uppercase()),
        is_raw,
    );

    if analysis.bump == BumpType::None {
        log("No version bump required.", is_raw);
        if is_raw {
            emit_raw(&current_version, &config.preset, output);
        }
        return Ok(ExitCode::NoBump);
    }

    log("Triggering commits:", is_raw);
    if !is_raw {
        for commit in &analysis.triggering_commits {
            log(&format!("  - {}", commit), is_raw);
        }
    }

    // Warn about unknown commit types
    if !analysis.unknown_commits.is_empty() && !is_raw {
        log("Warning: The following commits have unknown or unconfigured types and did not trigger a version bump:", is_raw);
        for commit in &analysis.unknown_commits {
            log(&format!("  - {}", commit), is_raw);
        }
        log("Consider configuring these types in .versionrc.json or using standard Conventional Commits types.", is_raw);
    }

    let new_version = current_version.bump(analysis.bump);

    if config.raw {
        // --raw or --dry-run — don't modify files
        emit_raw(&new_version, &config.preset, output);
        return Ok(ExitCode::Ok);
    }

    let updated_files = strategy.update_files(&new_version)?;
    log(&format!("Updated to {}", new_version), is_raw);

    // Generate changelog if enabled
    if config.changelog {
        changelog::generate_changelog_entry(&new_version, &commits, analysis.bump)?;
        log("Updated CHANGELOG.md", is_raw);
    }

    let mut all_updated_files = updated_files.clone();
    if config.changelog {
        all_updated_files.push("CHANGELOG.md".to_string());
    }

    if !all_updated_files.is_empty() {
        git::commit_changes(
            &new_version.to_string(),
            &all_updated_files,
            &config.commit_prefix,
        )?;
    }

    if config.tag {
        git::create_tag(
            &new_version.to_string(),
            &config.tag_prefix,
            release_notes_message.as_deref(),
        )?;

        // Update major/minor version tags if requested
        if config.update_major_tag || config.update_minor_tag {
            git::update_movable_tags(
                &new_version,
                &config.tag_prefix,
                config.update_major_tag,
                config.update_minor_tag,
            )?;
        }
    }

    if config.push {
        if config.force_push {
            // --force-push requires --git-branch (validated above), so
            // args.git_branch is guaranteed non-empty here.
            git::push_branch(&args.git_branch, true)?;
        } else if !args.git_branch.is_empty() {
            // Named branch push (no force): use the consolidated helper.
            git::push_branch(&args.git_branch, false)?;
        } else if config.update_major_tag || config.update_minor_tag {
            git::push_with_force_tags("")?;
        } else {
            git::push("")?;
        }
        let mut actions = vec!["Pushed changes"];
        if config.tag {
            actions.push("and tags");
        }
        log(&format!("{}.", actions.join(" ")), is_raw);
    } else {
        // Only log if we effectively did something (commit or tag)
        if !updated_files.is_empty() || config.tag {
            let mut actions = vec!["Committed"];
            if config.tag {
                actions.push("and tagged");
            }
            log(&format!("{} locally.", actions.join(" ")), is_raw);
        }
    }

    Ok(ExitCode::Ok)
}

fn run_bump_type(args: &Args, output: Output) -> BumperResult<()> {
    let mut config = Config::load();

    if let Some(preset) = &args.preset {
        config.preset = preset.clone();
    }
    if args.package_files.is_none() {
        config.package_files = match config.preset.as_str() {
            "rust" => vec!["Cargo.toml".to_string()],
            "node" => vec!["package.json".to_string()],
            "git" => vec![],
            _ => vec!["package.json".to_string()],
        };
    }
    if let Some(tag_prefix) = &args.tag_prefix {
        config.tag_prefix = tag_prefix.clone();
    }
    if let Some(package_files) = &args.package_files {
        config.package_files = package_files.split(',').map(|s| s.to_string()).collect();
    }
    if let Some(git_user_name) = &args.git_user_name {
        config.git_user_name = git_user_name.clone();
    }
    if let Some(git_user_email) = &args.git_user_email {
        config.git_user_email = git_user_email.clone();
    }

    git::set_git_config(&config.git_user_name, &config.git_user_email)?;

    let strategy = load_strategy(&config);
    let current_version = strategy.get_current_version()?;

    let last_tag = git::get_last_tag()?;
    let commits = git::get_commits_since_tag(last_tag.as_deref())?;

    if output == Output::Json {
        let analysis = analyse_commits(&commits, &config);
        let json = serde_json::json!({
            "bump_type": analysis.bump.as_str(),
            "current_version": current_version.to_string(),
            "triggering_commits": analysis.triggering_commits,
            "unknown_commits": analysis.unknown_commits,
        });
        println!(
            "{}",
            serde_json::to_string_pretty(&json).expect("failed to serialize bump-type JSON output")
        );
        return Ok(());
    }

    if commits.is_empty() {
        println!("none");
        return Ok(());
    }

    let analysis = analyse_commits(&commits, &config);
    println!("{}", analysis.bump.as_str());

    Ok(())
}

fn main() {
    match run() {
        Ok(ExitCode::Ok) => process::exit(0),
        Ok(ExitCode::NoBump) => process::exit(0),
        Err(e) => {
            eprintln!("Error: {}", e);
            process::exit(1);
        }
    }
}

/// Parses a release branch name like `release/v5.2.1` into a `Version`.
/// Returns `None` if the branch name does not match the expected pattern.
fn parse_release_branch(branch: &str) -> Option<versioner::Version> {
    // Strip an optional leading "release/" prefix.
    let version_str = branch.strip_prefix("release/").unwrap_or(branch);
    // Must be just the version with no other suffix.
    let prefix = "v";
    let stripped = version_str.strip_prefix(prefix).unwrap_or(version_str);
    versioner::Version::parse(stripped).ok()
}

/// Resolves a merged release PR to a tag specification via the GitHub API.
/// Emits either JSON (when output is `Output::Json`) or human-readable text.
fn run_release(pr_number: u32, output: Output) -> BumperResult<()> {
    use std::process::Command;

    // Require a GitHub token in the env.
    let token = std::env::var("GH_TOKEN")
        .ok()
        .or_else(|| std::env::var("GITHUB_TOKEN").ok())
        .ok_or_else(|| {
            BumperError::InvalidConfig(
                "--release-from-pr requires GH_TOKEN or GITHUB_TOKEN in the environment"
                    .to_string(),
            )
        })?;

    // Use `gh api` to fetch the PR. We could use reqwest directly, but `gh`
    // is already a dependency in the workflow environment and avoids adding
    // a new HTTP crate.
    let output_json = Command::new("gh")
        .args([
            "api",
            "--method",
            "GET",
            &format!("/repos/{{owner}}/{{repo}}/pulls/{}", pr_number),
        ])
        .env("GH_TOKEN", &token)
        .output()
        .map_err(|e| BumperError::GitError(format!("Failed to invoke gh CLI: {}", e)))?;

    if !output_json.status.success() {
        let stderr = String::from_utf8_lossy(&output_json.stderr);
        return Err(BumperError::InvalidConfig(format!(
            "gh api failed: {}",
            stderr.trim()
        )));
    }

    let pr: serde_json::Value = serde_json::from_slice(&output_json.stdout)
        .map_err(|e| BumperError::InvalidConfig(format!("Failed to parse PR JSON: {}", e)))?;

    // Validate the PR is merged.
    let merged = pr.get("merged").and_then(|v| v.as_bool()).unwrap_or(false);
    if !merged {
        return Err(BumperError::InvalidConfig(format!(
            "PR #{} is not merged",
            pr_number
        )));
    }

    // Extract the head branch name.
    let head_ref = pr
        .get("head")
        .and_then(|h| h.get("ref"))
        .and_then(|r| r.as_str())
        .ok_or_else(|| BumperError::InvalidConfig(format!("PR #{} has no head.ref", pr_number)))?;

    // Parse the version from the head branch.
    let version = parse_release_branch(head_ref).ok_or_else(|| {
        BumperError::InvalidConfig(format!(
            "PR #{} head branch '{}' does not match release/v<semver>",
            pr_number, head_ref
        ))
    })?;

    // Extract the merge commit SHA.
    let merge_commit_sha = pr
        .get("merge_commit_sha")
        .and_then(|s| s.as_str())
        .ok_or_else(|| {
            BumperError::InvalidConfig(format!("PR #{} has no merge_commit_sha", pr_number))
        })?;

    let title = pr
        .get("title")
        .and_then(|t| t.as_str())
        .unwrap_or("")
        .to_string();
    let body = pr
        .get("body")
        .and_then(|b| b.as_str())
        .unwrap_or("")
        .to_string();
    let tag_name = format!("v{}", version);
    let major_tag_name = format!("v{}", version.major);

    if output == Output::Json {
        let json = serde_json::json!({
            "version": version.to_string(),
            "tag_name": tag_name,
            "major_tag_name": major_tag_name,
            "merge_commit_sha": merge_commit_sha,
            "title": title,
            "body": body,
        });
        println!(
            "{}",
            serde_json::to_string_pretty(&json).expect("failed to serialize release JSON output")
        );
    } else {
        println!("version:        {}", version);
        println!("tag_name:      {}", tag_name);
        println!("major_tag:     {}", major_tag_name);
        println!("merge_commit:  {}", merge_commit_sha);
        println!("title:         {}", title);
    }

    Ok(())
}

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

    #[test]
    fn parse_release_branch_with_prefix() {
        let v = parse_release_branch("release/v5.2.1").unwrap();
        assert_eq!(v.to_string(), "5.2.1");
    }

    #[test]
    fn parse_release_branch_without_prefix() {
        let v = parse_release_branch("v5.2.1").unwrap();
        assert_eq!(v.to_string(), "5.2.1");
    }

    #[test]
    fn parse_release_branch_bare_version() {
        let v = parse_release_branch("5.2.1").unwrap();
        assert_eq!(v.to_string(), "5.2.1");
    }

    #[test]
    fn parse_release_branch_invalid() {
        assert!(parse_release_branch("feature-x").is_none());
        assert!(parse_release_branch("release/v").is_none());
        assert!(parse_release_branch("").is_none());
    }
}