cargo-rail 0.11.0

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
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
//! Additional operations for SystemGit (commit walking, remotes, etc.)

use super::SystemGit;
use super::system::CommitInfo;
use crate::error::{GitError, RailError, RailResult, ResultExt};
use crate::progress;
use crate::utils;
use std::path::{Path, PathBuf};

impl SystemGit {
  /// Normalize a path to be relative to the work tree
  ///
  /// If the path is absolute, strips the work tree prefix.
  /// If the path is already relative or stripping fails, returns the path as-is.
  fn normalize_path<'a>(&self, path: &'a Path) -> &'a Path {
    if path.is_absolute() {
      path.strip_prefix(&self.worktree_root).unwrap_or(path)
    } else {
      path
    }
  }

  /// Get commit history from HEAD with optional limit
  ///
  /// Returns commits in reverse chronological order (newest first).
  /// Uses parallel batch processing for optimal performance.
  pub fn commit_history(&self, limit: Option<usize>) -> RailResult<Vec<CommitInfo>> {
    let mut args = vec!["log", "--format=%H"];
    let limit_str;
    if let Some(max) = limit {
      limit_str = format!("-{}", max);
      args.push(&limit_str);
    }

    let output = self.run_git(&args)?;
    let shas: Vec<String> = String::from_utf8_lossy(&output.stdout)
      .lines()
      .map(|s| s.trim().to_string())
      .filter(|s| !s.is_empty())
      .collect();

    // Fetch commit info in parallel using bulk operation
    self.get_commits_bulk(&shas)
  }

  /// Get files changed in a specific commit
  ///
  /// Returns list of (path, change_type) where change_type is A(dded), M(odified), D(eleted),
  /// R(enamed), or C(opied).
  ///
  /// For renames and copies, both the old and new paths are returned:
  /// - Old path with 'D' (deleted from old location)
  /// - New path with 'A' (added at new location)
  pub fn get_changed_files(&self, commit_sha: &str) -> RailResult<Vec<(PathBuf, char)>> {
    let output = self.run_git(&["diff-tree", "--no-commit-id", "--name-status", "-r", "-z", commit_sha])?;
    parse_name_status_output_z(&output.stdout)
  }

  /// Get all files that changed between two refs.
  ///
  /// Returns list of (path, change_type) where change_type is A(dded), M(odified), D(eleted),
  /// R(enamed), or C(opied).
  ///
  /// For renames and copies, both the old and new paths are returned:
  /// - Old path with 'D' (deleted from old location)
  /// - New path with 'A' (added at new location)
  ///
  /// # Performance
  /// Uses `git diff --name-status` which is optimized for listing changes.
  /// Typically <100ms even for large diffs with 1000s of files.
  pub fn get_changed_files_between(&self, base_ref: &str, head_ref: Option<&str>) -> RailResult<Vec<(PathBuf, char)>> {
    let mut args = vec!["diff", "--name-status", base_ref];
    let head_owned;
    if let Some(head) = head_ref {
      head_owned = head.to_string();
      args.push(&head_owned);
    }

    args.insert(2, "-z");
    let output = self.run_git(&args)?;
    parse_name_status_output_z(&output.stdout)
  }

  /// Get the merge-base (common ancestor) between two refs
  ///
  /// This is useful for finding the point where a feature branch diverged
  /// from the main branch, which gives more accurate change detection in CI.
  pub fn get_merge_base(&self, ref1: &str, ref2: &str) -> RailResult<String> {
    let output = self.run_git(&["merge-base", ref1, ref2])?;
    let sha = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if sha.is_empty() {
      return Err(crate::error::RailError::message(format!(
        "no common ancestor between {} and {}",
        ref1, ref2
      )));
    }
    Ok(sha)
  }

  /// Get commits touching a specific path in a range
  ///
  /// Returns commits in chronological order (oldest first).
  pub fn get_commits_touching_path(
    &self,
    path: &Path,
    since_sha: Option<&str>,
    until_ref: &str,
  ) -> RailResult<Vec<CommitInfo>> {
    let relative_path = self.normalize_path(path);
    let git_path = relative_path.to_str().unwrap_or("");

    let mut args = vec!["log", "--reverse", "--format=%H"];
    let range_arg;

    // Add range
    if let Some(since) = since_sha {
      range_arg = format!("{}..{}", since, until_ref);
      args.push(&range_arg);
    } else {
      args.push(until_ref);
    }

    // Add path filter
    args.push("--");
    args.push(git_path);

    let output = self.run_git(&args)?;
    let shas: Vec<String> = String::from_utf8_lossy(&output.stdout)
      .lines()
      .map(|s| s.trim().to_string())
      .filter(|s| !s.is_empty())
      .collect();

    // Fetch commit info sequentially to preserve order
    let mut commits = Vec::with_capacity(shas.len());
    for sha in shas {
      commits.push(self.get_commit(&sha)?);
    }

    Ok(commits)
  }

  /// Get commits touching any of the given paths (batched for performance)
  /// Returns commits in chronological order (oldest first), deduplicated
  pub fn get_commits_touching_paths(
    &self,
    paths: &[PathBuf],
    since_sha: Option<&str>,
    until_ref: &str,
  ) -> RailResult<Vec<CommitInfo>> {
    if paths.is_empty() {
      return Ok(Vec::new());
    }

    // Normalize all paths
    let relative_paths: Vec<String> = paths
      .iter()
      .map(|path| self.normalize_path(path).to_str().unwrap_or("").to_string())
      .collect();

    let mut args = vec!["log", "--reverse", "--format=%H"];
    let range_arg;

    // Add range
    if let Some(since) = since_sha {
      range_arg = format!("{}..{}", since, until_ref);
      args.push(&range_arg);
    } else {
      args.push(until_ref);
    }

    // Add all path filters
    args.push("--");
    let path_refs: Vec<&str> = relative_paths.iter().map(|s| s.as_str()).collect();
    args.extend(path_refs);

    let output = self.run_git(&args)?;
    let shas: Vec<String> = String::from_utf8_lossy(&output.stdout)
      .lines()
      .map(|s| s.trim().to_string())
      .filter(|s| !s.is_empty())
      .collect();

    // Fetch commit info sequentially to preserve order (already deduplicated by git)
    let mut commits = Vec::with_capacity(shas.len());
    for sha in shas {
      commits.push(self.get_commit(&sha)?);
    }

    Ok(commits)
  }

  /// Get commit metadata for a single SHA
  ///
  /// Uses `git log -1 --format` for efficient single-commit lookup.
  pub fn get_commit(&self, sha: &str) -> RailResult<CommitInfo> {
    // Format: %H (hash) %an (author name) %ae (author email) %at (author time)
    //         %cn (committer name) %ce (committer email) %ct (committer time)
    //         %P (parent hashes) %B (body)
    let format = "%H%n%an%n%ae%n%at%n%cn%n%ce%n%ct%n%P%n%B";
    let format_arg = format!("--format={}", format);

    let output = self.run_git_with_error(&["log", "-1", &format_arg, sha], |_| {
      RailError::Git(GitError::CommitNotFound { sha: sha.to_string() })
    })?;

    parse_commit_output(&output.stdout)
  }

  /// List all files at a specific commit under a path
  pub fn list_files_at_commit(&self, commit_sha: &str, path: &Path) -> RailResult<Vec<PathBuf>> {
    let spec = if path.as_os_str().is_empty() {
      commit_sha.to_string()
    } else {
      let git_path = utils::path_to_git_format(path);
      format!("{}:{}", commit_sha, git_path)
    };

    // Use run_git_check since failure is not an error (empty result)
    if !self.run_git_check(&["ls-tree", "-r", "--name-only", &spec]) {
      return Ok(vec![]);
    }

    // If successful, get the output
    let output = self.run_git(&["ls-tree", "-r", "--name-only", &spec])?;
    let files = String::from_utf8_lossy(&output.stdout)
      .lines()
      .map(PathBuf::from)
      .collect();

    Ok(files)
  }

  /// Collect all files from a tree recursively
  ///
  /// Uses bulk file reading for 100x+ speedup on large trees.
  pub fn collect_tree_files(&self, commit_sha: &str, path: &Path) -> RailResult<Vec<(PathBuf, Vec<u8>)>> {
    let files = self.list_files_at_commit(commit_sha, path)?;

    if files.is_empty() {
      return Ok(vec![]);
    }

    // Build paths first (owned), then create references for bulk API
    let paths: Vec<PathBuf> = files.iter().map(|file| path.join(file)).collect();
    let items: Vec<(&str, &Path)> = paths.iter().map(|p| (commit_sha, p.as_path())).collect();

    // Read all files in one batch (100x+ faster than loop)
    let contents = self.read_files_bulk(&items)?;

    // Combine full paths (with crate prefix) with contents
    let results: Vec<(PathBuf, Vec<u8>)> = paths.into_iter().zip(contents).collect();

    Ok(results)
  }

  /// Add a remote repository
  pub fn add_remote(&self, name: &str, url: &str) -> RailResult<()> {
    match self.run_git(&["remote", "add", name, url]) {
      Ok(_) => Ok(()),
      Err(e) => {
        // Check if error is because remote already exists
        if let RailError::Git(GitError::CommandFailed { stderr, .. }) = &e
          && stderr.contains("already exists")
        {
          return Ok(());
        }
        Err(e)
      }
    }
  }

  /// List all remotes
  pub fn list_remotes(&self) -> RailResult<Vec<(String, String)>> {
    // Use run_git_check since failure returns empty list
    if !self.run_git_check(&["remote", "-v"]) {
      return Ok(vec![]);
    }

    let output = self.run_git(&["remote", "-v"])?;
    let stdout = String::from_utf8_lossy(&output.stdout);
    let mut remotes = Vec::new();

    for line in stdout.lines() {
      // Format: "origin  git@github.com:user/repo.git (fetch)"
      let parts: Vec<&str> = line.split_whitespace().collect();
      if parts.len() >= 2 && line.contains("(fetch)") {
        remotes.push((parts[0].to_string(), parts[1].to_string()));
      }
    }

    Ok(remotes)
  }

  /// Push to remote
  pub fn push_to_remote(&self, remote_name: &str, branch: &str) -> RailResult<()> {
    progress!("   Pushing to remote '{}'...", remote_name);

    self.run_git_with_error(&["push", "-u", remote_name, branch], |stderr| {
      RailError::Git(GitError::PushFailed {
        remote: remote_name.to_string(),
        branch: branch.to_string(),
        reason: stderr.to_string(),
      })
    })?;

    progress!("   ✅ Pushed to {}/{}", remote_name, branch);
    Ok(())
  }

  /// Fetch from remote
  pub fn fetch_from_remote(&self, remote_name: &str) -> RailResult<()> {
    progress!("   Fetching from remote '{}'...", remote_name);

    self.run_git(&["fetch", remote_name])?;

    progress!("   ✅ Fetched from {}", remote_name);
    Ok(())
  }

  /// Check if remote exists
  pub fn has_remote(&self, name: &str) -> RailResult<bool> {
    let remotes = self.list_remotes()?;
    Ok(remotes.iter().any(|(n, _)| n == name))
  }

  /// Create a branch
  pub fn create_branch(&self, branch_name: &str) -> RailResult<()> {
    self.run_git(&["branch", branch_name])?;
    Ok(())
  }

  /// Checkout a branch
  pub fn checkout_branch(&self, branch_name: &str) -> RailResult<()> {
    self.run_git(&["checkout", branch_name])?;
    Ok(())
  }

  /// Check if a local branch exists
  pub fn branch_exists(&self, branch_name: &str) -> RailResult<bool> {
    let ref_name = format!("refs/heads/{}", branch_name);
    Ok(self.run_git_check(&["show-ref", "--verify", "--quiet", &ref_name]))
  }

  /// Create and checkout a branch
  pub fn create_and_checkout_branch(&self, branch_name: &str) -> RailResult<()> {
    self.create_branch(branch_name)?;
    self.checkout_branch(branch_name)?;
    Ok(())
  }

  /// Create a commit with specific metadata
  ///
  /// Creates a commit and returns its SHA.
  pub fn create_commit_with_metadata(
    &self,
    message: &str,
    author_name: &str,
    author_email: &str,
    timestamp: i64,
    parent_shas: &[String],
  ) -> RailResult<String> {
    // Stage all changes
    self.run_git(&["add", "-A"])?;

    // Write tree
    let tree_output = self.run_git(&["write-tree"])?;
    let tree_sha = String::from_utf8_lossy(&tree_output.stdout).trim().to_string();

    // Build commit-tree command (needs custom env vars, so we use git_cmd directly)
    let author_date = format!("{} +0000", timestamp);
    let mut cmd = self.git_cmd();
    cmd
      .env("GIT_AUTHOR_NAME", author_name)
      .env("GIT_AUTHOR_EMAIL", author_email)
      .env("GIT_AUTHOR_DATE", &author_date)
      .env("GIT_COMMITTER_NAME", author_name)
      .env("GIT_COMMITTER_EMAIL", author_email)
      .env("GIT_COMMITTER_DATE", &author_date)
      .arg("commit-tree")
      .arg(&tree_sha)
      .arg("-m")
      .arg(message);

    // Add parent arguments
    for parent in parent_shas {
      cmd.arg("-p").arg(parent);
    }

    let output = cmd.output().context("Failed to create commit")?;

    if !output.status.success() {
      let stderr = String::from_utf8_lossy(&output.stderr);
      return Err(RailError::Git(GitError::CommandFailed {
        command: "git commit-tree".to_string(),
        stderr: stderr.to_string(),
      }));
    }

    let commit_sha = String::from_utf8_lossy(&output.stdout).trim().to_string();

    // Update HEAD
    self.run_git(&["reset", "--soft", &commit_sha])?;

    Ok(commit_sha)
  }

  /// Read multiple files in bulk using git cat-file --batch
  ///
  /// This is 100x+ faster than calling read_file_at_commit in a loop.
  /// Uses a single subprocess with `git cat-file --batch` to read all files.
  ///
  /// Used by `collect_tree_files` for optimal performance.
  ///
  /// # Performance
  /// - Single subprocess call (vs N calls for N files)
  /// - Can read 1000+ files in <500ms
  /// - Processes files in parallel chunks using rayon
  ///
  /// Output order matches input order. Missing files produce empty byte vectors.
  pub fn read_files_bulk(&self, items: &[(&str, &Path)]) -> RailResult<Vec<Vec<u8>>> {
    use std::io::Write;
    use std::process::{Command, Stdio};

    if items.is_empty() {
      return Ok(vec![]);
    }

    // Start cat-file --batch process
    let mut child = Command::new("git")
      .arg("-C")
      .arg(&self.repo_path)
      .args(["cat-file", "--batch"])
      .stdin(Stdio::piped())
      .stdout(Stdio::piped())
      .stderr(Stdio::piped())
      .spawn()
      .context("Failed to spawn git cat-file")?;

    let mut stdin = child
      .stdin
      .take()
      .ok_or_else(|| RailError::message("Failed to open stdin"))?;

    // Write all requests to stdin
    for (commit_sha, path) in items {
      let relative_path = self.normalize_path(path);
      let git_path = utils::path_to_git_format(relative_path);
      let spec = format!("{}:{}\n", commit_sha, git_path);
      stdin
        .write_all(spec.as_bytes())
        .context("Failed to write to git cat-file stdin")?;
    }

    drop(stdin); // Close stdin to signal we're done

    // Read output
    let output = child.wait_with_output().context("Failed to read git cat-file output")?;

    if !output.status.success() {
      let stderr = String::from_utf8_lossy(&output.stderr);
      return Err(RailError::Git(GitError::CommandFailed {
        command: "git cat-file --batch".to_string(),
        stderr: stderr.to_string(),
      }));
    }

    // Parse batch output
    // Format: "<sha> <type> <size>\n<content>\n"
    // Or for missing files: "<spec> missing\n"
    let mut results = Vec::with_capacity(items.len());
    let stdout = &output.stdout[..];
    let mut pos = 0;

    for _ in 0..items.len() {
      // Read header line
      let line_end = stdout[pos..]
        .iter()
        .position(|&b| b == b'\n')
        .ok_or_else(|| RailError::message("Invalid cat-file output: missing newline"))?;
      let header = &stdout[pos..pos + line_end];
      pos += line_end + 1;

      // Check if file is missing
      if header.ends_with(b" missing") {
        results.push(vec![]);
        continue;
      }

      // Parse size from header: "<sha> <type> <size>"
      let parts: Vec<&[u8]> = header.split(|&b| b == b' ').collect();
      if parts.len() < 3 {
        return Err(RailError::message(format!(
          "Invalid cat-file header: {}",
          String::from_utf8_lossy(header)
        )));
      }

      let size_str = String::from_utf8_lossy(parts[2]);
      let size: usize = size_str
        .parse()
        .map_err(|_| RailError::message(format!("Invalid size in cat-file output: {}", size_str)))?;

      // Read content
      if pos + size > stdout.len() {
        return Err(RailError::message("Unexpected end of cat-file output"));
      }

      let content = stdout[pos..pos + size].to_vec();
      pos += size;

      // Skip trailing newline
      if pos < stdout.len() && stdout[pos] == b'\n' {
        pos += 1;
      }

      results.push(content);
    }

    Ok(results)
  }

  /// Get multiple commits in bulk (parallel processing)
  ///
  /// Uses rayon to fetch commits in parallel chunks.
  /// Used by `commit_history` for optimal performance.
  ///
  /// # Performance
  /// - Processes commits in parallel using rayon
  /// - Can fetch 1000+ commits in <2s
  ///
  /// Output order matches input SHA order.
  pub fn get_commits_bulk(&self, shas: &[String]) -> RailResult<Vec<CommitInfo>> {
    use rayon::prelude::*;

    let commits: Result<Vec<_>, _> = shas.par_iter().map(|sha| self.get_commit(sha)).collect();

    commits
  }

  /// Resolve a git reference (tag, branch) to a commit SHA
  pub fn resolve_reference(&self, ref_name: &str) -> RailResult<String> {
    self.run_git_stdout(&["rev-parse", ref_name])
  }
}

/// Parse `git diff --name-status -z` output into (path, change_type) pairs.
///
/// Handles:
/// - Simple changes: `M\0path\0` (Modified), `A\0path\0` (Added), `D\0path\0` (Deleted)
/// - Renames: `R100\0old_path\0new_path\0` - emits both (old_path, 'D') and (new_path, 'A')
/// - Copies: `C100\0src_path\0dest_path\0` - emits both (src_path, 'M') and (dest_path, 'A')
/// - Paths with spaces/newlines/tabs: handled safely via NUL separation
///
/// The status codes from git are:
/// - A: Added
/// - C: Copied (followed by similarity percentage)
/// - D: Deleted
/// - M: Modified
/// - R: Renamed (followed by similarity percentage)
/// - T: Type changed
/// - U: Unmerged
/// - X: Unknown
fn parse_name_status_output_z(output: &[u8]) -> RailResult<Vec<(PathBuf, char)>> {
  let mut files = Vec::new();

  let mut parts = output.split(|&b| b == 0);
  loop {
    let Some(status_bytes) = parts.next() else {
      break;
    };
    if status_bytes.is_empty() {
      continue;
    }

    let status = String::from_utf8_lossy(status_bytes);
    let change_type = status.chars().next().unwrap_or('M');

    let mut next_path = || parts.next().filter(|p| !p.is_empty());

    match change_type {
      'R' => {
        // Rename: R100\0old_path\0new_path\0
        // Treat as: delete from old location, add at new location
        let Some(old_path) = next_path() else {
          continue;
        };
        let Some(new_path) = next_path() else {
          // Fallback: if only one path, treat as modified
          files.push((PathBuf::from(String::from_utf8_lossy(old_path).into_owned()), 'M'));
          continue;
        };

        files.push((PathBuf::from(String::from_utf8_lossy(old_path).into_owned()), 'D'));
        files.push((PathBuf::from(String::from_utf8_lossy(new_path).into_owned()), 'A'));
      }
      'C' => {
        // Copy: C100\0src_path\0dest_path\0
        // Source still exists (mark as touched), dest is new
        let Some(src_path) = next_path() else {
          continue;
        };
        let Some(dest_path) = next_path() else {
          files.push((PathBuf::from(String::from_utf8_lossy(src_path).into_owned()), 'A'));
          continue;
        };

        files.push((PathBuf::from(String::from_utf8_lossy(src_path).into_owned()), 'M'));
        files.push((PathBuf::from(String::from_utf8_lossy(dest_path).into_owned()), 'A'));
      }
      'A' | 'D' | 'M' | 'T' | 'U' => {
        let Some(path) = next_path() else {
          continue;
        };
        files.push((PathBuf::from(String::from_utf8_lossy(path).into_owned()), change_type));
      }
      _ => {
        // Unknown status - treat as modified if we have a path
        let Some(path) = next_path() else {
          continue;
        };
        files.push((PathBuf::from(String::from_utf8_lossy(path).into_owned()), 'M'));
      }
    }
  }

  Ok(files)
}

/// Parse git log output into CommitInfo
///
/// Format is %H%n%an%n%ae%n%at%n%cn%n%ce%n%ct%n%P%n%B
/// Which gives us: hash, author name, author email, author time,
///                 committer name, committer email, committer time,
///                 parent hashes, body
fn parse_commit_output(data: &[u8]) -> RailResult<CommitInfo> {
  let output = String::from_utf8_lossy(data);
  let mut lines = output.lines();

  let sha = lines
    .next()
    .ok_or_else(|| RailError::message("Missing commit SHA"))?
    .to_string();
  let author = lines
    .next()
    .ok_or_else(|| RailError::message("Missing author name"))?
    .to_string();
  let author_email = lines
    .next()
    .ok_or_else(|| RailError::message("Missing author email"))?
    .to_string();
  let timestamp = lines
    .next()
    .and_then(|s| s.parse::<i64>().ok())
    .ok_or_else(|| RailError::message("Missing/invalid author timestamp"))?;
  let committer = lines
    .next()
    .ok_or_else(|| RailError::message("Missing committer name"))?
    .to_string();
  let committer_email = lines
    .next()
    .ok_or_else(|| RailError::message("Missing committer email"))?
    .to_string();
  let _committer_timestamp = lines.next(); // We don't use this
  let parents_line = lines.next().unwrap_or("");
  let parent_shas = if parents_line.is_empty() {
    vec![]
  } else {
    parents_line.split_whitespace().map(|s| s.to_string()).collect()
  };

  // Rest is commit message
  let message: Vec<String> = lines.map(|s| s.to_string()).collect();
  let message = message.join("\n").trim().to_string();

  Ok(CommitInfo {
    sha,
    author,
    author_email,
    committer,
    committer_email,
    message,
    timestamp,
    parent_shas,
  })
}

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

  /// Helper to get the git repository root
  fn find_git_root() -> PathBuf {
    env::current_dir().unwrap()
  }

  #[test]
  fn test_commit_history() {
    let git = SystemGit::open(&find_git_root()).unwrap();

    // Test with limit
    let commits = git.commit_history(Some(5)).unwrap();
    assert!(!commits.is_empty());
    assert!(commits.len() <= 5);

    // Check first commit has required fields
    let first = &commits[0];
    assert!(!first.sha.is_empty());
    assert_eq!(first.sha.len(), 40); // Full SHA
    assert!(!first.author.is_empty());
    assert!(first.timestamp > 0);
    assert!(!first.message.is_empty());
  }

  #[test]
  fn test_get_changed_files() {
    let git = SystemGit::open(&find_git_root()).unwrap();
    let head = git.head_commit().unwrap();

    // Get changed files for HEAD
    let changed = git.get_changed_files(&head).unwrap();

    // HEAD should have at least one changed file (unless it's the initial commit)
    // Just verify the call succeeds and returns valid data
    for (path, change_type) in changed {
      assert!(!path.as_os_str().is_empty());
      assert!(['A', 'M', 'D', 'R', 'C'].contains(&change_type));
    }
  }

  #[test]
  fn test_get_commits_bulk() {
    let git = SystemGit::open(&find_git_root()).unwrap();

    // Get some commit SHAs
    let history = git.commit_history(Some(5)).unwrap();
    let shas: Vec<String> = history.iter().map(|c| c.sha.clone()).collect();

    // Fetch them in bulk
    let commits = git.get_commits_bulk(&shas).unwrap();

    assert_eq!(commits.len(), shas.len());

    // Verify all commits match
    for (i, commit) in commits.iter().enumerate() {
      assert_eq!(commit.sha, shas[i]);
    }
  }

  #[test]
  fn test_read_files_bulk() {
    let git = SystemGit::open(&find_git_root()).unwrap();
    let head = git.head_commit().unwrap();

    // Prepare items to read
    let paths = [
      PathBuf::from("Cargo.toml"),
      PathBuf::from("README.md"),
      PathBuf::from("this-does-not-exist.txt"),
    ];
    let items: Vec<(&str, &Path)> = paths.iter().map(|p| (head.as_str(), p.as_path())).collect();

    let results = git.read_files_bulk(&items).unwrap();

    assert_eq!(results.len(), 3);

    // First two should have content (Cargo.toml and README.md exist)
    assert!(!results[0].is_empty(), "Cargo.toml should exist");
    assert!(!results[1].is_empty(), "README.md should exist");

    // Third should be empty (file doesn't exist)
    assert!(results[2].is_empty(), "Non-existent file should be empty");

    // Verify Cargo.toml content
    let cargo_toml = String::from_utf8_lossy(&results[0]);
    assert!(cargo_toml.contains("package") || cargo_toml.contains("dependencies"));
  }

  #[test]
  fn test_read_files_bulk_empty() {
    let git = SystemGit::open(&find_git_root()).unwrap();

    // Empty input should return empty output
    let results = git.read_files_bulk(&[]).unwrap();
    assert!(results.is_empty());
  }

  #[test]
  fn test_get_commits_touching_path() {
    let git = SystemGit::open(&find_git_root()).unwrap();

    // Get commits that touched Cargo.toml
    let commits = git
      .get_commits_touching_path(Path::new("Cargo.toml"), None, "HEAD")
      .unwrap();

    // Cargo.toml should have been modified at least once
    assert!(!commits.is_empty(), "Cargo.toml should have commits");

    // Verify chronological order (oldest first)
    if commits.len() >= 2 {
      assert!(
        commits[0].timestamp <= commits[1].timestamp,
        "Commits should be in chronological order"
      );
    }
  }

  #[test]
  fn test_collect_tree_files_with_bulk() {
    let git = SystemGit::open(&find_git_root()).unwrap();
    let head = git.head_commit().unwrap();

    // Collect files from src/ directory at HEAD
    let files = git.collect_tree_files(&head, Path::new("src")).unwrap();

    // Should have at least main.rs and some core files
    assert!(!files.is_empty(), "src/ should contain files");

    // Verify all files have valid paths
    for (path, _content) in &files {
      assert!(!path.as_os_str().is_empty(), "Path should not be empty");
      // Most source files should have content (some may be empty but most won't be)
    }

    // Verify at least one Rust file exists with actual content
    let has_rust_with_content = files
      .iter()
      .any(|(path, content)| path.extension().and_then(|s| s.to_str()) == Some("rs") && !content.is_empty());
    assert!(has_rust_with_content, "Should have at least one .rs file with content");

    // Test with empty path (root directory) - should get all files
    let all_files = git.collect_tree_files(&head, Path::new("")).unwrap();
    assert!(
      all_files.len() >= files.len(),
      "Root should have at least as many files as src/"
    );
  }

  #[test]
  fn test_collect_tree_files_nonexistent() {
    let git = SystemGit::open(&find_git_root()).unwrap();
    let head = git.head_commit().unwrap();

    // Try to collect from non-existent directory
    let files = git
      .collect_tree_files(&head, Path::new("this-directory-does-not-exist-12345"))
      .unwrap();

    // Should return empty list for non-existent directory
    assert!(files.is_empty(), "Non-existent directory should return empty list");
  }

  #[test]
  fn test_parse_name_status_simple() {
    // Simple modifications
    let output = b"M\0src/main.rs\0A\0src/new.rs\0D\0src/old.rs\0";
    let result = parse_name_status_output_z(output).unwrap();

    assert_eq!(result.len(), 3);
    assert_eq!(result[0], (PathBuf::from("src/main.rs"), 'M'));
    assert_eq!(result[1], (PathBuf::from("src/new.rs"), 'A'));
    assert_eq!(result[2], (PathBuf::from("src/old.rs"), 'D'));
  }

  #[test]
  fn test_parse_name_status_rename() {
    // Rename: R100 (100% similarity) old_path -> new_path
    let output = b"R100\0src/old_name.rs\0src/new_name.rs\0";
    let result = parse_name_status_output_z(output).unwrap();

    // Should emit both: delete from old, add at new
    assert_eq!(result.len(), 2);
    assert_eq!(result[0], (PathBuf::from("src/old_name.rs"), 'D'));
    assert_eq!(result[1], (PathBuf::from("src/new_name.rs"), 'A'));
  }

  #[test]
  fn test_parse_name_status_copy() {
    // Copy: C100 (100% similarity) src_path -> dest_path
    let output = b"C095\0src/original.rs\0src/copied.rs\0";
    let result = parse_name_status_output_z(output).unwrap();

    // Should emit: source touched, dest added
    assert_eq!(result.len(), 2);
    assert_eq!(result[0], (PathBuf::from("src/original.rs"), 'M'));
    assert_eq!(result[1], (PathBuf::from("src/copied.rs"), 'A'));
  }

  #[test]
  fn test_parse_name_status_paths_with_spaces() {
    // Paths with spaces are handled by NUL separation
    let output = b"M\0path with spaces/file name.rs\0A\0another path/new file.txt\0";
    let result = parse_name_status_output_z(output).unwrap();

    assert_eq!(result.len(), 2);
    assert_eq!(result[0], (PathBuf::from("path with spaces/file name.rs"), 'M'));
    assert_eq!(result[1], (PathBuf::from("another path/new file.txt"), 'A'));
  }

  #[test]
  fn test_parse_name_status_rename_with_spaces() {
    // Rename with spaces in paths
    let output = b"R100\0old path/old file.rs\0new path/new file.rs\0";
    let result = parse_name_status_output_z(output).unwrap();

    assert_eq!(result.len(), 2);
    assert_eq!(result[0], (PathBuf::from("old path/old file.rs"), 'D'));
    assert_eq!(result[1], (PathBuf::from("new path/new file.rs"), 'A'));
  }

  #[test]
  fn test_parse_name_status_mixed() {
    // Mixed changes in one diff
    let output = b"M\0src/lib.rs\0R100\0src/old.rs\0src/renamed.rs\0A\0src/new.rs\0D\0src/deleted.rs\0";
    let result = parse_name_status_output_z(output).unwrap();

    assert_eq!(result.len(), 5);
    assert_eq!(result[0], (PathBuf::from("src/lib.rs"), 'M'));
    assert_eq!(result[1], (PathBuf::from("src/old.rs"), 'D')); // Rename source
    assert_eq!(result[2], (PathBuf::from("src/renamed.rs"), 'A')); // Rename dest
    assert_eq!(result[3], (PathBuf::from("src/new.rs"), 'A'));
    assert_eq!(result[4], (PathBuf::from("src/deleted.rs"), 'D'));
  }

  #[test]
  fn test_parse_name_status_empty() {
    let result = parse_name_status_output_z(b"").unwrap();
    assert!(result.is_empty());

    let result = parse_name_status_output_z(b"\0\0").unwrap();
    assert!(result.is_empty());
  }

  #[test]
  fn test_parse_name_status_type_change() {
    // Type change (e.g., file to symlink)
    let output = b"T\0src/link.rs\0";
    let result = parse_name_status_output_z(output).unwrap();

    assert_eq!(result.len(), 1);
    assert_eq!(result[0], (PathBuf::from("src/link.rs"), 'T'));
  }
}