tokmd-git 1.10.0

Streaming git log adapter for tokmd analysis.
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
//! Integration tests for tokmd-git crate.
//!
//! These tests create a temporary git repository to test the git functions,
//! ensuring they work even when the test is run from a non-git directory
//! (e.g., during mutation testing).

use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use std::process::Command;
use tokmd_git::{GitRangeMode, collect_history, get_added_lines, git_available, repo_root};

/// Create a `Command` for git that ignores inherited `GIT_DIR`/`GIT_WORK_TREE`
/// and runs in the given directory.
fn git_in(dir: &Path) -> Command {
    let mut cmd = Command::new("git");
    cmd.env_remove("GIT_DIR")
        .env_remove("GIT_WORK_TREE")
        .current_dir(dir);
    cmd
}

/// Run `git rev-parse HEAD` in the given directory and return the SHA.
fn get_head_sha(dir: &Path) -> String {
    let output = git_in(dir)
        .args(["rev-parse", "HEAD"])
        .output()
        .expect("git rev-parse HEAD should succeed");
    assert!(output.status.success(), "git rev-parse HEAD failed");
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

/// Create an empty initialized git repository with one seed commit.
fn init_test_repo(suffix: &str) -> Option<TempGitRepo> {
    if !git_available() {
        return None;
    }

    let unique_id = format!(
        "{}-{:?}-{}-{}",
        std::process::id(),
        std::thread::current().id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0),
        suffix
    );
    let temp_dir = std::env::temp_dir().join(format!("tokmd-git-test-{}", unique_id));

    if temp_dir.exists() {
        std::fs::remove_dir_all(&temp_dir).ok();
    }

    std::fs::create_dir_all(&temp_dir).ok()?;

    let status = git_in(&temp_dir).args(["init"]).output().ok()?;
    if !status.status.success() {
        std::fs::remove_dir_all(&temp_dir).ok();
        return None;
    }

    git_in(&temp_dir)
        .args(["config", "user.email", "test@example.com"])
        .output()
        .ok()?;
    git_in(&temp_dir)
        .args(["config", "user.name", "Test User"])
        .output()
        .ok()?;

    // Seed commit so HEAD exists
    std::fs::write(temp_dir.join("README.md"), "initial").ok()?;
    git_in(&temp_dir).args(["add", "README.md"]).output().ok()?;
    let commit = git_in(&temp_dir)
        .args(["commit", "-m", "Initial commit"])
        .output()
        .ok()?;
    if !commit.status.success() {
        std::fs::remove_dir_all(&temp_dir).ok();
        return None;
    }

    Some(TempGitRepo { path: temp_dir })
}

/// Helper to create a temporary git repository with some commits.
fn create_test_repo() -> Option<TempGitRepo> {
    if !git_available() {
        return None;
    }

    // Use thread ID and a random number to avoid conflicts between concurrent tests
    let unique_id = format!(
        "{}-{:?}-{}",
        std::process::id(),
        std::thread::current().id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0)
    );
    let temp_dir = std::env::temp_dir().join(format!("tokmd-git-test-{}", unique_id));

    // Clean up any existing directory first
    if temp_dir.exists() {
        std::fs::remove_dir_all(&temp_dir).ok();
    }

    std::fs::create_dir_all(&temp_dir).ok()?;

    // Initialize git repo
    let status = git_in(&temp_dir).args(["init"]).output().ok()?;
    if !status.status.success() {
        std::fs::remove_dir_all(&temp_dir).ok();
        return None;
    }

    // Configure git user for commits
    git_in(&temp_dir)
        .args(["config", "user.email", "test@example.com"])
        .output()
        .ok()?;
    git_in(&temp_dir)
        .args(["config", "user.name", "Test User"])
        .output()
        .ok()?;

    // Create first commit with a file
    let file1 = temp_dir.join("file1.txt");
    std::fs::write(&file1, "content1").ok()?;
    git_in(&temp_dir).args(["add", "file1.txt"]).output().ok()?;
    let commit1 = git_in(&temp_dir)
        .args(["commit", "-m", "First commit"])
        .output()
        .ok()?;
    if !commit1.status.success() {
        std::fs::remove_dir_all(&temp_dir).ok();
        return None;
    }

    // Create second commit with another file
    let file2 = temp_dir.join("file2.txt");
    std::fs::write(&file2, "content2").ok()?;
    git_in(&temp_dir).args(["add", "file2.txt"]).output().ok()?;
    git_in(&temp_dir)
        .args(["commit", "-m", "Second commit"])
        .output()
        .ok()?;

    // Create third commit modifying existing file
    std::fs::write(&file1, "modified content").ok()?;
    git_in(&temp_dir).args(["add", "file1.txt"]).output().ok()?;
    git_in(&temp_dir)
        .args(["commit", "-m", "Third commit"])
        .output()
        .ok()?;

    Some(TempGitRepo { path: temp_dir })
}

/// RAII wrapper for cleanup of temp git repo.
struct TempGitRepo {
    path: PathBuf,
}

impl Drop for TempGitRepo {
    fn drop(&mut self) {
        std::fs::remove_dir_all(&self.path).ok();
    }
}

/// Test that git_available returns true when git is installed.
/// This test assumes git is available in the test environment.
#[test]
fn test_git_available_returns_true() {
    // On CI and dev machines, git should be available
    assert!(
        git_available(),
        "git should be available in the test environment"
    );
}

/// Test that repo_root returns a valid path for a git repository.
#[test]
fn test_repo_root_returns_path_for_valid_repo() {
    let repo = create_test_repo().expect("Should create test repo");

    let root = repo_root(&repo.path);
    assert!(
        root.is_some(),
        "repo_root should return Some for a valid git repository"
    );

    let root_path = root.unwrap();
    assert!(root_path.exists(), "repo root should exist on disk");
    assert!(
        root_path.join(".git").exists(),
        "repo root should contain .git directory"
    );
}

/// Test that repo_root returns None for a non-repository path.
#[test]
fn test_repo_root_returns_none_for_non_repo() {
    // Create a directory that is definitely not a git repo
    let non_repo_path =
        std::env::temp_dir().join(format!("tokmd-test-not-a-repo-{}", std::process::id()));
    std::fs::create_dir_all(&non_repo_path).ok();

    // Ensure no .git directory exists
    let git_dir = non_repo_path.join(".git");
    if git_dir.exists() {
        std::fs::remove_dir_all(&git_dir).ok();
    }

    let root = repo_root(&non_repo_path);

    // If the temp directory itself is inside a git repo, this might return Some
    // So we check that if it returns Some, it's not the non_repo_path itself
    if let Some(found_root) = root {
        // The found root should not be our test directory
        assert_ne!(
            found_root, non_repo_path,
            "repo_root should not return the test directory as a git root"
        );
    }

    // Clean up
    std::fs::remove_dir_all(&non_repo_path).ok();
}

/// Test that collect_history returns commits for a git repository.
#[test]
fn test_collect_history_returns_commits() {
    let repo = create_test_repo().expect("Should create test repo");
    let root = repo_root(&repo.path).expect("Should find repo root");

    // Use None for max_commits to read all output
    let commits = collect_history(&root, None, None).expect("Should collect history successfully");

    assert!(!commits.is_empty(), "repo should have commits");
    assert_eq!(commits.len(), 3, "Should have 3 commits");

    // Verify commit structure - all commits should have valid timestamps
    for commit in &commits {
        assert!(commit.timestamp > 0, "Commit should have valid timestamp");
        assert!(
            !commit.author.is_empty(),
            "Commit should have non-empty author"
        );
    }
}

/// Test that repo_root result contains the actual path, not just empty.
#[test]
fn test_repo_root_path_is_not_empty() {
    let repo = create_test_repo().expect("Should create test repo");
    let root = repo_root(&repo.path).expect("Should find repo root");

    // The path should not be empty
    assert!(
        !root.as_os_str().is_empty(),
        "repo root path should not be empty"
    );

    // It should be a real directory
    assert!(root.is_dir(), "repo root should be a directory");
}

/// Test commit has files.
#[test]
fn test_commits_have_files() {
    let repo = create_test_repo().expect("Should create test repo");
    let root = repo_root(&repo.path).expect("Should find repo root");

    // Get all commits
    let commits = collect_history(&root, None, None).expect("Should collect history");

    // All commits in our test repo should have files
    for commit in &commits {
        assert!(
            !commit.files.is_empty(),
            "Each commit should have associated files"
        );
    }
}

/// Test that history collection fails gracefully for non-existent path.
#[test]
fn test_collect_history_fails_for_invalid_path() {
    let invalid_path = Path::new("/this/path/does/not/exist/anywhere/tokmd-test");

    let result = collect_history(invalid_path, None, None);

    // Should fail because the path doesn't exist
    assert!(
        result.is_err(),
        "collect_history should fail for invalid path"
    );
}

/// Test that repo_root returns the correct path for a subdirectory.
#[test]
fn test_repo_root_from_subdirectory() {
    let repo = create_test_repo().expect("Should create test repo");

    // Create a subdirectory
    let subdir = repo.path.join("subdir");
    std::fs::create_dir_all(&subdir).ok();

    let root = repo_root(&subdir);
    assert!(
        root.is_some(),
        "repo_root should return Some for subdirectory of git repo"
    );

    let root_path = root.unwrap();
    // Normalize paths for comparison
    let expected = repo.path.canonicalize().ok();
    let actual = root_path.canonicalize().ok();

    assert_eq!(
        expected, actual,
        "repo_root should return the repo root, not the subdirectory"
    );
}

/// Test that max_commits limit is respected exactly.
/// This test verifies that when asking for 2 commits from a repo with 3,
/// we get exactly 2 (not more, not less).
///
/// Note: When we break early from reading git output, the git process may
/// exit with non-zero status due to broken pipe. We handle this by catching
/// the error and checking if we got the expected commits anyway.
#[test]
fn test_max_commits_exact_limit() {
    let repo = create_test_repo().expect("Should create test repo");
    let root = repo_root(&repo.path).expect("Should find repo root");

    // Our test repo has exactly 3 commits. If we ask for 3 (or more),
    // git will output all commits and exit normally.
    // If we ask for 2, git may fail due to broken pipe, but that's OK.

    // Test with limit equal to total commits (should succeed reliably)
    let commits = collect_history(&root, Some(3), None).expect("Should collect history");
    assert_eq!(
        commits.len(),
        3,
        "Should get exactly 3 commits when max_commits=3"
    );

    // Test with limit greater than total commits (should succeed reliably)
    let commits = collect_history(&root, Some(10), None).expect("Should collect history");
    assert_eq!(
        commits.len(),
        3,
        "Should get all 3 commits when max_commits exceeds total"
    );

    // Test with lower limit (may fail due to broken pipe, but verifies the limit works)
    // We use Ok().is_ok_and() to handle potential broken pipe errors gracefully
    let result = collect_history(&root, Some(2), None);
    if let Ok(commits) = result {
        assert!(
            commits.len() <= 2,
            "Should get at most 2 commits when max_commits=2, got {}",
            commits.len()
        );
    }
    // If it fails, that's acceptable - the early break causes broken pipe
}

/// Helper to create a repo with commits having multiple files.
fn create_test_repo_with_multi_file_commits() -> Option<TempGitRepo> {
    if !git_available() {
        return None;
    }

    let unique_id = format!(
        "{}-{:?}-{}-multifile",
        std::process::id(),
        std::thread::current().id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0)
    );
    let temp_dir = std::env::temp_dir().join(format!("tokmd-git-test-{}", unique_id));

    if temp_dir.exists() {
        std::fs::remove_dir_all(&temp_dir).ok();
    }

    std::fs::create_dir_all(&temp_dir).ok()?;

    // Initialize git repo
    let status = git_in(&temp_dir).args(["init"]).output().ok()?;
    if !status.status.success() {
        std::fs::remove_dir_all(&temp_dir).ok();
        return None;
    }

    // Configure git user for commits
    git_in(&temp_dir)
        .args(["config", "user.email", "test@example.com"])
        .output()
        .ok()?;
    git_in(&temp_dir)
        .args(["config", "user.name", "Test User"])
        .output()
        .ok()?;

    // Create a commit with 5 files
    for i in 1..=5 {
        let file = temp_dir.join(format!("file{}.txt", i));
        std::fs::write(&file, format!("content{}", i)).ok()?;
    }
    git_in(&temp_dir).args(["add", "."]).output().ok()?;
    let commit_result = git_in(&temp_dir)
        .args(["commit", "-m", "Commit with 5 files"])
        .output()
        .ok()?;
    if !commit_result.status.success() {
        std::fs::remove_dir_all(&temp_dir).ok();
        return None;
    }

    Some(TempGitRepo { path: temp_dir })
}

/// Test that max_commit_files limit is respected exactly.
/// If we ask for max 2 files per commit, a commit with 5 files should only show 2.
#[test]
fn test_max_commit_files_exact_limit() {
    let repo = create_test_repo_with_multi_file_commits().expect("Should create test repo");
    let root = repo_root(&repo.path).expect("Should find repo root");

    // The commit has 5 files. If we limit to 2, we should get exactly 2 files.
    let commits =
        collect_history(&root, None, Some(2)).expect("Should collect history with file limit");

    assert_eq!(commits.len(), 1, "Should have exactly 1 commit");

    let commit = &commits[0];
    assert_eq!(
        commit.files.len(),
        2,
        "Commit should have exactly 2 files when max_commit_files=2, got: {:?}",
        commit.files
    );
}

/// Test that max_commit_files limit of 1 gives exactly 1 file.
#[test]
fn test_max_commit_files_limit_one() {
    let repo = create_test_repo_with_multi_file_commits().expect("Should create test repo");
    let root = repo_root(&repo.path).expect("Should find repo root");

    // The commit has 5 files. If we limit to 1, we should get exactly 1 file.
    let commits =
        collect_history(&root, None, Some(1)).expect("Should collect history with file limit");

    assert_eq!(commits.len(), 1, "Should have exactly 1 commit");

    let commit = &commits[0];
    assert_eq!(
        commit.files.len(),
        1,
        "Commit should have exactly 1 file when max_commit_files=1, got: {:?}",
        commit.files
    );
}

/// Test that max_commit_files=0 gives 0 files.
#[test]
fn test_max_commit_files_limit_zero() {
    let repo = create_test_repo_with_multi_file_commits().expect("Should create test repo");
    let root = repo_root(&repo.path).expect("Should find repo root");

    // The commit has 5 files. If we limit to 0, we should get 0 files.
    let commits =
        collect_history(&root, None, Some(0)).expect("Should collect history with file limit");

    assert_eq!(commits.len(), 1, "Should have exactly 1 commit");

    let commit = &commits[0];
    assert_eq!(
        commit.files.len(),
        0,
        "Commit should have 0 files when max_commit_files=0, got: {:?}",
        commit.files
    );
}

/// Test that without file limit, all files are returned.
#[test]
fn test_no_max_commit_files_returns_all() {
    let repo = create_test_repo_with_multi_file_commits().expect("Should create test repo");
    let root = repo_root(&repo.path).expect("Should find repo root");

    // Without file limit, we should get all 5 files
    let commits = collect_history(&root, None, None).expect("Should collect history");

    assert_eq!(commits.len(), 1, "Should have exactly 1 commit");

    let commit = &commits[0];
    assert_eq!(
        commit.files.len(),
        5,
        "Commit should have all 5 files when no limit, got: {:?}",
        commit.files
    );
}

// ============================================================================
// get_added_lines tests
// ============================================================================

/// Modifying a file between two commits produces correct added line numbers.
#[test]
fn test_get_added_lines_single_file() {
    let repo = init_test_repo("added-single").expect("Should create test repo");
    let base_sha = get_head_sha(&repo.path);

    // Replace README.md content so the diff shows 3 added lines
    std::fs::write(repo.path.join("README.md"), "line1\nline2\nline3\n").unwrap();
    git_in(&repo.path)
        .args(["add", "README.md"])
        .output()
        .unwrap();
    git_in(&repo.path)
        .args(["commit", "-m", "modify readme"])
        .output()
        .unwrap();
    let head_sha = get_head_sha(&repo.path);

    let result = get_added_lines(&repo.path, &base_sha, &head_sha, GitRangeMode::TwoDot)
        .expect("get_added_lines should succeed");

    assert!(
        result.contains_key(&PathBuf::from("README.md")),
        "Should contain README.md, got keys: {:?}",
        result.keys().collect::<Vec<_>>()
    );
    let lines = &result[&PathBuf::from("README.md")];
    let expected: BTreeSet<usize> = [1, 2, 3].into_iter().collect();
    assert_eq!(*lines, expected, "Should report lines 1-3 as added");
}

/// Changes across multiple files and a brand-new file are all captured.
#[test]
fn test_get_added_lines_multiple_files() {
    let repo = init_test_repo("added-multi").expect("Should create test repo");
    let base_sha = get_head_sha(&repo.path);

    // Modify existing file
    std::fs::write(repo.path.join("README.md"), "updated\n").unwrap();
    // Add a brand-new file
    std::fs::write(repo.path.join("new.txt"), "alpha\nbeta\n").unwrap();
    git_in(&repo.path)
        .args(["add", "README.md", "new.txt"])
        .output()
        .unwrap();
    git_in(&repo.path)
        .args(["commit", "-m", "multi-file change"])
        .output()
        .unwrap();
    let head_sha = get_head_sha(&repo.path);

    let result = get_added_lines(&repo.path, &base_sha, &head_sha, GitRangeMode::TwoDot)
        .expect("get_added_lines should succeed");

    assert!(
        result.contains_key(&PathBuf::from("README.md")),
        "Should contain README.md"
    );
    assert!(
        result.contains_key(&PathBuf::from("new.txt")),
        "Should contain new.txt"
    );
    let new_lines = &result[&PathBuf::from("new.txt")];
    let expected: BTreeSet<usize> = [1, 2].into_iter().collect();
    assert_eq!(*new_lines, expected, "new.txt should have lines 1-2 added");
}

/// Same SHA for base and head returns empty map.
#[test]
fn test_get_added_lines_no_changes() {
    let repo = init_test_repo("added-noop").expect("Should create test repo");
    let sha = get_head_sha(&repo.path);

    let result = get_added_lines(&repo.path, &sha, &sha, GitRangeMode::TwoDot)
        .expect("get_added_lines should succeed");

    assert!(
        result.is_empty(),
        "Same base and head should yield empty map, got: {:?}",
        result
    );
}

/// Brand-new file reports all lines as added.
#[test]
fn test_get_added_lines_new_file_all_lines() {
    let repo = init_test_repo("added-newfile").expect("Should create test repo");
    let base_sha = get_head_sha(&repo.path);

    std::fs::write(repo.path.join("brand_new.txt"), "alpha\nbeta\ngamma\n").unwrap();
    git_in(&repo.path)
        .args(["add", "brand_new.txt"])
        .output()
        .unwrap();
    git_in(&repo.path)
        .args(["commit", "-m", "add brand new file"])
        .output()
        .unwrap();
    let head_sha = get_head_sha(&repo.path);

    let result = get_added_lines(&repo.path, &base_sha, &head_sha, GitRangeMode::TwoDot)
        .expect("get_added_lines should succeed");

    let key = PathBuf::from("brand_new.txt");
    assert!(result.contains_key(&key), "Should contain brand_new.txt");
    let expected: BTreeSet<usize> = [1, 2, 3].into_iter().collect();
    assert_eq!(result[&key], expected, "All 3 lines should be added");
}

/// Deleted file does not appear in result.
#[test]
fn test_get_added_lines_deleted_file_excluded() {
    let repo = init_test_repo("added-deleted").expect("Should create test repo");
    let base_sha = get_head_sha(&repo.path);

    git_in(&repo.path)
        .args(["rm", "README.md"])
        .output()
        .unwrap();
    git_in(&repo.path)
        .args(["commit", "-m", "delete readme"])
        .output()
        .unwrap();
    let head_sha = get_head_sha(&repo.path);

    let result = get_added_lines(&repo.path, &base_sha, &head_sha, GitRangeMode::TwoDot)
        .expect("get_added_lines should succeed");

    assert!(
        !result.contains_key(&PathBuf::from("README.md")),
        "Deleted file should not appear in added lines"
    );
    assert!(result.is_empty(), "Only deletion should yield empty map");
}

/// Nonexistent ref returns Err.
#[test]
fn test_get_added_lines_invalid_ref_errors() {
    let repo = init_test_repo("added-badref").expect("Should create test repo");

    let result = get_added_lines(
        &repo.path,
        "deadbeef0000000000000000000000000000dead",
        "HEAD",
        GitRangeMode::TwoDot,
    );

    assert!(
        result.is_err(),
        "Nonexistent base ref should produce an error"
    );
}

/// Nested paths (e.g. `src/lib.rs`) are correctly keyed.
#[test]
fn test_get_added_lines_subdirectory_paths() {
    let repo = init_test_repo("added-subdir").expect("Should create test repo");
    let base_sha = get_head_sha(&repo.path);

    let src_dir = repo.path.join("src");
    std::fs::create_dir_all(&src_dir).unwrap();
    std::fs::write(src_dir.join("lib.rs"), "fn main() {}\n").unwrap();
    git_in(&repo.path)
        .args(["add", "src/lib.rs"])
        .output()
        .unwrap();
    git_in(&repo.path)
        .args(["commit", "-m", "add nested file"])
        .output()
        .unwrap();
    let head_sha = get_head_sha(&repo.path);

    let result = get_added_lines(&repo.path, &base_sha, &head_sha, GitRangeMode::TwoDot)
        .expect("get_added_lines should succeed");

    let key = PathBuf::from("src/lib.rs");
    assert!(
        result.contains_key(&key),
        "Should contain src/lib.rs, got keys: {:?}",
        result.keys().collect::<Vec<_>>()
    );
    let expected: BTreeSet<usize> = [1].into_iter().collect();
    assert_eq!(result[&key], expected, "Should report line 1 as added");
}