infigraph-core 1.0.0

AST-powered code analysis framework — parser, graph, diff, and analysis engine
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
//! Semantic diff between two git refs at the symbol level.
//!
//! Instead of a line diff, this compares the extracted symbol graphs of two
//! git tree-states and classifies each change as Added / Removed / Modified /
//! SignatureChanged.  The caller supplies a project root and two git refs
//! (e.g. "HEAD~1", "main"); the module checks out each ref into a temp
//! worktree, indexes it with the current language registry, and returns a
//! structured `SymbolDiff`.

use std::collections::{HashMap, HashSet};
use std::path::Path;

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

use crate::extract;
use crate::lang::LanguageRegistry;

/// How a symbol changed between two refs.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ChangeKind {
    /// Symbol exists in new ref but not in old ref.
    Added,
    /// Symbol exists in old ref but not in new ref.
    Removed,
    /// Symbol exists in both; signature_hash changed (parameter / return type change).
    SignatureChanged,
    /// Symbol exists in both; body changed but signature is the same.
    Modified,
    /// Symbol moved to a different file.
    Moved { from_file: String },
}

impl std::fmt::Display for ChangeKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ChangeKind::Added => write!(f, "ADDED"),
            ChangeKind::Removed => write!(f, "REMOVED"),
            ChangeKind::SignatureChanged => write!(f, "SIGNATURE_CHANGED"),
            ChangeKind::Modified => write!(f, "MODIFIED"),
            ChangeKind::Moved { from_file } => write!(f, "MOVED(from:{})", from_file),
        }
    }
}

/// A single symbol-level change.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SymbolChange {
    pub name: String,
    pub kind: String,
    pub file: String,
    pub change: ChangeKind,
    /// Callers in the current graph (populated by caller when graph is available).
    pub caller_count: usize,
}

/// Full semantic diff result.
#[derive(Debug, Default)]
pub struct SymbolDiff {
    pub old_ref: String,
    pub new_ref: String,
    pub changes: Vec<SymbolChange>,
}

impl SymbolDiff {
    pub fn added(&self) -> impl Iterator<Item = &SymbolChange> {
        self.changes
            .iter()
            .filter(|c| c.change == ChangeKind::Added)
    }
    pub fn removed(&self) -> impl Iterator<Item = &SymbolChange> {
        self.changes
            .iter()
            .filter(|c| c.change == ChangeKind::Removed)
    }
    pub fn modified(&self) -> impl Iterator<Item = &SymbolChange> {
        self.changes.iter().filter(|c| {
            matches!(
                c.change,
                ChangeKind::Modified | ChangeKind::SignatureChanged | ChangeKind::Moved { .. }
            )
        })
    }
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// A flat symbol record used during diff (file + name + kind + sig_hash).
#[derive(Clone)]
struct FlatSym {
    file: String,
    name: String,
    kind: String,
    sig_hash: String,
}

/// Compute a symbol-level diff between `old_ref` and `new_ref` in `project_root`.
///
/// Uses `git archive` to extract each ref into a temp directory so no
/// working-tree modifications are needed.
pub fn semantic_diff(
    project_root: &Path,
    old_ref: &str,
    new_ref: &str,
    registry: &LanguageRegistry,
) -> Result<SymbolDiff> {
    let changed = compute_changed_files(project_root, old_ref, new_ref);

    let (old_filter, new_filter) = match &changed {
        Some(cf) => (Some(&cf.old_ref_files), Some(&cf.new_ref_files)),
        None => (None, None),
    };

    let old_symbols = extract_ref_symbols(project_root, old_ref, registry, old_filter)
        .with_context(|| format!("failed to extract symbols for ref '{}'", old_ref))?;
    let new_symbols = extract_ref_symbols(project_root, new_ref, registry, new_filter)
        .with_context(|| format!("failed to extract symbols for ref '{}'", new_ref))?;

    Ok(diff_symbol_maps(old_ref, new_ref, old_symbols, new_symbols))
}

struct ChangedFiles {
    old_ref_files: HashSet<String>,
    new_ref_files: HashSet<String>,
}

fn compute_changed_files(
    project_root: &Path,
    old_ref: &str,
    new_ref: &str,
) -> Option<ChangedFiles> {
    let output = std::process::Command::new("git")
        .args(["diff", "--name-status", "--no-renames", old_ref, new_ref])
        .current_dir(project_root)
        .output()
        .ok()?;

    if !output.status.success() {
        eprintln!(
            "infigraph: git diff --name-status failed for {}..{}, falling back to full extraction",
            old_ref, new_ref
        );
        return None;
    }

    let text = String::from_utf8_lossy(&output.stdout);
    let mut old_ref_files = HashSet::new();
    let mut new_ref_files = HashSet::new();

    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        let mut parts = line.splitn(2, '\t');
        let status = parts.next().unwrap_or("").trim();
        let path = match parts.next() {
            Some(p) => p.trim().to_string(),
            None => continue,
        };

        match status {
            "A" => {
                new_ref_files.insert(path);
            }
            "D" => {
                old_ref_files.insert(path);
            }
            _ => {
                old_ref_files.insert(path.clone());
                new_ref_files.insert(path);
            }
        }
    }

    Some(ChangedFiles {
        old_ref_files,
        new_ref_files,
    })
}

// ---------------------------------------------------------------------------
// Extract symbols for a git ref
// ---------------------------------------------------------------------------

/// Extract all symbols from a git ref by using `git archive | tar -x` into a
/// temp directory, then walking files through the language registry.
const MAX_ARCHIVE_ARGS: usize = 500;

fn extract_ref_symbols(
    project_root: &Path,
    git_ref: &str,
    registry: &LanguageRegistry,
    file_filter: Option<&HashSet<String>>,
) -> Result<HashMap<String, FlatSym>> {
    if let Some(filter) = file_filter {
        if filter.is_empty() {
            return Ok(HashMap::new());
        }
    }

    let is_working_tree = git_ref == "HEAD" || git_ref == "WORKING";

    if is_working_tree {
        return extract_dir_symbols(project_root, project_root, registry, file_filter);
    }

    let tmp = tempfile::tempdir().context("failed to create temp dir")?;

    let use_filtered_archive = file_filter
        .map(|f| f.len() <= MAX_ARCHIVE_ARGS)
        .unwrap_or(false);

    let archive_output = if use_filtered_archive {
        let filter = file_filter.unwrap();
        let mut args: Vec<&str> = vec!["archive", "--format=tar", git_ref, "--"];
        args.extend(filter.iter().map(|s| s.as_str()));
        std::process::Command::new("git")
            .args(&args)
            .current_dir(project_root)
            .output()
            .context("git archive (filtered) failed")?
    } else {
        std::process::Command::new("git")
            .args(["archive", "--format=tar", git_ref])
            .current_dir(project_root)
            .output()
            .context("git archive failed")?
    };

    if !archive_output.status.success() {
        let err = String::from_utf8_lossy(&archive_output.stderr);
        if use_filtered_archive {
            eprintln!(
                "infigraph: filtered git archive for {} failed, falling back to full archive: {}",
                git_ref,
                err.trim()
            );
            let full_output = std::process::Command::new("git")
                .args(["archive", "--format=tar", git_ref])
                .current_dir(project_root)
                .output()
                .context("git archive (full fallback) failed")?;
            if !full_output.status.success() {
                let err2 = String::from_utf8_lossy(&full_output.stderr);
                anyhow::bail!("git archive {} failed: {}", git_ref, err2.trim());
            }
            return untar_and_extract(tmp.path(), &full_output.stdout, registry, file_filter);
        }
        anyhow::bail!("git archive {} failed: {}", git_ref, err.trim());
    }

    untar_and_extract(tmp.path(), &archive_output.stdout, registry, file_filter)
}

fn untar_and_extract(
    tmp_dir: &Path,
    tar_data: &[u8],
    registry: &LanguageRegistry,
    file_filter: Option<&HashSet<String>>,
) -> Result<HashMap<String, FlatSym>> {
    let mut tar = std::process::Command::new("tar")
        .args(["-x", "-C", tmp_dir.to_str().unwrap_or(".")])
        .stdin(std::process::Stdio::piped())
        .spawn()
        .context("failed to spawn tar")?;

    if let Some(stdin) = tar.stdin.take() {
        use std::io::Write;
        let mut w = stdin;
        w.write_all(tar_data)?;
    }
    tar.wait().context("tar wait failed")?;

    extract_dir_symbols(tmp_dir, tmp_dir, registry, file_filter)
}

fn extract_dir_symbols(
    root: &Path,
    dir: &Path,
    registry: &LanguageRegistry,
    file_filter: Option<&HashSet<String>>,
) -> Result<HashMap<String, FlatSym>> {
    let mut map = HashMap::new();
    collect_symbols(root, dir, registry, file_filter, &mut map)?;
    Ok(map)
}

static SKIP_DIRS: &[&str] = &[
    ".git",
    "node_modules",
    ".venv",
    "venv",
    "target",
    "build",
    "dist",
    "__pycache__",
    ".tox",
    ".infigraph",
];

fn collect_symbols(
    root: &Path,
    dir: &Path,
    registry: &LanguageRegistry,
    file_filter: Option<&HashSet<String>>,
    map: &mut HashMap<String, FlatSym>,
) -> Result<()> {
    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();
        let name = entry.file_name();
        let name_str = name.to_string_lossy();

        if path.is_dir() {
            if !SKIP_DIRS.contains(&name_str.as_ref()) && !name_str.starts_with('.') {
                collect_symbols(root, &path, registry, file_filter, map)?;
            }
        } else if path.is_file() {
            let rel = path
                .strip_prefix(root)
                .unwrap_or(&path)
                .to_string_lossy()
                .replace('\\', "/");
            if let Some(filter) = file_filter {
                if !filter.contains(&rel) {
                    continue;
                }
            }
            let Ok(source) = std::fs::read(&path) else {
                continue;
            };
            let Some(pack) = registry.for_file_with_content(&rel, &source) else {
                continue;
            };
            let Ok(extraction) = extract::extract_file(&rel, &source, pack) else {
                continue;
            };
            let file = extraction.file.clone();
            for sym in &extraction.symbols {
                let kind_str = sym.kind.as_str().to_string();
                // Key: "file::name::kind" — stable across refs
                let key = format!("{}::{}::{}", file, sym.name, kind_str);
                map.insert(
                    key,
                    FlatSym {
                        file: file.clone(),
                        name: sym.name.clone(),
                        kind: kind_str,
                        sig_hash: sym.signature_hash.clone(),
                    },
                );
            }
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Diff two symbol maps
// ---------------------------------------------------------------------------

fn diff_symbol_maps(
    old_ref: &str,
    new_ref: &str,
    old: HashMap<String, FlatSym>,
    new: HashMap<String, FlatSym>,
) -> SymbolDiff {
    let mut changes = Vec::new();

    // Build name→sym map for old (for move detection)
    let old_by_name: HashMap<String, &FlatSym> = old
        .values()
        .map(|s| (format!("{}::{}", s.name, s.kind), s))
        .collect();

    // Check new symbols against old
    for (key, new_sym) in &new {
        if let Some(old_sym) = old.get(key) {
            // Same file+name+kind — check signature change
            if old_sym.sig_hash != new_sym.sig_hash
                && !old_sym.sig_hash.is_empty()
                && !new_sym.sig_hash.is_empty()
            {
                changes.push(SymbolChange {
                    name: new_sym.name.clone(),
                    kind: new_sym.kind.clone(),
                    file: new_sym.file.clone(),
                    change: ChangeKind::SignatureChanged,
                    caller_count: 0,
                });
            }
        } else {
            // Not in old by key. Check if name+kind existed in a different file (move).
            let name_key = format!("{}::{}", new_sym.name, new_sym.kind);
            if let Some(old_sym) = old_by_name.get(&name_key) {
                if old_sym.file != new_sym.file {
                    changes.push(SymbolChange {
                        name: new_sym.name.clone(),
                        kind: new_sym.kind.clone(),
                        file: new_sym.file.clone(),
                        change: ChangeKind::Moved {
                            from_file: old_sym.file.clone(),
                        },
                        caller_count: 0,
                    });
                    continue;
                }
            }
            // Truly new
            changes.push(SymbolChange {
                name: new_sym.name.clone(),
                kind: new_sym.kind.clone(),
                file: new_sym.file.clone(),
                change: ChangeKind::Added,
                caller_count: 0,
            });
        }
    }

    // Removed: in old but not in new (excluding moves already captured)
    let moved_names: std::collections::HashSet<String> = changes
        .iter()
        .filter_map(|c| {
            if matches!(c.change, ChangeKind::Moved { .. }) {
                Some(format!("{}::{}", c.name, c.kind))
            } else {
                None
            }
        })
        .collect();

    for (key, old_sym) in &old {
        if !new.contains_key(key) {
            let name_key = format!("{}::{}", old_sym.name, old_sym.kind);
            if !moved_names.contains(&name_key) {
                changes.push(SymbolChange {
                    name: old_sym.name.clone(),
                    kind: old_sym.kind.clone(),
                    file: old_sym.file.clone(),
                    change: ChangeKind::Removed,
                    caller_count: 0,
                });
            }
        }
    }

    // Sort: Removed first, then Added, then modified kinds
    changes.sort_by_key(|c| match &c.change {
        ChangeKind::Removed => 0,
        ChangeKind::SignatureChanged => 1,
        ChangeKind::Modified => 2,
        ChangeKind::Moved { .. } => 3,
        ChangeKind::Added => 4,
    });

    SymbolDiff {
        old_ref: old_ref.to_string(),
        new_ref: new_ref.to_string(),
        changes,
    }
}

// ---------------------------------------------------------------------------
// Formatting
// ---------------------------------------------------------------------------

pub fn format_diff(diff: &SymbolDiff) -> String {
    if diff.changes.is_empty() {
        return format!(
            "No symbol-level changes between '{}' and '{}'.",
            diff.old_ref, diff.new_ref
        );
    }

    let added = diff.added().count();
    let removed = diff.removed().count();
    let modified = diff.modified().count();

    let mut out = format!(
        "Semantic diff {}{}  [+{} added  -{} removed  ~{} modified]\n\n",
        diff.old_ref, diff.new_ref, added, removed, modified
    );

    let mut cur_file = String::new();
    for c in &diff.changes {
        if c.file != cur_file {
            out.push_str(&format!("  {}\n", c.file));
            cur_file = c.file.clone();
        }
        let callers = if c.caller_count > 0 {
            format!("  [{} callers]", c.caller_count)
        } else {
            String::new()
        };
        out.push_str(&format!(
            "    {:>20}  {:<10} {}{}\n",
            c.change.to_string(),
            c.kind,
            c.name,
            callers
        ));
    }

    out
}