semantic-diff 0.9.0

A terminal diff viewer with AI-powered semantic grouping (Claude CLI / Copilot)
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
//! Stress tests for CLI git-diff drop-in replacement.
//!
//! Tests the actual binary against real git repos with various `git diff` argument
//! patterns: HEAD, --staged, --cached, 2-dot, 3-dot, path limiters, invalid refs,
//! non-git directories, and combinations.

use std::process::{Command, Stdio};

/// Path to the compiled binary.
fn bin() -> String {
    env!("CARGO_BIN_EXE_semantic-diff").to_string()
}

/// Helper: create a temp git repo with an initial commit and a staged modification.
/// Returns the temp dir (must be kept alive for the repo to exist).
fn setup_repo() -> tempfile::TempDir {
    let tmp = tempfile::tempdir().expect("create tempdir");
    let repo = tmp.path();

    run_git(repo, &["init"]);
    run_git(repo, &["config", "user.email", "test@test.com"]);
    run_git(repo, &["config", "user.name", "Test"]);

    std::fs::write(repo.join("file_a.txt"), "line 1\nline 2\nline 3\n").unwrap();
    std::fs::write(repo.join("file_b.txt"), "alpha\nbeta\ngamma\n").unwrap();
    std::fs::create_dir_all(repo.join("src")).unwrap();
    std::fs::write(repo.join("src/main.rs"), "fn main() {}\n").unwrap();

    run_git(repo, &["add", "."]);
    run_git(repo, &["commit", "-m", "initial commit"]);

    // Modify files to create a diff
    std::fs::write(repo.join("file_a.txt"), "line 1\nline 2 modified\nline 3\n").unwrap();
    std::fs::write(
        repo.join("src/main.rs"),
        "fn main() {\n    println!(\"hello\");\n}\n",
    )
    .unwrap();

    tmp
}

/// Helper: create a repo with two branches for range-based tests.
fn setup_repo_with_branches() -> tempfile::TempDir {
    let tmp = tempfile::tempdir().expect("create tempdir");
    let repo = tmp.path();

    run_git(repo, &["init"]);
    run_git(repo, &["config", "user.email", "test@test.com"]);
    run_git(repo, &["config", "user.name", "Test"]);

    std::fs::write(repo.join("shared.txt"), "base content\n").unwrap();
    run_git(repo, &["add", "."]);
    run_git(repo, &["commit", "-m", "base commit"]);

    // Create feature branch
    run_git(repo, &["checkout", "-b", "feature"]);
    std::fs::write(repo.join("feature.txt"), "feature work\n").unwrap();
    std::fs::write(repo.join("shared.txt"), "base content\nfeature addition\n").unwrap();
    run_git(repo, &["add", "."]);
    run_git(repo, &["commit", "-m", "feature commit"]);

    // Go back to main and make a diverging change
    run_git(repo, &["checkout", "master"]).or_else(|| run_git_opt(repo, &["checkout", "main"]));

    std::fs::write(repo.join("main_only.txt"), "main work\n").unwrap();
    run_git(repo, &["add", "."]);
    run_git(repo, &["commit", "-m", "main diverge"]);

    tmp
}

/// Run a git command, panicking on failure.
fn run_git(dir: &std::path::Path, args: &[&str]) -> Option<()> {
    let output = Command::new("git")
        .args(args)
        .current_dir(dir)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .expect("git command failed to execute");
    if output.status.success() {
        Some(())
    } else {
        None
    }
}

/// Run a git command, returning None instead of panicking on failure.
fn run_git_opt(dir: &std::path::Path, args: &[&str]) -> Option<()> {
    run_git(dir, args)
}

/// Run semantic-diff with given args in a given directory, returning (exit_code, stdout, stderr).
fn run_semantic_diff(dir: &std::path::Path, args: &[&str]) -> (i32, String, String) {
    let output = Command::new(bin())
        .args(args)
        .current_dir(dir)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .expect("semantic-diff binary failed to execute");

    let code = output.status.code().unwrap_or(-1);
    let stdout = String::from_utf8_lossy(&output.stdout).to_string();
    let stderr = String::from_utf8_lossy(&output.stderr).to_string();
    (code, stdout, stderr)
}

// ============================================================
// E2E: Basic argument patterns
// ============================================================

/// No args = unstaged changes (drop-in for `git diff`).
#[test]
fn e2e_no_args_shows_unstaged_changes() {
    let tmp = setup_repo();
    let (_code, _stdout, stderr) = run_semantic_diff(tmp.path(), &[]);

    // Should detect changes (file_a.txt and src/main.rs are modified but unstaged)
    // The binary will try to init a TUI and fail (no TTY), but it should NOT say "No changes detected"
    assert!(
        !stderr.contains("No changes detected"),
        "Should detect unstaged changes, stderr: {stderr}"
    );
    // Accept non-zero exit (no TTY) — the important thing is it found changes
}

/// HEAD arg = all changes vs HEAD.
#[test]
fn e2e_head_arg() {
    let tmp = setup_repo();
    let (_code, _stdout, stderr) = run_semantic_diff(tmp.path(), &["HEAD"]);

    assert!(
        !stderr.contains("No changes detected"),
        "HEAD should show changes, stderr: {stderr}"
    );
}

/// --staged with staged changes.
#[test]
fn e2e_staged_flag() {
    let tmp = setup_repo();
    let repo = tmp.path();

    // Stage one file
    run_git(repo, &["add", "file_a.txt"]);

    let (_code, _stdout, stderr) = run_semantic_diff(repo, &["--staged"]);
    assert!(
        !stderr.contains("No changes detected"),
        "--staged should show staged changes, stderr: {stderr}"
    );
}

/// --cached is an alias for --staged.
#[test]
fn e2e_cached_flag() {
    let tmp = setup_repo();
    let repo = tmp.path();

    run_git(repo, &["add", "file_a.txt"]);

    let (_code, _stdout, stderr) = run_semantic_diff(repo, &["--cached"]);
    assert!(
        !stderr.contains("No changes detected"),
        "--cached should show staged changes, stderr: {stderr}"
    );
}

/// --staged with no staged changes should show "No changes detected".
#[test]
fn e2e_staged_no_changes() {
    let tmp = setup_repo();
    // Don't stage anything
    let (code, _stdout, stderr) = run_semantic_diff(tmp.path(), &["--staged"]);
    assert!(
        stderr.contains("No changes detected"),
        "--staged with nothing staged should show no changes, stderr: {stderr}"
    );
    assert_eq!(code, 0, "Should exit cleanly");
}

// ============================================================
// E2E: Range-based comparisons
// ============================================================

/// Two-dot range between branches.
#[test]
fn e2e_two_dot_range() {
    let tmp = setup_repo_with_branches();
    let repo = tmp.path();

    // Determine default branch name (could be "main" or "master")
    let default_branch = detect_default_branch(repo);

    let range = format!("{default_branch}..feature");
    let (_code, _stdout, stderr) = run_semantic_diff(repo, &[&range]);

    assert!(
        !stderr.contains("No changes detected"),
        "Two-dot range should show changes, stderr: {stderr}"
    );
}

/// Three-dot range (merge-base).
#[test]
fn e2e_three_dot_range() {
    let tmp = setup_repo_with_branches();
    let repo = tmp.path();

    let default_branch = detect_default_branch(repo);

    let range = format!("{default_branch}...feature");
    let (_code, _stdout, stderr) = run_semantic_diff(repo, &[&range]);

    assert!(
        !stderr.contains("No changes detected"),
        "Three-dot range should show changes, stderr: {stderr}"
    );
}

/// Two explicit refs.
#[test]
fn e2e_two_refs() {
    let tmp = setup_repo_with_branches();
    let repo = tmp.path();

    let default_branch = detect_default_branch(repo);

    let (_code, _stdout, stderr) = run_semantic_diff(repo, &[&default_branch, "feature"]);

    assert!(
        !stderr.contains("No changes detected"),
        "Two refs should show changes, stderr: {stderr}"
    );
}

// ============================================================
// E2E: Path limiters
// ============================================================

/// Path limiter narrows diff to specific files.
#[test]
fn e2e_path_limiter_matching_file() {
    let tmp = setup_repo();
    let (_code, _stdout, stderr) = run_semantic_diff(tmp.path(), &["HEAD", "--", "file_a.txt"]);

    assert!(
        !stderr.contains("No changes detected"),
        "Path limiter with matching file should show changes, stderr: {stderr}"
    );
}

/// Path limiter with non-matching path shows no changes.
#[test]
fn e2e_path_limiter_no_match() {
    let tmp = setup_repo();
    let (code, _stdout, stderr) =
        run_semantic_diff(tmp.path(), &["HEAD", "--", "nonexistent/"]);

    assert!(
        stderr.contains("No changes detected"),
        "Path limiter with no match should show no changes, stderr: {stderr}"
    );
    assert_eq!(code, 0);
}

/// Path limiter with directory.
#[test]
fn e2e_path_limiter_directory() {
    let tmp = setup_repo();
    let (_code, _stdout, stderr) = run_semantic_diff(tmp.path(), &["HEAD", "--", "src/"]);

    assert!(
        !stderr.contains("No changes detected"),
        "Path limiter with src/ should show changes, stderr: {stderr}"
    );
}

/// Multiple path limiters.
#[test]
fn e2e_multiple_path_limiters() {
    let tmp = setup_repo();
    let (_code, _stdout, stderr) = run_semantic_diff(
        tmp.path(),
        &["HEAD", "--", "file_a.txt", "src/main.rs"],
    );

    assert!(
        !stderr.contains("No changes detected"),
        "Multiple path limiters should show changes, stderr: {stderr}"
    );
}

// ============================================================
// E2E: Error handling
// ============================================================

/// Invalid ref should not panic.
#[test]
fn e2e_invalid_ref_no_panic() {
    let tmp = setup_repo();
    let (_code, _stdout, stderr) = run_semantic_diff(tmp.path(), &["nonexistent_branch_xyz"]);

    // git diff will produce empty output or error, tool should handle gracefully
    assert_no_unexpected_panic(&stderr, "invalid ref");
}

/// Non-git directory should not panic.
#[test]
fn e2e_non_git_directory() {
    let tmp = tempfile::tempdir().unwrap();
    // Do NOT init git
    let (_code, _stdout, stderr) = run_semantic_diff(tmp.path(), &[]);

    assert_no_unexpected_panic(&stderr, "non-git directory");
    // Should fail gracefully (git diff itself will fail)
}

/// --version flag should work.
#[test]
fn e2e_version_flag() {
    let tmp = setup_repo();
    let (code, _stdout, stderr) = run_semantic_diff(tmp.path(), &["--version"]);

    assert_eq!(code, 0, "Should exit 0 for --version");
    assert_no_unexpected_panic(&stderr, "--version");
}

/// --help flag should work.
#[test]
fn e2e_help_flag() {
    let tmp = setup_repo();
    let (code, _stdout, stderr) = run_semantic_diff(tmp.path(), &["--help"]);

    assert_eq!(code, 0, "Should exit 0 for --help");
    assert_no_unexpected_panic(&stderr, "--help");
}

// ============================================================
// E2E: HEAD~N and other rev specifiers
// ============================================================

/// HEAD~1 should work.
#[test]
fn e2e_head_tilde() {
    let tmp = setup_repo();
    let repo = tmp.path();

    // Make another commit so HEAD~1 is valid
    std::fs::write(repo.join("file_b.txt"), "alpha\nbeta modified\ngamma\n").unwrap();
    run_git(repo, &["add", "."]);
    run_git(repo, &["commit", "-m", "second commit"]);

    // Now modify again
    std::fs::write(repo.join("file_b.txt"), "alpha\nbeta modified again\ngamma\n").unwrap();

    let (_code, _stdout, stderr) = run_semantic_diff(repo, &["HEAD~1"]);
    assert!(
        !stderr.contains("No changes detected"),
        "HEAD~1 should show changes, stderr: {stderr}"
    );
}

/// Range HEAD~2..HEAD should work.
#[test]
fn e2e_head_tilde_range() {
    let tmp = setup_repo();
    let repo = tmp.path();

    // Make a second commit
    run_git(repo, &["add", "."]);
    run_git(repo, &["commit", "-m", "second commit"]);

    // Make a third commit with changes
    std::fs::write(repo.join("file_b.txt"), "modified\n").unwrap();
    run_git(repo, &["add", "."]);
    run_git(repo, &["commit", "-m", "third commit"]);

    let (_code, _stdout, stderr) = run_semantic_diff(repo, &["HEAD~2..HEAD"]);
    assert!(
        !stderr.contains("No changes detected"),
        "HEAD~2..HEAD should show changes, stderr: {stderr}"
    );
}

// ============================================================
// E2E: Commit-to-commit (no working tree)
// ============================================================

/// Two committed refs with no working tree involvement.
#[test]
fn e2e_commit_to_commit() {
    let tmp = setup_repo();
    let repo = tmp.path();

    // Capture first commit hash
    let first_hash = git_output(repo, &["rev-parse", "HEAD"]);

    // Make a second commit
    run_git(repo, &["add", "."]);
    run_git(repo, &["commit", "-m", "second commit"]);

    let second_hash = git_output(repo, &["rev-parse", "HEAD"]);

    let (_code, _stdout, stderr) =
        run_semantic_diff(repo, &[&first_hash, &second_hash]);
    assert!(
        !stderr.contains("No changes detected"),
        "Commit-to-commit should show changes, stderr: {stderr}"
    );
}

// ============================================================
// E2E: Stress — empty repo states
// ============================================================

/// Fresh repo with no commits, no args.
#[test]
fn e2e_no_commits_no_args() {
    let tmp = tempfile::tempdir().unwrap();
    run_git(tmp.path(), &["init"]);

    let (_code, _stdout, stderr) = run_semantic_diff(tmp.path(), &[]);

    assert_no_unexpected_panic(&stderr, "no commits, no args");
}

/// Fresh repo with no commits, HEAD arg (HEAD doesn't exist yet).
#[test]
fn e2e_no_commits_head_arg() {
    let tmp = tempfile::tempdir().unwrap();
    run_git(tmp.path(), &["init"]);

    let (_code, _stdout, stderr) = run_semantic_diff(tmp.path(), &["HEAD"]);

    assert_no_unexpected_panic(&stderr, "no commits, HEAD arg");
}

// ============================================================
// E2E: Stress — large diff with args
// ============================================================

/// Large number of files with HEAD arg.
#[test]
fn e2e_many_files_with_head() {
    let tmp = setup_repo();
    let repo = tmp.path();

    // Create 200 files
    for i in 0..200 {
        std::fs::write(
            repo.join(format!("gen_{i}.txt")),
            format!("content {i}\n"),
        )
        .unwrap();
    }
    run_git(repo, &["add", "."]);
    run_git(repo, &["commit", "-m", "add many files"]);

    // Modify all of them
    for i in 0..200 {
        std::fs::write(
            repo.join(format!("gen_{i}.txt")),
            format!("modified content {i}\n"),
        )
        .unwrap();
    }

    let (_code, _stdout, stderr) = run_semantic_diff(repo, &["HEAD"]);
    assert!(
        !stderr.contains("No changes detected"),
        "200 modified files should show changes, stderr: {stderr}"
    );
    assert_no_unexpected_panic(&stderr, "many files with HEAD");
}

// ============================================================
// E2E: Staged + ref combinations
// ============================================================

/// --staged with a specific ref.
#[test]
fn e2e_staged_with_ref() {
    let tmp = setup_repo();
    let repo = tmp.path();

    // Commit the current changes
    run_git(repo, &["add", "."]);
    run_git(repo, &["commit", "-m", "second commit"]);

    // Stage a new change
    std::fs::write(repo.join("file_a.txt"), "totally new content\n").unwrap();
    run_git(repo, &["add", "file_a.txt"]);

    let (_code, _stdout, stderr) = run_semantic_diff(repo, &["--staged", "HEAD~1"]);
    assert!(
        !stderr.contains("No changes detected"),
        "--staged HEAD~1 should show changes, stderr: {stderr}"
    );
}

// ============================================================
// Helpers
// ============================================================

/// Check stderr for unexpected panics. TUI-init panics ("failed to initialize terminal")
/// are expected in tests (no TTY). Only flag panics unrelated to terminal setup.
fn assert_no_unexpected_panic(stderr: &str, context: &str) {
    if stderr.contains("panicked") {
        // TUI-init panic is expected — ratatui::init() fails without TTY
        let is_tui_panic = stderr.contains("failed to initialize terminal")
            || stderr.contains("reader source not set");
        assert!(
            is_tui_panic,
            "{context}: unexpected panic (not TUI-related), stderr: {stderr}"
        );
    }
}

/// Detect the default branch name (main or master).
fn detect_default_branch(repo: &std::path::Path) -> String {
    let output = Command::new("git")
        .args(["branch", "--list", "main"])
        .current_dir(repo)
        .output()
        .unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    if stdout.contains("main") {
        "main".to_string()
    } else {
        "master".to_string()
    }
}

/// Get trimmed stdout from a git command.
fn git_output(repo: &std::path::Path, args: &[&str]) -> String {
    let output = Command::new("git")
        .args(args)
        .current_dir(repo)
        .output()
        .unwrap();
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}