M0: compilable skeleton — Kigi 0.1.0 fork surgery
Hard fork of xai-org/grok-build (Apache-2.0) re-targeted as Kigi, an
unofficial Kimi Code CLI community build.
Rename & identity
- 72 xai-*/xai-grok-* crates -> kigi-* (explicit: xai-grok-pager-bin ->
kigi-bin [binary `kigi`], xai-grok-pager -> kigi-tui; rest mechanical);
ptyctl, ptyctl-cli, third_party/ unchanged; proto package
xai.grok.tools.v1 -> kigi.tools.v1
- Config home ~/.kigi (KIGI_SHARE_DIR override), env prefix GROK_* ->
KIGI_*, `kigi --version` carries the unofficial-community-build notice
- clap identity, help text, startup banner, prompt templates rebranded
(templates re-encrypted)
Deletions (PRD removal list #5/#6/#7/#9/#10)
- voice input (xai-grok-voice) and all TUI wiring
- telemetry: Mixpanel client, external OTel stream, Sentry, OTLP layers,
trace/GCS/S3 upload queues (kigi-file-utils halved), workspace upload
module & dc_log, heap-profile uploader, auth-diagnostics uploader,
session-analytics halves of feedback; local zero-egress observability
preserved in new kigi-log crate (unified log, --debug firehose,
subsystem file logs, opt-in instrumentation)
- announcements (crate, remote-settings fields, TUI surfaces)
- plugin marketplace (crate, sources/browse/CTA/extensions-modal tab);
direct plugin install/uninstall/update via kigi-agent git_install kept
- relay/gateway/assets endpoints and features (agent relay, headless
relay transport, gateway bridge, LeaderEnvUrls); leader IPC socket now
~/.kigi/leader.sock + KIGI_LEADER_SOCKET, no ws-url derivation
- functional types rehomed instead of deleted: PermissionMode ->
kigi-config-types, McpInitStrategy -> kigi-mcp, PrCreationSource ->
session signals, TerminalDiagnostics -> kigi-pager-render, agent_id ->
shell util
Endpoints
- kigi-env rewritten: single production KigiEndpoints {coding_api_base_url
https://api.kimi.com/coding/v1 (KIGI_CODE_BASE_URL), oauth_host
https://auth.kimi.com (KIGI_OAUTH_HOST), update_base_url (GitHub
Releases API), upgrade_page_url}; GrokBuildEnvironment enum deleted
Toolchain & workspace hygiene
- Rust 1.97.0 pinned; edition 2024; full cargo update; git2 hoisted to
workspace at 0.21 (Option->Result API migration), quick-xml 0.41
- Root Cargo.toml hand-maintained (PRD §8.1): version 0.1.0 inherited by
all members, members sorted, unused deps pruned
- cargo-deny advisories gate (deny.toml with documented transitive
exceptions); CI workflow (check/clippy/fmt/deny/test, macOS+Linux)
- cross-crate test seams re-gated behind `test-support` cargo feature;
insta snapshot baselines renamed to the kigi_tui prefix
- clippy --workspace --all-targets: zero warnings; fmt clean
Fixes surfaced by the port
- updater probe/installer divergence (bin/kigi vs bin/grok symlink set)
- idle model-metadata refresh dead under KIGI_CODE_BASE_URL override
(new is_effective_coding_endpoint_url, loopback+override aware)
- macOS symlinked-TMPDIR fixture canonicalization (foreign_sessions,
fast-worktree); RSS measurement tests serialized via serial_test
Docs & legal (Apache §4)
- NOTICE added (upstream attribution + change statement); THIRD-PARTY
notices sustained; kigi-tools ported-code notices extended; README,
CONTRIBUTING, SECURITY, AGENTS.md rewritten
Out of scope for M0 (tracked): Kimi auth/inference (M1), search/fetch,
command parity, config import (M2), Computer Hub excision & final
brand-token sweep (M2), distribution & self-update rewrite (M3).
This commit is contained in:
@@ -0,0 +1,233 @@
|
||||
//! Benchmark for comparing git CLI vs git2 file listing.
|
||||
//!
|
||||
//! Usage: cargo run --bin bench_file_listing --release -- [path] [cli|git2|git2-index|both]
|
||||
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::time::Instant;
|
||||
|
||||
use git2::{Repository, StatusOptions};
|
||||
use kigi_codebase_graph::LanguageRegistry;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let path_str = if let Some(p) = args.get(1) {
|
||||
p.clone()
|
||||
} else if let Ok(p) = std::env::var("BENCH_REPO_ROOT").or_else(|_| std::env::var("XAI_ROOT")) {
|
||||
p
|
||||
} else {
|
||||
eprintln!("Usage: bench_file_listing <path> [cli|git2|git2-index|both]");
|
||||
eprintln!("Or set BENCH_REPO_ROOT to a large checkout to bench against");
|
||||
std::process::exit(1);
|
||||
};
|
||||
let mode = args.get(2).map(|s| s.as_str()).unwrap_or("both");
|
||||
|
||||
let root_path = Path::new(&path_str);
|
||||
let registry = LanguageRegistry::new();
|
||||
|
||||
match mode {
|
||||
"cli" => {
|
||||
let start = Instant::now();
|
||||
let files = collect_files_cli(root_path, ®istry);
|
||||
let elapsed = start.elapsed();
|
||||
println!("CLI: {} files in {:?}", files.len(), elapsed);
|
||||
}
|
||||
"git2" => {
|
||||
let start = Instant::now();
|
||||
let files = collect_files_git2(root_path, ®istry);
|
||||
let elapsed = start.elapsed();
|
||||
println!("git2: {} files in {:?}", files.len(), elapsed);
|
||||
}
|
||||
"git2-index" => {
|
||||
let start = Instant::now();
|
||||
let files = collect_files_git2_index_only(root_path, ®istry);
|
||||
let elapsed = start.elapsed();
|
||||
println!("git2 (index only): {} files in {:?}", files.len(), elapsed);
|
||||
}
|
||||
_ => {
|
||||
// Run all three methods multiple times for comparison
|
||||
println!("Benchmarking file listing for: {}", root_path.display());
|
||||
println!();
|
||||
|
||||
let iterations = 5;
|
||||
|
||||
// Warm up
|
||||
let _ = collect_files_cli(root_path, ®istry);
|
||||
let _ = collect_files_git2(root_path, ®istry);
|
||||
let _ = collect_files_git2_index_only(root_path, ®istry);
|
||||
|
||||
// CLI benchmark
|
||||
let mut cli_times = Vec::with_capacity(iterations);
|
||||
let mut cli_count = 0;
|
||||
for _ in 0..iterations {
|
||||
let start = Instant::now();
|
||||
let files = collect_files_cli(root_path, ®istry);
|
||||
cli_times.push(start.elapsed());
|
||||
cli_count = files.len();
|
||||
}
|
||||
|
||||
// git2 benchmark (with untracked)
|
||||
let mut git2_times = Vec::with_capacity(iterations);
|
||||
let mut git2_count = 0;
|
||||
for _ in 0..iterations {
|
||||
let start = Instant::now();
|
||||
let files = collect_files_git2(root_path, ®istry);
|
||||
git2_times.push(start.elapsed());
|
||||
git2_count = files.len();
|
||||
}
|
||||
|
||||
// git2 index-only benchmark
|
||||
let mut git2_index_times = Vec::with_capacity(iterations);
|
||||
let mut git2_index_count = 0;
|
||||
for _ in 0..iterations {
|
||||
let start = Instant::now();
|
||||
let files = collect_files_git2_index_only(root_path, ®istry);
|
||||
git2_index_times.push(start.elapsed());
|
||||
git2_index_count = files.len();
|
||||
}
|
||||
|
||||
// Print results
|
||||
let cli_avg = cli_times.iter().sum::<std::time::Duration>() / iterations as u32;
|
||||
let git2_avg = git2_times.iter().sum::<std::time::Duration>() / iterations as u32;
|
||||
let git2_index_avg =
|
||||
git2_index_times.iter().sum::<std::time::Duration>() / iterations as u32;
|
||||
|
||||
println!("Results ({} iterations):", iterations);
|
||||
println!(
|
||||
" CLI: {} files, avg {:?}",
|
||||
cli_count, cli_avg
|
||||
);
|
||||
println!(
|
||||
" git2 (+ untracked): {} files, avg {:?}",
|
||||
git2_count, git2_avg
|
||||
);
|
||||
println!(
|
||||
" git2 (index only): {} files, avg {:?}",
|
||||
git2_index_count, git2_index_avg
|
||||
);
|
||||
println!();
|
||||
|
||||
let speedup = cli_avg.as_secs_f64() / git2_index_avg.as_secs_f64();
|
||||
if speedup > 1.0 {
|
||||
println!("git2 (index only) is {:.2}x faster than CLI", speedup);
|
||||
} else {
|
||||
println!("CLI is {:.2}x faster than git2 (index only)", 1.0 / speedup);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Collect files using git CLI (original approach)
|
||||
fn collect_files_cli(root_path: &Path, registry: &LanguageRegistry) -> Vec<std::path::PathBuf> {
|
||||
// Get tracked files
|
||||
let tracked_output = Command::new("git")
|
||||
.args(["ls-files"])
|
||||
.current_dir(root_path)
|
||||
.output();
|
||||
|
||||
let tracked_output = match tracked_output {
|
||||
Ok(o) if o.status.success() => o,
|
||||
_ => return vec![],
|
||||
};
|
||||
|
||||
// Get untracked files
|
||||
let untracked_output = Command::new("git")
|
||||
.args(["ls-files", "--others", "--exclude-standard"])
|
||||
.current_dir(root_path)
|
||||
.output()
|
||||
.ok();
|
||||
|
||||
let tracked_str = String::from_utf8_lossy(&tracked_output.stdout);
|
||||
let mut files: Vec<std::path::PathBuf> = tracked_str
|
||||
.lines()
|
||||
.filter(|line| registry.is_supported(Path::new(line)))
|
||||
.map(|line| root_path.join(line))
|
||||
.collect();
|
||||
|
||||
if let Some(output) = untracked_output
|
||||
&& output.status.success()
|
||||
{
|
||||
let untracked_str = String::from_utf8_lossy(&output.stdout);
|
||||
let untracked_files: Vec<std::path::PathBuf> = untracked_str
|
||||
.lines()
|
||||
.filter(|line| registry.is_supported(Path::new(line)))
|
||||
.map(|line| root_path.join(line))
|
||||
.collect();
|
||||
files.extend(untracked_files);
|
||||
}
|
||||
|
||||
files
|
||||
}
|
||||
|
||||
/// Collect files using git2 (new approach)
|
||||
fn collect_files_git2(root_path: &Path, registry: &LanguageRegistry) -> Vec<std::path::PathBuf> {
|
||||
let repo = match Repository::open(root_path) {
|
||||
Ok(r) => r,
|
||||
Err(_) => return vec![],
|
||||
};
|
||||
|
||||
let index = match repo.index() {
|
||||
Ok(i) => i,
|
||||
Err(_) => return vec![],
|
||||
};
|
||||
|
||||
let mut files: Vec<std::path::PathBuf> = index
|
||||
.iter()
|
||||
.filter_map(|entry| {
|
||||
let path_str = std::str::from_utf8(&entry.path).ok()?;
|
||||
if registry.is_supported(Path::new(path_str)) {
|
||||
Some(root_path.join(path_str))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Get untracked files
|
||||
let mut status_opts = StatusOptions::new();
|
||||
status_opts
|
||||
.include_untracked(true)
|
||||
.recurse_untracked_dirs(true)
|
||||
.exclude_submodules(true);
|
||||
|
||||
if let Ok(statuses) = repo.statuses(Some(&mut status_opts)) {
|
||||
for status_entry in statuses.iter() {
|
||||
if status_entry.status().is_wt_new()
|
||||
&& let Ok(path_str) = status_entry.path()
|
||||
&& registry.is_supported(Path::new(path_str))
|
||||
{
|
||||
files.push(root_path.join(path_str));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
files
|
||||
}
|
||||
|
||||
/// Collect files using git2 index only (tracked files only, no untracked)
|
||||
fn collect_files_git2_index_only(
|
||||
root_path: &Path,
|
||||
registry: &LanguageRegistry,
|
||||
) -> Vec<std::path::PathBuf> {
|
||||
let repo = match Repository::open(root_path) {
|
||||
Ok(r) => r,
|
||||
Err(_) => return vec![],
|
||||
};
|
||||
|
||||
let index = match repo.index() {
|
||||
Ok(i) => i,
|
||||
Err(_) => return vec![],
|
||||
};
|
||||
|
||||
index
|
||||
.iter()
|
||||
.filter_map(|entry| {
|
||||
let path_str = std::str::from_utf8(&entry.path).ok()?;
|
||||
if registry.is_supported(Path::new(path_str)) {
|
||||
Some(root_path.join(path_str))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
//! Benchmark binary for index building.
|
||||
|
||||
use std::path::Path;
|
||||
use std::time::Instant;
|
||||
|
||||
use kigi_codebase_graph::{IndexBuilder, LanguageRegistry};
|
||||
|
||||
// Use mimalloc for faster allocation in multi-threaded workloads
|
||||
#[global_allocator]
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let path = if let Some(p) = args.get(1) {
|
||||
p.clone()
|
||||
} else if let Ok(p) = std::env::var("BENCH_REPO_ROOT").or_else(|_| std::env::var("XAI_ROOT")) {
|
||||
p
|
||||
} else {
|
||||
eprintln!("Usage: bench_index <path>");
|
||||
eprintln!("Or set BENCH_REPO_ROOT to a large checkout to bench against");
|
||||
std::process::exit(1);
|
||||
};
|
||||
|
||||
// First, verify all queries compile
|
||||
println!("Verifying query compilation...");
|
||||
let registry = LanguageRegistry::new();
|
||||
for ext in &["ts", "tsx", "js", "jsx", "rs", "go", "py"] {
|
||||
match registry.for_extension(ext) {
|
||||
Some(config) => match config.compile_query() {
|
||||
Ok(query) => {
|
||||
println!(" .{}: OK ({} patterns)", ext, query.pattern_count());
|
||||
}
|
||||
Err(e) => {
|
||||
println!(" .{}: FAILED - {:?}", ext, e);
|
||||
}
|
||||
},
|
||||
None => println!(" .{}: NOT SUPPORTED", ext),
|
||||
}
|
||||
}
|
||||
println!();
|
||||
|
||||
let root_path = Path::new(&path);
|
||||
|
||||
println!("Building index for: {}", root_path.display());
|
||||
let start = Instant::now();
|
||||
|
||||
let index = IndexBuilder::new()
|
||||
.build(root_path)
|
||||
.expect("Failed to build index");
|
||||
|
||||
let elapsed = start.elapsed();
|
||||
let (file_count, defs, refs) = index.stats();
|
||||
|
||||
println!("Files indexed: {}", file_count);
|
||||
println!(
|
||||
"Indexed {} definitions, {} references in {:?}",
|
||||
defs, refs, elapsed
|
||||
);
|
||||
println!("Aliases: {}", index.alias_count());
|
||||
println!(
|
||||
"Files/sec: {:.0}",
|
||||
file_count as f64 / elapsed.as_secs_f64()
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,394 @@
|
||||
//! CLI tool for code graph navigation.
|
||||
//!
|
||||
//! Provides go-to-definition and go-to-references functionality.
|
||||
//!
|
||||
//! # Usage
|
||||
//!
|
||||
//! ```bash
|
||||
//! # Build the index for a repository
|
||||
//! code-graph index /path/to/repo
|
||||
//!
|
||||
//! # Build the index with custom cache location
|
||||
//! code-graph index /path/to/repo --cache /path/to/cache.bin
|
||||
//!
|
||||
//! # Go to definition (by position)
|
||||
//! code-graph definition /path/to/repo --file src/main.rs --row 10 --col 15
|
||||
//!
|
||||
//! # Go to definition (by symbol name)
|
||||
//! code-graph definition /path/to/repo --symbol MyStruct
|
||||
//!
|
||||
//! # Go to references (by position)
|
||||
//! code-graph references /path/to/repo --file src/main.rs --row 10 --col 15
|
||||
//!
|
||||
//! # Go to references (by symbol name)
|
||||
//! code-graph references /path/to/repo --symbol MyStruct
|
||||
//!
|
||||
//! # Show index statistics
|
||||
//! code-graph stats /path/to/repo
|
||||
//! ```
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::Instant;
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
use kigi_codebase_graph::{
|
||||
IndexBuilder, Navigator, ScopeGraphIndex, get_cache_path, load_index, save_index,
|
||||
};
|
||||
|
||||
// Use mimalloc for faster allocation
|
||||
#[global_allocator]
|
||||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "code-graph")]
|
||||
#[command(author, version, about = "High-performance code navigation tool", long_about = None)]
|
||||
struct Cli {
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Commands {
|
||||
/// Build or rebuild the index for a repository
|
||||
Index {
|
||||
/// Path to the repository
|
||||
path: PathBuf,
|
||||
/// Custom cache file path (default: <repo>/.goto_index.bin)
|
||||
#[arg(short, long)]
|
||||
cache: Option<PathBuf>,
|
||||
/// Force rebuild even if cache exists
|
||||
#[arg(short, long)]
|
||||
force: bool,
|
||||
/// Number of threads to use
|
||||
#[arg(short, long)]
|
||||
threads: Option<usize>,
|
||||
},
|
||||
|
||||
/// Go to definition for a symbol
|
||||
Definition {
|
||||
/// Path to the repository
|
||||
path: PathBuf,
|
||||
/// Custom cache file path (default: <repo>/.goto_index.bin)
|
||||
#[arg(long)]
|
||||
cache: Option<PathBuf>,
|
||||
/// File path (for position-based lookup)
|
||||
#[arg(short, long)]
|
||||
file: Option<PathBuf>,
|
||||
/// Row number (1-indexed, for position-based lookup)
|
||||
#[arg(short, long)]
|
||||
row: Option<usize>,
|
||||
/// Column number (1-indexed, for position-based lookup)
|
||||
#[arg(short, long)]
|
||||
col: Option<usize>,
|
||||
/// Symbol name (for direct lookup)
|
||||
#[arg(short, long)]
|
||||
symbol: Option<String>,
|
||||
/// Output as JSON
|
||||
#[arg(long)]
|
||||
json: bool,
|
||||
},
|
||||
|
||||
/// Go to references for a symbol
|
||||
References {
|
||||
/// Path to the repository
|
||||
path: PathBuf,
|
||||
/// Custom cache file path (default: <repo>/.goto_index.bin)
|
||||
#[arg(long)]
|
||||
cache: Option<PathBuf>,
|
||||
/// File path (for position-based lookup)
|
||||
#[arg(short, long)]
|
||||
file: Option<PathBuf>,
|
||||
/// Row number (1-indexed, for position-based lookup)
|
||||
#[arg(short, long)]
|
||||
row: Option<usize>,
|
||||
/// Column number (1-indexed, for position-based lookup)
|
||||
#[arg(short, long)]
|
||||
col: Option<usize>,
|
||||
/// Symbol name (for direct lookup)
|
||||
#[arg(short, long)]
|
||||
symbol: Option<String>,
|
||||
/// Include definition in results
|
||||
#[arg(long)]
|
||||
include_definition: bool,
|
||||
/// Output as JSON
|
||||
#[arg(long)]
|
||||
json: bool,
|
||||
},
|
||||
|
||||
/// Show index statistics
|
||||
Stats {
|
||||
/// Path to the repository
|
||||
path: PathBuf,
|
||||
/// Custom cache file path (default: <repo>/.goto_index.bin)
|
||||
#[arg(long)]
|
||||
cache: Option<PathBuf>,
|
||||
},
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cli = Cli::parse();
|
||||
|
||||
match cli.command {
|
||||
Commands::Index {
|
||||
path,
|
||||
cache,
|
||||
force,
|
||||
threads,
|
||||
} => {
|
||||
cmd_index(&path, cache.as_deref(), force, threads);
|
||||
}
|
||||
Commands::Definition {
|
||||
path,
|
||||
cache,
|
||||
file,
|
||||
row,
|
||||
col,
|
||||
symbol,
|
||||
json,
|
||||
} => {
|
||||
cmd_definition(&path, cache.as_deref(), file, row, col, symbol, json);
|
||||
}
|
||||
Commands::References {
|
||||
path,
|
||||
cache,
|
||||
file,
|
||||
row,
|
||||
col,
|
||||
symbol,
|
||||
include_definition,
|
||||
json,
|
||||
} => {
|
||||
cmd_references(
|
||||
&path,
|
||||
cache.as_deref(),
|
||||
file,
|
||||
row,
|
||||
col,
|
||||
symbol,
|
||||
include_definition,
|
||||
json,
|
||||
);
|
||||
}
|
||||
Commands::Stats { path, cache } => {
|
||||
cmd_stats(&path, cache.as_deref());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the effective cache path - use custom if provided, otherwise default.
|
||||
fn effective_cache_path(repo_path: &Path, custom_cache: Option<&Path>) -> PathBuf {
|
||||
custom_cache
|
||||
.map(|p| p.to_path_buf())
|
||||
.unwrap_or_else(|| get_cache_path(repo_path))
|
||||
}
|
||||
|
||||
/// Load index from cache or build if necessary.
|
||||
fn load_or_build_index(repo_path: &Path, cache_path: &Path) -> ScopeGraphIndex {
|
||||
if let Ok(index) = load_index(cache_path) {
|
||||
println!("Loaded index from cache: {}", cache_path.display());
|
||||
return index;
|
||||
}
|
||||
|
||||
println!("Building index for: {}", repo_path.display());
|
||||
let start = Instant::now();
|
||||
|
||||
let index = IndexBuilder::new()
|
||||
.build(repo_path)
|
||||
.expect("Failed to build index");
|
||||
|
||||
let elapsed = start.elapsed();
|
||||
let (files, defs, refs) = index.stats();
|
||||
println!(
|
||||
"Built index: {} files, {} defs, {} refs in {:?}",
|
||||
files, defs, refs, elapsed
|
||||
);
|
||||
|
||||
// Save to cache
|
||||
if let Err(e) = save_index(cache_path, &index) {
|
||||
println!("Warning: Failed to save cache: {}", e);
|
||||
} else {
|
||||
println!("Saved cache to: {}", cache_path.display());
|
||||
}
|
||||
|
||||
index
|
||||
}
|
||||
|
||||
fn cmd_index(path: &Path, custom_cache: Option<&Path>, _force: bool, threads: Option<usize>) {
|
||||
let cache_path = effective_cache_path(path, custom_cache);
|
||||
|
||||
println!("Building index for: {}", path.display());
|
||||
let start = Instant::now();
|
||||
|
||||
let mut builder = IndexBuilder::new();
|
||||
if let Some(t) = threads {
|
||||
builder = builder.with_threads(t);
|
||||
}
|
||||
|
||||
let index = builder.build(path).expect("Failed to build index");
|
||||
|
||||
let elapsed = start.elapsed();
|
||||
let (files, defs, refs) = index.stats();
|
||||
|
||||
println!("Index built successfully!");
|
||||
println!(" Files indexed: {}", files);
|
||||
println!(" Definitions: {}", defs);
|
||||
println!(" References: {}", refs);
|
||||
println!(" Aliases: {}", index.alias_count());
|
||||
println!(" Time: {:?}", elapsed);
|
||||
|
||||
// Always save when explicitly indexing
|
||||
if let Err(e) = save_index(&cache_path, &index) {
|
||||
println!("Error saving cache: {}", e);
|
||||
std::process::exit(1);
|
||||
} else {
|
||||
println!(" Cache saved: {}", cache_path.display());
|
||||
}
|
||||
}
|
||||
|
||||
fn cmd_definition(
|
||||
repo_path: &Path,
|
||||
custom_cache: Option<&Path>,
|
||||
file: Option<PathBuf>,
|
||||
row: Option<usize>,
|
||||
col: Option<usize>,
|
||||
symbol: Option<String>,
|
||||
json: bool,
|
||||
) {
|
||||
let cache_path = effective_cache_path(repo_path, custom_cache);
|
||||
let index = load_or_build_index(repo_path, &cache_path);
|
||||
let navigator = Navigator::new(index);
|
||||
|
||||
let result = match (file, row, col, symbol) {
|
||||
// Position-based lookup
|
||||
(Some(file_path), Some(r), Some(c), _) => {
|
||||
let abs_path = if file_path.is_absolute() {
|
||||
file_path
|
||||
} else {
|
||||
repo_path.join(&file_path)
|
||||
};
|
||||
|
||||
match navigator.goto_definition(&abs_path, r, c) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Symbol-based lookup
|
||||
(_, _, _, Some(sym)) => navigator.goto_definition_by_name(&sym, None),
|
||||
_ => {
|
||||
println!("Error: Must provide either --file, --row, --col OR --symbol");
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
if json {
|
||||
print_json(&result);
|
||||
} else {
|
||||
println!("Symbol: {}", result.symbol);
|
||||
println!("Definitions ({}):", result.locations.len());
|
||||
for loc in &result.locations {
|
||||
println!(" {}:{}", loc.path, loc.line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn cmd_references(
|
||||
repo_path: &Path,
|
||||
custom_cache: Option<&Path>,
|
||||
file: Option<PathBuf>,
|
||||
row: Option<usize>,
|
||||
col: Option<usize>,
|
||||
symbol: Option<String>,
|
||||
include_definition: bool,
|
||||
json: bool,
|
||||
) {
|
||||
let cache_path = effective_cache_path(repo_path, custom_cache);
|
||||
let index = load_or_build_index(repo_path, &cache_path);
|
||||
let navigator = Navigator::new(index);
|
||||
|
||||
let result = match (file, row, col, symbol) {
|
||||
// Position-based lookup
|
||||
(Some(file_path), Some(r), Some(c), _) => {
|
||||
let abs_path = if file_path.is_absolute() {
|
||||
file_path
|
||||
} else {
|
||||
repo_path.join(&file_path)
|
||||
};
|
||||
|
||||
match navigator.goto_references(&abs_path, r, c, include_definition) {
|
||||
Ok(r) => r,
|
||||
Err(e) => {
|
||||
println!("Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Symbol-based lookup
|
||||
(_, _, _, Some(sym)) => navigator.goto_references_by_name(&sym, None, include_definition),
|
||||
_ => {
|
||||
println!("Error: Must provide either --file, --row, --col OR --symbol");
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
if json {
|
||||
print_json(&result);
|
||||
} else {
|
||||
println!("Symbol: {}", result.symbol);
|
||||
println!("References ({}):", result.locations.len());
|
||||
for loc in &result.locations {
|
||||
if let Some(sym) = &loc.symbol {
|
||||
println!(" {}:{} (as {})", loc.path, loc.line, sym);
|
||||
} else {
|
||||
println!(" {}:{}", loc.path, loc.line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn cmd_stats(path: &Path, custom_cache: Option<&Path>) {
|
||||
let cache_path = effective_cache_path(path, custom_cache);
|
||||
let index = load_or_build_index(path, &cache_path);
|
||||
let (files, defs, refs) = index.stats();
|
||||
|
||||
println!("Index Statistics for: {}", path.display());
|
||||
println!(" Cache location: {}", cache_path.display());
|
||||
println!(" Files indexed: {}", files);
|
||||
println!(" Definitions: {}", defs);
|
||||
println!(" References: {}", refs);
|
||||
println!(" Aliases: {}", index.alias_count());
|
||||
|
||||
// Top symbols by reference count
|
||||
let ref_counts = index.top_referenced_symbols(10);
|
||||
|
||||
println!("\nTop 10 most referenced symbols:");
|
||||
for (name, count) in &ref_counts {
|
||||
println!(" {:6} {}", count, name);
|
||||
}
|
||||
}
|
||||
|
||||
fn print_json(result: &kigi_codebase_graph::NavigationResult) {
|
||||
use serde_json::json;
|
||||
|
||||
let locations: Vec<_> = result
|
||||
.locations
|
||||
.iter()
|
||||
.map(|loc| {
|
||||
json!({
|
||||
"path": &loc.path,
|
||||
"line": loc.line,
|
||||
"symbol": loc.symbol,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
let output = json!({
|
||||
"symbol": result.symbol,
|
||||
"locations": locations,
|
||||
});
|
||||
|
||||
println!("{}", serde_json::to_string_pretty(&output).unwrap());
|
||||
}
|
||||
Reference in New Issue
Block a user