Skip to content

Commit 759f4a9

Browse files
committed
Add graph query CLI commands
1 parent b62257a commit 759f4a9

10 files changed

Lines changed: 1166 additions & 1 deletion

File tree

docs/cli/about.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ iconType: "solid"
88
The Graph-sitter CLI helps you:
99

1010
- Parse a local repository into graph summary data
11+
- Inspect files and trace call relationships from the command line
1112
- Diagnose parse time, memory use, and graph size for a local repository
1213
- Initialize Graph-sitter in your repository
1314
- Create and run codemods
14-
- Run one-shot transformations by import path
15+
- Run one-shot transformations and focused renames
1516

1617
<Note>
1718
For installation instructions, see our [Getting
@@ -68,6 +69,9 @@ graph-sitter create my-codemod .
6869
<Card title="parse" icon="diagram-project" href="/cli/parse">
6970
Parse a repository and print graph summary counts.
7071
</Card>
72+
<Card title="graph" icon="diagram-project" href="/cli/graph">
73+
Inspect files, trace usages, trace callees, and rename symbols.
74+
</Card>
7175
<Card title="diagnose" icon="gauge" href="/cli/diagnose">
7276
Report parse time, memory use, and graph size for a repository.
7377
</Card>

docs/cli/graph.mdx

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
title: "Graph Commands"
3+
sidebarTitle: "graph"
4+
icon: "diagram-project"
5+
iconType: "solid"
6+
---
7+
8+
Graph-sitter includes high-level CLI commands for inspecting local code structure,
9+
tracing call relationships, and applying focused renames without writing custom
10+
codemods.
11+
12+
```bash
13+
graph-sitter inspect src/app.py
14+
graph-sitter using src/app.py:handler --depth 2
15+
graph-sitter usages src/app.py:helper --depth 2
16+
graph-sitter rename src/app.py:helper --to execute_helper --write
17+
```
18+
19+
## Target Syntax
20+
21+
Use `FILE:SYMBOL` for functions, classes, methods, and other named symbols:
22+
23+
```bash
24+
graph-sitter using src/app.py:handler
25+
graph-sitter usages src/app.py:Service.run
26+
graph-sitter rename src/app.py:handler --to run_handler --write
27+
```
28+
29+
The CLI also accepts `FILE::SYMBOL` and dotted shorthand such as
30+
`src/app.py.handler` when it can resolve the file unambiguously.
31+
32+
## inspect
33+
34+
`inspect` prints source-file structure: line count, imports, classes, functions,
35+
and function call summaries.
36+
37+
```bash
38+
graph-sitter inspect FILE [PATH] [OPTIONS]
39+
```
40+
41+
Options:
42+
43+
- `--level summary|functions|calls|full`: Choose how much detail to print.
44+
Defaults to `functions`.
45+
- `--format summary|json`: Choose human-readable or machine-readable output.
46+
- `--max-functions N`: Limit function rows. Defaults to `200`.
47+
- `--max-calls N`: Limit call names per function. Defaults to `20`.
48+
49+
Examples:
50+
51+
```bash
52+
graph-sitter inspect src/app.py .
53+
graph-sitter inspect src/app.py . --level calls
54+
graph-sitter inspect src/app.py . --level full --format json
55+
```
56+
57+
## using
58+
59+
`using` traces outbound calls from a target. With `--depth 2`, the output
60+
includes the target's direct callees and the callees those functions call.
61+
62+
```bash
63+
graph-sitter using TARGET [PATH] [OPTIONS]
64+
```
65+
66+
Options:
67+
68+
- `--depth N`: Recursion depth through resolved outbound calls. Defaults to `1`.
69+
- `--max-results N`: Maximum call edges to print. Defaults to `200`.
70+
- `--format summary|json`: Choose human-readable or machine-readable output.
71+
72+
Example:
73+
74+
```bash
75+
graph-sitter using src/app.py:handler . --depth 3 --format json
76+
```
77+
78+
## usages
79+
80+
`usages` traces inbound call sites for a target. With `--depth 2`, the output
81+
includes direct callers and callers of those callers.
82+
83+
```bash
84+
graph-sitter usages TARGET [PATH] [OPTIONS]
85+
```
86+
87+
Options:
88+
89+
- `--depth N`: Recursion depth through inbound callers. Defaults to `1`.
90+
- `--max-results N`: Maximum call edges to print. Defaults to `200`.
91+
- `--format summary|json`: Choose human-readable or machine-readable output.
92+
93+
Example:
94+
95+
```bash
96+
graph-sitter usages src/app.py:helper . --depth 2
97+
```
98+
99+
## rename
100+
101+
`rename` resolves a target symbol and renames it across Graph-sitter's resolved
102+
references. A plain command is a dry run; pass `--write` to modify files.
103+
104+
```bash
105+
graph-sitter rename TARGET [PATH] --to NEW_NAME [OPTIONS]
106+
```
107+
108+
Options:
109+
110+
- `--to NEW_NAME`: Required new symbol name.
111+
- `--check`: Preview target and reference counts without writing. This is the
112+
default.
113+
- `--write`: Apply the rename and write files to disk.
114+
- `--format summary|json`: Choose human-readable or machine-readable output.
115+
116+
Examples:
117+
118+
```bash
119+
graph-sitter rename src/app.py:helper . --to execute_helper
120+
graph-sitter rename src/app.py:helper . --to execute_helper --write
121+
```
122+
123+
## Shared Options
124+
125+
All graph commands accept the same parse controls as `parse`:
126+
127+
- `--backend python|rust|auto`: Choose the graph backend.
128+
- `--fallback python|error`: Choose fallback behavior when the Rust backend is
129+
unavailable. Defaults to `python` for these commands.
130+
- `--language auto|python|typescript`: Choose the repository language.
131+
- `--subdir PATH`: Limit parsing to a repository-relative subdirectory or file.
132+
Repeat to include multiple paths.
133+
134+
## With uvx
135+
136+
```bash
137+
uvx --python 3.13 graph-sitter inspect src/app.py .
138+
uvx --python 3.13 graph-sitter using src/app.py:handler . --depth 2 --format json
139+
uvx --python 3.13 graph-sitter usages src/app.py:helper . --depth 2
140+
uvx --python 3.13 graph-sitter rename src/app.py:helper . --to execute_helper --write
141+
```

docs/cli/uvx.mdx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ temporary environment.
1010

1111
```bash
1212
uvx --python 3.13 graph-sitter parse .
13+
uvx --python 3.13 graph-sitter inspect src/app.py .
14+
uvx --python 3.13 graph-sitter usages src/app.py:handler . --depth 2
1315
uvx --python 3.13 graph-sitter transform ./codemods/rename.py:rename . --check
1416
uvx --python 3.13 graph-sitter run rename-function . --check
1517
```
@@ -86,6 +88,23 @@ Write JSON to a file when another tool consumes the graph summary:
8688
uvx --python 3.13 graph-sitter parse . --format json --output graph-sitter-index.json
8789
```
8890

91+
## Graph Queries
92+
93+
The high-level graph commands do not require `.codegen` initialization.
94+
95+
```bash
96+
uvx --python 3.13 graph-sitter inspect src/app.py .
97+
uvx --python 3.13 graph-sitter using src/app.py:handler . --depth 2 --format json
98+
uvx --python 3.13 graph-sitter usages src/app.py:helper . --depth 2
99+
uvx --python 3.13 graph-sitter rename src/app.py:helper . --to execute_helper --write
100+
```
101+
102+
Use `--subdir` for large repositories:
103+
104+
```bash
105+
uvx --python 3.13 graph-sitter using src/app.py:handler ./monorepo --subdir packages/app --depth 2
106+
```
107+
89108
## Transform
90109

91110
`transform` runs a one-shot Python transform by file path or import path. It

src/graph_sitter/cli/cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
from graph_sitter.cli.commands.diagnose.main import diagnose_command
77
from graph_sitter.cli.commands.doctor.main import doctor_command
88
from graph_sitter.cli.commands.init.main import init_command
9+
from graph_sitter.cli.commands.inspect.main import inspect_command
910
from graph_sitter.cli.commands.list.main import list_command
1011
from graph_sitter.cli.commands.lsp.lsp import lsp_command
1112
from graph_sitter.cli.commands.notebook.main import notebook_command
1213
from graph_sitter.cli.commands.parse.main import parse_command
14+
from graph_sitter.cli.commands.rename.main import rename_command
1315
from graph_sitter.cli.commands.reset.main import reset_command
1416
from graph_sitter.cli.commands.run.main import run_command
1517
from graph_sitter.cli.commands.start.main import start_command
1618
from graph_sitter.cli.commands.style_debug.main import style_debug_command
1719
from graph_sitter.cli.commands.transform.main import transform_command
1820
from graph_sitter.cli.commands.update.main import update_command
21+
from graph_sitter.cli.commands.usages.main import usages_command
22+
from graph_sitter.cli.commands.using.main import using_command
1923

2024
click.rich_click.USE_RICH_MARKUP = True
2125
install(show_locals=True)
@@ -32,6 +36,10 @@ def main():
3236
main.add_command(doctor_command)
3337
main.add_command(diagnose_command)
3438
main.add_command(parse_command)
39+
main.add_command(inspect_command)
40+
main.add_command(usages_command)
41+
main.add_command(using_command)
42+
main.add_command(rename_command)
3543
main.add_command(run_command)
3644
main.add_command(transform_command)
3745
main.add_command(create_command)

0 commit comments

Comments
 (0)