@gkoreli/ghx 2.0.0 → 2.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -4
- package/npm/bin/ghx +22 -0
- package/package.json +13 -11
- package/v1/README.md +0 -74
- package/v1/SKILL.md +0 -314
- package/v1/ghx +0 -385
package/README.md
CHANGED
|
@@ -17,18 +17,39 @@ ghx eliminates this by encoding the right defaults into every command. One call
|
|
|
17
17
|
## Install
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
|
-
#
|
|
21
|
-
|
|
20
|
+
# Zero install — just run it
|
|
21
|
+
npx @gkoreli/ghx explore vercel/next.js
|
|
22
22
|
|
|
23
23
|
# Homebrew
|
|
24
24
|
brew install gkoreli/tap/ghx
|
|
25
25
|
|
|
26
|
-
#
|
|
27
|
-
|
|
26
|
+
# npm (global)
|
|
27
|
+
npm install -g @gkoreli/ghx
|
|
28
|
+
|
|
29
|
+
# Go
|
|
30
|
+
go install github.com/gkoreli/ghx/v2@latest
|
|
31
|
+
|
|
32
|
+
# Build from source
|
|
33
|
+
cd v2 && go build -o ghx .
|
|
28
34
|
```
|
|
29
35
|
|
|
30
36
|
Requires: [gh CLI](https://cli.github.com/) authenticated (`gh auth login`).
|
|
31
37
|
|
|
38
|
+
### MCP Config (Claude Desktop, Cursor)
|
|
39
|
+
|
|
40
|
+
```json
|
|
41
|
+
{
|
|
42
|
+
"mcpServers": {
|
|
43
|
+
"ghx": {
|
|
44
|
+
"command": "npx",
|
|
45
|
+
"args": ["@gkoreli/ghx", "serve"]
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
No install step — npx downloads and caches the binary on first run.
|
|
52
|
+
|
|
32
53
|
## Commands
|
|
33
54
|
|
|
34
55
|
```bash
|
package/npm/bin/ghx
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const { platform, arch } = process;
|
|
3
|
+
|
|
4
|
+
const PLATFORMS = {
|
|
5
|
+
darwin: { arm64: "@gkoreli/ghx-darwin-arm64/ghx", x64: "@gkoreli/ghx-darwin-x64/ghx" },
|
|
6
|
+
linux: { arm64: "@gkoreli/ghx-linux-arm64/ghx", x64: "@gkoreli/ghx-linux-x64/ghx" },
|
|
7
|
+
win32: { arm64: "@gkoreli/ghx-win32-arm64/ghx.exe", x64: "@gkoreli/ghx-win32-x64/ghx.exe" },
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const bin = PLATFORMS[platform]?.[arch];
|
|
11
|
+
if (!bin) {
|
|
12
|
+
console.error(`ghx: unsupported platform ${platform}/${arch}`);
|
|
13
|
+
process.exitCode = 1;
|
|
14
|
+
} else {
|
|
15
|
+
const result = require("child_process").spawnSync(
|
|
16
|
+
require.resolve(bin),
|
|
17
|
+
process.argv.slice(2),
|
|
18
|
+
{ shell: false, stdio: "inherit" }
|
|
19
|
+
);
|
|
20
|
+
if (result.error) throw result.error;
|
|
21
|
+
process.exitCode = result.status;
|
|
22
|
+
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkoreli/ghx",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "GitHub code exploration
|
|
3
|
+
"version": "2.1.5",
|
|
4
|
+
"description": "Agent-first GitHub code exploration. GraphQL batching, code maps, codemode TypeScript sandbox, MCP server.",
|
|
5
5
|
"bin": {
|
|
6
|
-
"ghx": "
|
|
6
|
+
"ghx": "npm/bin/ghx"
|
|
7
7
|
},
|
|
8
8
|
"keywords": [
|
|
9
9
|
"github",
|
|
@@ -16,20 +16,22 @@
|
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
|
-
"url": "https://github.com/gkoreli/ghx"
|
|
19
|
+
"url": "git+https://github.com/gkoreli/ghx.git"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
|
-
"
|
|
23
|
-
"v1/SKILL.md",
|
|
22
|
+
"npm/bin/ghx",
|
|
24
23
|
"README.md",
|
|
25
24
|
"LICENSE"
|
|
26
25
|
],
|
|
27
|
-
"os": [
|
|
28
|
-
"darwin",
|
|
29
|
-
"linux",
|
|
30
|
-
"win32"
|
|
31
|
-
],
|
|
32
26
|
"engines": {
|
|
33
27
|
"node": ">=16"
|
|
28
|
+
},
|
|
29
|
+
"optionalDependencies": {
|
|
30
|
+
"@gkoreli/ghx-darwin-arm64": "2.1.5",
|
|
31
|
+
"@gkoreli/ghx-darwin-x64": "2.1.5",
|
|
32
|
+
"@gkoreli/ghx-linux-arm64": "2.1.5",
|
|
33
|
+
"@gkoreli/ghx-linux-x64": "2.1.5",
|
|
34
|
+
"@gkoreli/ghx-win32-arm64": "2.1.5",
|
|
35
|
+
"@gkoreli/ghx-win32-x64": "2.1.5"
|
|
34
36
|
}
|
|
35
37
|
}
|
package/v1/README.md
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
# ghx v1 — Bash Implementation
|
|
2
|
-
|
|
3
|
-
> For the current Go version with codemode, MCP server, and typed stubs, see the [root README](../README.md).
|
|
4
|
-
|
|
5
|
-
The bash implementation — zero dependencies beyond `gh` CLI. Useful if you want a single shell script you can drop anywhere without compiling Go.
|
|
6
|
-
|
|
7
|
-
## Install
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
# npm
|
|
11
|
-
npm install -g @gkoreli/ghx
|
|
12
|
-
|
|
13
|
-
# npx (zero install)
|
|
14
|
-
npx @gkoreli/ghx --help
|
|
15
|
-
|
|
16
|
-
# curl
|
|
17
|
-
curl -sf https://raw.githubusercontent.com/gkoreli/ghx/mainline/v1/install.sh | sh
|
|
18
|
-
|
|
19
|
-
# manual
|
|
20
|
-
cp v1/ghx /usr/local/bin/ghx
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
Requires [`gh` CLI](https://cli.github.com/) authenticated (`gh auth login`).
|
|
24
|
-
|
|
25
|
-
### Platform Support
|
|
26
|
-
|
|
27
|
-
| Platform | Status | Notes |
|
|
28
|
-
|----------|--------|-------|
|
|
29
|
-
| macOS | ✅ Native | bash + readlink -f (12.3+) |
|
|
30
|
-
| Linux | ✅ Native | bash + GNU coreutils |
|
|
31
|
-
| Windows | ✅ Git Bash / WSL | Ships with Git for Windows |
|
|
32
|
-
|
|
33
|
-
## Usage
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
ghx repos "react state management" # Search repos with README preview
|
|
37
|
-
ghx explore plausible/analytics # Branch + tree + README in 1 API call
|
|
38
|
-
ghx read plausible/analytics mix.exs assets/js/dashboard/stats/bar.js # Batch read
|
|
39
|
-
ghx read plausible/analytics --map lib/plausible/stats/query.ex # Code map (~92% reduction)
|
|
40
|
-
ghx read plausible/analytics --grep "defmodule" lib/plausible/stats/query.ex
|
|
41
|
-
ghx read plausible/analytics --lines 42-80 lib/plausible/stats/query.ex
|
|
42
|
-
ghx search "useState repo:facebook/react" # Code search with matching lines
|
|
43
|
-
ghx tree plausible/analytics assets/js # Full recursive tree
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## Code Map (`--map`)
|
|
47
|
-
|
|
48
|
-
Extracts only structural declarations — imports, exports, function signatures, class definitions, type declarations. Implementation bodies are stripped.
|
|
49
|
-
|
|
50
|
-
| File | Full | Map | Reduction |
|
|
51
|
-
|------|------|-----|-----------|
|
|
52
|
-
| repomix/parseFile.ts | 5,599 | 812 | 86% |
|
|
53
|
-
| github-mcp/repositories.go | 68,862 | 1,551 | 97.7% |
|
|
54
|
-
| aider/repomap.py | 27,346 | 1,496 | 94.5% |
|
|
55
|
-
|
|
56
|
-
Average: **92% reduction**. Map 16 files in the space of reading 1 file fully.
|
|
57
|
-
|
|
58
|
-
Supported: TypeScript/JavaScript, Python, Go, Rust, Java/Kotlin, Ruby. Generic fallback for unknown extensions.
|
|
59
|
-
|
|
60
|
-
## How It Works
|
|
61
|
-
|
|
62
|
-
Wraps `gh` CLI with GraphQL batching. `repos` and `explore` batch search + metadata + README into 1 call. `read` uses GraphQL aliases to fetch up to 10 files in 1 call. `search` hits REST `/search/code` with `text_matches` for matching context and 200-char token protection.
|
|
63
|
-
|
|
64
|
-
## Agent Skill
|
|
65
|
-
|
|
66
|
-
```bash
|
|
67
|
-
ghx skill # outputs SKILL.md to stdout
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
See [`SKILL.md`](./SKILL.md) for agent-optimized instructions.
|
|
71
|
-
|
|
72
|
-
## License
|
|
73
|
-
|
|
74
|
-
MIT
|
package/v1/SKILL.md
DELETED
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: ghx
|
|
3
|
-
description: GitHub code exploration for AI agents. Use for repo exploration, reading remote files, code search, code maps. Wraps gh CLI with GraphQL batching — one command does what takes 3-5 API calls.
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# ghx — GitHub Code Exploration for AI Agents
|
|
7
|
-
|
|
8
|
-
Use `ghx` via `execute_bash` for anything on GitHub — repos, files, code search. Authenticated via `gh` CLI, structured output, zero context overhead.
|
|
9
|
-
|
|
10
|
-
## Why This Exists
|
|
11
|
-
|
|
12
|
-
Agents exploring GitHub face a reliability gap: *"Did I find nothing because nothing exists, or because I used the tool wrong?"* Raw `gh` commands have silent failure modes — `gh search code` wraps in quotes without telling you, `gh api contents/` returns base64, README requires a separate call. The agent can't distinguish "no results" from "wrong flags."
|
|
13
|
-
|
|
14
|
-
ghx eliminates this by encoding the right defaults into every command. One call returns enough context to decide the next action. You opt into the ghx skill and stop worrying about whether you searched correctly — the right behavior is the default behavior.
|
|
15
|
-
|
|
16
|
-
## Commands
|
|
17
|
-
|
|
18
|
-
```bash
|
|
19
|
-
ghx explore <owner/repo> # Branch + tree + README in 1 API call
|
|
20
|
-
ghx explore <owner/repo> <path> # Subdirectory listing
|
|
21
|
-
ghx read <owner/repo> <f1> [f2] [f3] # Read 1-10 files in 1 API call (GraphQL batching)
|
|
22
|
-
ghx read <owner/repo> --map <f1> [f2] # Structural map: signatures, imports, types (~92% token reduction)
|
|
23
|
-
ghx read <owner/repo> --grep "pat" <f> # Read file, show only matching lines (2 lines context)
|
|
24
|
-
ghx read <owner/repo> --lines 42-80 <f> # Read specific line range
|
|
25
|
-
ghx repos "<query>" # Search repos with README preview (default: 10 results)
|
|
26
|
-
ghx repos "<query>" --limit 5 # Limit repo results (max: 20)
|
|
27
|
-
ghx search "<query>" # Code search (AND matching, default: 30 results)
|
|
28
|
-
ghx search "<query>" --limit 10 # Limit code search results (max: 100)
|
|
29
|
-
ghx search --full "<query>" # Code search without line truncation (for minified files)
|
|
30
|
-
ghx tree <owner/repo> [path] # Full recursive tree listing
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
**Exit codes:** 0 = results returned, 1 = no results (query valid), 2 = usage error (bad flags/args).
|
|
34
|
-
**Flag safety:** Unknown flags always error (exit 2). Never silently absorbed into queries.
|
|
35
|
-
|
|
36
|
-
## Chain of Thought: Progressive Disclosure
|
|
37
|
-
|
|
38
|
-
**Always start surgical, escalate only when needed.** This mirrors how developers work: scan structure → identify interesting files → read specific sections.
|
|
39
|
-
|
|
40
|
-
```
|
|
41
|
-
1. ghx explore owner/repo → What's in this repo? (structure + README)
|
|
42
|
-
2. ghx read owner/repo --map *.ts → What do these files define? (signatures only, 92% fewer tokens)
|
|
43
|
-
3. ghx read owner/repo --grep "X" f → Where exactly is X in this file? (targeted lines)
|
|
44
|
-
4. ghx read owner/repo f → Show me the full file (only when needed)
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
**Why this order matters:** At 92% reduction, `--map` lets you scan 7 files in the space of reading 1 full file. The agent can understand an entire module's structure before committing context to any single file. Aider's docs confirm: *"The LLM can see classes, methods and function signatures from everywhere in the repo. This alone may give it enough context to solve many tasks."*
|
|
48
|
-
|
|
49
|
-
**When to escalate beyond ghx:**
|
|
50
|
-
- "Understand this entire module" → `gitingest https://github.com/owner/repo/tree/branch/path -i "*.ts" -o - 2>/dev/null`
|
|
51
|
-
- "Compressed view of a codebase" → `npx repomix --remote owner/repo --compress --include "src/**" --stdout`
|
|
52
|
-
|
|
53
|
-
## Search Query Syntax
|
|
54
|
-
|
|
55
|
-
`ghx search` uses the GitHub REST code search API (legacy). Multi-word queries use AND matching — both words must appear in the file but not necessarily adjacent. This is different from `gh search code` which silently wraps in quotes (exact phrase).
|
|
56
|
-
|
|
57
|
-
**Output format:**
|
|
58
|
-
```
|
|
59
|
-
201472 results (showing 30) ← stderr (total + page count)
|
|
60
|
-
jquery/jquery src/attributes/classes.js: addClass: function( value ) { ← stdout (repo path: matching line)
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
Agents get: result count (stderr) + one line per result with matching context (stdout).
|
|
64
|
-
|
|
65
|
-
```bash
|
|
66
|
-
ghx search "addClass repo:jquery/jquery" # Scoped to repo
|
|
67
|
-
ghx search "useState language:typescript" # Language filter
|
|
68
|
-
ghx search "filename:package.json repo:owner/repo" # Find specific filename
|
|
69
|
-
ghx search "form path:cgi-bin extension:py" # Path + extension filter
|
|
70
|
-
ghx search '"progress_bar" repo:plausible/analytics' # Exact phrase (shell quotes around double quotes)
|
|
71
|
-
ghx search "path:llms.txt" # Find files by name
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
**Valid REST API qualifiers:** `repo:`, `org:`, `user:`, `path:`, `filename:`, `extension:`, `language:`, `in:file`, `in:path`, `size:`, `fork:true`
|
|
75
|
-
|
|
76
|
-
**Web-only (DO NOT USE — silently treated as literal text):** `OR`, `NOT`, `symbol:`, `content:`, `is:`, regex (`/pattern/`), `enterprise:`, glob in `path:`. ghx warns on stderr if you use these.
|
|
77
|
-
|
|
78
|
-
**Rate limit:** 9 req/min for code search (strictest endpoint). Authentication required — `gh auth login` first.
|
|
79
|
-
|
|
80
|
-
**Special characters:** Dots act as word separators, not wildcards. `console.log` matches files with both `console` and `log` — it does NOT match `consolelog`.
|
|
81
|
-
|
|
82
|
-
## Search Strategy for Agents
|
|
83
|
-
|
|
84
|
-
**Search is the entry point.** Agents search first, then read. Bad search = wasted follow-up reads = token explosion. ghx search is designed to give you enough context to decide your next action in one call.
|
|
85
|
-
|
|
86
|
-
### Reading search output
|
|
87
|
-
|
|
88
|
-
```
|
|
89
|
-
90 results (showing 30) ← stderr: is this too broad?
|
|
90
|
-
⚠ Lines truncated to 200 chars (use --full for complete fragments) ← stderr: token protection kicked in
|
|
91
|
-
⚠ Query too broad — add repo:, language:, or path: to narrow ← stderr: >1000 results
|
|
92
|
-
jquery/jquery src/attributes/classes.js: addClass: function( value ) { ← stdout: repo path: matching line
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
**Decision tree after seeing results:**
|
|
96
|
-
- `0 results` → query too specific, broaden (remove qualifiers, try synonyms)
|
|
97
|
-
- `1-30 results` → good. Scan matching lines, `ghx read` the relevant files
|
|
98
|
-
- `30-1000 results` → workable but noisy. Add `repo:`, `language:`, or `path:` to narrow
|
|
99
|
-
- `>1000 results` → too broad. MUST add qualifiers before trusting results
|
|
100
|
-
- `⚠ incomplete` → query timed out, results are partial. Narrow the scope
|
|
101
|
-
|
|
102
|
-
### Token protection (safe by default)
|
|
103
|
-
|
|
104
|
-
ghx truncates each matching line to 200 chars. This prevents minified JS files (10,000+ char lines) from exploding your context window. One untruncated minified result can consume more tokens than the other 29 results combined.
|
|
105
|
-
|
|
106
|
-
- **Default**: 200 char truncation. You see `⚠ Lines truncated` on stderr only when it triggers.
|
|
107
|
-
- **`--full`**: Disables truncation. Use when you specifically need the complete matching line.
|
|
108
|
-
- **When to use `--full`**: Almost never. The truncated line is enough to decide "relevant" or "skip." Use `ghx read` to get the full file context after you've identified the right file.
|
|
109
|
-
|
|
110
|
-
### Search refinement chain of thought
|
|
111
|
-
|
|
112
|
-
```
|
|
113
|
-
1. ghx search "useState" → 201K results. Too broad.
|
|
114
|
-
2. ghx search "useState language:typescript" → 50K results. Still broad.
|
|
115
|
-
3. ghx search "useState repo:vercel/next.js" → 89 results. Workable.
|
|
116
|
-
4. ghx search "useState path:packages/next extension:tsx repo:vercel/next.js" → 12 results. Surgical.
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
**Refine, don't paginate.** At 9 req/min, pagination burns rate limit on the same broad query. Adding one qualifier is always better than fetching page 2.
|
|
120
|
-
|
|
121
|
-
### Two search systems (why some things don't work)
|
|
122
|
-
|
|
123
|
-
GitHub has two code search engines. The REST API (what ghx uses) is the legacy one. The web UI uses Blackbird (new). No programmatic tool — ghx, `gh` CLI, GitHub MCP, Octocode — can access Blackbird. This is a platform limitation, not a ghx limitation.
|
|
124
|
-
|
|
125
|
-
**What this means for agents:**
|
|
126
|
-
- `OR`, `NOT`, `symbol:`, regex, `content:`, `is:` → web-only, don't use
|
|
127
|
-
- `repo:`, `path:`, `filename:`, `language:`, `extension:`, `in:`, `size:`, `fork:` → work in REST API
|
|
128
|
-
- ghx warns on stderr if you use web-only qualifiers, but the results will be wrong
|
|
129
|
-
|
|
130
|
-
### ghx search vs `gh search code`
|
|
131
|
-
|
|
132
|
-
| Behavior | `ghx search` | `gh search code` |
|
|
133
|
-
|---|---|---|
|
|
134
|
-
| Multi-word matching | AND (both words anywhere) | Exact phrase (words must be adjacent) |
|
|
135
|
-
| Matching context | Shows matching line per result | No matching context |
|
|
136
|
-
| Result count | stderr: "90 results (showing 30)" | Not shown |
|
|
137
|
-
| Token protection | 200 char truncation, `--full` opt-out | None |
|
|
138
|
-
| Web-only warnings | Warns on stderr | Silent |
|
|
139
|
-
| Rate limit | Same (9 req/min) | Same |
|
|
140
|
-
|
|
141
|
-
AND matching is almost always what agents want. `gh search code "useState fetchData"` returns zero results if the words aren't adjacent — with no error. `ghx search "useState fetchData"` finds files containing both terms.
|
|
142
|
-
|
|
143
|
-
## Gotchas
|
|
144
|
-
|
|
145
|
-
1. **Web-only qualifiers silently degrade.** `symbol:`, `OR`, `NOT`, `content:`, `is:`, regex — these only work in GitHub's new web code search (Blackbird). The REST API treats them as literal text. `symbol:foo` searches for the TEXT "symbol:foo" inside files. ghx warns on stderr, but the results will be wrong. No programmatic tool can use these features — it's a GitHub platform limitation.
|
|
146
|
-
|
|
147
|
-
2. **`filename:` vs `path:` — both valid, different systems.** `filename:package.json` works in the REST API (legacy) for exact filename match. `path:` also works and is more flexible (matches directories too). In the NEW web code search, only `path:` works — `filename:` is not recognized. Since ghx uses the REST API, both work.
|
|
148
|
-
|
|
149
|
-
3. **`language:markdown` won't find `.txt` files.** GitHub's linguist detection doesn't classify .txt as markdown. Use `extension:txt` instead. `language:` = linguist detection, `extension:` = literal file extension.
|
|
150
|
-
|
|
151
|
-
4. **`gh search code` silently wraps queries in quotes.** `gh search code "foo bar"` sends `q="foo bar"` (exact phrase), not `q=foo bar` (AND). If the words aren't adjacent in the file, you get zero results with no error. `ghx search` sends AND queries — both words must appear but in any order. This is almost always what you want. ghx also shows result count on stderr and matching line context — `gh search code` shows neither.
|
|
152
|
-
|
|
153
|
-
5. **GraphQL returns null for missing paths.** `object(expression: "branch:path")` returns null silently if the path doesn't exist. No error. `ghx` handles this, but if using `gh api graphql` directly, check for null.
|
|
154
|
-
|
|
155
|
-
6. **Flag ordering in `read` command.** `ghx read owner/repo file --map` works. `ghx read --map owner/repo file` does NOT — repo must be the first positional arg.
|
|
156
|
-
|
|
157
|
-
7. **Not all repos use `main`.** cli/cli uses `trunk`, others use `master`. `ghx` handles this automatically. For raw `gh api` calls, query the default branch first: `gh repo view owner/repo --json defaultBranchRef --jq '.defaultBranchRef.name'`
|
|
158
|
-
|
|
159
|
-
8. **`gh` field names are inconsistent.** `stargazersCount` (search) vs `stargazerCount` (repo view). Always check with `--json` (no fields) to see available fields for any command.
|
|
160
|
-
|
|
161
|
-
9. **`gh api repos/.../contents/` returns base64 by default.** Without `-H "Accept: application/vnd.github.raw+json"`, you get a JSON blob with base64-encoded content. `ghx read` returns plain text via GraphQL — no decoding needed.
|
|
162
|
-
|
|
163
|
-
10. **`gh search repos` and `gh search code` use different rate limit pools.** Repo search: 30/min (generous). Code search: 10/min (restrictive). Don't assume one rate limit applies to both.
|
|
164
|
-
|
|
165
|
-
11. **Unknown flags are rejected, not silently absorbed.** `ghx search "query" --json` exits 2 with a clear error. This is intentional — silent flag absorption was the #1 cause of agent failures (flags like `--limit` would get concatenated into the query string, corrupting it). If you get exit 2, check your flags.
|
|
166
|
-
|
|
167
|
-
## Anti-Patterns
|
|
168
|
-
|
|
169
|
-
- ❌ `web_fetch`/`web_search` on github.com — returns HTML noise, wastes thousands of tokens for zero useful information
|
|
170
|
-
- ❌ `gh api repos/.../contents/<path>` WITHOUT `-H "Accept: application/vnd.github.raw+json"` — returns base64-encoded JSON blob instead of readable text
|
|
171
|
-
- ❌ Reading entire large files when you need 10 lines — use `--grep "pattern"` or `--lines N-M`
|
|
172
|
-
- ❌ Multiple sequential `gh api` calls for explore workflows — use `ghx explore` (1 GraphQL call) or `ghx read` (batch files)
|
|
173
|
-
- ❌ Using web-only qualifiers (`OR`, `NOT`, `symbol:`, regex) in `ghx search` — silently treated as literal text, returns wrong results. ghx warns but can't prevent it
|
|
174
|
-
- ❌ Firing multiple code search requests in parallel — 9 req/min rate limit, you'll get 403s
|
|
175
|
-
- ❌ Dumping entire repos into context for a specific question — use targeted `ghx` commands. Reserve `gitingest`/`repomix` for "understand this whole module" tasks
|
|
176
|
-
- ❌ Relying on `gh search code` for multi-word queries — silently wraps in quotes (exact phrase), returns nothing when words aren't adjacent. Use `ghx search` (AND matching + matching context)
|
|
177
|
-
- ❌ Using `ghx search` to find repos — ghx search is for code. Use `ghx repos "query"` for repo discovery
|
|
178
|
-
- ❌ Using `gh` for batch file reads — 1 API call per file, base64 encoded. Use `ghx read repo f1 f2 f3` (1 GraphQL call, plain text)
|
|
179
|
-
- ❌ Using `gh repo view` to explore a repo — gets metadata but not tree listing or README content in one call. Use `ghx explore` (1 call for all three)
|
|
180
|
-
|
|
181
|
-
## Best Practices
|
|
182
|
-
|
|
183
|
-
- **Batch file reads.** `ghx read owner/repo f1 f2 f3` = 1 API call. Three separate reads = 3 calls.
|
|
184
|
-
- **Map before reading.** `ghx read --map` first to understand structure, then `--grep` or `--lines` for specifics.
|
|
185
|
-
- **Refine search, don't paginate.** If `ghx search` shows "201472 results (showing 30)", add qualifiers (`repo:`, `language:`, `path:`) — don't try to page through. 9 req/min rate limit makes pagination expensive.
|
|
186
|
-
- **Use `--limit` to control token budget.** `ghx repos "query" --limit 5` for quick checks, `--limit 15` for thorough discovery. `ghx search "query" --limit 10` when you only need top results.
|
|
187
|
-
- **Check exit codes.** 0 = got results, 1 = no results (query was valid, broaden it), 2 = usage error (fix your command).
|
|
188
|
-
- **Use `gh api --cache 1h`** for repeated lookups when using raw `gh` commands.
|
|
189
|
-
- **Use `--json fields --jq 'expr'`** on `gh` commands to get structured output and reduce noise.
|
|
190
|
-
- **Piped output is machine-formatted.** Tab-delimited, no truncation, no color codes — agents always get clean output.
|
|
191
|
-
|
|
192
|
-
## The `--map` Flag: Why It Matters
|
|
193
|
-
|
|
194
|
-
`--map` extracts only structural declarations (imports, exports, function/class/type signatures) via per-language regex patterns. Tested on 6 real files across TypeScript, Python, Go:
|
|
195
|
-
|
|
196
|
-
| Metric | Result |
|
|
197
|
-
|--------|--------|
|
|
198
|
-
| Average token reduction | 92% |
|
|
199
|
-
| Files scannable per context window | 7x more than full reads |
|
|
200
|
-
| Implementation | ~15 lines of bash, zero dependencies |
|
|
201
|
-
|
|
202
|
-
Output includes line numbers and token stats:
|
|
203
|
-
```
|
|
204
|
-
=== src/core/parseFile.ts (5544 bytes) ===
|
|
205
|
-
21:import type { RepomixConfigMerged } from '../../config/configSchema.js';
|
|
206
|
-
35:export const CHUNK_SEPARATOR = '⋮----';
|
|
207
|
-
38:export const parseFile = async (fileContent: string, filePath: string, config: RepomixConfigMerged) =>
|
|
208
|
-
107:const getLanguageParserSingleton = async () =>
|
|
209
|
-
# map: 812/5544 chars (~1386 tokens full, ~203 tokens map)
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
Supported: TypeScript/JavaScript, Python, Go, Rust, Java/Kotlin, Ruby. Generic fallback for unknown extensions.
|
|
213
|
-
|
|
214
|
-
## Examples
|
|
215
|
-
|
|
216
|
-
### Simple: Explore a repo and read a file
|
|
217
|
-
|
|
218
|
-
```bash
|
|
219
|
-
# What's in this repo?
|
|
220
|
-
ghx explore plausible/analytics
|
|
221
|
-
|
|
222
|
-
# Read the main config
|
|
223
|
-
ghx read plausible/analytics config/runtime.exs
|
|
224
|
-
```
|
|
225
|
-
|
|
226
|
-
### Advanced: Research a codebase you've never seen
|
|
227
|
-
|
|
228
|
-
```bash
|
|
229
|
-
# 1. Explore structure
|
|
230
|
-
ghx explore yamadashy/repomix
|
|
231
|
-
|
|
232
|
-
# 2. Map the core module — understand what exists (92% fewer tokens)
|
|
233
|
-
ghx read yamadashy/repomix --map src/core/output/outputGenerate.ts src/core/file/fileProcess.ts src/core/treeSitter/parseFile.ts
|
|
234
|
-
|
|
235
|
-
# 3. Found interesting function in map output — grep for usage details
|
|
236
|
-
ghx read yamadashy/repomix --grep "processFiles" src/core/file/fileProcess.ts
|
|
237
|
-
|
|
238
|
-
# 4. Search across the whole repo for a pattern
|
|
239
|
-
ghx search "CHUNK_SEPARATOR repo:yamadashy/repomix"
|
|
240
|
-
# → stderr: "3 results (showing 3)"
|
|
241
|
-
# → stdout: yamadashy/repomix src/core/output/outputGenerate.ts: const CHUNK_SEPARATOR = '⋮----';
|
|
242
|
-
|
|
243
|
-
# 5. Read specific lines of a file you've narrowed down
|
|
244
|
-
ghx read yamadashy/repomix --lines 38-65 src/core/treeSitter/parseFile.ts
|
|
245
|
-
|
|
246
|
-
# 6. If you need the full picture of a subdirectory, escalate:
|
|
247
|
-
# gitingest https://github.com/yamadashy/repomix/tree/main/src/core -i "*.ts" -o - 2>/dev/null
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
## Complementary Tools
|
|
251
|
-
|
|
252
|
-
| Goal | Tool | Why |
|
|
253
|
-
|------|------|-----|
|
|
254
|
-
| Surgical exploration | `ghx` | Batched API calls, zero overhead, targeted extraction |
|
|
255
|
-
| Holistic understanding | `gitingest` / `repomix --compress` | Dump entire module for broad reasoning |
|
|
256
|
-
| PRs, issues, CI | `gh pr view`, `gh issue view`, `gh pr checks` | Purpose-built commands |
|
|
257
|
-
|
|
258
|
-
## ghx vs gh: When to Use What
|
|
259
|
-
|
|
260
|
-
**ghx is a complement to gh, not a replacement.** Use ghx for code exploration. Use gh for everything else.
|
|
261
|
-
|
|
262
|
-
### Use ghx (code exploration)
|
|
263
|
-
|
|
264
|
-
| Task | Command | Why ghx wins |
|
|
265
|
-
|------|---------|-------------|
|
|
266
|
-
| Code search | `ghx search "query"` | AND matching (gh uses exact phrase), matching context, 37x token reduction on minified files, result count + warnings on stderr |
|
|
267
|
-
| Repo search | `ghx repos "query"` | 1 GraphQL call gets name + stars + language + README preview. gh needs 1+N calls for same info, returns worse ranking, no README |
|
|
268
|
-
| Repo overview | `ghx explore owner/repo` | 1 GraphQL call gets description + tree + README (gh needs 3 calls) |
|
|
269
|
-
| Read multiple files | `ghx read owner/repo f1 f2 f3` | 1 GraphQL call for N files (gh needs N calls, returns base64) |
|
|
270
|
-
| Targeted extraction | `ghx read --grep "pat" f` | Built-in grep with context lines — no shell piping |
|
|
271
|
-
| Code map | `ghx read --map f1 f2` | ~92% token reduction, no gh equivalent |
|
|
272
|
-
|
|
273
|
-
### Use gh (everything else)
|
|
274
|
-
|
|
275
|
-
| Task | Command | Why gh wins |
|
|
276
|
-
|------|---------|-------------|
|
|
277
|
-
| Issues | `gh issue list/view -R owner/repo` | ghx doesn't touch issues |
|
|
278
|
-
| Pull requests | `gh pr list/view/diff/checks -R owner/repo` | ghx doesn't touch PRs |
|
|
279
|
-
| Releases | `gh release list -R owner/repo` | ghx doesn't touch releases |
|
|
280
|
-
| Repo metadata | `gh repo view owner/repo --json stargazerCount,forkCount` | Detailed stats beyond what ghx repos shows |
|
|
281
|
-
| Auth | `gh auth login/status` | ghx depends on gh for auth |
|
|
282
|
-
| Create/update | `gh issue create`, `gh pr create` | ghx is read-only |
|
|
283
|
-
|
|
284
|
-
### Rate limits (from GitHub docs)
|
|
285
|
-
|
|
286
|
-
| Endpoint | Limit | Used by |
|
|
287
|
-
|---|---|---|
|
|
288
|
-
| Core REST | 5,000/hour | gh commands, ghx tree |
|
|
289
|
-
| GraphQL | 5,000/hour | ghx explore, ghx read |
|
|
290
|
-
| Search (repos, issues) | 30/min | `gh search repos/issues` |
|
|
291
|
-
| Code search | 10/min (budget 9) | `ghx search`, `gh search code` |
|
|
292
|
-
|
|
293
|
-
Code search is 50x more restricted than core REST. This is why "refine don't paginate" matters for search but not for explore/read.
|
|
294
|
-
|
|
295
|
-
## `gh` CLI Quick Reference
|
|
296
|
-
|
|
297
|
-
```bash
|
|
298
|
-
# Repos
|
|
299
|
-
gh search repos "<query>" -L 10 --json fullName,description,stargazersCount
|
|
300
|
-
gh repo view owner/repo --json defaultBranchRef --jq '.defaultBranchRef.name'
|
|
301
|
-
|
|
302
|
-
# PRs
|
|
303
|
-
gh pr view 123 -R owner/repo # Title, body, status
|
|
304
|
-
gh pr diff 123 -R owner/repo # Full diff
|
|
305
|
-
gh pr checks 123 -R owner/repo # CI status
|
|
306
|
-
|
|
307
|
-
# Issues
|
|
308
|
-
gh issue view 456 -R owner/repo
|
|
309
|
-
gh issue list -R owner/repo -S "query" -L 20
|
|
310
|
-
|
|
311
|
-
# Raw API (always use the raw header for files)
|
|
312
|
-
gh api repos/owner/repo/contents/path -H "Accept: application/vnd.github.raw+json"
|
|
313
|
-
gh api repos/owner/repo/git/trees/main --jq '.tree[].path' # List structure
|
|
314
|
-
```
|
package/v1/ghx
DELETED
|
@@ -1,385 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
# ghx — GitHub code exploration for agents and humans
|
|
3
|
-
# Efficient GitHub repo exploration via GraphQL batching, code maps, and targeted extraction.
|
|
4
|
-
# One command does what takes 3-5 API calls with any other tool.
|
|
5
|
-
|
|
6
|
-
set -euo pipefail
|
|
7
|
-
|
|
8
|
-
VERSION="0.2.1"
|
|
9
|
-
|
|
10
|
-
cmd="${1:-help}"
|
|
11
|
-
shift || true
|
|
12
|
-
|
|
13
|
-
case "$cmd" in
|
|
14
|
-
|
|
15
|
-
# ─── explore: branch + tree + README in one GraphQL call ───
|
|
16
|
-
explore)
|
|
17
|
-
repo="$1"; shift || true
|
|
18
|
-
path="${1:-}"
|
|
19
|
-
owner="${repo%%/*}"
|
|
20
|
-
name="${repo##*/}"
|
|
21
|
-
|
|
22
|
-
# First get default branch
|
|
23
|
-
branch=$(gh repo view "$repo" --json defaultBranchRef --jq '.defaultBranchRef.name')
|
|
24
|
-
|
|
25
|
-
if [[ -z "$path" ]]; then
|
|
26
|
-
# Root exploration: tree + README
|
|
27
|
-
gh api graphql -f query="
|
|
28
|
-
{
|
|
29
|
-
repository(owner: \"$owner\", name: \"$name\") {
|
|
30
|
-
description
|
|
31
|
-
tree: object(expression: \"$branch:\") {
|
|
32
|
-
... on Tree { entries { name type } }
|
|
33
|
-
}
|
|
34
|
-
readme: object(expression: \"$branch:README.md\") {
|
|
35
|
-
... on Blob { text }
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}" --jq '{
|
|
39
|
-
description: .data.repository.description,
|
|
40
|
-
branch: "'"$branch"'",
|
|
41
|
-
files: [.data.repository.tree.entries[] | "\(if .type == "tree" then .name + "/" else .name end)"],
|
|
42
|
-
readme: (.data.repository.readme.text // "(no README.md)")
|
|
43
|
-
}'
|
|
44
|
-
else
|
|
45
|
-
# Subdir exploration: tree listing
|
|
46
|
-
gh api graphql -f query="
|
|
47
|
-
{
|
|
48
|
-
repository(owner: \"$owner\", name: \"$name\") {
|
|
49
|
-
tree: object(expression: \"$branch:$path\") {
|
|
50
|
-
... on Tree { entries { name type } }
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}" --jq '{
|
|
54
|
-
path: "'"$path"'",
|
|
55
|
-
entries: [.data.repository.tree.entries[] | "\(if .type == "tree" then .name + "/" else .name end)"]
|
|
56
|
-
}'
|
|
57
|
-
fi
|
|
58
|
-
;;
|
|
59
|
-
|
|
60
|
-
# ─── read: read 1-10 files in one GraphQL call ───
|
|
61
|
-
# Supports --grep "pattern" to filter output to matching lines (with 2 lines context)
|
|
62
|
-
# Supports --lines N-M to extract specific line ranges
|
|
63
|
-
read)
|
|
64
|
-
repo="$1"; shift
|
|
65
|
-
owner="${repo%%/*}"
|
|
66
|
-
name="${repo##*/}"
|
|
67
|
-
|
|
68
|
-
# Parse flags (must come before file paths)
|
|
69
|
-
grep_pattern=""
|
|
70
|
-
line_range=""
|
|
71
|
-
map_mode=""
|
|
72
|
-
files=()
|
|
73
|
-
while [[ $# -gt 0 ]]; do
|
|
74
|
-
case "$1" in
|
|
75
|
-
--grep) grep_pattern="$2"; shift 2 ;;
|
|
76
|
-
--lines) line_range="$2"; shift 2 ;;
|
|
77
|
-
--map) map_mode=1; shift ;;
|
|
78
|
-
--*) echo "ghx read: unknown flag '$1'" >&2; exit 2 ;;
|
|
79
|
-
*) files+=("$1"); shift ;;
|
|
80
|
-
esac
|
|
81
|
-
done
|
|
82
|
-
|
|
83
|
-
if [[ ${#files[@]} -eq 0 ]]; then
|
|
84
|
-
echo "Usage: ghx read <owner/repo> <path1> [path2] [--grep pattern] [--lines N-M]" >&2
|
|
85
|
-
exit 2
|
|
86
|
-
fi
|
|
87
|
-
|
|
88
|
-
# Get default branch
|
|
89
|
-
branch=$(gh repo view "$repo" --json defaultBranchRef --jq '.defaultBranchRef.name')
|
|
90
|
-
|
|
91
|
-
# Build GraphQL query with aliases
|
|
92
|
-
query="{ repository(owner: \"$owner\", name: \"$name\") {"
|
|
93
|
-
for i in "${!files[@]}"; do
|
|
94
|
-
query+=" f$i: object(expression: \"$branch:${files[$i]}\") { ... on Blob { text byteSize } }"
|
|
95
|
-
done
|
|
96
|
-
query+=" } }"
|
|
97
|
-
|
|
98
|
-
# Execute and format output
|
|
99
|
-
result=$(gh api graphql -f query="$query")
|
|
100
|
-
|
|
101
|
-
for i in "${!files[@]}"; do
|
|
102
|
-
text=$(echo "$result" | jq -r ".data.repository.f$i.text // empty")
|
|
103
|
-
size=$(echo "$result" | jq -r ".data.repository.f$i.byteSize // empty")
|
|
104
|
-
if [[ -n "$text" ]]; then
|
|
105
|
-
echo "=== ${files[$i]} ($size bytes) ==="
|
|
106
|
-
if [[ -n "$grep_pattern" ]]; then
|
|
107
|
-
echo "$text" | grep -n -i -C2 "$grep_pattern" || echo "(no matches for '$grep_pattern')"
|
|
108
|
-
elif [[ -n "$line_range" ]]; then
|
|
109
|
-
start="${line_range%-*}"
|
|
110
|
-
end="${line_range#*-}"
|
|
111
|
-
echo "$text" | sed -n "${start},${end}p"
|
|
112
|
-
elif [[ -n "$map_mode" ]]; then
|
|
113
|
-
ext="${files[$i]##*.}"
|
|
114
|
-
case "$ext" in
|
|
115
|
-
ts|tsx|js|jsx|mjs) pat='^(import |export |const |let |var |function |class |interface |type |enum )' ;;
|
|
116
|
-
py) pat='^(import |from |class |def | def | def |@)' ;;
|
|
117
|
-
go) pat='^(package |import |func |type |var |const )' ;;
|
|
118
|
-
rs) pat='^(use |pub |fn |struct |enum |trait |impl |type |mod |const )' ;;
|
|
119
|
-
java|kt) pat='^(import |public |private |protected |class |interface |enum |@)' ;;
|
|
120
|
-
rb) pat='^(require |class |module |def | def | def )' ;;
|
|
121
|
-
*) pat='^(import |export |function |class |def |func |type |const |pub )' ;;
|
|
122
|
-
esac
|
|
123
|
-
echo "$text" | grep -nE "$pat" || echo "(no signatures detected)"
|
|
124
|
-
chars=$(echo "$text" | wc -c | tr -d ' ')
|
|
125
|
-
map_chars=$(echo "$text" | grep -E "$pat" | wc -c | tr -d ' ')
|
|
126
|
-
echo "# map: ${map_chars}/${chars} chars (~$(( chars / 4 )) tokens full, ~$(( map_chars / 4 )) tokens map)"
|
|
127
|
-
else
|
|
128
|
-
echo "$text"
|
|
129
|
-
fi
|
|
130
|
-
echo ""
|
|
131
|
-
else
|
|
132
|
-
echo "=== ${files[$i]} (not found) ==="
|
|
133
|
-
echo ""
|
|
134
|
-
fi
|
|
135
|
-
done
|
|
136
|
-
;;
|
|
137
|
-
|
|
138
|
-
# ─── search: code search via REST API ───
|
|
139
|
-
search)
|
|
140
|
-
# Parse flags
|
|
141
|
-
full_mode=""
|
|
142
|
-
limit=""
|
|
143
|
-
args=()
|
|
144
|
-
while [[ $# -gt 0 ]]; do
|
|
145
|
-
case "$1" in
|
|
146
|
-
--full) full_mode=1; shift ;;
|
|
147
|
-
--limit) [[ $# -lt 2 ]] && { echo "ghx search: --limit requires a value" >&2; exit 2; }
|
|
148
|
-
limit="$2"; shift 2 ;;
|
|
149
|
-
--*) echo "ghx search: unknown flag '$1'" >&2; exit 2 ;;
|
|
150
|
-
*) args+=("$1"); shift ;;
|
|
151
|
-
esac
|
|
152
|
-
done
|
|
153
|
-
query="${args[*]}"
|
|
154
|
-
if [[ -z "$query" ]]; then
|
|
155
|
-
echo "Usage: ghx search <query> [--full] [--limit N]" >&2
|
|
156
|
-
echo "Examples: 'useState repo:owner/repo' | 'path:src/ extension:tsx language:typescript'" >&2
|
|
157
|
-
exit 2
|
|
158
|
-
fi
|
|
159
|
-
# Validate and clamp limit (API max: 100)
|
|
160
|
-
if [[ -n "$limit" ]]; then
|
|
161
|
-
[[ "$limit" =~ ^[0-9]+$ ]] || { echo "ghx search: --limit must be a number, got '$limit'" >&2; exit 2; }
|
|
162
|
-
(( limit > 100 )) && { echo "⚠ --limit clamped to 100 (API max)" >&2; limit=100; }
|
|
163
|
-
fi
|
|
164
|
-
|
|
165
|
-
# Prerequisite checks
|
|
166
|
-
if ! command -v gh &>/dev/null; then
|
|
167
|
-
echo "ghx requires the GitHub CLI (gh). Install: https://cli.github.com" >&2
|
|
168
|
-
exit 2
|
|
169
|
-
fi
|
|
170
|
-
if ! gh auth status &>/dev/null; then
|
|
171
|
-
echo "GitHub code search requires authentication. Run: gh auth login" >&2
|
|
172
|
-
exit 2
|
|
173
|
-
fi
|
|
174
|
-
|
|
175
|
-
# Warn on web-only qualifiers (they silently become literal text in REST API)
|
|
176
|
-
for pat in 'symbol:' 'content:' 'is:' ' OR ' ' NOT ' '/.*/' 'enterprise:'; do
|
|
177
|
-
if [[ "$query" == *$pat* ]]; then
|
|
178
|
-
echo "⚠ '$pat' is web-only (Blackbird) — REST API treats it as literal text" >&2
|
|
179
|
-
fi
|
|
180
|
-
done
|
|
181
|
-
|
|
182
|
-
# Search with text_matches for matching context
|
|
183
|
-
per_page="${limit:-30}"
|
|
184
|
-
response=$(gh api /search/code --method GET \
|
|
185
|
-
-H "Accept: application/vnd.github.text-match+json" \
|
|
186
|
-
-f q="$query" -f per_page="$per_page" 2>&1) || {
|
|
187
|
-
echo "$response" >&2
|
|
188
|
-
exit 1
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
# Result count + incomplete warning to stderr
|
|
192
|
-
total=$(echo "$response" | jq -r '.total_count')
|
|
193
|
-
incomplete=$(echo "$response" | jq -r '.incomplete_results')
|
|
194
|
-
count=$(echo "$response" | jq '.items | length')
|
|
195
|
-
echo "$total results (showing $count)" >&2
|
|
196
|
-
[[ "$incomplete" == "true" ]] && echo "⚠ Results may be incomplete (query timed out)" >&2
|
|
197
|
-
[[ "$total" -gt 1000 ]] 2>/dev/null && echo "⚠ Query too broad — add repo:, language:, or path: to narrow" >&2
|
|
198
|
-
[[ "$count" -eq 0 ]] && exit 1
|
|
199
|
-
|
|
200
|
-
# Output: repo path: matching context from fragment
|
|
201
|
-
# Default: 200 char window centered on the match (prevents minified JS token explosion)
|
|
202
|
-
# Uses matches[0].indices to find the actual match position in the fragment
|
|
203
|
-
if [[ -n "$full_mode" ]]; then
|
|
204
|
-
echo "$response" | jq -r '
|
|
205
|
-
.items[] |
|
|
206
|
-
.repository.full_name as $repo |
|
|
207
|
-
.path as $path |
|
|
208
|
-
(.text_matches // []) as $tm |
|
|
209
|
-
if ($tm | length) > 0 then
|
|
210
|
-
($tm[0].fragment | gsub("\\n"; " ") | gsub("\\s+"; " ") |
|
|
211
|
-
gsub("^\\s+"; "") | gsub("\\s+$"; "")) as $line |
|
|
212
|
-
"\($repo) \($path): \($line)"
|
|
213
|
-
else
|
|
214
|
-
"\($repo) \($path)"
|
|
215
|
-
end'
|
|
216
|
-
else
|
|
217
|
-
truncated=$(echo "$response" | jq '[.items[] |
|
|
218
|
-
(.text_matches // []) as $tm |
|
|
219
|
-
if ($tm | length) > 0 then
|
|
220
|
-
($tm[0].fragment | length) > 200
|
|
221
|
-
else false end] | any')
|
|
222
|
-
[[ "$truncated" == "true" ]] && echo "⚠ Lines truncated to 200 chars (use --full for complete fragments)" >&2
|
|
223
|
-
echo "$response" | jq -r '
|
|
224
|
-
.items[] |
|
|
225
|
-
.repository.full_name as $repo |
|
|
226
|
-
.path as $path |
|
|
227
|
-
(.text_matches // []) as $tm |
|
|
228
|
-
if ($tm | length) > 0 then
|
|
229
|
-
$tm[0] as $m |
|
|
230
|
-
($m.fragment | gsub("\\n"; " ") | gsub("\\s+"; " ")) as $flat |
|
|
231
|
-
(if ($m.matches | length) > 0 then
|
|
232
|
-
$m.matches[0].indices[0] as $start |
|
|
233
|
-
([$start - 80, 0] | max) as $from |
|
|
234
|
-
($from + 200) as $to |
|
|
235
|
-
(if $from > 0 then "…" else "" end) as $prefix |
|
|
236
|
-
(if $to < ($flat | length) then "…" else "" end) as $suffix |
|
|
237
|
-
$prefix + ($flat[$from:$to] | gsub("^\\s+"; "") | gsub("\\s+$"; "")) + $suffix
|
|
238
|
-
else
|
|
239
|
-
$flat[:200]
|
|
240
|
-
end) as $line |
|
|
241
|
-
"\($repo) \($path): \($line)"
|
|
242
|
-
else
|
|
243
|
-
"\($repo) \($path)"
|
|
244
|
-
end'
|
|
245
|
-
fi
|
|
246
|
-
;;
|
|
247
|
-
|
|
248
|
-
# ─── repos: search repos with README preview in 1 GraphQL call ───
|
|
249
|
-
repos)
|
|
250
|
-
limit=""
|
|
251
|
-
args=()
|
|
252
|
-
while [[ $# -gt 0 ]]; do
|
|
253
|
-
case "$1" in
|
|
254
|
-
--limit) [[ $# -lt 2 ]] && { echo "ghx repos: --limit requires a value" >&2; exit 2; }
|
|
255
|
-
limit="$2"; shift 2 ;;
|
|
256
|
-
--*) echo "ghx repos: unknown flag '$1'" >&2; exit 2 ;;
|
|
257
|
-
*) args+=("$1"); shift ;;
|
|
258
|
-
esac
|
|
259
|
-
done
|
|
260
|
-
query="${args[*]}"
|
|
261
|
-
if [[ -z "$query" ]]; then
|
|
262
|
-
echo "Usage: ghx repos <query> [--limit N]" >&2
|
|
263
|
-
exit 2
|
|
264
|
-
fi
|
|
265
|
-
# Validate and clamp limit (GraphQL max: 100, but README fetching makes >20 expensive)
|
|
266
|
-
first="${limit:-10}"
|
|
267
|
-
[[ "$first" =~ ^[0-9]+$ ]] || { echo "ghx repos: --limit must be a number, got '$first'" >&2; exit 2; }
|
|
268
|
-
(( first > 20 )) && { echo "⚠ --limit clamped to 20 (README fetching makes larger values slow)" >&2; first=20; }
|
|
269
|
-
|
|
270
|
-
response=$(gh api graphql -f query='
|
|
271
|
-
{
|
|
272
|
-
search(query: "'"$query"'", type: REPOSITORY, first: '"$first"') {
|
|
273
|
-
repositoryCount
|
|
274
|
-
nodes {
|
|
275
|
-
... on Repository {
|
|
276
|
-
nameWithOwner
|
|
277
|
-
description
|
|
278
|
-
stargazerCount
|
|
279
|
-
primaryLanguage { name }
|
|
280
|
-
object(expression: "HEAD:README.md") {
|
|
281
|
-
... on Blob { text }
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}')
|
|
287
|
-
|
|
288
|
-
echo "$response" | jq -r '.data.search.repositoryCount | "\(.) repos found"' >&2
|
|
289
|
-
count=$(echo "$response" | jq '.data.search.nodes | length')
|
|
290
|
-
[[ "$count" -eq 0 ]] && exit 1
|
|
291
|
-
echo "$response" | jq -r '
|
|
292
|
-
.data.search.nodes[] |
|
|
293
|
-
.nameWithOwner as $name |
|
|
294
|
-
(.stargazerCount | tostring) as $stars |
|
|
295
|
-
((.primaryLanguage.name // "?")) as $lang |
|
|
296
|
-
(.description // "") as $desc |
|
|
297
|
-
((.object.text // "") | gsub("\\n"; " ") | gsub("\\s+"; " ") | gsub("^\\s+"; "") |
|
|
298
|
-
gsub("\\[!\\[[^]]*\\]\\([^)]*\\)\\]\\([^)]*\\)"; "") |
|
|
299
|
-
gsub("!\\[[^]]*\\]\\([^)]*\\)"; "") |
|
|
300
|
-
gsub("\\[!\\[[^]]*\\]\\([^)]*\\)\\]"; "") |
|
|
301
|
-
gsub("\\[![^]]*\\]\\([^)]*\\)"; "") |
|
|
302
|
-
gsub("<img[^>]*>"; "") |
|
|
303
|
-
gsub("<div[^>]*>"; "") | gsub("</div>"; "") |
|
|
304
|
-
gsub("<br[^>]*/?>"; "") |
|
|
305
|
-
gsub("<p[^>]*>"; "") | gsub("</p>"; "") |
|
|
306
|
-
gsub("<a[^>]*>"; "") | gsub("</a>"; "") |
|
|
307
|
-
gsub("\\s+"; " ") | gsub("^\\s+"; "") |
|
|
308
|
-
.[:300]) as $readme |
|
|
309
|
-
"\($name) (\($stars)★ \($lang)) \($desc)" +
|
|
310
|
-
(if ($readme | length) > 0 then "\n " + $readme + (if ($readme | length) >= 300 then "…" else "" end) else "" end)
|
|
311
|
-
'
|
|
312
|
-
;;
|
|
313
|
-
|
|
314
|
-
# ─── skill: output SKILL.md for agent context injection ───
|
|
315
|
-
skill)
|
|
316
|
-
script_dir="$(dirname "$(readlink -f "$0")")"
|
|
317
|
-
skill_file="$script_dir/SKILL.md"
|
|
318
|
-
if [[ -f "$skill_file" ]]; then
|
|
319
|
-
cat "$skill_file"
|
|
320
|
-
else
|
|
321
|
-
echo "SKILL.md not found at $skill_file" >&2
|
|
322
|
-
exit 1
|
|
323
|
-
fi
|
|
324
|
-
;;
|
|
325
|
-
|
|
326
|
-
# ─── tree: recursive tree listing via REST API ───
|
|
327
|
-
tree)
|
|
328
|
-
repo="$1"; shift || true
|
|
329
|
-
path="${1:-}"
|
|
330
|
-
owner="${repo%%/*}"
|
|
331
|
-
name="${repo##*/}"
|
|
332
|
-
|
|
333
|
-
branch=$(gh repo view "$repo" --json defaultBranchRef --jq '.defaultBranchRef.name')
|
|
334
|
-
|
|
335
|
-
gh api "repos/$owner/$name/git/trees/$branch?recursive=1" --jq '
|
|
336
|
-
[.tree[] | select(.type == "blob") | .path] |
|
|
337
|
-
if "'"$path"'" != "" then
|
|
338
|
-
[.[] | select(startswith("'"$path"'/"))] |
|
|
339
|
-
map(ltrimstr("'"$path"'/"))
|
|
340
|
-
else . end |
|
|
341
|
-
.[]
|
|
342
|
-
'
|
|
343
|
-
;;
|
|
344
|
-
|
|
345
|
-
# ─── help ───
|
|
346
|
-
version|-v|--version)
|
|
347
|
-
echo "ghx $VERSION"
|
|
348
|
-
;;
|
|
349
|
-
|
|
350
|
-
help|*)
|
|
351
|
-
cat <<'EOF'
|
|
352
|
-
ghx — GitHub code exploration for agents and humans
|
|
353
|
-
|
|
354
|
-
Commands:
|
|
355
|
-
ghx explore <owner/repo> [path] Branch + tree + README in 1 API call
|
|
356
|
-
ghx read <owner/repo> <f1> [f2..] Read 1-10 files in 1 API call
|
|
357
|
-
ghx repos <query> [--limit N] Search repos with README preview (default: 10)
|
|
358
|
-
ghx search "<query>" [--limit N] Code search (AND matching, default: 30)
|
|
359
|
-
ghx search --full "<query>" Code search without line truncation
|
|
360
|
-
ghx skill Output SKILL.md (for agent context injection)
|
|
361
|
-
ghx tree <owner/repo> [path] Full recursive tree listing
|
|
362
|
-
|
|
363
|
-
Read flags:
|
|
364
|
-
--grep <pattern> Filter output to matching lines (case-insensitive, 2 lines context)
|
|
365
|
-
--lines <N-M> Extract specific line range
|
|
366
|
-
--map Structural signatures only (~92% token reduction)
|
|
367
|
-
|
|
368
|
-
Exit codes:
|
|
369
|
-
0 Success with results
|
|
370
|
-
1 No results (query valid, nothing matched)
|
|
371
|
-
2 Usage error (bad flags, missing args)
|
|
372
|
-
|
|
373
|
-
Examples:
|
|
374
|
-
ghx explore plausible/analytics
|
|
375
|
-
ghx read plausible/analytics mix.exs assets/js/dashboard/stats/bar.js
|
|
376
|
-
ghx read plausible/analytics src/app.ts --grep "useState"
|
|
377
|
-
ghx read plausible/analytics src/app.ts --lines 42-80
|
|
378
|
-
ghx repos "react state management"
|
|
379
|
-
ghx search "useState repo:plausible/analytics"
|
|
380
|
-
ghx search "filename:package.json repo:plausible/analytics"
|
|
381
|
-
ghx tree plausible/analytics assets/js
|
|
382
|
-
EOF
|
|
383
|
-
;;
|
|
384
|
-
|
|
385
|
-
esac
|