cargo-rail 0.13.4

Graph-aware testing, dependency unification, and crate extraction for Rust monorepos
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Lightweight changelog generation
//!
//! Replaces git-cliff with a minimal, supply-chain-safe implementation.
//! Uses winnow for zero-copy parsing of conventional commits.

use crate::error::{RailError, RailResult};
use std::path::Path;
use std::process::Command;
use winnow::Parser;
use winnow::ascii::{space0, till_line_ending};
use winnow::combinator::{opt, preceded, terminated};
use winnow::token::{take_till, take_until};

// Winnow 0.7.x uses Result, not PResult
type PResult<T> = winnow::error::Result<T>;

/// A parsed commit message following conventional commits format
#[derive(Debug, Clone, PartialEq)]
pub struct ConventionalCommit<'a> {
  /// Type of commit (feat, fix, etc.)
  pub commit_type: CommitType,
  /// Optional scope
  pub scope: Option<&'a str>,
  /// Whether this is a breaking change
  pub breaking: bool,
  /// Commit description
  pub description: &'a str,
  /// Optional commit body
  pub body: Option<&'a str>,
  /// Commit SHA
  pub sha: &'a str,
}

/// Type of conventional commit
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CommitType {
  /// New feature
  Feature,
  /// Bug fix
  Fix,
  /// Breaking change
  Breaking,
  /// Maintenance tasks
  Chore,
  /// Documentation changes
  Docs,
  /// Code style changes
  Style,
  /// Code refactoring
  Refactor,
  /// Performance improvements
  Perf,
  /// Test additions or changes
  Test,
  /// Build system changes
  Build,
  /// CI configuration changes
  Ci,
  /// Uncategorized changes
  Other,
}

impl CommitType {
  /// Get string representation of commit type
  pub fn as_str(&self) -> &'static str {
    match self {
      Self::Feature => "feat",
      Self::Fix => "fix",
      Self::Breaking => "breaking",
      Self::Chore => "chore",
      Self::Docs => "docs",
      Self::Style => "style",
      Self::Refactor => "refactor",
      Self::Perf => "perf",
      Self::Test => "test",
      Self::Build => "build",
      Self::Ci => "ci",
      Self::Other => "other",
    }
  }

  /// Get emoji representation for this commit type
  pub fn emoji(&self) -> &'static str {
    match self {
      Self::Feature => "",
      Self::Fix => "🐛",
      Self::Breaking => "⚠️",
      Self::Chore => "🔧",
      Self::Docs => "📝",
      Self::Style => "💄",
      Self::Refactor => "♻️",
      Self::Perf => "",
      Self::Test => "",
      Self::Build => "🏗️",
      Self::Ci => "👷",
      Self::Other => "📦",
    }
  }

  /// Get section title for changelog output
  pub fn section_title(&self) -> &'static str {
    match self {
      Self::Feature => "Features",
      Self::Fix => "Bug Fixes",
      Self::Breaking => "BREAKING CHANGES",
      Self::Chore => "Chores",
      Self::Docs => "Documentation",
      Self::Style => "Styling",
      Self::Refactor => "Refactoring",
      Self::Perf => "Performance",
      Self::Test => "Testing",
      Self::Build => "Build",
      Self::Ci => "CI",
      Self::Other => "Other Changes",
    }
  }
}

/// Parse a GitHub remote URL into (org, repo)
fn parse_github_remote(url: &str) -> Option<(String, String)> {
  let trimmed = url.trim().trim_end_matches(".git").trim_end_matches('/');

  let repo_part = if let Some(ssh) = trimmed.strip_prefix("git@github.com:") {
    ssh
  } else if let Some(ssh) = trimmed.strip_prefix("ssh://git@github.com/") {
    ssh
  } else {
    trimmed.strip_prefix("https://github.com/")?
  };

  let mut parts = repo_part.split('/');
  let org = parts.next()?;
  let repo = parts.next()?;

  Some((org.to_string(), repo.to_string()))
}

fn git_command(workspace_root: &Path) -> Command {
  let mut cmd = Command::new("git");
  cmd.current_dir(workspace_root);
  cmd
}

/// Detect GitHub repository from the git remote
pub fn detect_github_repo(workspace_root: &Path) -> Option<(String, String)> {
  let output = git_command(workspace_root)
    .args(["config", "--get", "remote.origin.url"])
    .output()
    .ok()?;

  if !output.status.success() {
    return None;
  }

  let url = String::from_utf8_lossy(&output.stdout);
  parse_github_remote(&url)
}

/// Parse commit type from string
fn parse_commit_type(input: &mut &str) -> PResult<CommitType> {
  take_till(1.., ['(', '!', ':'])
    .verify_map(|value: &str| match value {
      "feat" => Some(CommitType::Feature),
      "fix" => Some(CommitType::Fix),
      "chore" => Some(CommitType::Chore),
      "docs" => Some(CommitType::Docs),
      "style" => Some(CommitType::Style),
      "refactor" => Some(CommitType::Refactor),
      "perf" => Some(CommitType::Perf),
      "test" => Some(CommitType::Test),
      "build" => Some(CommitType::Build),
      "ci" => Some(CommitType::Ci),
      _ => None,
    })
    .parse_next(input)
}

/// Parse optional scope in parentheses: (scope)
fn parse_scope<'a>(input: &mut &'a str) -> PResult<Option<&'a str>> {
  opt(preceded('(', terminated(take_until(0.., ')'), ')'))).parse_next(input)
}

/// Parse breaking change indicator: ! before colon
fn parse_breaking(input: &mut &str) -> PResult<bool> {
  opt('!').map(|o| o.is_some()).parse_next(input)
}

/// Parse the description after colon
fn parse_description<'a>(input: &mut &'a str) -> PResult<&'a str> {
  preceded((':', space0), till_line_ending).parse_next(input)
}

/// Extract PR references (#123) from text
fn extract_pr_numbers(text: &str) -> Vec<u32> {
  let mut prs = Vec::new();

  for word in text.split_whitespace() {
    if let Some(num_str) = word.strip_prefix("(#").and_then(|s| s.strip_suffix(')'))
      && let Ok(num) = num_str.parse::<u32>()
    {
      prs.push(num);
      continue;
    }

    if let Some(num_str) = word.strip_prefix('#') {
      let numeric = num_str.trim_end_matches(|c: char| !c.is_ascii_digit());
      if let Ok(num) = numeric.parse::<u32>() {
        prs.push(num);
      }
    }
  }

  prs.sort_unstable();
  prs.dedup();
  prs
}

/// Parse a conventional commit message
///
/// Format: type(scope)!: description
/// Examples:
/// - feat: add new feature
/// - fix(parser): fix parsing bug
/// - feat!: breaking change
pub fn parse_conventional_commit<'a>(sha: &'a str, subject: &'a str, body: Option<&'a str>) -> ConventionalCommit<'a> {
  // Try to parse as conventional commit
  let mut input = subject;

  let result = (parse_commit_type, parse_scope, parse_breaking, parse_description).parse_next(&mut input);

  match result {
    Ok((commit_type, scope, breaking_marker, description)) => {
      // Check for BREAKING CHANGE in body
      let has_breaking_body = body
        .map(|b| b.contains("BREAKING CHANGE:") || b.contains("BREAKING-CHANGE:"))
        .unwrap_or(false);

      let breaking = breaking_marker || has_breaking_body;
      let final_type = if breaking && commit_type != CommitType::Breaking {
        CommitType::Breaking
      } else {
        commit_type
      };

      ConventionalCommit {
        commit_type: final_type,
        scope,
        breaking,
        description,
        body,
        sha,
      }
    }
    Err(_) => {
      // Not a conventional commit - classify as "other"
      ConventionalCommit {
        commit_type: CommitType::Other,
        scope: None,
        breaking: false,
        description: subject,
        body,
        sha,
      }
    }
  }
}

/// Changelog generator
pub struct ChangelogGenerator {
  /// Workspace root directory
  workspace_root: std::path::PathBuf,
  /// GitHub repository (org, repo) if detected
  github_repo: Option<(String, String)>,
}

impl ChangelogGenerator {
  /// Create a new changelog generator
  pub fn new(workspace_root: &Path) -> Self {
    Self {
      workspace_root: workspace_root.to_path_buf(),
      github_repo: detect_github_repo(workspace_root),
    }
  }

  /// Get the detected GitHub repository (org, repo)
  pub fn github_repo(&self) -> Option<&(String, String)> {
    self.github_repo.as_ref()
  }

  fn short_sha<'a>(&self, sha: &'a str) -> &'a str {
    sha.get(..7).unwrap_or(sha)
  }

  fn format_sha(&self, sha: &str) -> String {
    if let Some((org, repo)) = &self.github_repo {
      return format!(
        "[{}](https://github.com/{}/{}/commit/{})",
        self.short_sha(sha),
        org,
        repo,
        sha
      );
    }

    self.short_sha(sha).to_string()
  }

  fn format_pr_links(&self, commit: &ConventionalCommit) -> Option<String> {
    let mut text = commit.description.to_string();
    if let Some(body) = commit.body {
      text.push(' ');
      text.push_str(body);
    }

    let prs = extract_pr_numbers(&text);
    if prs.is_empty() {
      return None;
    }

    if let Some((org, repo)) = &self.github_repo {
      Some(format_pr_links(&prs, |pr| {
        format!("[#{}](https://github.com/{}/{}/pull/{})", pr, org, repo, pr)
      }))
    } else {
      Some(format_pr_links(&prs, |pr| format!("#{}", pr)))
    }
  }

  fn format_description(&self, commit: &ConventionalCommit) -> String {
    let mut desc = commit.description.trim().to_string();

    if commit.breaking {
      desc = format!("[**breaking**] {}", desc);
    }

    desc
  }

  fn format_entry(&self, commit: &ConventionalCommit) -> String {
    let scope_prefix = commit.scope.map(|s| format!("**{}**: ", s)).unwrap_or_default();
    let sha = self.format_sha(commit.sha);
    let desc = self.format_description(commit);

    if let Some(prs) = self.format_pr_links(commit) {
      format!("- {}{} {} ({})\n", scope_prefix, desc, prs, sha)
    } else {
      format!("- {}{} ({})\n", scope_prefix, desc, sha)
    }
  }

  /// Generate changelog from git history
  ///
  /// Uses conventional-commit grouping and optional path filtering for
  /// per-crate changelog generation in monorepos.
  pub fn generate(&self, from_tag: Option<&str>, to_ref: &str, paths: Option<&[&Path]>) -> RailResult<String> {
    // 1. Get commits from git
    let commits = self.get_commits(from_tag, to_ref, paths)?;

    // 2. Parse commits
    let parsed: Vec<ConventionalCommit> = commits
      .iter()
      .map(|(sha, subject, body)| parse_conventional_commit(sha, subject, body.as_deref()))
      .collect();

    // 3. Group by type - pre-allocate based on expected distribution
    // Most commits are features/fixes, breaking changes are rare
    let commit_count = parsed.len();
    let mut breaking = Vec::with_capacity(commit_count / 10); // ~10% breaking
    let mut features = Vec::with_capacity(commit_count / 3); // ~33% features
    let mut fixes = Vec::with_capacity(commit_count / 3); // ~33% fixes
    let mut other_groups: std::collections::HashMap<CommitType, Vec<&ConventionalCommit>> =
      std::collections::HashMap::new();

    for commit in &parsed {
      match commit.commit_type {
        CommitType::Breaking => breaking.push(commit),
        CommitType::Feature => features.push(commit),
        CommitType::Fix => fixes.push(commit),
        _ => {
          other_groups.entry(commit.commit_type).or_default().push(commit);
        }
      }
    }

    // 4. Format as markdown
    let mut changelog = String::new();

    // Breaking changes first (most important)
    if !breaking.is_empty() {
      changelog.push_str(&format!(
        "### {} {}\n\n",
        CommitType::Breaking.emoji(),
        CommitType::Breaking.section_title()
      ));
      for commit in breaking {
        changelog.push_str(&self.format_entry(commit));
      }
      changelog.push('\n');
    }

    // Features
    if !features.is_empty() {
      changelog.push_str(&format!(
        "### {} {}\n\n",
        CommitType::Feature.emoji(),
        CommitType::Feature.section_title()
      ));
      for commit in features {
        changelog.push_str(&self.format_entry(commit));
      }
      changelog.push('\n');
    }

    // Fixes
    if !fixes.is_empty() {
      changelog.push_str(&format!(
        "### {} {}\n\n",
        CommitType::Fix.emoji(),
        CommitType::Fix.section_title()
      ));
      for commit in fixes {
        changelog.push_str(&self.format_entry(commit));
      }
      changelog.push('\n');
    }

    // Other types (sorted by type)
    let mut types: Vec<_> = other_groups.keys().collect();
    types.sort_by_key(|t| t.as_str());

    for commit_type in types {
      let commits = &other_groups[commit_type];
      changelog.push_str(&format!(
        "### {} {}\n\n",
        commit_type.emoji(),
        commit_type.section_title()
      ));
      for commit in commits {
        changelog.push_str(&self.format_entry(commit));
      }
      changelog.push('\n');
    }

    Ok(changelog)
  }

  /// Get commits from git log
  ///
  /// Returns Vec<(sha, subject, body)>
  fn get_commits(
    &self,
    from_tag: Option<&str>,
    to_ref: &str,
    paths: Option<&[&Path]>,
  ) -> RailResult<Vec<(String, String, Option<String>)>> {
    let range = if let Some(from) = from_tag {
      format!("{}..{}", from, to_ref)
    } else {
      to_ref.to_string()
    };

    let mut cmd = git_command(&self.workspace_root);
    cmd.args([
      "log",
      &range,
      "--format=%H%x00%s%x00%b%x00",
      "--no-merges", // Skip merge commits
    ]);

    // Add path filters if specified
    if let Some(paths) = paths
      && !paths.is_empty()
    {
      cmd.arg("--");
      for path in paths {
        cmd.arg(path);
      }
    }

    let output = cmd
      .output()
      .map_err(|e| RailError::message(format!("Failed to execute git log: {}", e)))?;

    if !output.status.success() {
      let stderr = String::from_utf8_lossy(&output.stderr);
      return Err(RailError::message(format!("git log failed: {}", stderr)));
    }

    let log = String::from_utf8_lossy(&output.stdout);
    let mut commits = Vec::new();

    // Parse git log output (format: SHA\0SUBJECT\0BODY\0)
    for commit_block in log.split("\0\0") {
      if commit_block.trim().is_empty() {
        continue;
      }

      let mut parts = commit_block.splitn(3, '\0');
      let Some(sha) = parts.next() else {
        continue;
      };
      let Some(subject) = parts.next() else {
        continue;
      };
      let body_raw = parts.next().unwrap_or_default();

      let sha = sha.trim().to_string();
      let subject = subject.trim().to_string();
      let body = if !body_raw.trim().is_empty() {
        Some(body_raw.trim().to_string())
      } else {
        None
      };

      commits.push((sha, subject, body));
    }

    Ok(commits)
  }
}

fn format_pr_links<F>(prs: &[u32], mut render: F) -> String
where
  F: FnMut(u32) -> String,
{
  let mut out = String::new();
  for (idx, pr) in prs.iter().copied().enumerate() {
    if idx > 0 {
      out.push(' ');
    }
    out.push_str(&render(pr));
  }
  out
}

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

  #[test]
  fn test_parse_conventional_commit_feature() {
    let commit = parse_conventional_commit("abc123", "feat: add new feature", None);
    assert_eq!(commit.commit_type, CommitType::Feature);
    assert_eq!(commit.scope, None);
    assert!(!commit.breaking);
    assert_eq!(commit.description, "add new feature");
  }

  #[test]
  fn test_parse_conventional_commit_with_scope() {
    let commit = parse_conventional_commit("abc123", "fix(parser): fix parsing bug", None);
    assert_eq!(commit.commit_type, CommitType::Fix);
    assert_eq!(commit.scope, Some("parser"));
    assert!(!commit.breaking);
    assert_eq!(commit.description, "fix parsing bug");
  }

  #[test]
  fn test_parse_conventional_commit_breaking() {
    let commit = parse_conventional_commit("abc123", "feat!: breaking change", None);
    assert_eq!(commit.commit_type, CommitType::Breaking);
    assert!(commit.breaking);
    assert_eq!(commit.description, "breaking change");
  }

  #[test]
  fn test_parse_conventional_commit_breaking_in_body() {
    let commit = parse_conventional_commit(
      "abc123",
      "feat: some feature",
      Some("BREAKING CHANGE: this breaks stuff"),
    );
    assert_eq!(commit.commit_type, CommitType::Breaking);
    assert!(commit.breaking);
  }

  #[test]
  fn test_parse_non_conventional_commit() {
    let commit = parse_conventional_commit("abc123", "Update README", None);
    assert_eq!(commit.commit_type, CommitType::Other);
    assert_eq!(commit.description, "Update README");
  }

  #[test]
  fn test_parse_conventional_commit_ci() {
    let commit = parse_conventional_commit("abc123", "ci: update release workflow", None);
    assert_eq!(commit.commit_type, CommitType::Ci);
    assert_eq!(commit.scope, None);
    assert!(!commit.breaking);
    assert_eq!(commit.description, "update release workflow");
  }

  #[test]
  fn parse_github_remote_supports_common_patterns() {
    assert_eq!(
      parse_github_remote("git@github.com:org/repo.git"),
      Some(("org".to_string(), "repo".to_string()))
    );

    assert_eq!(
      parse_github_remote("https://github.com/org/repo"),
      Some(("org".to_string(), "repo".to_string()))
    );

    assert_eq!(
      parse_github_remote("ssh://git@github.com/org/repo"),
      Some(("org".to_string(), "repo".to_string()))
    );
  }

  #[test]
  fn parse_github_remote_non_github_returns_none() {
    assert_eq!(parse_github_remote("git@gitlab.com:org/repo.git"), None);
  }

  #[test]
  fn extract_pr_numbers_supports_common_patterns() {
    let prs = extract_pr_numbers("feat(auth): add login (#12) closes #34 and refs #34");
    assert_eq!(prs, vec![12, 34]);
  }

  #[test]
  fn format_entry_includes_pr_and_links_when_available() {
    let commit = ConventionalCommit {
      commit_type: CommitType::Feature,
      scope: Some("api"),
      breaking: false,
      description: "redesign REST endpoints (#123)",
      body: Some("closes #456"),
      sha: "abcdef1234567890",
    };

    let generator = ChangelogGenerator {
      workspace_root: std::path::PathBuf::new(),
      github_repo: Some(("org".to_string(), "repo".to_string())),
    };

    let line = generator.format_entry(&commit);

    assert!(line.contains("[#123](https://github.com/org/repo/pull/123)"));
    assert!(line.contains("[#456](https://github.com/org/repo/pull/456)"));
    assert!(line.contains("**api**: redesign REST endpoints (#123)"));
    assert!(line.contains("[abcdef1](https://github.com/org/repo/commit/abcdef1234567890)"));
  }

  #[test]
  fn format_entry_marks_breaking_inline() {
    let commit = ConventionalCommit {
      commit_type: CommitType::Breaking,
      scope: None,
      breaking: true,
      description: "change API",
      body: None,
      sha: "1234567",
    };

    let generator = ChangelogGenerator {
      workspace_root: std::path::PathBuf::new(),
      github_repo: None,
    };

    let line = generator.format_entry(&commit);
    assert!(line.contains("[**breaking**] change API"));
  }

  #[test]
  fn format_entry_without_github_keeps_plain_pr_refs() {
    let commit = ConventionalCommit {
      commit_type: CommitType::Feature,
      scope: None,
      breaking: false,
      description: "add feature (#12)",
      body: Some("follow-up #34"),
      sha: "abcdef1234567890",
    };

    let generator = ChangelogGenerator {
      workspace_root: std::path::PathBuf::new(),
      github_repo: None,
    };

    let line = generator.format_entry(&commit);
    assert!(line.contains("#12 #34"));
    assert!(line.contains("(abcdef1)"));
  }
}