Skip to content

Commit 431e119

Browse files
authored
Merge pull request #16 from churcho/fix/documentation
Fix documentation to use `code_search code` subcommand prefix
2 parents 030eed7 + dcb4381 commit 431e119

38 files changed

Lines changed: 411 additions & 347 deletions

File tree

README.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,29 @@ The database is automatically created at `.code_search/surrealdb.rocksdb` in you
4747
Import a call graph JSON file (generated by [ex_ast](https://github.com/CamonZ/ex_ast)):
4848

4949
```bash
50-
code_search import --file call_graph.json
50+
code_search code import --file call_graph.json
5151
```
5252

5353
### 3. Query the data
5454

5555
```bash
5656
# Find where a function is defined
57-
code_search location get_user MyApp.Accounts
57+
code_search code location get_user MyApp.Accounts
5858

5959
# What does this function call?
60-
code_search calls-from MyApp.Accounts get_user
60+
code_search code calls-from MyApp.Accounts get_user
6161

6262
# Who calls this function?
63-
code_search calls-to MyApp.Repo get
63+
code_search code calls-to MyApp.Repo get
6464

6565
# Trace the full call chain from a function
66-
code_search trace MyApp.Web.UserController show
66+
code_search code trace MyApp.Web.UserController show
6767

6868
# Find unused functions
69-
code_search unused -P
69+
code_search code unused -P
7070

7171
# Find hotspots (most called functions)
72-
code_search hotspots -l 10
72+
code_search code hotspots -l 10
7373
```
7474

7575
## Output Formats
@@ -82,7 +82,7 @@ All commands support three output formats via `--format` or `-o`:
8282

8383
## Commands
8484

85-
Use `code_search <command> --help` for detailed help on any command.
85+
Use `code_search code <command> --help` for detailed help on any command.
8686

8787
### Query Commands
8888

@@ -169,26 +169,26 @@ If `--db` is not specified, commands automatically search for the database in th
169169

170170
```bash
171171
# Find all functions that accept a User.t type
172-
code_search accepts "User.t"
172+
code_search code accepts "User.t"
173173

174174
# Find modules with circular dependencies
175-
code_search cycles MyApp.Core
175+
code_search code cycles MyApp.Core
176176

177177
# Find the most complex functions in a namespace
178-
code_search complexity MyApp.Accounts --min 10
178+
code_search code complexity MyApp.Accounts --min 10
179179

180180
# Trace how a controller action flows through the codebase
181-
code_search trace MyApp.Web.UserController create --depth 10
181+
code_search code trace MyApp.Web.UserController create --depth 10
182182

183183
# Find all paths from an API endpoint to a database call
184-
code_search path --from-module MyApp.API --from-function create_user \
184+
code_search code path --from-module MyApp.API --from-function create_user \
185185
--to-module MyApp.Repo --to-function insert
186186

187187
# Find god modules (too large, too connected)
188-
code_search god-modules --min-functions 30 --min-loc 500
188+
code_search code god-modules --min-functions 30 --min-loc 500
189189

190190
# Find boundary modules that many depend on
191-
code_search boundaries --min-ratio 3.0
191+
code_search code boundaries --min-ratio 3.0
192192
```
193193

194194
## Claude Code Integration
@@ -234,6 +234,22 @@ git config code-search.mix-env test # Default: dev
234234

235235
See [docs/GIT_HOOKS.md](docs/GIT_HOOKS.md) for detailed documentation and troubleshooting.
236236

237+
### Git Worktrees
238+
239+
When using [git worktrees](https://git-scm.com/docs/git-worktree), each
240+
worktree must have its own local database. Run setup and import once in
241+
each worktree directory:
242+
243+
```sh
244+
mix compile
245+
ex_ast --output /tmp/call_graph.json
246+
code_search setup
247+
code_search code import --file /tmp/call_graph.json
248+
```
249+
250+
See [docs/WORKTREES.md](docs/WORKTREES.md) for full details and
251+
troubleshooting.
252+
237253
### Using with Claude Code
238254

239255
Once templates are installed, Claude Code can automatically help you explore your codebase:

docs/GIT_HOOKS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ When you make a commit, the post-commit hook:
8181
- Uses the configured Mix environment
8282
- Outputs JSON to a temporary file
8383

84-
4. **Updates database**: Runs `code_search import` to update the database
84+
4. **Updates database**: Runs `code_search code import` to update the database
8585
- Database path auto-resolves to `.code_search/surrealdb.rocksdb`
8686
- Uses configured project name if set (optional)
8787
- Performs upsert operations (updates existing records, inserts new ones)
@@ -224,7 +224,7 @@ mix compile --debug-info
224224
ex_ast --git-diff HEAD~1 --format json --output changes.json
225225

226226
# Import
227-
code_search --db call_graph.db import --file changes.json
227+
code_search --db call_graph.db code import --file changes.json
228228
```
229229

230230
Or for a different git reference:
@@ -246,7 +246,7 @@ The same approach works in CI/CD pipelines. Example GitHub Actions workflow:
246246
run: |
247247
mix compile --debug-info
248248
ex_ast --git-diff HEAD~1 --format json --output changes.json
249-
code_search --db call_graph.db import --file changes.json
249+
code_search --db call_graph.db code import --file changes.json
250250
```
251251
252252
## Performance Characteristics

docs/NEW_COMMANDS.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use crate::output::{OutputFormat, Outputable};
5656
#[derive(Args, Debug)]
5757
#[command(after_help = "\
5858
Examples:
59-
code_search <name> --arg value # Example usage")]
59+
code_search code <name> --arg value # Example usage")]
6060
pub struct <Name>Cmd {
6161
/// Description of the argument
6262
#[arg(short, long)]
@@ -148,34 +148,31 @@ impl Outputable for <Name>Result {
148148

149149
See [examples/output_tests.rs.example](./examples/output_tests.rs.example) for a reference showing the snapshot testing pattern. The example includes a helper for generating the actual output values to use in your snapshots.
150150

151-
### 8. Register the command (`src/commands/mod.rs`)
151+
### 8. Register the command
152152

153-
Add the module declaration and public export:
153+
Add the module declaration and public export in `src/commands/mod.rs`:
154154

155155
```rust
156156
mod <name>;
157157

158158
pub use <name>::<Name>Cmd;
159159
```
160160

161-
Add the variant to the `Command` enum:
161+
Add the variant to the `CodeCommand` enum in `src/commands/code.rs`:
162162

163163
```rust
164164
#[derive(Subcommand, Debug)]
165165
#[enum_dispatch(CommandRunner)]
166-
pub enum Command {
166+
pub enum CodeCommand {
167167
/// Existing commands...
168168
Import(ImportCmd),
169169

170170
/// Description of your command
171171
<Name>(<Name>Cmd),
172-
173-
#[command(external_subcommand)]
174-
Unknown(Vec<String>),
175172
}
176173
```
177174

178-
**Note:** The `#[enum_dispatch(CommandRunner)]` attribute is already on the `Command` enum. The `enum_dispatch` crate automatically generates the dispatch logic for all variants. You do NOT need to add a match arm in `Command::run()` - the `CommandRunner` implementation you added to your command's `mod.rs` file (in step 2) is all that's needed!
175+
**Note:** The top-level `Command` enum in `mod.rs` has only `Setup` and `Code(CodeCommand)`. All code analysis commands are registered in `CodeCommand` (in `code.rs`), not in `Command`. The `#[enum_dispatch(CommandRunner)]` attribute automatically generates dispatch logic for all variants. You do NOT need to add a match arm the `CommandRunner` implementation you added to your command's `mod.rs` file (in step 2) is all that's needed!
179176

180177
The dispatch is handled entirely by the `enum_dispatch` procedural macro at compile time, which is faster and more maintainable than manual match arms.
181178

@@ -184,7 +181,7 @@ The dispatch is handled entirely by the `enum_dispatch` procedural macro at comp
184181
```bash
185182
cargo build
186183
cargo test
187-
cargo run -- <name> --help
184+
cargo run -- code <name> --help
188185
```
189186

190187
## Checklist
@@ -219,7 +216,7 @@ cargo run -- <name> --help
219216
- [ ] Registered command in `src/commands/mod.rs`
220217
- [ ] Added module declaration: `mod <name>;`
221218
- [ ] Added public export: `pub use <name>::<Name>Cmd;`
222-
- [ ] Added enum variant to `Command` enum (dispatch is automatic via `#[enum_dispatch(CommandRunner)]`)
219+
- [ ] Added enum variant to `CodeCommand` in `src/commands/code.rs` (dispatch is automatic via `#[enum_dispatch(CommandRunner)]`)
223220
- [ ] **No match arm needed** - enum_dispatch handles it automatically!
224221
- [ ] Verified with `cargo build && cargo test`
225222

docs/WORKTREES.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Using code_search with Git Worktrees
2+
3+
Each worktree must have its own local database. The tool uses an embedded
4+
RocksDB which only allows one process to hold the lock at a time — if multiple
5+
worktrees share the global `~/.code_search/surrealdb.rocksdb` you will get:
6+
7+
```
8+
Error: "Failed to connect to SurrealDB: IO error: While lock file: LOCK:
9+
Resource temporarily unavailable"
10+
```
11+
12+
## Setup per worktree
13+
14+
Run this once in each worktree root:
15+
16+
```sh
17+
mix compile
18+
ex_ast --output /tmp/call_graph.json
19+
code_search setup
20+
code_search code import --file /tmp/call_graph.json
21+
rm /tmp/call_graph.json
22+
```
23+
24+
`code_search setup` creates `.code_search/surrealdb.rocksdb` in the current
25+
directory. The tool's path resolution always prefers a local `.code_search/`
26+
over the global `~/.code_search/`, so each worktree gets its own isolated
27+
database and lock file.
28+
29+
## Rebuilding a stale or corrupt database
30+
31+
If you see a "record already exists" or lock error on a worktree that already
32+
has a local DB:
33+
34+
```sh
35+
rm -rf .code_search/surrealdb.rocksdb
36+
mix compile
37+
ex_ast --output /tmp/call_graph.json
38+
code_search setup
39+
code_search code import --file /tmp/call_graph.json
40+
rm /tmp/call_graph.json
41+
```
42+
43+
## Important notes
44+
45+
- Never rely on `~/.code_search/surrealdb.rocksdb` in a multi-worktree setup
46+
- Two processes cannot share the same RocksDB — this is an embedded database
47+
limitation, not a bug in code_search
48+
- Each worktree's `.code_search/` should be in `.gitignore` (it is by default)
49+
- The git hook installed via `code_search setup --install-hooks` handles
50+
incremental updates automatically after each commit, so you only need to do
51+
the full import once per worktree

templates/agents/code-search-explorer.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ You are an expert Elixir/Erlang codebase explorer powered by the `code_search` C
2020
When asked to explore a codebase:
2121

2222
1. **Identify the query type**:
23-
- Finding definitions → Use `code_search location` or `code_search function`
24-
- Understanding calls → Use `code_search calls-from` or `code_search calls-to`
25-
- Tracing paths → Use `code_search trace` or `code_search reverse-trace`
26-
- Module analysis → Use `code_search browse-module` or `code_search depends-on`
27-
- Quality checks → Use `code_search unused`, `code_search hotspots`, etc.
23+
- Finding definitions → Use `code_search code location` or `code_search code function`
24+
- Understanding calls → Use `code_search code calls-from` or `code_search code calls-to`
25+
- Tracing paths → Use `code_search code trace` or `code_search code reverse-trace`
26+
- Module analysis → Use `code_search code browse-module` or `code_search code depends-on`
27+
- Quality checks → Use `code_search code unused`, `code_search code hotspots`, etc.
2828

2929
2. **Execute queries efficiently**:
3030
- Always use `--format toon` for token-efficient output
@@ -52,7 +52,7 @@ The database is automatically searched in:
5252

5353
Override if needed:
5454
```bash
55-
code_search --db /path/to/db.rocksdb <command>
55+
code_search --db /path/to/db.rocksdb code <command>
5656
```
5757

5858
## Example Workflow
@@ -61,17 +61,17 @@ When user asks: "Where is the authenticate function defined and what calls it?"
6161

6262
1. Find definition:
6363
```bash
64-
code_search --format toon location authenticate
64+
code_search --format toon code location authenticate
6565
```
6666

6767
2. Find callers:
6868
```bash
69-
code_search --format toon calls-to --function authenticate
69+
code_search --format toon code calls-to --function authenticate
7070
```
7171

7272
3. If results show a specific module, explore it:
7373
```bash
74-
code_search --format toon browse-module AuthModule
74+
code_search --format toon code browse-module AuthModule
7575
```
7676

7777
4. Read source for context:
@@ -83,17 +83,17 @@ code_search --format toon browse-module AuthModule
8383

8484
| Task | Command |
8585
|------|---------|
86-
| Find function | `code_search --format toon location <name>` |
87-
| Browse module | `code_search --format toon browse-module <module>` |
88-
| What calls X? | `code_search --format toon calls-to --function <name>` |
89-
| What does X call? | `code_search --format toon calls-from <module> <function>` |
90-
| Trace execution | `code_search --format toon trace <module> <function> --depth N` |
91-
| Find path A→B | `code_search --format toon path --from-module A --to-module B` |
92-
| Module deps | `code_search --format toon depends-on <module>` |
93-
| Who depends on X? | `code_search --format toon depended-by <module>` |
94-
| Unused code | `code_search --format toon unused` |
95-
| Hotspots | `code_search --format toon hotspots --kind incoming` |
96-
| Code smells | `code_search --format toon god-modules`, `complexity`, etc. |
86+
| Find function | `code_search --format toon code location <name>` |
87+
| Browse module | `code_search --format toon code browse-module <module>` |
88+
| What calls X? | `code_search --format toon code calls-to --function <name>` |
89+
| What does X call? | `code_search --format toon code calls-from <module> <function>` |
90+
| Trace execution | `code_search --format toon code trace <module> <function> --depth N` |
91+
| Find path A→B | `code_search --format toon code path --from-module A --to-module B` |
92+
| Module deps | `code_search --format toon code depends-on <module>` |
93+
| Who depends on X? | `code_search --format toon code depended-by <module>` |
94+
| Unused code | `code_search --format toon code unused` |
95+
| Hotspots | `code_search --format toon code hotspots --kind incoming` |
96+
| Code smells | `code_search --format toon code god-modules`, `complexity`, etc. |
9797

9898
## Important Notes
9999

templates/hooks/post-commit

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# from the last commit. It:
77
# 1. Ensures the project is compiled with debug info
88
# 2. Extracts AST data for changed files using ex_ast --git-diff
9-
# 3. Imports the data into the database using code_search import
9+
# 3. Imports the data into the database using code_search code import
1010
#
1111
# Installation:
1212
# cp hooks/post-commit .git/hooks/post-commit
@@ -105,7 +105,7 @@ fi
105105
# Database path will be auto-resolved to .code_search/surrealdb.rocksdb
106106
info "Importing data..."
107107

108-
if code_search import --file "${TEMP_JSON}" 2>&1; then
108+
if code_search code import --file "${TEMP_JSON}" 2>&1; then
109109
info "Database updated successfully!"
110110
else
111111
error "Database import failed"

templates/skills/accepts/SKILL.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Find functions that take certain types as input parameters. Use this to understa
1414
## Usage
1515

1616
```bash
17-
code_search --format toon accepts <PATTERN> [MODULE] [OPTIONS]
17+
code_search --format toon code accepts <PATTERN> [MODULE] [OPTIONS]
1818
```
1919

2020
## Arguments
@@ -34,10 +34,10 @@ code_search --format toon accepts <PATTERN> [MODULE] [OPTIONS]
3434
## Examples
3535

3636
```bash
37-
code_search accepts "User.t" # Find functions accepting User.t
38-
code_search accepts "map()" # Find functions accepting maps
39-
code_search accepts "User.t" MyApp # Filter to module MyApp
40-
code_search accepts -r "list\(.*\)" # Regex pattern matching
37+
code_search code accepts "User.t" # Find functions accepting User.t
38+
code_search code accepts "map()" # Find functions accepting maps
39+
code_search code accepts "User.t" MyApp # Filter to module MyApp
40+
code_search code accepts -r "list\(.*\)" # Regex pattern matching
4141
```
4242

4343
## Output Fields (toon format)

templates/skills/boundaries/SKILL.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Identify modules that act as architectural boundaries - modules that are heavily
1414
## Usage
1515

1616
```bash
17-
code_search --format toon boundaries [MODULE] [OPTIONS]
17+
code_search --format toon code boundaries [MODULE] [OPTIONS]
1818
```
1919

2020
## Arguments
@@ -35,11 +35,11 @@ code_search --format toon boundaries [MODULE] [OPTIONS]
3535
## Examples
3636

3737
```bash
38-
code_search boundaries # Find all boundary modules
39-
code_search boundaries MyApp.Web # Filter to MyApp.Web namespace
40-
code_search boundaries --min-incoming 5 # With minimum 5 incoming calls
41-
code_search boundaries --min-ratio 2.0 # With minimum 2.0 ratio
42-
code_search boundaries -l 20 # Show top 20 boundary modules
38+
code_search code boundaries # Find all boundary modules
39+
code_search code boundaries MyApp.Web # Filter to MyApp.Web namespace
40+
code_search code boundaries --min-incoming 5 # With minimum 5 incoming calls
41+
code_search code boundaries --min-ratio 2.0 # With minimum 2.0 ratio
42+
code_search code boundaries -l 20 # Show top 20 boundary modules
4343
```
4444

4545
## Output Fields (toon format)

0 commit comments

Comments
 (0)