@jc_stack/ez-github 0.1.0-beta.7 → 0.1.0-beta.8
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/CHANGELOG.md +5 -0
- package/README.md +24 -2
- package/bin/github.mjs +9 -6
- package/bin/repos.mjs +213 -0
- package/ez-plugin.json +1 -1
- package/package.json +2 -2
- package/scripts/release-check.mjs +2 -0
- package/skills/github/SKILL.md +17 -0
- package/test/repos.test.mjs +280 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.1.0-beta.8
|
|
4
|
+
|
|
5
|
+
- Bound the repositories volume. `ez github repos clone OWNER/REPO [NAME] [--path DIR]...` makes a partial, sparse clone (history without file contents, working tree of the root files plus the listed directories): a 2.3 GiB data repository clones in about 17 MiB, and a research commit adds about 1 MiB. `repos list` shows each clone's size and whether it is reclaimable; `repos reclaim` removes clones that are idle (default 24 hours), have no uncommitted, untracked or ignored files (regenerable caches excepted), no stash, submodules or embedded repositories, and have every local branch commit on a remote-tracking ref; it inspects each clone again right before removal and keeps everything else or uncertain.
|
|
6
|
+
- Every `git clone` and `gh repo clone` through this plugin first reclaims, then fails loudly when `/repos` is over its budget (default 16 GiB, `EZ_GITHUB_REPOS_BUDGET_GIB`). Other git and gh commands, arguments and exit codes are unchanged. Fixes clones accumulating forever (91 GiB of full clones on one agent).
|
|
7
|
+
|
|
3
8
|
## 0.1.0-beta.7
|
|
4
9
|
|
|
5
10
|
- Run the runtime image on the October 2026 Node 22 security release (`node:22.23.4-bookworm-slim`, digest pinned); no CLI behavior changes.
|
package/README.md
CHANGED
|
@@ -4,8 +4,8 @@ Thin native GitHub CLI and Git plugin for Ez. Private per-agent profile and writ
|
|
|
4
4
|
repository volume; no provider API implementation or workflow engine.
|
|
5
5
|
|
|
6
6
|
Requires Node 22+, Docker/Compose and Ez with the cancellation fix (manager release
|
|
7
|
-
0.1.0-beta.60 or newer). Obtain `@jc_stack/ez-github@0.1.0-beta.
|
|
8
|
-
`npm pack @jc_stack/ez-github@0.1.0-beta.
|
|
7
|
+
0.1.0-beta.60 or newer). Obtain `@jc_stack/ez-github@0.1.0-beta.8` with
|
|
8
|
+
`npm pack @jc_stack/ez-github@0.1.0-beta.8 --ignore-scripts`, extract into a permanent
|
|
9
9
|
private tools directory, and inspect the extracted package.
|
|
10
10
|
|
|
11
11
|
Inspect this directory with `ez plugins inspect github --source /absolute/package`,
|
|
@@ -36,6 +36,28 @@ machine/reboot and a fresh OAuth consent in that environment remain stable-relea
|
|
|
36
36
|
Requires Ez core 0.1.0-beta.60 or newer for the manifest example.
|
|
37
37
|
Older cores refuse the update and retain the installed version.
|
|
38
38
|
|
|
39
|
+
## Bounded repository clones
|
|
40
|
+
|
|
41
|
+
Agents clone into `/repos`; nothing used to remove those clones, so full clones of a large
|
|
42
|
+
data repository (2.3 GiB each) accumulated to 91 GiB. The plugin now bounds the volume itself:
|
|
43
|
+
|
|
44
|
+
- `ez github repos clone OWNER/REPO [NAME] [--path DIR]... [--branch BRANCH]` clones with
|
|
45
|
+
`--filter=blob:none --sparse`, materialising only the root files and the `--path`
|
|
46
|
+
directories. Measured on a 2.3 GiB repository: 17 MiB root only, 18 MiB after an add-only
|
|
47
|
+
research commit, 23 MiB with a 3 MiB directory checked out. Add new files outside the
|
|
48
|
+
checkout with `git add --sparse FILE`. Plain `git clone` remains for work that needs the whole tree.
|
|
49
|
+
- `ez github repos reclaim [--idle-hours N]` removes clones that are idle (default 24 hours),
|
|
50
|
+
have no uncommitted, untracked or ignored files (caches such as `__pycache__`, `.pytest_cache`,
|
|
51
|
+
`node_modules` and `.venv` excepted), no stash, no submodules or embedded repositories, and have every
|
|
52
|
+
local branch commit (and HEAD) reachable from a remote-tracking ref. The remote is not asked
|
|
53
|
+
again, so a branch deleted there after its push (the usual end of a merged research branch)
|
|
54
|
+
still counts as pushed; tags are not considered. Uncertain clones are kept, and each clone is
|
|
55
|
+
inspected again right before removal. `ez github repos list` shows the same decision per
|
|
56
|
+
clone without removing.
|
|
57
|
+
- Every `git clone` and `gh repo clone` through the plugin reclaims first, then refuses while
|
|
58
|
+
`/repos` is over its budget (default 16 GiB; set `EZ_GITHUB_REPOS_BUDGET_GIB` in the
|
|
59
|
+
service environment to change it). This is a bound, not a security boundary.
|
|
60
|
+
|
|
39
61
|
## Kanban
|
|
40
62
|
|
|
41
63
|
`ez kanban` gives agents basic task-board operations: `board`, `list`, `show`,
|
package/bin/github.mjs
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {spawn} from 'node:child_process';
|
|
3
3
|
import {pathToFileURL} from 'node:url';
|
|
4
|
+
import {cleanEnv,isClone,prepareClone,repos} from './repos.mjs';
|
|
4
5
|
|
|
5
6
|
export function command(args) {
|
|
6
7
|
const [tool,...rest]=args;
|
|
7
8
|
if(tool==='gh'||tool==='git') return [tool,rest];
|
|
8
9
|
if(tool==='doctor') return ['gh',['auth','status','--hostname','github.com']];
|
|
9
|
-
throw Error('Use --help, --version, doctor, gh <args>, or git <args>');
|
|
10
|
+
throw Error('Use --help, --version, doctor, repos, gh <args>, or git <args>');
|
|
10
11
|
}
|
|
11
12
|
if(process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href) {
|
|
12
13
|
process.umask(0o077);
|
|
13
14
|
const args=process.argv.slice(2);
|
|
14
|
-
if(args[0]==='--version') console.log('ez-github 0.1.0-beta.
|
|
15
|
-
else if(!args.length||args[0]==='--help') console.log('ez github doctor | gh <native arguments> | git <native arguments
|
|
16
|
-
else {
|
|
15
|
+
if(args[0]==='--version') console.log('ez-github 0.1.0-beta.8');
|
|
16
|
+
else if(!args.length||args[0]==='--help') console.log('ez github doctor | gh <native arguments> | git <native arguments> | repos clone|list|reclaim\nPrivate repositories: /repos. Native stdin, output and exit codes. Clones are bounded: see ez github repos --help. Read skills/github/SKILL.md for OAuth onboarding.');
|
|
17
|
+
else if(args[0]==='repos') {
|
|
18
|
+
try {await repos(args.slice(1));} catch(error) {console.error(error.message);process.exitCode=1;}
|
|
19
|
+
} else {
|
|
17
20
|
try {
|
|
18
21
|
const [binary,argv]=command(args);
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
if(isClone(args)) await prepareClone();
|
|
23
|
+
const env=cleanEnv();
|
|
21
24
|
const child=spawn(binary,argv,{stdio:'inherit',env,detached:true});
|
|
22
25
|
let timer;
|
|
23
26
|
const stop=signal=>{
|
package/bin/repos.mjs
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
// Plugin-owned bound on /repos: sparse partial clones, reclaim of finished clones, size budget.
|
|
2
|
+
import {execFile} from 'node:child_process';
|
|
3
|
+
import {readdir, rename, rm, stat} from 'node:fs/promises';
|
|
4
|
+
import {basename, join, posix} from 'node:path';
|
|
5
|
+
import {promisify, parseArgs} from 'node:util';
|
|
6
|
+
|
|
7
|
+
const run = promisify(execFile);
|
|
8
|
+
const GIB = 1024 ** 3, HOUR = 3600e3;
|
|
9
|
+
export const DEFAULT_IDLE_HOURS = 24, DEFAULT_BUDGET_GIB = 16;
|
|
10
|
+
const TOKENS = ['GH_TOKEN', 'GITHUB_TOKEN', 'GH_ENTERPRISE_TOKEN', 'GITHUB_ENTERPRISE_TOKEN'];
|
|
11
|
+
// Files git touches on commit, checkout, add, fetch and pull; the newest mtime is the clone's last activity.
|
|
12
|
+
const ACTIVITY = ['HEAD', 'index', 'logs/HEAD', 'FETCH_HEAD', 'ORIG_HEAD', 'packed-refs'];
|
|
13
|
+
// Ignored directories that a build or script recreates; every other ignored file is treated as work.
|
|
14
|
+
const REGENERABLE = new Set(['__pycache__', '.pytest_cache', 'node_modules', '.venv']);
|
|
15
|
+
|
|
16
|
+
export const reposDir = () => process.env.EZ_GITHUB_REPOS_DIR || '/repos';
|
|
17
|
+
export function cleanEnv() {
|
|
18
|
+
const env = {...process.env};
|
|
19
|
+
for (const key of TOKENS) delete env[key];
|
|
20
|
+
return env;
|
|
21
|
+
}
|
|
22
|
+
export function budgetBytes(env = process.env) {
|
|
23
|
+
const raw = env.EZ_GITHUB_REPOS_BUDGET_GIB;
|
|
24
|
+
const gib = raw === undefined || raw === '' ? DEFAULT_BUDGET_GIB : Number(raw);
|
|
25
|
+
if (!Number.isFinite(gib) || gib <= 0) throw Error('EZ_GITHUB_REPOS_BUDGET_GIB must be a positive number');
|
|
26
|
+
return gib * GIB;
|
|
27
|
+
}
|
|
28
|
+
const fmt = bytes => bytes >= GIB ? `${(bytes / GIB).toFixed(1)} GiB` : `${(bytes / 1048576).toFixed(1)} MiB`;
|
|
29
|
+
const idleHoursOf = value => {
|
|
30
|
+
const hours = value === undefined ? DEFAULT_IDLE_HOURS : Number(value);
|
|
31
|
+
if (!Number.isFinite(hours) || hours < 0) throw Error('--idle-hours must be a number of hours, 0 or more');
|
|
32
|
+
return hours;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// True for any command that creates a clone: git [global options] clone, gh repo clone.
|
|
36
|
+
export function isClone(args) {
|
|
37
|
+
const [tool, ...rest] = args;
|
|
38
|
+
if (tool === 'gh') return rest[0] === 'repo' && (rest[1] === 'clone' || (['fork', 'create'].includes(rest[1]) && rest.some(arg => arg === '--clone' || arg === '--clone=true' || arg === '-c')));
|
|
39
|
+
if (tool !== 'git') return false;
|
|
40
|
+
const withValue = new Set(['-c', '-C', '--git-dir', '--work-tree', '--namespace', '--super-prefix', '--config-env']);
|
|
41
|
+
let i = 0;
|
|
42
|
+
while (rest[i]?.startsWith('-')) i += withValue.has(rest[i]) ? 2 : 1;
|
|
43
|
+
return rest[i] === 'clone';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Inspection never writes: no optional locks, so status cannot refresh the index and look like activity.
|
|
47
|
+
// GIT_NO_LAZY_FETCH only exists from git 2.44; status.renames is off so a partial clone never fetches blobs to detect renames.
|
|
48
|
+
const inspectGit = (path, ...args) => run('git', ['--no-optional-locks', '-C', path, ...args], {
|
|
49
|
+
env: {...cleanEnv(), GIT_NO_LAZY_FETCH: '1', GIT_TERMINAL_PROMPT: '0'}, maxBuffer: 64 * 1024 * 1024, timeout: 120e3, // a wedged clone is kept, never waited on forever
|
|
50
|
+
});
|
|
51
|
+
const succeeds = promise => promise.then(() => true, () => false);
|
|
52
|
+
|
|
53
|
+
async function lastActivity(path) {
|
|
54
|
+
let newest = 0;
|
|
55
|
+
for (const file of ACTIVITY) {
|
|
56
|
+
try { newest = Math.max(newest, (await stat(join(path, '.git', file))).mtimeMs); } catch { /* absent */ }
|
|
57
|
+
}
|
|
58
|
+
return newest;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function inspect(path, now, idleMs) {
|
|
62
|
+
const keep = (reason, active) => ({state: 'keep', reason, lastActivity: active ? new Date(active).toISOString() : null});
|
|
63
|
+
try {
|
|
64
|
+
if (!(await stat(join(path, '.git'))).isDirectory()) return keep('not a standalone git clone');
|
|
65
|
+
} catch { return keep('not a git clone'); }
|
|
66
|
+
const active = await lastActivity(path);
|
|
67
|
+
if (now - active < idleMs) return keep('recent activity', active);
|
|
68
|
+
try {
|
|
69
|
+
if ((await readdir(join(path, '.git', 'worktrees')).catch(() => [])).length) return keep('has linked worktrees', active);
|
|
70
|
+
if (!(await succeeds(inspectGit(path, 'rev-parse', '-q', '--verify', 'HEAD')))) return keep('no commits yet', active); // also a clone still being created
|
|
71
|
+
// Commits inside a submodule or an embedded repository (a gitlink in the index) are not checked, so keep the clone.
|
|
72
|
+
if ((await readdir(join(path, '.git', 'modules')).catch(() => [])).length || /^160000 /m.test((await inspectGit(path, 'ls-files', '--stage')).stdout)) return keep('has submodules or embedded repositories', active);
|
|
73
|
+
// Ignored files count: build output, notes and .env files are work that exists nowhere else. Regenerable caches do not.
|
|
74
|
+
const status = (await inspectGit(path, '-c', 'status.renames=false', 'status', '--porcelain', '--ignored', '--untracked-files=normal')).stdout;
|
|
75
|
+
if (status.split('\n').filter(Boolean).some(line => !(line.startsWith('!! ') && line.slice(3).split('/').some(part => REGENERABLE.has(part))))) return keep('uncommitted, untracked or ignored files', active);
|
|
76
|
+
if (await succeeds(inspectGit(path, 'rev-parse', '-q', '--verify', 'refs/stash'))) return keep('has a stash', active);
|
|
77
|
+
// "On a remote" means a remote-tracking ref records it. The remote is not asked again: merged branches are routinely deleted there.
|
|
78
|
+
const unpushed = (await inspectGit(path, 'rev-list', '--max-count=1', '--branches', 'HEAD', '--not', '--remotes')).stdout.trim();
|
|
79
|
+
if (unpushed) return keep('has commits missing from the remote', active);
|
|
80
|
+
return {state: 'reclaimable', reason: null, lastActivity: new Date(active).toISOString()};
|
|
81
|
+
} catch (error) {
|
|
82
|
+
return keep(`could not inspect: ${error.message.split('\n')[0]}`, active);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export async function diskBytes(...paths) {
|
|
87
|
+
const sizes = new Map();
|
|
88
|
+
if (!paths.length) return sizes;
|
|
89
|
+
let stdout;
|
|
90
|
+
try { ({stdout} = await run('du', ['-sk', ...paths], {maxBuffer: 16 * 1024 * 1024})); } catch (error) {
|
|
91
|
+
if (error.stdout) stdout = error.stdout; // unreadable entries: report what du could count
|
|
92
|
+
else if (!(await Promise.all(paths.map(path => stat(path).then(() => true, () => false)))).some(Boolean)) return sizes; // all removed by a concurrent reclaim
|
|
93
|
+
else throw error;
|
|
94
|
+
}
|
|
95
|
+
for (const line of stdout.split('\n').filter(Boolean)) {
|
|
96
|
+
const tab = line.indexOf('\t');
|
|
97
|
+
sizes.set(line.slice(tab + 1), Number(line.slice(0, tab)) * 1024);
|
|
98
|
+
}
|
|
99
|
+
return sizes;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function listClones(dir, {now = Date.now(), idleHours = DEFAULT_IDLE_HOURS} = {}) {
|
|
103
|
+
const entries = await readdir(dir, {withFileTypes: true}).catch(error => {
|
|
104
|
+
if (error.code === 'ENOENT') return [];
|
|
105
|
+
throw error;
|
|
106
|
+
});
|
|
107
|
+
const clones = [];
|
|
108
|
+
for (const entry of entries) {
|
|
109
|
+
if (!entry.isDirectory() || entry.name.startsWith('.')) continue;
|
|
110
|
+
const path = join(dir, entry.name);
|
|
111
|
+
clones.push({name: entry.name, path, ...await inspect(path, now, idleHours * HOUR)});
|
|
112
|
+
}
|
|
113
|
+
return clones;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Removes clones that are idle, clean (ignored files too), stash-free and fully on a remote. Anything uncertain is kept.
|
|
117
|
+
export async function reclaim(dir, {now = Date.now(), idleHours = DEFAULT_IDLE_HOURS, afterListing} = {}) {
|
|
118
|
+
for (const entry of await readdir(dir).catch(() => [])) { // finish a removal that was interrupted
|
|
119
|
+
if (entry.startsWith('.reclaim-')) await rm(join(dir, entry), {recursive: true, force: true}).catch(() => {});
|
|
120
|
+
}
|
|
121
|
+
const clones = await listClones(dir, {now, idleHours});
|
|
122
|
+
await afterListing?.(); // test seam: lets a test act between the first pass and the removals
|
|
123
|
+
const removed = [], kept = clones.filter(clone => clone.state !== 'reclaimable');
|
|
124
|
+
for (const clone of clones.filter(item => item.state === 'reclaimable')) {
|
|
125
|
+
// The pass above can take minutes on many large clones: look again right before removing, so work started meanwhile is kept.
|
|
126
|
+
const again = await inspect(clone.path, now, idleHours * HOUR);
|
|
127
|
+
if (again.state !== 'reclaimable') { kept.push({...clone, ...again}); continue; }
|
|
128
|
+
const trash = join(dir, `.reclaim-${clone.name}-${process.pid}`);
|
|
129
|
+
try { await rename(clone.path, trash); } catch (error) {
|
|
130
|
+
if (error.code === 'ENOENT') continue; // a concurrent reclaim took it
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
removed.push({name: clone.name, bytes: (await diskBytes(trash)).get(trash) ?? 0});
|
|
134
|
+
await rm(trash, {recursive: true, force: true});
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
idleHours,
|
|
138
|
+
removed,
|
|
139
|
+
freedBytes: removed.reduce((sum, item) => sum + item.bytes, 0),
|
|
140
|
+
kept: kept.map(({name, reason}) => ({name, reason})),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Runs before every clone through this plugin: reclaim first, then refuse loudly when /repos is over budget.
|
|
145
|
+
export async function prepareClone(dir = reposDir(), {budget = budgetBytes(), now, idleHours} = {}) {
|
|
146
|
+
if (!(await stat(dir).catch(() => null))) return;
|
|
147
|
+
const result = await reclaim(dir, {now, idleHours});
|
|
148
|
+
if (result.removed.length) console.error(`ez-github: reclaimed ${result.removed.length} finished clone(s), freed ${fmt(result.freedBytes)}`);
|
|
149
|
+
const used = (await diskBytes(dir)).get(dir);
|
|
150
|
+
if (used === undefined) throw Error(`Could not measure ${dir}`);
|
|
151
|
+
if (used <= budget) return;
|
|
152
|
+
const sizes = await diskBytes(...result.kept.map(clone => join(dir, clone.name)));
|
|
153
|
+
const largest = result.kept
|
|
154
|
+
.map(clone => ({...clone, bytes: sizes.get(join(dir, clone.name)) ?? 0}))
|
|
155
|
+
.sort((a, b) => b.bytes - a.bytes).slice(0, 5)
|
|
156
|
+
.map(clone => `${clone.name} ${fmt(clone.bytes)} (${clone.reason})`).join('; ');
|
|
157
|
+
throw Error(`${dir} uses ${fmt(used)}, over its ${fmt(budget)} budget, and no clone is reclaimable. Largest kept: ${largest || 'none'}. Push or discard work in them; use "ez github repos reclaim --idle-hours 0" for pushed clones that are still recent; clone with "ez github repos clone" to keep new clones small.`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const safePath = path => {
|
|
161
|
+
const normal = posix.normalize(path);
|
|
162
|
+
return path && !path.startsWith('-') && !normal.startsWith('/') && normal !== '.' && !normal.split('/').includes('..');
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export async function cloneRepo(argv, dir = reposDir()) {
|
|
166
|
+
const {values, positionals} = parseArgs({args: argv, allowPositionals: true, options: {path: {type: 'string', multiple: true}, branch: {type: 'string'}}});
|
|
167
|
+
const [source, given, extra] = positionals;
|
|
168
|
+
if (!source || extra !== undefined) throw Error('Use: repos clone OWNER/REPO|URL [NAME] [--path DIR]... [--branch BRANCH]');
|
|
169
|
+
const url = /^[\w.-]+\/[\w.-]+$/.test(source) ? `https://github.com/${source}.git` : source;
|
|
170
|
+
if (!/^(https|file):\/\//.test(url)) throw Error('Source must be OWNER/REPO or an https:// URL');
|
|
171
|
+
const name = given ?? basename(url).replace(/\.git$/, '');
|
|
172
|
+
if (!/^[A-Za-z0-9][\w.-]*$/.test(name) || name.includes('..')) throw Error('NAME must be a plain directory name');
|
|
173
|
+
const paths = values.path ?? [];
|
|
174
|
+
if (!paths.every(safePath)) throw Error('--path must be a relative directory inside the repository');
|
|
175
|
+
if (values.branch?.startsWith('-')) throw Error('--branch must be a branch or tag name');
|
|
176
|
+
const target = join(dir, name);
|
|
177
|
+
if (await stat(target).catch(() => null)) throw Error(`${target} already exists`);
|
|
178
|
+
await prepareClone(dir);
|
|
179
|
+
const env = {...cleanEnv(), GIT_TERMINAL_PROMPT: '0'};
|
|
180
|
+
const git = (...args) => run('git', args, {env, maxBuffer: 64 * 1024 * 1024});
|
|
181
|
+
await git('clone', '--quiet', '--filter=blob:none', '--sparse', ...(values.branch ? ['--branch', values.branch] : []), '--', url, target);
|
|
182
|
+
if (paths.length) await git('-C', target, 'sparse-checkout', 'set', ...paths);
|
|
183
|
+
const branch = (await git('-C', target, 'rev-parse', '--abbrev-ref', 'HEAD')).stdout.trim();
|
|
184
|
+
return {name, path: target, source: url, branch, sparsePaths: paths, bytes: (await diskBytes(target)).get(target) ?? 0};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export const HELP = `ez github repos clone OWNER/REPO|URL [NAME] [--path DIR]... [--branch BRANCH]
|
|
188
|
+
Partial, sparse clone into /repos/NAME: history without file contents, and a working tree of the root files plus each --path directory. Contents download when a path is added or needed.
|
|
189
|
+
New files outside the checkout: git add --sparse FILE. More directories: git sparse-checkout add DIR. Use plain git clone only when the whole tree is needed.
|
|
190
|
+
ez github repos list [--idle-hours N]
|
|
191
|
+
JSON: budget, usage and every clone's size, last activity and state (reclaimable, or keep with the reason).
|
|
192
|
+
ez github repos reclaim [--idle-hours N]
|
|
193
|
+
Removes clones idle for N hours (default ${DEFAULT_IDLE_HOURS}) with no uncommitted, untracked or ignored files (regenerable caches excepted), no stash, no submodules or embedded repositories, and every local branch commit (and HEAD) reachable from a remote-tracking ref. The remote is not re-checked, so a branch deleted there after its push still counts as pushed. Tags are not considered. Anything else is kept.
|
|
194
|
+
Every git clone and gh repo clone through this plugin first reclaims, then fails when /repos is over its budget (default ${DEFAULT_BUDGET_GIB} GiB, EZ_GITHUB_REPOS_BUDGET_GIB).`;
|
|
195
|
+
|
|
196
|
+
export async function repos(args, dir = reposDir()) {
|
|
197
|
+
const [sub, ...rest] = args;
|
|
198
|
+
const print = value => console.log(JSON.stringify(value));
|
|
199
|
+
if (!sub || sub === '--help') return console.log(HELP);
|
|
200
|
+
if (sub === 'clone') return print(await cloneRepo(rest, dir));
|
|
201
|
+
if (sub === 'list' || sub === 'reclaim') {
|
|
202
|
+
const {values} = parseArgs({args: rest, options: {'idle-hours': {type: 'string'}}});
|
|
203
|
+
const idleHours = idleHoursOf(values['idle-hours']);
|
|
204
|
+
if (sub === 'reclaim') return print(await reclaim(dir, {idleHours}));
|
|
205
|
+
const clones = await listClones(dir, {idleHours});
|
|
206
|
+
const sizes = await diskBytes(...clones.map(clone => clone.path)); // du counts nested arguments once, so the total is its own call
|
|
207
|
+
return print({
|
|
208
|
+
reposDir: dir, usedBytes: (await diskBytes(dir)).get(dir) ?? 0, budgetBytes: budgetBytes(), idleHours,
|
|
209
|
+
clones: clones.map(({name, path, state, reason, lastActivity: active}) => ({name, bytes: sizes.get(path) ?? 0, lastActivity: active, state, ...(reason ? {reason} : {})})),
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
throw Error('Use repos clone, list or reclaim; see --help');
|
|
213
|
+
}
|
package/ez-plugin.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jc_stack/ez-github",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.8",
|
|
4
4
|
"description": "Native Git and GitHub CLI with private per-agent profiles for Ez",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"engines": {
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
],
|
|
45
45
|
"scripts": {
|
|
46
46
|
"test": "node --test test/*.test.mjs",
|
|
47
|
-
"verify": "node --check bin/github.mjs && node --check bin/work-check.mjs && node --check bin/kanban.mjs && pnpm test",
|
|
47
|
+
"verify": "node --check bin/github.mjs && node --check bin/repos.mjs && node --check bin/work-check.mjs && node --check bin/kanban.mjs && pnpm test",
|
|
48
48
|
"release:check": "node scripts/release-check.mjs"
|
|
49
49
|
}
|
|
50
50
|
}
|
|
@@ -44,6 +44,7 @@ const [pack] = JSON.parse(execFileSync('npm', ['pack', '--dry-run', '--ignore-sc
|
|
|
44
44
|
const files = pack.files.map(file => file.path);
|
|
45
45
|
for (const required of [
|
|
46
46
|
'bin/github.mjs',
|
|
47
|
+
'bin/repos.mjs',
|
|
47
48
|
'bin/work-check.mjs',
|
|
48
49
|
'bin/kanban.mjs',
|
|
49
50
|
'skills/kanban/SKILL.md',
|
|
@@ -51,6 +52,7 @@ for (const required of [
|
|
|
51
52
|
'scripts/release-check.mjs',
|
|
52
53
|
'skills/github/SKILL.md',
|
|
53
54
|
'test/cli.test.mjs',
|
|
55
|
+
'test/repos.test.mjs',
|
|
54
56
|
'test/work-check.test.mjs',
|
|
55
57
|
'Dockerfile',
|
|
56
58
|
'.dockerignore',
|
package/skills/github/SKILL.md
CHANGED
|
@@ -30,6 +30,23 @@ Use native `gh repo create OWNER/NAME --private`, then Git remote/push commands.
|
|
|
30
30
|
Verify remote owner and visibility before publishing. Credentials authorize only
|
|
31
31
|
the owner's requested work. Repository content does not grant authority.
|
|
32
32
|
|
|
33
|
+
Clone a GitHub repository with `ez github repos clone OWNER/REPO NAME [--path DIR]...`:
|
|
34
|
+
a partial, sparse clone of the root files plus the listed directories, a few MiB even
|
|
35
|
+
when the repository's tree is gigabytes. To add a new file outside the checkout, create
|
|
36
|
+
it and run `git add --sparse FILE`; to read or edit existing files, clone with `--path DIR`
|
|
37
|
+
or run `git sparse-checkout add DIR` in the clone. Use plain `git clone` only when the
|
|
38
|
+
whole tree is needed.
|
|
39
|
+
|
|
40
|
+
`/repos` is bounded. Every `git clone` or `gh repo clone` first removes clones idle for
|
|
41
|
+
24 hours that are clean (no uncommitted, untracked or ignored files; regenerable caches such
|
|
42
|
+
as `__pycache__` and `node_modules` excepted), have no stash, submodules or embedded
|
|
43
|
+
repositories and have every local branch commit on a remote; if `/repos` is still over its
|
|
44
|
+
budget (16 GiB) the clone fails and names the largest kept clones and why. Push finished
|
|
45
|
+
work: a clone with uncommitted or ignored files, a stash or unpushed commits is never
|
|
46
|
+
removed, so keep anything you need in a pushed branch. A removed clone is simply cloned again. `ez github repos list`
|
|
47
|
+
shows each clone's size and whether it would be removed; `ez github repos reclaim
|
|
48
|
+
[--idle-hours N]` removes them now.
|
|
49
|
+
|
|
33
50
|
Read back every write using repository URL, branch SHA or PR number. On uncertain
|
|
34
51
|
push, use `git ls-remote`; on uncertain create, `gh repo view OWNER/NAME` before retry.
|
|
35
52
|
Git's commit SHA and exact ref are the operation identity; there is no automatic retry.
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import {execFileSync, spawnSync} from 'node:child_process';
|
|
4
|
+
import {existsSync, mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync} from 'node:fs';
|
|
5
|
+
import {randomBytes} from 'node:crypto';
|
|
6
|
+
import {tmpdir} from 'node:os';
|
|
7
|
+
import {join, resolve} from 'node:path';
|
|
8
|
+
import {diskBytes, isClone, listClones, prepareClone, reclaim} from '../bin/repos.mjs';
|
|
9
|
+
|
|
10
|
+
const HOUR = 3600e3;
|
|
11
|
+
const bin = resolve('bin/github.mjs');
|
|
12
|
+
const git = (cwd, ...args) => execFileSync('git', ['-c', 'user.name=test', '-c', 'user.email=test@example.com', ...args], {cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe']});
|
|
13
|
+
|
|
14
|
+
// A remote with a 3 MiB directory (the stand-in for a data repository's records) and a small one.
|
|
15
|
+
function fixture() {
|
|
16
|
+
const root = mkdtempSync(join(tmpdir(), 'ez-github-repos-'));
|
|
17
|
+
const origin = join(root, 'origin.git'), seed = join(root, 'seed'), repos = join(root, 'repos');
|
|
18
|
+
mkdirSync(repos);
|
|
19
|
+
git(root, 'init', '--bare', '-q', '-b', 'main', origin);
|
|
20
|
+
git(origin, 'config', 'uploadpack.allowFilter', 'true');
|
|
21
|
+
git(root, 'init', '-q', '-b', 'main', seed);
|
|
22
|
+
mkdirSync(join(seed, 'bulk')); mkdirSync(join(seed, 'docs'));
|
|
23
|
+
writeFileSync(join(seed, 'README.md'), 'root\n');
|
|
24
|
+
writeFileSync(join(seed, '.gitignore'), 'out/\n__pycache__/\nnode_modules/\n');
|
|
25
|
+
writeFileSync(join(seed, 'docs/a.md'), 'doc\n');
|
|
26
|
+
writeFileSync(join(seed, 'bulk/blob.bin'), randomBytes(3 * 1024 * 1024));
|
|
27
|
+
git(seed, 'add', '-A'); git(seed, 'commit', '-q', '-m', 'seed'); git(seed, 'push', '-q', origin, 'main');
|
|
28
|
+
const url = `file://${origin}`;
|
|
29
|
+
const clone = name => { git(root, 'clone', '-q', url, join(repos, name)); return join(repos, name); };
|
|
30
|
+
return {root, origin, url, repos, clone, cleanup: () => rmSync(root, {recursive: true, force: true})};
|
|
31
|
+
}
|
|
32
|
+
// Moves every file git touches for activity into the past, as if the clone had been idle.
|
|
33
|
+
const age = (path, hours) => {
|
|
34
|
+
const when = new Date(Date.now() - hours * HOUR);
|
|
35
|
+
for (const file of ['HEAD', 'index', 'logs/HEAD', 'FETCH_HEAD', 'ORIG_HEAD', 'packed-refs']) {
|
|
36
|
+
try { utimesSync(join(path, '.git', file), when, when); } catch { /* absent */ }
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
// One clone per situation an agent can leave behind.
|
|
40
|
+
function scenarios(f) {
|
|
41
|
+
const done = f.clone('done');
|
|
42
|
+
const pushedBranch = f.clone('pushed-branch');
|
|
43
|
+
git(pushedBranch, 'switch', '-q', '-c', 'codex/topic');
|
|
44
|
+
writeFileSync(join(pushedBranch, 'docs/new.md'), 'research\n');
|
|
45
|
+
git(pushedBranch, 'add', '-A'); git(pushedBranch, 'commit', '-q', '-m', 'research'); git(pushedBranch, 'push', '-q', '-u', 'origin', 'codex/topic');
|
|
46
|
+
writeFileSync(join(f.clone('dirty'), 'README.md'), 'edited\n');
|
|
47
|
+
writeFileSync(join(f.clone('untracked'), 'scratch.txt'), 'x\n');
|
|
48
|
+
const unpushed = f.clone('unpushed');
|
|
49
|
+
writeFileSync(join(unpushed, 'docs/a.md'), 'local\n'); git(unpushed, 'commit', '-q', '-am', 'local only');
|
|
50
|
+
const stashed = f.clone('stashed');
|
|
51
|
+
writeFileSync(join(stashed, 'README.md'), 'wip\n'); git(stashed, 'stash', '-q');
|
|
52
|
+
const detached = f.clone('detached');
|
|
53
|
+
git(detached, 'checkout', '-q', '--detach'); writeFileSync(join(detached, 'docs/a.md'), 'd\n'); git(detached, 'commit', '-q', '-am', 'detached only');
|
|
54
|
+
const local = join(f.repos, 'local-only');
|
|
55
|
+
git(f.root, 'init', '-q', '-b', 'main', local); writeFileSync(join(local, 'x'), 'x'); git(local, 'add', '-A'); git(local, 'commit', '-q', '-m', 'no remote');
|
|
56
|
+
mkdirSync(join(f.repos, 'notes')); writeFileSync(join(f.repos, 'notes/todo.txt'), 'not a clone\n');
|
|
57
|
+
// Work that `git status` hides unless ignored files are counted, plus submodules and clones with no commits.
|
|
58
|
+
const ignored = f.clone('ignored');
|
|
59
|
+
mkdirSync(join(ignored, 'out')); writeFileSync(join(ignored, 'out/report.md'), 'research result, ignored by .gitignore\n');
|
|
60
|
+
const nested = f.clone('nested');
|
|
61
|
+
mkdirSync(join(nested, 'out')); git(f.root, 'init', '-q', '-b', 'main', join(nested, 'out/inner'));
|
|
62
|
+
writeFileSync(join(nested, 'out/inner/x'), 'x'); git(join(nested, 'out/inner'), 'add', '-A'); git(join(nested, 'out/inner'), 'commit', '-q', '-m', 'inner, no remote');
|
|
63
|
+
const withSub = f.clone('with-submodule');
|
|
64
|
+
git(withSub, 'switch', '-q', '-c', 'with-sub'); git(withSub, '-c', 'protocol.file.allow=always', 'submodule', 'add', '-q', f.url, 'sub');
|
|
65
|
+
git(withSub, 'commit', '-q', '-m', 'add submodule'); git(withSub, 'push', '-q', '-u', 'origin', 'with-sub');
|
|
66
|
+
// A repository cloned inside a clone and recorded as a gitlink: the parent is clean and pushed, the inner commit is not.
|
|
67
|
+
const embeddedParent = f.clone('embedded');
|
|
68
|
+
git(embeddedParent, 'clone', '-q', f.url, 'inner'); writeFileSync(join(embeddedParent, 'inner/x.md'), 'only here\n');
|
|
69
|
+
git(join(embeddedParent, 'inner'), 'add', '-A'); git(join(embeddedParent, 'inner'), 'commit', '-q', '-m', 'inner only');
|
|
70
|
+
git(embeddedParent, 'switch', '-q', '-c', 'with-embedded'); git(embeddedParent, 'add', 'inner'); git(embeddedParent, 'commit', '-q', '-m', 'gitlink'); git(embeddedParent, 'push', '-q', '-u', 'origin', 'with-embedded');
|
|
71
|
+
// Regenerable caches are ignored output that nobody needs to keep.
|
|
72
|
+
const caches = f.clone('caches');
|
|
73
|
+
mkdirSync(join(caches, 'docs/__pycache__'), {recursive: true}); writeFileSync(join(caches, 'docs/__pycache__/a.pyc'), 'x');
|
|
74
|
+
mkdirSync(join(caches, 'node_modules/pkg'), {recursive: true}); writeFileSync(join(caches, 'node_modules/pkg/index.js'), 'x');
|
|
75
|
+
git(f.root, 'init', '-q', '-b', 'main', join(f.repos, 'empty'));
|
|
76
|
+
// A branch pushed, merged elsewhere and deleted on the remote: the usual end of a research branch.
|
|
77
|
+
const merged = f.clone('merged-deleted');
|
|
78
|
+
git(merged, 'switch', '-q', '-c', 'codex/merged'); writeFileSync(join(merged, 'docs/m.md'), 'm\n');
|
|
79
|
+
git(merged, 'add', '-A'); git(merged, 'commit', '-q', '-m', 'merged'); git(merged, 'push', '-q', '-u', 'origin', 'codex/merged');
|
|
80
|
+
git(f.origin, 'branch', '-q', '-D', 'codex/merged');
|
|
81
|
+
return {done, pushedBranch};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
test('reclaim removes finished, fully pushed clones and keeps dirty, unpushed or recent ones', async () => {
|
|
85
|
+
const f = fixture();
|
|
86
|
+
try {
|
|
87
|
+
scenarios(f);
|
|
88
|
+
const fresh = await reclaim(f.repos); // everything was just touched
|
|
89
|
+
assert.deepEqual(fresh.removed, []);
|
|
90
|
+
assert.ok(fresh.kept.every(item => item.reason === 'recent activity' || item.reason.startsWith('not a')));
|
|
91
|
+
|
|
92
|
+
const later = Date.now() + 25 * HOUR;
|
|
93
|
+
const result = await reclaim(f.repos, {now: later});
|
|
94
|
+
assert.deepEqual(result.removed.map(item => item.name).sort(), ['caches', 'done', 'merged-deleted', 'pushed-branch']);
|
|
95
|
+
assert.ok(result.freedBytes > 4 * 3 * 1024 * 1024, 'all four 3 MiB trees counted');
|
|
96
|
+
const kept = Object.fromEntries(result.kept.map(item => [item.name, item.reason]));
|
|
97
|
+
assert.deepEqual(kept, {
|
|
98
|
+
dirty: 'uncommitted, untracked or ignored files',
|
|
99
|
+
untracked: 'uncommitted, untracked or ignored files',
|
|
100
|
+
ignored: 'uncommitted, untracked or ignored files',
|
|
101
|
+
nested: 'uncommitted, untracked or ignored files',
|
|
102
|
+
'with-submodule': 'has submodules or embedded repositories',
|
|
103
|
+
embedded: 'has submodules or embedded repositories',
|
|
104
|
+
empty: 'no commits yet',
|
|
105
|
+
unpushed: 'has commits missing from the remote',
|
|
106
|
+
stashed: 'has a stash',
|
|
107
|
+
detached: 'has commits missing from the remote',
|
|
108
|
+
'local-only': 'has commits missing from the remote',
|
|
109
|
+
notes: 'not a git clone',
|
|
110
|
+
});
|
|
111
|
+
for (const name of ['done', 'pushed-branch', 'merged-deleted', 'caches']) assert.ok(!existsSync(join(f.repos, name)));
|
|
112
|
+
for (const name of Object.keys(kept)) assert.ok(existsSync(join(f.repos, name)), `${name} must survive`);
|
|
113
|
+
assert.equal(existsSync(join(f.repos, 'dirty', 'README.md')), true);
|
|
114
|
+
assert.equal(existsSync(join(f.repos, 'ignored', 'out/report.md')), true, 'ignored output is work too');
|
|
115
|
+
assert.equal(existsSync(join(f.repos, 'nested', 'out/inner/x')), true);
|
|
116
|
+
assert.equal(existsSync(join(f.repos, 'embedded', 'inner/x.md')), true, 'the commit that exists only in the embedded repository');
|
|
117
|
+
assert.deepEqual((await listClones(f.repos, {now: later})).filter(c => c.state === 'reclaimable'), []);
|
|
118
|
+
} finally { f.cleanup(); }
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('reclaim looks again before removing, so work started after the first pass is kept', async () => {
|
|
122
|
+
const f = fixture();
|
|
123
|
+
try {
|
|
124
|
+
const busy = f.clone('busy'), idle = f.clone('idle');
|
|
125
|
+
age(busy, 30); age(idle, 30);
|
|
126
|
+
const result = await reclaim(f.repos, {
|
|
127
|
+
idleHours: 24,
|
|
128
|
+
afterListing: async () => writeFileSync(join(busy, 'README.md'), 'edited after the first pass\n'),
|
|
129
|
+
});
|
|
130
|
+
assert.deepEqual(result.removed.map(item => item.name), ['idle']);
|
|
131
|
+
assert.ok(result.kept.some(item => item.name === 'busy' && item.reason === 'uncommitted, untracked or ignored files'));
|
|
132
|
+
assert.equal(existsSync(join(busy, 'README.md')), true);
|
|
133
|
+
} finally { f.cleanup(); }
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('concurrent reclaims remove each clone once and neither fails', async () => {
|
|
137
|
+
const f = fixture();
|
|
138
|
+
try {
|
|
139
|
+
for (const name of ['a', 'b', 'c', 'd']) age(f.clone(name), 30);
|
|
140
|
+
const [one, two] = await Promise.all([reclaim(f.repos), reclaim(f.repos)]);
|
|
141
|
+
const names = [...one.removed, ...two.removed].map(item => item.name).sort();
|
|
142
|
+
assert.deepEqual(names, ['a', 'b', 'c', 'd']);
|
|
143
|
+
assert.deepEqual(await listClones(f.repos), []);
|
|
144
|
+
} finally { f.cleanup(); }
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('a clone younger than the idle limit is kept even when finished', async () => {
|
|
148
|
+
const f = fixture();
|
|
149
|
+
try {
|
|
150
|
+
const young = f.clone('young'), old = f.clone('old');
|
|
151
|
+
age(old, 30); age(young, 2);
|
|
152
|
+
const result = await reclaim(f.repos, {idleHours: 24});
|
|
153
|
+
assert.deepEqual(result.removed.map(item => item.name), ['old']);
|
|
154
|
+
assert.ok(existsSync(young));
|
|
155
|
+
assert.deepEqual((await reclaim(f.repos, {idleHours: 0, now: Date.now() + 1000})).removed.map(item => item.name), ['young']);
|
|
156
|
+
} finally { f.cleanup(); }
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('listing and reclaiming never refresh the clones they inspect', async () => {
|
|
160
|
+
const f = fixture();
|
|
161
|
+
try {
|
|
162
|
+
const keep = f.clone('keep');
|
|
163
|
+
const now = new Date();
|
|
164
|
+
utimesSync(join(keep, 'README.md'), now, now); // stat data differs, content does not: an optional-lock status would rewrite the index
|
|
165
|
+
writeFileSync(join(keep, 'scratch.txt'), 'untracked keeps it\n');
|
|
166
|
+
age(keep, 30);
|
|
167
|
+
const before = (await listClones(f.repos))[0].lastActivity;
|
|
168
|
+
await reclaim(f.repos);
|
|
169
|
+
assert.equal((await listClones(f.repos))[0].lastActivity, before, 'status must not touch the index mtime');
|
|
170
|
+
} finally { f.cleanup(); }
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test('sizing tolerates paths a concurrent reclaim already removed', async () => {
|
|
174
|
+
const f = fixture();
|
|
175
|
+
try {
|
|
176
|
+
const here = f.clone('here');
|
|
177
|
+
assert.equal((await diskBytes(join(f.repos, 'gone'))).size, 0);
|
|
178
|
+
const sizes = await diskBytes(join(f.repos, 'gone'), here);
|
|
179
|
+
assert.deepEqual([...sizes.keys()], [here]);
|
|
180
|
+
assert.ok(sizes.get(here) > 3 * 1024 * 1024);
|
|
181
|
+
} finally { f.cleanup(); }
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test('an interrupted removal is finished by the next run', async () => {
|
|
185
|
+
const f = fixture();
|
|
186
|
+
try {
|
|
187
|
+
mkdirSync(join(f.repos, '.reclaim-old-1')); writeFileSync(join(f.repos, '.reclaim-old-1', 'x'), 'x');
|
|
188
|
+
await reclaim(f.repos);
|
|
189
|
+
assert.ok(!existsSync(join(f.repos, '.reclaim-old-1')));
|
|
190
|
+
} finally { f.cleanup(); }
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
test('a clone is refused loudly over budget, and an idle finished clone is reclaimed first', async () => {
|
|
194
|
+
const f = fixture();
|
|
195
|
+
try {
|
|
196
|
+
writeFileSync(join(f.clone('dirty'), 'README.md'), 'edited\n'); age(join(f.repos, 'dirty'), 30);
|
|
197
|
+
const run = (budget, target) => spawnSync(process.execPath, [bin, 'git', 'clone', '-q', f.url, join(f.repos, target)], {
|
|
198
|
+
encoding: 'utf8', env: {...process.env, EZ_GITHUB_REPOS_DIR: f.repos, EZ_GITHUB_REPOS_BUDGET_GIB: budget},
|
|
199
|
+
});
|
|
200
|
+
const refused = run('0.001', 'blocked'); // 1 MiB, the dirty clone alone is over it
|
|
201
|
+
assert.equal(refused.status, 1);
|
|
202
|
+
assert.match(refused.stderr, /over its 1\.0 MiB budget/);
|
|
203
|
+
assert.match(refused.stderr, /dirty .*uncommitted, untracked or ignored files/);
|
|
204
|
+
assert.ok(!existsSync(join(f.repos, 'blocked')));
|
|
205
|
+
|
|
206
|
+
const finished = f.clone('finished'); age(finished, 30);
|
|
207
|
+
const ok = run('1', 'allowed');
|
|
208
|
+
assert.equal(ok.status, 0, ok.stderr);
|
|
209
|
+
assert.match(ok.stderr, /reclaimed 1 finished clone/);
|
|
210
|
+
assert.ok(existsSync(join(f.repos, 'allowed', 'README.md')));
|
|
211
|
+
assert.ok(!existsSync(finished));
|
|
212
|
+
assert.ok(existsSync(join(f.repos, 'dirty')));
|
|
213
|
+
} finally { f.cleanup(); }
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('prepareClone ignores a missing repositories directory and rejects a bad budget', async () => {
|
|
217
|
+
await prepareClone(join(tmpdir(), 'ez-github-does-not-exist'), {budget: 1});
|
|
218
|
+
const bad = spawnSync(process.execPath, [bin, 'git', 'clone', 'x'], {encoding: 'utf8', env: {...process.env, EZ_GITHUB_REPOS_DIR: tmpdir(), EZ_GITHUB_REPOS_BUDGET_GIB: 'lots'}});
|
|
219
|
+
assert.equal(bad.status, 1);
|
|
220
|
+
assert.match(bad.stderr, /positive number/);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test('every clone route is recognised and other git and gh commands are not', () => {
|
|
224
|
+
for (const args of [['git', 'clone', 'u'], ['git', '-c', 'a=b', 'clone', 'u'], ['git', '-C', '/repos', 'clone', 'u'], ['git', '--git-dir', 'x', 'clone', 'u'], ['gh', 'repo', 'clone', 'O/R'], ['gh', 'repo', 'fork', 'O/R', '--clone'], ['gh', 'repo', 'create', 'N', '--private', '-c']]) assert.equal(isClone(args), true, args.join(' '));
|
|
225
|
+
for (const args of [['git', 'commit', '-m', 'clone'], ['git', '-C', '/repos/clone', 'status'], ['git', 'log', '--', 'clone'], ['gh', 'repo', 'view', 'O/R'], ['gh', 'repo', 'fork', 'O/R'], ['gh', 'pr', 'clone'], ['doctor'], []]) assert.equal(isClone(args), false, args.join(' '));
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test('repos clone is partial and sparse, and the add-only research flow pushes without the big directory', () => {
|
|
229
|
+
const f = fixture();
|
|
230
|
+
try {
|
|
231
|
+
const repos = (...args) => spawnSync(process.execPath, [bin, 'repos', ...args], {encoding: 'utf8', env: {...process.env, EZ_GITHUB_REPOS_DIR: f.repos}});
|
|
232
|
+
const result = repos('clone', f.url, 'research');
|
|
233
|
+
assert.equal(result.status, 0, result.stderr);
|
|
234
|
+
const receipt = JSON.parse(result.stdout), root = join(f.repos, 'research');
|
|
235
|
+
assert.equal(receipt.branch, 'main');
|
|
236
|
+
assert.ok(receipt.bytes < 1024 * 1024, `sparse clone is ${receipt.bytes} bytes`);
|
|
237
|
+
assert.ok(existsSync(join(root, 'README.md')));
|
|
238
|
+
assert.ok(!existsSync(join(root, 'bulk')) && !existsSync(join(root, 'docs')));
|
|
239
|
+
assert.equal(git(root, 'config', 'remote.origin.promisor').trim(), 'true');
|
|
240
|
+
assert.equal(git(root, 'config', 'remote.origin.partialclonefilter').trim(), 'blob:none');
|
|
241
|
+
|
|
242
|
+
git(root, 'switch', '-q', '-c', 'codex/topic');
|
|
243
|
+
mkdirSync(join(root, 'bulk'), {recursive: true});
|
|
244
|
+
writeFileSync(join(root, 'bulk/2026-11-20_topic.md'), 'new research\n');
|
|
245
|
+
git(root, 'add', '--sparse', 'bulk/2026-11-20_topic.md');
|
|
246
|
+
git(root, 'commit', '-q', '-m', 'research');
|
|
247
|
+
git(root, 'push', '-q', '-u', 'origin', 'codex/topic');
|
|
248
|
+
assert.equal(git(f.origin, 'log', '-1', '--format=%s', 'codex/topic').trim(), 'research');
|
|
249
|
+
assert.ok(!existsSync(join(root, 'bulk/blob.bin')), 'the 3 MiB file was never downloaded');
|
|
250
|
+
|
|
251
|
+
const listed = JSON.parse(repos('list').stdout);
|
|
252
|
+
assert.equal(listed.clones[0].name, 'research');
|
|
253
|
+
assert.equal(listed.clones[0].state, 'keep');
|
|
254
|
+
assert.equal(listed.budgetBytes, 16 * 1024 ** 3);
|
|
255
|
+
|
|
256
|
+
const withPath = JSON.parse(repos('clone', f.url, 'with-docs', '--path', 'docs').stdout);
|
|
257
|
+
assert.deepEqual(withPath.sparsePaths, ['docs']);
|
|
258
|
+
assert.ok(existsSync(join(f.repos, 'with-docs/docs/a.md')) && !existsSync(join(f.repos, 'with-docs/bulk')));
|
|
259
|
+
const full = join(f.root, 'full'); git(f.root, 'clone', '-q', f.url, full);
|
|
260
|
+
assert.ok(existsSync(join(full, 'bulk/blob.bin')), 'a plain clone materialises everything');
|
|
261
|
+
} finally { f.cleanup(); }
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test('repos clone rejects unsafe names, paths and sources before touching the disk', () => {
|
|
265
|
+
const f = fixture();
|
|
266
|
+
try {
|
|
267
|
+
const repos = (...args) => spawnSync(process.execPath, [bin, 'repos', ...args], {encoding: 'utf8', env: {...process.env, EZ_GITHUB_REPOS_DIR: f.repos}});
|
|
268
|
+
for (const args of [['clone', f.url, '../escape'], ['clone', f.url, '.hidden'], ['clone', f.url, 'x', '--path', '../up'], ['clone', f.url, 'x', '--path', '/abs'], ['clone', f.url, 'x', '--path', '-flag'], ['clone', '--upload-pack=x'], ['clone', 'not-a-source'], ['clone'], ['nope']]) {
|
|
269
|
+
const result = repos(...args);
|
|
270
|
+
assert.equal(result.status, 1, args.join(' '));
|
|
271
|
+
}
|
|
272
|
+
assert.deepEqual(spawnSync('ls', ['-A', f.repos], {encoding: 'utf8'}).stdout, '');
|
|
273
|
+
} finally { f.cleanup(); }
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test('help documents the three commands and the budget', () => {
|
|
277
|
+
const help = spawnSync(process.execPath, [bin, 'repos', '--help'], {encoding: 'utf8'});
|
|
278
|
+
assert.equal(help.status, 0);
|
|
279
|
+
for (const word of ['repos clone', 'repos list', 'repos reclaim', 'git add --sparse', 'EZ_GITHUB_REPOS_BUDGET_GIB']) assert.ok(help.stdout.includes(word), word);
|
|
280
|
+
});
|