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
//! Deep tests for tokmd-git: git log parsing, hotspot detection, coupling,
//! freshness, churn, intent classification, and edge cases.

use tokmd_git::{GitCommit, GitRangeMode, classify_intent};
use tokmd_types::CommitIntentKind;

// ===========================================================================
// 1. GitCommit struct construction
// ===========================================================================

#[test]
fn git_commit_with_all_fields() {
    let c = GitCommit {
        timestamp: 1_700_000_000,
        author: "dev@example.com".to_string(),
        hash: Some("abc123def456".to_string()),
        subject: "feat: add parser".to_string(),
        files: vec!["src/parser.rs".to_string()],
    };
    assert_eq!(c.timestamp, 1_700_000_000);
    assert_eq!(c.author, "dev@example.com");
    assert_eq!(c.hash.as_deref(), Some("abc123def456"));
    assert_eq!(c.files.len(), 1);
}

#[test]
fn git_commit_with_no_hash() {
    let c = GitCommit {
        timestamp: 0,
        author: String::new(),
        hash: None,
        subject: String::new(),
        files: vec![],
    };
    assert!(c.hash.is_none());
    assert!(c.files.is_empty());
}

// ===========================================================================
// 2. GitRangeMode formatting
// ===========================================================================

#[test]
fn range_mode_two_dot_format_with_tags() {
    assert_eq!(
        GitRangeMode::TwoDot.format("v1.0.0", "v2.0.0"),
        "v1.0.0..v2.0.0"
    );
}

#[test]
fn range_mode_three_dot_format_with_branches() {
    assert_eq!(
        GitRangeMode::ThreeDot.format("origin/main", "feature/abc"),
        "origin/main...feature/abc"
    );
}

#[test]
fn range_mode_default_is_two_dot() {
    assert_eq!(GitRangeMode::default(), GitRangeMode::TwoDot);
}

#[test]
fn range_mode_equality() {
    assert_eq!(GitRangeMode::TwoDot, GitRangeMode::TwoDot);
    assert_eq!(GitRangeMode::ThreeDot, GitRangeMode::ThreeDot);
    assert_ne!(GitRangeMode::TwoDot, GitRangeMode::ThreeDot);
}

#[test]
fn range_mode_format_with_empty_strings() {
    assert_eq!(GitRangeMode::TwoDot.format("", ""), "..");
    assert_eq!(GitRangeMode::ThreeDot.format("", ""), "...");
}

// ===========================================================================
// 3. classify_intent – conventional commits
// ===========================================================================

#[test]
fn classify_intent_conventional_feat() {
    assert_eq!(
        classify_intent("feat: add new parser"),
        CommitIntentKind::Feat
    );
}

#[test]
fn classify_intent_conventional_feat_with_scope() {
    assert_eq!(
        classify_intent("feat(cli): add verbose flag"),
        CommitIntentKind::Feat
    );
}

#[test]
fn classify_intent_conventional_fix() {
    assert_eq!(
        classify_intent("fix: correct null pointer"),
        CommitIntentKind::Fix
    );
}

#[test]
fn classify_intent_conventional_breaking_change() {
    assert_eq!(
        classify_intent("feat!: remove deprecated API"),
        CommitIntentKind::Feat
    );
}

#[test]
fn classify_intent_conventional_refactor() {
    assert_eq!(
        classify_intent("refactor: extract helper"),
        CommitIntentKind::Refactor
    );
}

#[test]
fn classify_intent_conventional_docs() {
    assert_eq!(
        classify_intent("docs: update README"),
        CommitIntentKind::Docs
    );
}

#[test]
fn classify_intent_conventional_test() {
    assert_eq!(
        classify_intent("test: add unit tests"),
        CommitIntentKind::Test
    );
}

#[test]
fn classify_intent_conventional_chore() {
    assert_eq!(
        classify_intent("chore: bump dependencies"),
        CommitIntentKind::Chore
    );
}

#[test]
fn classify_intent_conventional_ci() {
    assert_eq!(
        classify_intent("ci: add GitHub Actions workflow"),
        CommitIntentKind::Ci
    );
}

#[test]
fn classify_intent_conventional_build() {
    assert_eq!(
        classify_intent("build: update Cargo.toml"),
        CommitIntentKind::Build
    );
}

#[test]
fn classify_intent_conventional_perf() {
    assert_eq!(
        classify_intent("perf: optimize hot path"),
        CommitIntentKind::Perf
    );
}

#[test]
fn classify_intent_conventional_style() {
    assert_eq!(
        classify_intent("style: apply rustfmt"),
        CommitIntentKind::Style
    );
}

#[test]
fn classify_intent_revert_prefix() {
    assert_eq!(
        classify_intent("Revert \"feat: add parser\""),
        CommitIntentKind::Revert
    );
}

#[test]
fn classify_intent_revert_conventional() {
    assert_eq!(
        classify_intent("revert: undo breaking change"),
        CommitIntentKind::Revert
    );
}

// ===========================================================================
// 4. classify_intent – keyword heuristic fallback
// ===========================================================================

#[test]
fn classify_intent_keyword_fix() {
    assert_eq!(
        classify_intent("Fix crash on empty input"),
        CommitIntentKind::Fix
    );
}

#[test]
fn classify_intent_keyword_add() {
    assert_eq!(
        classify_intent("Add support for YAML"),
        CommitIntentKind::Feat
    );
}

#[test]
fn classify_intent_keyword_implement() {
    assert_eq!(
        classify_intent("Implement caching layer"),
        CommitIntentKind::Feat
    );
}

#[test]
fn classify_intent_keyword_refactor() {
    assert_eq!(
        classify_intent("Refactor config loading"),
        CommitIntentKind::Refactor
    );
}

#[test]
fn classify_intent_keyword_doc() {
    assert_eq!(
        classify_intent("Update doc comments"),
        CommitIntentKind::Docs
    );
}

#[test]
fn classify_intent_keyword_readme() {
    assert_eq!(
        classify_intent("Update readme with examples"),
        CommitIntentKind::Docs
    );
}

#[test]
fn classify_intent_keyword_test() {
    assert_eq!(
        classify_intent("Extend test coverage"),
        CommitIntentKind::Test
    );
}

#[test]
fn classify_intent_keyword_optimize() {
    assert_eq!(
        classify_intent("Optimize memory usage"),
        CommitIntentKind::Perf
    );
}

#[test]
fn classify_intent_keyword_lint() {
    assert_eq!(
        classify_intent("Apply lint suggestions"),
        CommitIntentKind::Style
    );
}

#[test]
fn classify_intent_keyword_pipeline() {
    assert_eq!(
        classify_intent("Update pipeline config"),
        CommitIntentKind::Ci
    );
}

#[test]
fn classify_intent_keyword_deps() {
    assert_eq!(
        classify_intent("Bump deps to latest"),
        CommitIntentKind::Build
    );
}

#[test]
fn classify_intent_keyword_cleanup() {
    assert_eq!(
        classify_intent("Cleanup unused imports"),
        CommitIntentKind::Chore
    );
}

// ===========================================================================
// 5. classify_intent – edge cases
// ===========================================================================

#[test]
fn classify_intent_empty_string() {
    assert_eq!(classify_intent(""), CommitIntentKind::Other);
}

#[test]
fn classify_intent_whitespace_only() {
    assert_eq!(classify_intent("   "), CommitIntentKind::Other);
}

#[test]
fn classify_intent_unknown_subject() {
    assert_eq!(classify_intent("Initial commit"), CommitIntentKind::Other);
}

#[test]
fn classify_intent_case_insensitive_conventional() {
    // Conventional prefix comparison is case-insensitive
    assert_eq!(
        classify_intent("FEAT: loud feature"),
        CommitIntentKind::Feat
    );
    assert_eq!(classify_intent("Fix: quiet fix"), CommitIntentKind::Fix);
}

#[test]
fn classify_intent_word_boundary_prevents_false_match() {
    // "prefix" contains "fix" but not as a word
    assert_ne!(classify_intent("prefix some change"), CommitIntentKind::Fix);
    // "testing" contains "test" but at word boundary "test" + "ing"
    // Actually "test" IS at a word boundary in "testing" since
    // the check looks for non-alphanumeric after the word.
    // Let's verify with something that truly embeds the word:
    assert_ne!(
        classify_intent("detest the linter"),
        CommitIntentKind::Test,
        "detest embeds 'test' but not at word boundary"
    );
}

// ===========================================================================
// 6. collect_history + rev_exists with real git repos
// ===========================================================================

fn git_cmd() -> std::process::Command {
    let mut cmd = std::process::Command::new("git");
    cmd.env_remove("GIT_DIR").env_remove("GIT_WORK_TREE");
    cmd
}

fn test_git(dir: &std::path::Path) -> std::process::Command {
    let mut cmd = git_cmd();
    cmd.arg("-C").arg(dir);
    cmd
}

fn init_repo(dir: &std::path::Path) {
    test_git(dir).arg("init").output().unwrap();
    test_git(dir)
        .args(["config", "user.email", "test@test.com"])
        .output()
        .unwrap();
    test_git(dir)
        .args(["config", "user.name", "Test User"])
        .output()
        .unwrap();
}

fn add_commit(dir: &std::path::Path, file: &str, content: &str, msg: &str) {
    let path = dir.join(file);
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).unwrap();
    }
    std::fs::write(&path, content).unwrap();
    test_git(dir).args(["add", "."]).output().unwrap();
    test_git(dir).args(["commit", "-m", msg]).output().unwrap();
}

#[test]
fn collect_history_empty_repo() {
    if !tokmd_git::git_available() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    // Empty repo with no commits — git log returns nothing
    let result = tokmd_git::collect_history(dir.path(), None, None);
    // On some git versions this succeeds with 0 commits, on others it fails
    if let Ok(commits) = result {
        assert!(commits.is_empty());
    }
}

#[test]
fn collect_history_single_commit() {
    if !tokmd_git::git_available() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    add_commit(dir.path(), "main.rs", "fn main() {}", "feat: init");

    let commits = tokmd_git::collect_history(dir.path(), None, None).unwrap();
    assert_eq!(commits.len(), 1);
    assert_eq!(commits[0].author, "test@test.com");
    assert_eq!(commits[0].subject, "feat: init");
    assert!(commits[0].files.contains(&"main.rs".to_string()));
    assert!(commits[0].hash.is_some());
    assert!(commits[0].timestamp > 0);
}

#[test]
fn collect_history_multiple_commits_order() {
    if !tokmd_git::git_available() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    add_commit(dir.path(), "a.rs", "1", "first");
    add_commit(dir.path(), "b.rs", "2", "second");
    add_commit(dir.path(), "c.rs", "3", "third");

    let commits = tokmd_git::collect_history(dir.path(), None, None).unwrap();
    assert_eq!(commits.len(), 3);
    // git log outputs newest first
    assert_eq!(commits[0].subject, "third");
    assert_eq!(commits[1].subject, "second");
    assert_eq!(commits[2].subject, "first");
}

#[test]
fn collect_history_max_commits_limit() {
    if !tokmd_git::git_available() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    for i in 0..5 {
        add_commit(
            dir.path(),
            &format!("f{i}.rs"),
            &format!("{i}"),
            &format!("commit {i}"),
        );
    }

    let result = tokmd_git::collect_history(dir.path(), Some(2), None);
    if let Ok(commits) = result {
        assert!(commits.len() <= 2, "max_commits should limit to 2");
    }
}

#[test]
fn collect_history_max_commit_files_limit() {
    if !tokmd_git::git_available() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    // Create a commit with many files
    for i in 0..10 {
        let path = dir.path().join(format!("file{i}.rs"));
        std::fs::write(&path, format!("content {i}")).unwrap();
    }
    test_git(dir.path()).args(["add", "."]).output().unwrap();
    test_git(dir.path())
        .args(["commit", "-m", "many files"])
        .output()
        .unwrap();

    let commits = tokmd_git::collect_history(dir.path(), None, Some(3)).unwrap();
    assert_eq!(commits.len(), 1);
    assert!(
        commits[0].files.len() <= 3,
        "max_commit_files should limit files per commit"
    );
}

#[test]
fn collect_history_subdirectory_files() {
    if !tokmd_git::git_available() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    add_commit(
        dir.path(),
        "src/lib.rs",
        "pub fn hello() {}",
        "feat: add lib",
    );

    let commits = tokmd_git::collect_history(dir.path(), None, None).unwrap();
    assert_eq!(commits.len(), 1);
    assert!(commits[0].files.contains(&"src/lib.rs".to_string()));
}

// ===========================================================================
// 7. rev_exists
// ===========================================================================

#[test]
fn rev_exists_on_nonexistent_dir() {
    let dir = std::path::PathBuf::from("/nonexistent/path/xyz123");
    assert!(!tokmd_git::rev_exists(&dir, "HEAD"));
}

#[test]
fn rev_exists_non_repo_directory() {
    let dir = tempfile::tempdir().unwrap();
    assert!(!tokmd_git::rev_exists(dir.path(), "HEAD"));
}

#[test]
fn rev_exists_head_after_commit() {
    if !tokmd_git::git_available() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    add_commit(dir.path(), "f.txt", "x", "init");
    assert!(tokmd_git::rev_exists(dir.path(), "HEAD"));
}

#[test]
fn rev_exists_bogus_ref() {
    if !tokmd_git::git_available() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    add_commit(dir.path(), "f.txt", "x", "init");
    assert!(!tokmd_git::rev_exists(dir.path(), "nonexistent-branch-xyz"));
}

// ===========================================================================
// 8. repo_root
// ===========================================================================

#[test]
fn repo_root_returns_some_for_git_repo() {
    if !tokmd_git::git_available() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    add_commit(dir.path(), "f.txt", "x", "init");
    let root = tokmd_git::repo_root(dir.path());
    assert!(root.is_some(), "repo_root should find git repo");
}

#[test]
fn repo_root_returns_none_for_non_repo() {
    let dir = tempfile::tempdir().unwrap();
    assert!(tokmd_git::repo_root(dir.path()).is_none());
}

// ===========================================================================
// 9. git_available
// ===========================================================================

#[test]
fn git_available_returns_bool() {
    // Just verify it doesn't panic and returns a bool
    let _available: bool = tokmd_git::git_available();
}

// ===========================================================================
// 10. resolve_base_ref
// ===========================================================================

#[test]
fn resolve_base_ref_returns_none_for_explicit_nonexistent() {
    if !tokmd_git::git_available() {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    init_repo(dir.path());
    add_commit(dir.path(), "f.txt", "x", "init");
    // Explicit ref (not "main") that doesn't exist → None, no fallback
    assert_eq!(tokmd_git::resolve_base_ref(dir.path(), "v99.99.99"), None);
}