grubble 4.9.1

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
use clap::Parser;
use std::process;

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

use analyser::{analyse_commits, BumpType};
use config::Config;
use error::BumperResult;
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,

    /// 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,
}

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

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

    let is_bump_type = args.bump_type;
    let is_dry_run = args.dry_run;

    // Handle --bump-type mode
    if is_bump_type {
        run_bump_type(&args)?;
        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.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;
    }

    let quiet = args.quiet;

    let is_raw = args.raw;

    // 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)?;

    // Sync package version if behind latest tag
    if let Some(tag_ver) = last_tag_version {
        if config.preset != "git" && 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 {
            println!("{}", current_version);
        }
        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 {
            println!("{}", current_version);
        }
        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 is_raw {
        println!("{}", new_version);
        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.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) -> 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 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(1),
        Err(e) => {
            eprintln!("Error: {}", e);
            process::exit(1);
        }
    }
}