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
mod cmd;
pub mod style;
use clap::{Parser, Subcommand, ValueEnum};
use tracing_subscriber::EnvFilter;
use cmd::blast_radius::BlastFormat;
use gitcortex_viz::VizFormat;
#[derive(Parser)]
#[command(name = "gcx", version, about = "GitCortex knowledge-graph CLI")]
struct Cli {
/// When to emit ANSI colour: auto (TTY only), always, never.
/// Also respects NO_COLOR, CLICOLOR=0, TERM=dumb when set to auto.
#[arg(long, value_enum, default_value_t = style::ColorMode::Auto, global = true)]
color: style::ColorMode,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Install git hooks and run the initial index for this repo.
Init {
/// Also install the GitHub Actions blast-radius workflow.
#[arg(long)]
ci: bool,
/// Editor to configure: none, auto, claude, cursor, windsurf, copilot, antigravity, codex, all.
/// No editor configuration is written unless this flag is supplied.
#[arg(long, value_name = "EDITOR")]
editor: Option<String>,
/// Permit changes to editor configuration outside this repository.
#[arg(long)]
global_editor_config: bool,
/// Permit changes to a Git hooks path shared outside this repository.
#[arg(long)]
shared_git_hooks: bool,
},
/// Remove GitCortex hooks and editor integrations from this repository.
Deinit {
/// Show every planned change without modifying files.
#[arg(long)]
dry_run: bool,
/// Also remove .gitcortex/ and this repository's machine-local graph data.
#[arg(long)]
purge: bool,
/// Also remove GitCortex entries from editor configuration outside this repository.
#[arg(long)]
global_editor_config: bool,
/// Permit removing GitCortex blocks from a shared external Git hooks path.
#[arg(long)]
shared_git_hooks: bool,
},
/// Incremental index triggered by a git hook.
Hook {
/// Called from post-checkout; records the new branch without re-indexing.
#[arg(long)]
branch_switch: bool,
},
/// Start the MCP server (stdio transport by default).
Serve {
/// Expose all individual tools in addition to the `gcx` dispatch tool.
/// By default only the compact single-tool schema is exposed to minimise
/// per-turn token overhead (~200 tokens vs ~14 000 in full mode).
#[arg(long)]
full: bool,
},
/// Internal repository daemon used to multiplex local MCP clients.
#[command(name = "__serve-daemon", hide = true)]
ServeDaemon {
#[arg(long)]
repo_root: std::path::PathBuf,
},
/// One-shot query commands — useful for manual testing.
#[command(subcommand)]
Query(QueryCmd),
/// Visualise the knowledge graph in the browser or as DOT output.
Viz {
/// Branch to visualise.
#[arg(long, default_value = "main")]
branch: String,
/// Output format.
#[arg(long, default_value = "web", value_enum)]
format: VizFormat,
/// HTTP port (web mode only).
#[arg(long, default_value_t = 5678)]
port: u16,
},
/// Show the blast radius of changes between two branches.
BlastRadius {
/// Base branch (the target you're merging into).
#[arg(long, default_value = "main")]
base: String,
/// Head branch (the branch with changes).
#[arg(long, default_value = "HEAD")]
head: String,
/// BFS depth for transitive caller discovery.
#[arg(long, default_value_t = 2)]
depth: u8,
/// Output format.
#[arg(long, default_value = "text", value_enum)]
format: BlastFormat,
},
/// Export the knowledge graph as a Markdown map, JSON, or a CLAUDE.md symbol block.
Export {
/// Branch to export (defaults to current branch).
#[arg(long)]
branch: Option<String>,
/// Output format: markdown (default codebase map), json (symbols + edges).
#[arg(long, default_value = "markdown", value_enum)]
format: cmd::export::ExportFormat,
/// Upsert a compressed top-symbols table into CLAUDE.md between
/// `<!-- gcx:symbols -->` markers, so assistants get high-value symbols
/// pre-loaded with zero tool calls. Overrides --format.
#[arg(long)]
claude_md: bool,
/// How many top-ranked symbols to inject with --claude-md.
#[arg(long, default_value_t = 40)]
top: usize,
},
/// Show indexed node/edge counts for the current branch.
Status {
/// Branch to inspect (defaults to current branch).
#[arg(long)]
branch: Option<String>,
},
/// Wipe the graph store for this repo so a fresh full index can run.
Clean,
/// Diagnose setup issues: hooks, store, index freshness, MCP registration.
Doctor,
/// Check for a newer release and print the right update command.
Update,
}
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum AgentOutputFormat {
Text,
AgentJson,
}
#[derive(Subcommand)]
pub enum QueryCmd {
/// Look up all nodes with the given name.
LookupSymbol {
name: String,
#[arg(long, default_value = "main")]
branch: String,
},
/// Find all callers of a function. Use --depth for multi-hop traversal (1–5).
FindCallers {
/// Exact short name or qualified symbol name. Ambiguous short names
/// return candidates without traversing unrelated call graphs.
name: String,
#[arg(long, default_value_t = 1)]
depth: u8,
/// Maximum caller evidence rows returned after ranking.
#[arg(long, default_value_t = 25)]
limit: usize,
/// Global response budget used by agent-json output.
#[arg(long, default_value_t = 600)]
budget_tokens: usize,
#[arg(long, value_enum, default_value_t = AgentOutputFormat::Text)]
format: AgentOutputFormat,
#[arg(long, default_value = "main")]
branch: String,
},
/// List all definitions in a source file.
ListDefinitions {
file: String,
#[arg(long, default_value = "main")]
branch: String,
},
/// 360° view of a symbol: definition, callers, callees, and type usages.
SymbolContext {
name: String,
/// Maximum callers/callees/used_by rows returned per relation.
#[arg(long, default_value_t = 25)]
limit: usize,
/// Global response budget used by agent-json output.
#[arg(long, default_value_t = 800)]
budget_tokens: usize,
#[arg(long, value_enum, default_value_t = AgentOutputFormat::Text)]
format: AgentOutputFormat,
#[arg(long, default_value = "main")]
branch: String,
},
/// Find all functions/methods called by a function. Use --depth for multi-hop (1–5).
FindCallees {
name: String,
#[arg(long, default_value_t = 1)]
depth: u8,
#[arg(long, default_value = "main")]
branch: String,
},
/// Find all types that implement or inherit a trait or interface.
FindImplementors {
name: String,
#[arg(long, default_value = "main")]
branch: String,
},
/// Find the shortest call path between two functions (max 6 hops).
TracePath {
from: String,
to: String,
#[arg(long, value_enum, default_value_t = AgentOutputFormat::Text)]
format: AgentOutputFormat,
#[arg(long, default_value = "main")]
branch: String,
},
/// Find symbols with no callers or type references (dead code candidates).
FindUnused {
/// Optional kind filter: function, method, struct, trait, interface, enum, constant.
#[arg(long)]
kind: Option<String>,
#[arg(long, default_value = "main")]
branch: String,
},
/// Show all nodes and edges within N hops of a seed symbol.
GetSubgraph {
name: String,
#[arg(long, default_value_t = 1)]
depth: u8,
/// Direction: in (callers/ancestors), out (callees/descendants), both.
#[arg(long, default_value = "both")]
direction: String,
/// Maximum ranked relation evidence rows returned.
#[arg(long, default_value_t = 20)]
limit: usize,
/// Global response budget used by agent-json output.
#[arg(long, default_value_t = 400)]
budget_tokens: usize,
#[arg(long, value_enum, default_value_t = AgentOutputFormat::Text)]
format: AgentOutputFormat,
#[arg(long, default_value = "main")]
branch: String,
},
/// Render a wiki-style markdown page for a symbol.
Wiki {
name: String,
#[arg(long, default_value = "main")]
branch: String,
},
/// Fuzzy search over the graph by name + qualified path.
Search {
query: String,
#[arg(long, default_value_t = 10)]
limit: usize,
#[arg(long, default_value_t = 600)]
budget_tokens: usize,
#[arg(long, value_enum, default_value_t = AgentOutputFormat::Text)]
format: AgentOutputFormat,
#[arg(long, default_value = "main")]
branch: String,
},
/// Generate a guided tour of the codebase (omit --seed for global tour).
Tour {
#[arg(long)]
seed: Option<String>,
#[arg(long, default_value_t = 6)]
limit: usize,
#[arg(long, default_value = "main")]
branch: String,
},
/// Find high-centrality hub symbols with many inbound calls.
FindGodNodes {
#[arg(long, default_value_t = 10)]
min_in_degree: u32,
#[arg(long, default_value_t = 20)]
limit: usize,
#[arg(long, default_value = "main")]
branch: String,
},
/// Detect code communities via label-propagation clustering.
FindClusters {
#[arg(long, default_value_t = 3)]
min_cluster_size: usize,
#[arg(long, default_value_t = 20)]
limit: usize,
#[arg(long, default_value = "main")]
branch: String,
},
}
fn main() {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.with_writer(std::io::stderr)
.init();
let cli = Cli::parse();
style::init(cli.color);
let result = match cli.command {
Commands::Init {
ci,
editor,
global_editor_config,
shared_git_hooks,
} => cmd::init::run(
ci,
editor.as_deref(),
global_editor_config,
shared_git_hooks,
),
Commands::Deinit {
dry_run,
purge,
global_editor_config,
shared_git_hooks,
} => cmd::deinit::run(dry_run, purge, global_editor_config, shared_git_hooks),
Commands::Hook { branch_switch } => cmd::hook::run(branch_switch),
Commands::Serve { full } => cmd::serve::run(!full),
Commands::ServeDaemon { repo_root } => cmd::serve::run_daemon(repo_root),
Commands::Query(q) => cmd::query::run(q),
Commands::Viz {
branch,
format,
port,
} => gitcortex_viz::run(branch, port, format),
Commands::BlastRadius {
base,
head,
depth,
format,
} => cmd::blast_radius::run(base, head, depth, format),
Commands::Export {
branch,
format,
claude_md,
top,
} => cmd::export::run(branch, format, claude_md, top),
Commands::Status { branch } => cmd::status::run(branch),
Commands::Clean => cmd::clean::run(),
Commands::Doctor => cmd::doctor::run(),
Commands::Update => cmd::update::run(),
};
if let Err(e) = result {
eprintln!("error: {e:#}");
std::process::exit(1);
}
}