leindex 1.9.0

LeIndex MCP and semantic code search engine for AI tools and large codebases
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
//! Live Git status parsing without line-oriented or whitespace-sensitive paths.
#![allow(missing_docs)]

use std::path::{Path, PathBuf};
use std::process::Command;

/// Error returned when Git cannot provide a source inventory.
#[derive(Debug)]
pub enum GitInventoryError {
    /// The path is not inside a Git worktree.
    NotRepository,
    /// Git returned a non-zero status.
    Failed(String),
    /// The subprocess could not be started.
    Io(std::io::Error),
}

impl std::fmt::Display for GitInventoryError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotRepository => f.write_str("not a git repository"),
            Self::Failed(message) => f.write_str(message),
            Self::Io(error) => error.fmt(f),
        }
    }
}

impl std::error::Error for GitInventoryError {}

/// Return whether `root` is inside a Git worktree without scanning it.
pub fn is_worktree(root: &Path) -> bool {
    Command::new("git")
        .args(["rev-parse", "--is-inside-work-tree"])
        .current_dir(root)
        .output()
        .map(|output| output.status.success() && output.stdout.starts_with(b"true"))
        .unwrap_or(false)
}

/// Return the committed tree identity used to validate a generation snapshot.
pub fn tree_oid(root: &Path) -> Result<Option<String>, GitInventoryError> {
    let output = Command::new("git")
        .args(["rev-parse", "HEAD^{tree}"])
        .current_dir(root)
        .output()
        .map_err(GitInventoryError::Io)?;
    if !output.status.success() {
        if String::from_utf8_lossy(&output.stderr).contains("not a git repository") {
            return Ok(None);
        }
        return Err(GitInventoryError::Failed(
            String::from_utf8_lossy(&output.stderr).trim().to_owned(),
        ));
    }
    Ok(Some(
        String::from_utf8_lossy(&output.stdout).trim().to_owned(),
    ))
}

/// Enumerate tracked and non-ignored untracked files at a Git boundary.
///
/// Git remains the source of truth for ignore rules and gitlinks. The returned
/// paths are absolute, sorted, and never include descendants of nested
/// repositories or submodules.
pub fn source_inventory(root: &Path) -> Result<Vec<PathBuf>, GitInventoryError> {
    let root = root.canonicalize().map_err(GitInventoryError::Io)?;
    if !git_output(&root, &["rev-parse", "--show-toplevel"])?
        .status
        .success()
    {
        return Err(GitInventoryError::NotRepository);
    }

    let list_output = successful_git_output(
        &root,
        &[
            "ls-files",
            "-z",
            "--cached",
            "--others",
            "--exclude-standard",
        ],
    )?;
    let stage_output = successful_git_output(&root, &["ls-files", "-z", "--stage"])?;
    let gitlinks = gitlink_paths(&root, &stage_output.stdout);
    let mut paths = inventory_paths(&root, &list_output.stdout, &gitlinks);
    paths.sort();
    paths.dedup();
    Ok(paths)
}

fn git_output(root: &Path, arguments: &[&str]) -> Result<std::process::Output, GitInventoryError> {
    Command::new("git")
        .args(arguments)
        .current_dir(root)
        .output()
        .map_err(GitInventoryError::Io)
}

fn successful_git_output(
    root: &Path,
    arguments: &[&str],
) -> Result<std::process::Output, GitInventoryError> {
    let output = git_output(root, arguments)?;
    if output.status.success() {
        return Ok(output);
    }
    Err(GitInventoryError::Failed(
        String::from_utf8_lossy(&output.stderr).trim().to_owned(),
    ))
}

fn gitlink_paths(root: &Path, stage_output: &[u8]) -> Vec<PathBuf> {
    stage_output
        .split(|byte| *byte == b'\0')
        .filter_map(|record| {
            let tab = record.iter().position(|byte| *byte == b'\t')?;
            let (header, raw_path) = record.split_at(tab);
            let raw_path = raw_path.get(1..)?;
            (header.split(|byte| *byte == b' ').next() == Some(b"160000".as_slice()))
                .then(|| root.join(path_bytes(raw_path)))
        })
        .collect()
}

fn inventory_paths(root: &Path, list_output: &[u8], gitlinks: &[PathBuf]) -> Vec<PathBuf> {
    list_output
        .split(|byte| *byte == b'\0')
        .filter(|raw| !raw.is_empty())
        .map(|raw| root.join(path_bytes(raw)))
        .filter_map(|candidate| accepted_source_path(candidate, root, gitlinks))
        .collect()
}

fn accepted_source_path(candidate: PathBuf, root: &Path, gitlinks: &[PathBuf]) -> Option<PathBuf> {
    // Canonicalize first to resolve symlinks; reject if canonical target is outside root.
    let canonical = candidate.canonicalize().ok()?;
    if !canonical.starts_with(root)
        || gitlinks
            .iter()
            .any(|gitlink| candidate.starts_with(gitlink))
        || is_skipped_source_path(&canonical, root)
        || has_nested_git_boundary(&canonical, root)
        || !canonical.is_file()
    {
        return None;
    }

    // Return original candidate (before canonicalization) to preserve caller expectations.
    Some(candidate)
}

/// Return worktree files whose contents contain a fixed string.
///
/// Git performs the content prefilter against tracked files without forcing
/// callers to enumerate and read the entire worktree. Non-ignored untracked
/// files are included as candidates so live symbol fallback still sees edits
/// that have not been staged. The caller remains responsible for parsing and
/// applying language/scope filters.
pub fn source_candidates(root: &Path, needle: &str) -> Result<Vec<PathBuf>, GitInventoryError> {
    let root = root.canonicalize().map_err(GitInventoryError::Io)?;
    let top_output = Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .current_dir(&root)
        .output()
        .map_err(GitInventoryError::Io)?;
    if !top_output.status.success() {
        return Err(GitInventoryError::NotRepository);
    }
    let tracked = Command::new("git")
        .args(["grep", "--no-color", "-I", "-l", "-F", "-z", "--", needle])
        .current_dir(&root)
        .output()
        .map_err(GitInventoryError::Io)?;
    // git grep exits 1 for a valid search with no matches. Any other failure
    // is an actual Git error and should not silently broaden the scan.
    if !tracked.status.success() && tracked.status.code() != Some(1) {
        return Err(GitInventoryError::Failed(
            String::from_utf8_lossy(&tracked.stderr).trim().to_owned(),
        ));
    }

    let untracked = Command::new("git")
        .args(["ls-files", "-z", "--others", "--exclude-standard"])
        .current_dir(&root)
        .output()
        .map_err(GitInventoryError::Io)?;
    if !untracked.status.success() {
        return Err(GitInventoryError::Failed(
            String::from_utf8_lossy(&untracked.stderr).trim().to_owned(),
        ));
    }

    let mut paths = Vec::new();
    for raw in tracked
        .stdout
        .split(|byte| *byte == b'\0')
        .chain(untracked.stdout.split(|byte| *byte == b'\0'))
    {
        if raw.is_empty() {
            continue;
        }
        if let Some(candidate) = accepted_source_path(root.join(path_bytes(raw)), &root, &[]) {
            paths.push(candidate);
        }
    }
    paths.sort();
    paths.dedup();
    Ok(paths)
}

/// Return live source candidates changed relative to `HEAD`.
///
/// This is the bounded fallback for a catalog miss: an indexed generation
/// already covers the unchanged tree, so only modified/staged/untracked paths
/// need live parsing. Callers that cannot prove the catalog was built from the
/// current tree must use [`source_candidates`] instead.
pub fn changed_source_candidates(root: &Path) -> Result<Vec<PathBuf>, GitInventoryError> {
    let root = root.canonicalize().map_err(GitInventoryError::Io)?;
    let top_output = Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .current_dir(&root)
        .output()
        .map_err(GitInventoryError::Io)?;
    if !top_output.status.success() {
        return Err(GitInventoryError::NotRepository);
    }
    let changed = Command::new("git")
        .args(["diff", "--name-only", "-z", "HEAD", "--"])
        .current_dir(&root)
        .output()
        .map_err(GitInventoryError::Io)?;
    if !changed.status.success() {
        return Err(GitInventoryError::Failed(
            String::from_utf8_lossy(&changed.stderr).trim().to_owned(),
        ));
    }
    let untracked = Command::new("git")
        .args(["ls-files", "-z", "--others", "--exclude-standard"])
        .current_dir(&root)
        .output()
        .map_err(GitInventoryError::Io)?;
    if !untracked.status.success() {
        return Err(GitInventoryError::Failed(
            String::from_utf8_lossy(&untracked.stderr).trim().to_owned(),
        ));
    }

    let mut paths = Vec::new();
    for raw in changed
        .stdout
        .split(|byte| *byte == b'\0')
        .chain(untracked.stdout.split(|byte| *byte == b'\0'))
    {
        if raw.is_empty() {
            continue;
        }
        if let Some(candidate) = accepted_source_path(root.join(path_bytes(raw)), &root, &[]) {
            paths.push(candidate);
        }
    }
    paths.sort();
    paths.dedup();
    Ok(paths)
}

fn has_nested_git_boundary(path: &Path, root: &Path) -> bool {
    let mut current = path.parent();
    while let Some(directory) = current {
        if directory == root {
            break;
        }
        // A project may itself be a subdirectory of a larger worktree. Only
        // repositories below the requested root are boundaries; an ancestor
        // `.git` belongs to the containing workspace and must not hide every
        // source file in the project.
        if !directory.starts_with(root) {
            break;
        }
        if directory.join(".git").exists() {
            return true;
        }
        current = directory.parent();
    }
    false
}

fn is_skipped_source_path(path: &Path, root: &Path) -> bool {
    path.strip_prefix(root).ok().is_some_and(|relative| {
        relative.components().any(|component| {
            component
                .as_os_str()
                .to_str()
                .is_some_and(|name| crate::cli::skip_dirs::SKIP_DIRS.contains(&name))
        })
    })
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct GitStatus {
    pub modified: Vec<PathBuf>,
    pub staged: Vec<PathBuf>,
    pub untracked: Vec<PathBuf>,
    pub conflicted: Vec<PathBuf>,
    pub ignored: Vec<PathBuf>,
    pub deleted: Vec<PathBuf>,
    pub renames: Vec<GitRename>,
    pub submodules: Vec<GitSubmodule>,
    pub branch: Option<String>,
    pub head_oid: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitRename {
    pub from: PathBuf,
    pub to: PathBuf,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitSubmodule {
    pub path: PathBuf,
    pub state: String,
}

/// Committed gitlink identity used for a bounded PDG summary node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitSubmoduleSummary {
    pub path: PathBuf,
    pub commit_oid: String,
}

/// Read gitlink paths and commit OIDs without entering submodule worktrees.
pub fn submodule_summaries(root: &Path) -> Result<Vec<GitSubmoduleSummary>, GitInventoryError> {
    let top_output = Command::new("git")
        .args(["rev-parse", "--show-toplevel"])
        .current_dir(root)
        .output()
        .map_err(GitInventoryError::Io)?;
    if !top_output.status.success() {
        return Err(GitInventoryError::NotRepository);
    }
    let top = PathBuf::from(String::from_utf8_lossy(&top_output.stdout).trim());
    let output = Command::new("git")
        .args(["ls-files", "-z", "--stage"])
        .current_dir(&top)
        .output()
        .map_err(GitInventoryError::Io)?;
    if !output.status.success() {
        return Err(GitInventoryError::Failed(
            String::from_utf8_lossy(&output.stderr).trim().to_owned(),
        ));
    }
    let mut summaries = Vec::new();
    for record in output.stdout.split(|byte| *byte == b'\0') {
        let Some(tab) = record.iter().position(|byte| *byte == b'\t') else {
            continue;
        };
        let header = &record[..tab];
        let raw_path = &record[tab + 1..];
        let fields = header.split(|byte| *byte == b' ').collect::<Vec<_>>();
        if fields.len() < 3 || fields[0] != b"160000" {
            continue;
        }
        let gitlink_path = top.join(path_bytes(raw_path));
        // Canonicalize and filter to only gitlinks within the requested project root.
        let Some(canonical) = gitlink_path.canonicalize().ok() else {
            continue;
        };
        if canonical.starts_with(root) {
            summaries.push(GitSubmoduleSummary {
                path: gitlink_path,
                commit_oid: String::from_utf8_lossy(fields[1]).into_owned(),
            });
        }
    }
    summaries.sort_by(|a, b| a.path.cmp(&b.path));
    Ok(summaries)
}

#[derive(Debug)]
pub enum GitStatusError {
    NotRepository,
    Failed(String),
    Io(std::io::Error),
}

impl std::fmt::Display for GitStatusError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotRepository => write!(f, "not a git repository"),
            Self::Failed(message) => f.write_str(message),
            Self::Io(error) => error.fmt(f),
        }
    }
}

impl std::error::Error for GitStatusError {}

/// Run the one status command used by live status consumers.
pub fn status(root: &Path) -> Result<GitStatus, GitStatusError> {
    let output = Command::new("git")
        .args([
            "status",
            "--porcelain=v2",
            "-z",
            "--branch",
            "--untracked-files=all",
        ])
        .current_dir(root)
        .output()
        .map_err(GitStatusError::Io)?;
    if output.status.success() {
        return Ok(parse_status(&output.stdout));
    }

    let message = String::from_utf8_lossy(&output.stderr).trim().to_owned();
    if message.contains("not a git repository") {
        Err(GitStatusError::NotRepository)
    } else {
        Err(GitStatusError::Failed(message))
    }
}

/// Parse `git status --porcelain=v2 -z --branch` output.
///
/// Records are split only on NUL delimiters; path fields are otherwise opaque
/// byte strings, so spaces, tabs, and rename arrows remain part of their path.
pub fn parse_status(bytes: &[u8]) -> GitStatus {
    let mut status = GitStatus::default();
    let mut fields = bytes.split(|byte| *byte == b'\0');

    while let Some(record) = fields.next() {
        if record.is_empty() {
            continue;
        }
        match record[0] {
            b'#' => parse_header(record, &mut status),
            b'1' => parse_ordinary(record, &mut status),
            b'2' => parse_rename(record, fields.next(), &mut status),
            b'u' => parse_unmerged(record, &mut status),
            b'?' => status.untracked.push(path_field(record, 2)),
            b'!' => status.ignored.push(path_field(record, 2)),
            _ => {}
        }
    }
    status
}

fn parse_header(record: &[u8], status: &mut GitStatus) {
    let mut fields = record.splitn(3, |byte| *byte == b' ');
    let _ = fields.next();
    let Some(key) = fields.next() else { return };
    let Some(value) = fields.next() else { return };
    match key {
        b"branch.head" if value != b"(detached)" => {
            status.branch = Some(String::from_utf8_lossy(value).into_owned())
        }
        b"branch.oid" if value != b"(initial)" => {
            status.head_oid = Some(String::from_utf8_lossy(value).into_owned())
        }
        _ => {}
    }
}

fn parse_ordinary(record: &[u8], status: &mut GitStatus) {
    let fields = record.splitn(9, |byte| *byte == b' ').collect::<Vec<_>>();
    if fields.len() != 9 {
        return;
    }
    let xy = fields[1];
    let submodule = fields[2];
    let path = path_bytes(fields[8]);
    classify_xy(xy, &path, status);
    if submodule.first() == Some(&b'S') {
        status.submodules.push(GitSubmodule {
            path,
            state: String::from_utf8_lossy(submodule).into_owned(),
        });
    }
}

fn parse_rename(record: &[u8], original: Option<&[u8]>, status: &mut GitStatus) {
    let fields = record.splitn(10, |byte| *byte == b' ').collect::<Vec<_>>();
    if fields.len() != 10 {
        return;
    }
    let xy = fields[1];
    let submodule = fields[2];
    let to = path_bytes(fields[9]);
    classify_xy(xy, &to, status);
    if let Some(from) = original {
        status.renames.push(GitRename {
            from: path_bytes(from),
            to: to.clone(),
        });
    }
    if submodule.first() == Some(&b'S') {
        status.submodules.push(GitSubmodule {
            path: to,
            state: String::from_utf8_lossy(submodule).into_owned(),
        });
    }
}

fn parse_unmerged(record: &[u8], status: &mut GitStatus) {
    let fields = record.splitn(11, |byte| *byte == b' ').collect::<Vec<_>>();
    if fields.len() == 11 {
        status.conflicted.push(path_bytes(fields[10]));
    }
}

fn classify_xy(xy: &[u8], path: &Path, status: &mut GitStatus) {
    let x = xy.first().copied().unwrap_or(b'.');
    let y = xy.get(1).copied().unwrap_or(b'.');
    if x != b'.' && x != b' ' {
        status.staged.push(path.to_path_buf());
    }
    if y != b'.' && y != b' ' {
        status.modified.push(path.to_path_buf());
    }
    if x == b'D' || y == b'D' {
        status.deleted.push(path.to_path_buf());
    }
}

fn path_field(record: &[u8], start: usize) -> PathBuf {
    path_bytes(record.get(start..).unwrap_or_default())
}

fn path_bytes(bytes: &[u8]) -> PathBuf {
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStringExt;
        PathBuf::from(std::ffi::OsString::from_vec(bytes.to_vec()))
    }
    #[cfg(not(unix))]
    {
        PathBuf::from(String::from_utf8_lossy(bytes).into_owned())
    }
}