infiniloom-engine 0.6.3

High-performance repository context engine for LLMs - AST parsing, token counting, and secret detection
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
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
//! Git integration for diff/log analysis
//!
//! Provides integration with Git for:
//! - Getting changed files between commits
//! - Extracting commit history
//! - Blame information for file importance

use std::path::Path;
use std::process::Command;
use thiserror::Error;

/// Git repository wrapper
pub struct GitRepo {
    path: String,
}

/// A git commit entry
#[derive(Debug, Clone)]
pub struct Commit {
    pub hash: String,
    pub short_hash: String,
    pub author: String,
    pub email: String,
    pub date: String,
    pub message: String,
}

/// A file changed in a commit
#[derive(Debug, Clone)]
pub struct ChangedFile {
    /// Current path (or new path for renames)
    pub path: String,
    /// Original path for renamed/copied files (None for add/modify/delete)
    pub old_path: Option<String>,
    pub status: FileStatus,
    pub additions: u32,
    pub deletions: u32,
}

/// File change status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileStatus {
    Added,
    Modified,
    Deleted,
    Renamed,
    Copied,
    Unknown,
}

impl FileStatus {
    fn from_char(c: char) -> Self {
        match c {
            'A' => Self::Added,
            'M' => Self::Modified,
            'D' => Self::Deleted,
            'R' => Self::Renamed,
            'C' => Self::Copied,
            _ => Self::Unknown,
        }
    }
}

/// Blame entry for a line
#[derive(Debug, Clone)]
pub struct BlameLine {
    pub commit: String,
    pub author: String,
    pub date: String,
    pub line_number: u32,
}

/// Type of line change in a diff
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiffLineType {
    /// Line was added
    Add,
    /// Line was removed
    Remove,
    /// Context line (unchanged)
    Context,
}

impl DiffLineType {
    /// Get string representation
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Add => "add",
            Self::Remove => "remove",
            Self::Context => "context",
        }
    }
}

/// A single line change within a diff hunk
#[derive(Debug, Clone)]
pub struct DiffLine {
    /// Type of change: add, remove, or context
    pub change_type: DiffLineType,
    /// Line number in the old file (None for additions)
    pub old_line: Option<u32>,
    /// Line number in the new file (None for deletions)
    pub new_line: Option<u32>,
    /// The actual line content (without +/- prefix)
    pub content: String,
}

/// A diff hunk representing a contiguous block of changes
#[derive(Debug, Clone)]
pub struct DiffHunk {
    /// File path this hunk belongs to (relative to repo root)
    pub file: String,
    /// Starting line in the old file
    pub old_start: u32,
    /// Number of lines in the old file section
    pub old_count: u32,
    /// Starting line in the new file
    pub new_start: u32,
    /// Number of lines in the new file section
    pub new_count: u32,
    /// Header line (e.g., "@@ -1,5 +1,7 @@ function name")
    pub header: String,
    /// Individual line changes within this hunk
    pub lines: Vec<DiffLine>,
}

/// Git errors
#[derive(Debug, Error)]
pub enum GitError {
    #[error("Not a git repository")]
    NotAGitRepo,
    #[error("Git command failed: {0}")]
    CommandFailed(String),
    #[error("Parse error: {0}")]
    ParseError(String),
}

impl GitRepo {
    /// Open a git repository
    pub fn open(path: &Path) -> Result<Self, GitError> {
        let git_dir = path.join(".git");
        if !git_dir.exists() {
            return Err(GitError::NotAGitRepo);
        }

        Ok(Self { path: path.to_string_lossy().to_string() })
    }

    /// Check if path is a git repository
    pub fn is_git_repo(path: &Path) -> bool {
        path.join(".git").exists()
    }

    /// Get current branch name
    pub fn current_branch(&self) -> Result<String, GitError> {
        let output = self.run_git(&["rev-parse", "--abbrev-ref", "HEAD"])?;
        Ok(output.trim().to_owned())
    }

    /// Get current commit hash
    pub fn current_commit(&self) -> Result<String, GitError> {
        let output = self.run_git(&["rev-parse", "HEAD"])?;
        Ok(output.trim().to_owned())
    }

    /// Get short commit hash
    pub fn short_hash(&self, commit: &str) -> Result<String, GitError> {
        let output = self.run_git(&["rev-parse", "--short", commit])?;
        Ok(output.trim().to_owned())
    }

    /// Get files changed between two commits
    pub fn diff_files(&self, from: &str, to: &str) -> Result<Vec<ChangedFile>, GitError> {
        // First get file status with --name-status (shows A/M/D/R/C status)
        let status_output = self.run_git(&["diff", "--name-status", from, to])?;

        // Then get line counts with --numstat (shows additions/deletions)
        let numstat_output = self.run_git(&["diff", "--numstat", from, to])?;

        // Build a map of path -> (additions, deletions) from numstat
        let mut stats: std::collections::HashMap<String, (u32, u32)> =
            std::collections::HashMap::new();
        for line in numstat_output.lines() {
            if line.is_empty() {
                continue;
            }
            let parts: Vec<&str> = line.split('\t').collect();
            if parts.len() >= 3 {
                // numstat format: additions<TAB>deletions<TAB>path
                // Binary files show "-" for additions/deletions
                let add = parts[0].parse::<u32>().unwrap_or(0);
                let del = parts[1].parse::<u32>().unwrap_or(0);
                let path = parts[2..].join("\t");
                stats.insert(path, (add, del));
            }
        }

        let mut files = Vec::new();

        // Parse name-status output
        for line in status_output.lines() {
            if line.is_empty() {
                continue;
            }

            let parts: Vec<&str> = line.split('\t').collect();
            if parts.is_empty() {
                continue;
            }

            let status_str = parts[0];
            let first_char = status_str.chars().next().unwrap_or(' ');
            let status = FileStatus::from_char(first_char);

            // Handle renamed/copied files: R100 or C100 followed by old_path and new_path
            let (path, old_path) = if (first_char == 'R' || first_char == 'C') && parts.len() >= 3 {
                // For renames: parts[1] = old_path, parts[2] = new_path
                (parts[2].to_owned(), Some(parts[1].to_owned()))
            } else if parts.len() >= 2 {
                // For other statuses: parts[1] is the path
                (parts[1].to_owned(), None)
            } else {
                continue;
            };

            // Look up line statistics
            let (additions, deletions) = stats.get(&path).copied().unwrap_or((0, 0));

            files.push(ChangedFile { path, old_path, status, additions, deletions });
        }

        Ok(files)
    }

    /// Get files changed in working tree
    ///
    /// Returns both staged and unstaged changes. For renames, the `old_path`
    /// field contains the original filename.
    pub fn status(&self) -> Result<Vec<ChangedFile>, GitError> {
        let output = self.run_git(&["status", "--porcelain"])?;

        let mut files = Vec::new();

        for line in output.lines() {
            if line.len() < 3 {
                continue;
            }

            // Git status --porcelain format: XY filename
            // X = staged status, Y = unstaged status
            let staged_char = line.chars().next().unwrap_or(' ');
            let unstaged_char = line.chars().nth(1).unwrap_or(' ');
            let path_part = &line[3..];

            // Determine the effective status (prefer staged, then unstaged)
            let (status, status_char) = if staged_char != ' ' && staged_char != '?' {
                // Has staged changes
                (
                    match staged_char {
                        'A' => FileStatus::Added,
                        'M' => FileStatus::Modified,
                        'D' => FileStatus::Deleted,
                        'R' => FileStatus::Renamed,
                        'C' => FileStatus::Copied,
                        _ => FileStatus::Unknown,
                    },
                    staged_char,
                )
            } else {
                // Only unstaged changes
                (
                    match unstaged_char {
                        '?' | 'A' => FileStatus::Added,
                        'M' => FileStatus::Modified,
                        'D' => FileStatus::Deleted,
                        'R' => FileStatus::Renamed,
                        _ => FileStatus::Unknown,
                    },
                    unstaged_char,
                )
            };

            // Handle renames: format is "old_path -> new_path"
            let (path, old_path) = if status_char == 'R' || status_char == 'C' {
                if let Some(arrow_pos) = path_part.find(" -> ") {
                    let old = path_part[..arrow_pos].to_owned();
                    let new = path_part[arrow_pos + 4..].to_owned();
                    (new, Some(old))
                } else {
                    (path_part.to_owned(), None)
                }
            } else {
                (path_part.to_owned(), None)
            };

            files.push(ChangedFile { path, old_path, status, additions: 0, deletions: 0 });
        }

        Ok(files)
    }

    /// Get recent commits
    pub fn log(&self, count: usize) -> Result<Vec<Commit>, GitError> {
        let output = self.run_git(&[
            "log",
            &format!("-{}", count),
            "--format=%H%n%h%n%an%n%ae%n%ad%n%s%n---COMMIT---",
            "--date=short",
        ])?;

        let mut commits = Vec::new();
        let mut lines = output.lines().peekable();

        while lines.peek().is_some() {
            let hash = lines.next().unwrap_or("").to_owned();
            if hash.is_empty() {
                continue;
            }

            let short_hash = lines.next().unwrap_or("").to_owned();
            let author = lines.next().unwrap_or("").to_owned();
            let email = lines.next().unwrap_or("").to_owned();
            let date = lines.next().unwrap_or("").to_owned();
            let message = lines.next().unwrap_or("").to_owned();

            // Skip separator
            while lines.peek().is_some_and(|l| *l != "---COMMIT---") {
                lines.next();
            }
            lines.next(); // Skip the separator

            commits.push(Commit { hash, short_hash, author, email, date, message });
        }

        Ok(commits)
    }

    /// Get commits that modified a specific file
    pub fn file_log(&self, path: &str, count: usize) -> Result<Vec<Commit>, GitError> {
        let output = self.run_git(&[
            "log",
            &format!("-{}", count),
            "--format=%H%n%h%n%an%n%ae%n%ad%n%s%n---COMMIT---",
            "--date=short",
            "--follow",
            "--",
            path,
        ])?;

        let mut commits = Vec::new();
        let commit_blocks: Vec<&str> = output.split("---COMMIT---").collect();

        for block in commit_blocks {
            let lines: Vec<&str> = block.lines().filter(|l| !l.is_empty()).collect();
            if lines.len() < 6 {
                continue;
            }

            commits.push(Commit {
                hash: lines[0].to_owned(),
                short_hash: lines[1].to_owned(),
                author: lines[2].to_owned(),
                email: lines[3].to_owned(),
                date: lines[4].to_owned(),
                message: lines[5].to_owned(),
            });
        }

        Ok(commits)
    }

    /// Get blame information for a file
    pub fn blame(&self, path: &str) -> Result<Vec<BlameLine>, GitError> {
        let output = self.run_git(&["blame", "--porcelain", path])?;

        let mut lines = Vec::new();
        let mut current_commit = String::new();
        let mut current_author = String::new();
        let mut current_date = String::new();
        let mut line_number = 0u32;

        for line in output.lines() {
            if line.starts_with('\t') {
                // This is the actual line content, create blame entry
                lines.push(BlameLine {
                    commit: current_commit.clone(),
                    author: current_author.clone(),
                    date: current_date.clone(),
                    line_number,
                });
            } else if line.len() >= 40 && line.chars().take(40).all(|c| c.is_ascii_hexdigit()) {
                // New commit hash line
                let parts: Vec<&str> = line.split_whitespace().collect();
                if !parts.is_empty() {
                    current_commit = parts[0][..8.min(parts[0].len())].to_string();
                    if parts.len() >= 3 {
                        line_number = parts[2].parse().unwrap_or(0);
                    }
                }
            } else if let Some(author) = line.strip_prefix("author ") {
                current_author = author.to_owned();
            } else if let Some(time) = line.strip_prefix("author-time ") {
                // Convert Unix timestamp to date
                if let Ok(ts) = time.parse::<i64>() {
                    current_date = format_timestamp(ts);
                }
            }
        }

        Ok(lines)
    }

    /// Get list of files tracked by git
    pub fn ls_files(&self) -> Result<Vec<String>, GitError> {
        let output = self.run_git(&["ls-files"])?;
        Ok(output.lines().map(String::from).collect())
    }

    /// Get diff content between two commits for a file
    pub fn diff_content(&self, from: &str, to: &str, path: &str) -> Result<String, GitError> {
        self.run_git(&["diff", from, to, "--", path])
    }

    /// Get diff content for uncommitted changes (working tree vs HEAD)
    /// Includes both staged and unstaged changes.
    pub fn uncommitted_diff(&self, path: &str) -> Result<String, GitError> {
        // Get both staged and unstaged changes combined
        self.run_git(&["diff", "HEAD", "--", path])
    }

    /// Get diff content for all uncommitted changes
    /// Returns combined diff for all changed files.
    pub fn all_uncommitted_diffs(&self) -> Result<String, GitError> {
        self.run_git(&["diff", "HEAD"])
    }

    /// Check if a file has uncommitted changes
    pub fn has_changes(&self, path: &str) -> Result<bool, GitError> {
        let output = self.run_git(&["status", "--porcelain", "--", path])?;
        Ok(!output.trim().is_empty())
    }

    /// Get the commit where a file was last modified
    pub fn last_modified_commit(&self, path: &str) -> Result<Commit, GitError> {
        let commits = self.file_log(path, 1)?;
        commits
            .into_iter()
            .next()
            .ok_or_else(|| GitError::ParseError("No commits found".to_owned()))
    }

    /// Calculate file importance based on recent changes
    pub fn file_change_frequency(&self, path: &str, days: u32) -> Result<u32, GitError> {
        let output = self.run_git(&[
            "log",
            &format!("--since={} days ago", days),
            "--oneline",
            "--follow",
            "--",
            path,
        ])?;

        Ok(output.lines().count() as u32)
    }

    /// Get file content at a specific git ref (commit, branch, tag)
    ///
    /// Uses `git show <ref>:<path>` to retrieve file content at that revision.
    ///
    /// # Arguments
    /// * `path` - File path relative to repository root
    /// * `git_ref` - Git ref (commit hash, branch name, tag, HEAD~n, etc.)
    ///
    /// # Returns
    /// File content as string, or error if file doesn't exist at that ref
    ///
    /// # Example
    /// ```ignore
    /// let repo = GitRepo::open(Path::new("."))?;
    /// let content = repo.file_at_ref("src/main.rs", "HEAD~5")?;
    /// ```
    pub fn file_at_ref(&self, path: &str, git_ref: &str) -> Result<String, GitError> {
        self.run_git(&["show", &format!("{}:{}", git_ref, path)])
    }

    /// Parse diff between two refs into structured hunks
    ///
    /// Returns detailed hunk information including line numbers for each change.
    ///
    /// # Arguments
    /// * `from_ref` - Starting ref (e.g., "main", "HEAD~5", commit hash)
    /// * `to_ref` - Ending ref (e.g., "HEAD", "feature-branch")
    /// * `path` - Optional file path to filter to a single file
    ///
    /// # Returns
    /// Vec of DiffHunk with structured line-level information
    pub fn diff_hunks(
        &self,
        from_ref: &str,
        to_ref: &str,
        path: Option<&str>,
    ) -> Result<Vec<DiffHunk>, GitError> {
        let output = match path {
            Some(p) => self.run_git(&["diff", "-U3", from_ref, to_ref, "--", p])?,
            None => self.run_git(&["diff", "-U3", from_ref, to_ref])?,
        };

        parse_diff_hunks(&output)
    }

    /// Parse uncommitted changes (working tree vs HEAD) into structured hunks
    ///
    /// # Arguments
    /// * `path` - Optional file path to filter to a single file
    ///
    /// # Returns
    /// Vec of DiffHunk for uncommitted changes
    pub fn uncommitted_hunks(&self, path: Option<&str>) -> Result<Vec<DiffHunk>, GitError> {
        let output = match path {
            Some(p) => self.run_git(&["diff", "-U3", "HEAD", "--", p])?,
            None => self.run_git(&["diff", "-U3", "HEAD"])?,
        };

        parse_diff_hunks(&output)
    }

    /// Parse staged changes into structured hunks
    ///
    /// # Arguments
    /// * `path` - Optional file path to filter to a single file
    ///
    /// # Returns
    /// Vec of DiffHunk for staged changes only
    pub fn staged_hunks(&self, path: Option<&str>) -> Result<Vec<DiffHunk>, GitError> {
        let output = match path {
            Some(p) => self.run_git(&["diff", "-U3", "--staged", "--", p])?,
            None => self.run_git(&["diff", "-U3", "--staged"])?,
        };

        parse_diff_hunks(&output)
    }

    /// Run a git command and return output
    fn run_git(&self, args: &[&str]) -> Result<String, GitError> {
        let output = Command::new("git")
            .current_dir(&self.path)
            .args(args)
            .output()
            .map_err(|e| GitError::CommandFailed(e.to_string()))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(GitError::CommandFailed(stderr.to_string()));
        }

        String::from_utf8(output.stdout).map_err(|e| GitError::ParseError(e.to_string()))
    }
}

/// Format Unix timestamp as YYYY-MM-DD
fn format_timestamp(ts: i64) -> String {
    // Simple formatting without chrono
    let secs_per_day = 86400;
    let days_since_epoch = ts / secs_per_day;

    // Approximate calculation (doesn't account for leap seconds)
    let mut year = 1970;
    let mut remaining_days = days_since_epoch;

    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if remaining_days < days_in_year {
            break;
        }
        remaining_days -= days_in_year;
        year += 1;
    }

    let days_in_months = if is_leap_year(year) {
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    } else {
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    };

    let mut month = 1;
    for days in days_in_months {
        if remaining_days < days {
            break;
        }
        remaining_days -= days;
        month += 1;
    }

    let day = remaining_days + 1;

    format!("{:04}-{:02}-{:02}", year, month, day)
}

fn is_leap_year(year: i64) -> bool {
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

/// Parse unified diff output into structured hunks
///
/// Handles the standard unified diff format with hunk headers like:
/// `@@ -start,count +start,count @@ optional context`
fn parse_diff_hunks(diff_output: &str) -> Result<Vec<DiffHunk>, GitError> {
    let mut hunks = Vec::new();
    let mut current_hunk: Option<DiffHunk> = None;
    let mut current_file = String::new();
    let mut old_line = 0u32;
    let mut new_line = 0u32;

    for line in diff_output.lines() {
        // Reset file tracking when we see a new diff header
        if line.starts_with("diff --git") {
            // Save previous hunk if exists before starting new file
            if let Some(hunk) = current_hunk.take() {
                hunks.push(hunk);
            }
            current_file = String::new();
            continue;
        }
        // Track file from "--- a/path" lines (old file path)
        if let Some(path) = line.strip_prefix("--- a/") {
            current_file = path.to_owned();
            continue;
        }
        // Track file from "+++ b/path" lines (new file path - prefer this)
        if let Some(path) = line.strip_prefix("+++ b/") {
            current_file = path.to_owned();
            continue;
        }
        // Handle /dev/null for new or deleted files
        if line.starts_with("--- /dev/null") || line.starts_with("+++ /dev/null") {
            continue;
        }

        // Check for hunk header: @@ -old_start,old_count +new_start,new_count @@ context
        if line.starts_with("@@") {
            // Save previous hunk if exists
            if let Some(hunk) = current_hunk.take() {
                hunks.push(hunk);
            }

            // Parse hunk header
            if let Some((old_start, old_count, new_start, new_count)) = parse_hunk_header(line) {
                old_line = old_start;
                new_line = new_start;

                current_hunk = Some(DiffHunk {
                    file: current_file.clone(),
                    old_start,
                    old_count,
                    new_start,
                    new_count,
                    header: line.to_owned(),
                    lines: Vec::new(),
                });
            }
        } else if let Some(ref mut hunk) = current_hunk {
            // Parse line within a hunk
            if let Some(first_char) = line.chars().next() {
                let (change_type, content) = match first_char {
                    '+' => (DiffLineType::Add, line[1..].to_owned()),
                    '-' => (DiffLineType::Remove, line[1..].to_owned()),
                    ' ' => (DiffLineType::Context, line[1..].to_owned()),
                    '\\' => continue, // "\ No newline at end of file"
                    _ => continue,    // Skip diff headers (diff --git, index, ---, +++)
                };

                let (old_ln, new_ln) = match change_type {
                    DiffLineType::Add => {
                        let nl = new_line;
                        new_line += 1;
                        (None, Some(nl))
                    },
                    DiffLineType::Remove => {
                        let ol = old_line;
                        old_line += 1;
                        (Some(ol), None)
                    },
                    DiffLineType::Context => {
                        let ol = old_line;
                        let nl = new_line;
                        old_line += 1;
                        new_line += 1;
                        (Some(ol), Some(nl))
                    },
                };

                hunk.lines.push(DiffLine {
                    change_type,
                    old_line: old_ln,
                    new_line: new_ln,
                    content,
                });
            }
        }
    }

    // Push final hunk
    if let Some(hunk) = current_hunk {
        hunks.push(hunk);
    }

    Ok(hunks)
}

/// Parse a hunk header line into (old_start, old_count, new_start, new_count)
///
/// Format: @@ -old_start,old_count +new_start,new_count @@ optional_context
/// Note: count defaults to 1 if omitted (e.g., @@ -5 +5,2 @@)
fn parse_hunk_header(header: &str) -> Option<(u32, u32, u32, u32)> {
    // Find the range specifications between @@ markers
    let header = header.strip_prefix("@@")?;
    let end_idx = header.find("@@")?;
    let range_part = header[..end_idx].trim();

    let parts: Vec<&str> = range_part.split_whitespace().collect();
    if parts.len() < 2 {
        return None;
    }

    // Parse old range: -start,count or -start
    let old_part = parts[0].strip_prefix('-')?;
    let (old_start, old_count) = parse_range(old_part)?;

    // Parse new range: +start,count or +start
    let new_part = parts[1].strip_prefix('+')?;
    let (new_start, new_count) = parse_range(new_part)?;

    Some((old_start, old_count, new_start, new_count))
}

/// Parse a range specification like "5,3" or "5" into (start, count)
fn parse_range(range: &str) -> Option<(u32, u32)> {
    if let Some((start_str, count_str)) = range.split_once(',') {
        let start = start_str.parse().ok()?;
        let count = count_str.parse().ok()?;
        Some((start, count))
    } else {
        let start = range.parse().ok()?;
        Some((start, 1)) // Default count is 1
    }
}

#[cfg(test)]
#[allow(clippy::str_to_string)]
mod tests {
    use super::*;
    use std::process::Command;
    use tempfile::TempDir;

    fn init_test_repo() -> TempDir {
        let temp = TempDir::new().unwrap();

        // Initialize git repo
        Command::new("git")
            .current_dir(temp.path())
            .args(["init"])
            .output()
            .unwrap();

        // Configure git
        Command::new("git")
            .current_dir(temp.path())
            .args(["config", "user.email", "test@test.com"])
            .output()
            .unwrap();

        Command::new("git")
            .current_dir(temp.path())
            .args(["config", "user.name", "Test"])
            .output()
            .unwrap();

        // Create a file and commit
        std::fs::write(temp.path().join("test.txt"), "hello").unwrap();

        Command::new("git")
            .current_dir(temp.path())
            .args(["add", "."])
            .output()
            .unwrap();

        Command::new("git")
            .current_dir(temp.path())
            .args(["commit", "-m", "Initial commit"])
            .output()
            .unwrap();

        temp
    }

    #[test]
    fn test_open_repo() {
        let temp = init_test_repo();
        let repo = GitRepo::open(temp.path());
        assert!(repo.is_ok());
    }

    #[test]
    fn test_not_a_repo() {
        let temp = TempDir::new().unwrap();
        let repo = GitRepo::open(temp.path());
        assert!(matches!(repo, Err(GitError::NotAGitRepo)));
    }

    #[test]
    fn test_current_branch() {
        let temp = init_test_repo();
        let repo = GitRepo::open(temp.path()).unwrap();
        let branch = repo.current_branch().unwrap();
        // Branch could be "main" or "master" depending on git config
        assert!(!branch.is_empty());
    }

    #[test]
    fn test_log() {
        let temp = init_test_repo();
        let repo = GitRepo::open(temp.path()).unwrap();
        let commits = repo.log(10).unwrap();
        assert!(!commits.is_empty());
        assert_eq!(commits[0].message, "Initial commit");
    }

    #[test]
    fn test_ls_files() {
        let temp = init_test_repo();
        let repo = GitRepo::open(temp.path()).unwrap();
        let files = repo.ls_files().unwrap();
        assert!(files.contains(&"test.txt".to_string()));
    }

    #[test]
    fn test_format_timestamp() {
        // 2024-01-01 00:00:00 UTC
        let ts = 1704067200;
        let date = format_timestamp(ts);
        assert_eq!(date, "2024-01-01");
    }

    #[test]
    fn test_file_at_ref() {
        let temp = init_test_repo();
        let repo = GitRepo::open(temp.path()).unwrap();

        // Get file content at HEAD
        let content = repo.file_at_ref("test.txt", "HEAD").unwrap();
        assert_eq!(content.trim(), "hello");

        // Modify the file and commit
        std::fs::write(temp.path().join("test.txt"), "world").unwrap();
        Command::new("git")
            .current_dir(temp.path())
            .args(["add", "."])
            .output()
            .unwrap();
        Command::new("git")
            .current_dir(temp.path())
            .args(["commit", "-m", "Update"])
            .output()
            .unwrap();

        // Check current HEAD has new content
        let new_content = repo.file_at_ref("test.txt", "HEAD").unwrap();
        assert_eq!(new_content.trim(), "world");

        // Check HEAD~1 still has old content
        let old_content = repo.file_at_ref("test.txt", "HEAD~1").unwrap();
        assert_eq!(old_content.trim(), "hello");
    }

    #[test]
    fn test_parse_hunk_header() {
        // Standard case
        let result = parse_hunk_header("@@ -1,5 +1,7 @@ fn main()");
        assert_eq!(result, Some((1, 5, 1, 7)));

        // No count (defaults to 1)
        let result = parse_hunk_header("@@ -1 +1 @@");
        assert_eq!(result, Some((1, 1, 1, 1)));

        // Mixed
        let result = parse_hunk_header("@@ -10,3 +15 @@");
        assert_eq!(result, Some((10, 3, 15, 1)));

        // Invalid
        let result = parse_hunk_header("not a header");
        assert_eq!(result, None);
    }

    #[test]
    fn test_parse_diff_hunks() {
        let diff = r#"diff --git a/test.txt b/test.txt
index abc123..def456 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
 line 1
-old line 2
+new line 2
+added line
 line 3
"#;

        let hunks = parse_diff_hunks(diff).unwrap();
        assert_eq!(hunks.len(), 1);

        let hunk = &hunks[0];
        assert_eq!(hunk.old_start, 1);
        assert_eq!(hunk.old_count, 3);
        assert_eq!(hunk.new_start, 1);
        assert_eq!(hunk.new_count, 4);
        assert_eq!(hunk.lines.len(), 5);

        // Check line types
        assert_eq!(hunk.lines[0].change_type, DiffLineType::Context);
        assert_eq!(hunk.lines[1].change_type, DiffLineType::Remove);
        assert_eq!(hunk.lines[2].change_type, DiffLineType::Add);
        assert_eq!(hunk.lines[3].change_type, DiffLineType::Add);
        assert_eq!(hunk.lines[4].change_type, DiffLineType::Context);

        // Check line numbers
        assert_eq!(hunk.lines[0].old_line, Some(1));
        assert_eq!(hunk.lines[0].new_line, Some(1));
        assert_eq!(hunk.lines[1].old_line, Some(2));
        assert_eq!(hunk.lines[1].new_line, None);
        assert_eq!(hunk.lines[2].old_line, None);
        assert_eq!(hunk.lines[2].new_line, Some(2));
    }

    #[test]
    fn test_diff_hunks() {
        let temp = init_test_repo();
        let repo = GitRepo::open(temp.path()).unwrap();

        // Modify file and commit
        std::fs::write(temp.path().join("test.txt"), "hello\nworld\n").unwrap();
        Command::new("git")
            .current_dir(temp.path())
            .args(["add", "."])
            .output()
            .unwrap();
        Command::new("git")
            .current_dir(temp.path())
            .args(["commit", "-m", "Add world"])
            .output()
            .unwrap();

        // Get hunks between commits
        let hunks = repo.diff_hunks("HEAD~1", "HEAD", Some("test.txt")).unwrap();
        assert!(!hunks.is_empty());

        // Verify we got structured data
        let hunk = &hunks[0];
        assert!(hunk.old_start > 0);
        assert!(!hunk.header.is_empty());
    }

    #[test]
    fn test_uncommitted_hunks() {
        let temp = init_test_repo();
        let repo = GitRepo::open(temp.path()).unwrap();

        // Make uncommitted change
        std::fs::write(temp.path().join("test.txt"), "modified content").unwrap();

        let hunks = repo.uncommitted_hunks(Some("test.txt")).unwrap();
        assert!(!hunks.is_empty());

        // Should have some changes
        let total_changes: usize = hunks.iter().map(|h| h.lines.len()).sum();
        assert!(total_changes > 0);
    }
}