thepopebot 1.2.76-beta.37 → 1.2.76-beta.38
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/lib/ai/workspace-setup.js +31 -69
- package/package.json +1 -1
|
@@ -15,99 +15,61 @@ async function run(cmd, args, opts) {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
18
|
+
* Set up the workspace once. If `.git` already exists, returns immediately
|
|
19
|
+
* and never touches git — git is the source of truth from then on.
|
|
20
20
|
*
|
|
21
|
-
*
|
|
21
|
+
* First-setup steps: gh auth setup-git, shallow clone the base branch,
|
|
22
|
+
* set git identity, checkout the feature branch (or stay on base).
|
|
22
23
|
*
|
|
23
24
|
* @param {object} opts
|
|
24
25
|
* @param {string} opts.workspaceDir - Absolute path to workspace directory (the git repo root)
|
|
25
26
|
* @param {string} opts.repo - GitHub owner/repo (e.g. "owner/repo")
|
|
26
27
|
* @param {string} opts.branch - Base branch (e.g. "main")
|
|
27
|
-
* @param {string} [opts.featureBranch] - Feature branch to create
|
|
28
|
+
* @param {string} [opts.featureBranch] - Feature branch to create on first setup
|
|
28
29
|
*/
|
|
29
30
|
export async function ensureWorkspaceRepo({ workspaceDir, repo, branch, featureBranch }) {
|
|
31
|
+
// Already set up — never touch git again.
|
|
32
|
+
if (existsSync(path.join(workspaceDir, '.git'))) return '';
|
|
33
|
+
|
|
34
|
+
if (!repo) throw new Error('ensureWorkspaceRepo: repo is required for initial clone');
|
|
35
|
+
if (!branch) throw new Error(`ensureWorkspaceRepo: branch is required (could not resolve default branch for ${repo})`);
|
|
36
|
+
|
|
30
37
|
const ghToken = getConfig('GH_TOKEN');
|
|
31
38
|
const env = { ...process.env };
|
|
32
39
|
if (ghToken) env.GH_TOKEN = ghToken;
|
|
33
40
|
|
|
41
|
+
mkdirSync(workspaceDir, { recursive: true });
|
|
34
42
|
const execOpts = { cwd: workspaceDir, env };
|
|
35
43
|
const log = [];
|
|
36
44
|
|
|
37
|
-
// 1. Create workspace directory
|
|
38
|
-
mkdirSync(workspaceDir, { recursive: true });
|
|
39
|
-
|
|
40
|
-
// 2. Configure git to use GH_TOKEN for GitHub HTTPS URLs (mirrors setup-git.sh)
|
|
41
45
|
if (ghToken) {
|
|
42
46
|
const out = await run('gh', ['auth', 'setup-git'], execOpts);
|
|
43
47
|
if (out) log.push(out);
|
|
44
48
|
}
|
|
45
49
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (!hasGit) {
|
|
49
|
-
if (!repo) throw new Error('ensureWorkspaceRepo: repo is required for initial clone');
|
|
50
|
-
if (!branch) throw new Error(`ensureWorkspaceRepo: branch is required (could not resolve default branch for ${repo})`);
|
|
51
|
-
const out = await run('git', ['clone', '--branch', branch, `https://github.com/${repo}`, '.'], execOpts);
|
|
52
|
-
log.push(`Cloned ${repo} (branch: ${branch})`);
|
|
53
|
-
if (out) log.push(out);
|
|
54
|
-
}
|
|
50
|
+
await run('git', ['clone', '--depth', '1', '--branch', branch, `https://github.com/${repo}`, '.'], execOpts);
|
|
51
|
+
log.push(`Cloned ${repo} (branch: ${branch}, depth 1)`);
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
await run('git', ['config', 'user.name', name], execOpts);
|
|
68
|
-
await run('git', ['config', 'user.email', email], execOpts);
|
|
69
|
-
log.push(`Git identity: ${name} <${email}>`);
|
|
70
|
-
} catch (err) {
|
|
71
|
-
console.error('[workspace-setup] Failed to set git identity:', err.message);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 4. Feature branch checkout
|
|
77
|
-
if (!featureBranch) return log.join('\n');
|
|
78
|
-
|
|
79
|
-
// Already on the right branch locally?
|
|
80
|
-
try {
|
|
81
|
-
await run('git', ['rev-parse', '--verify', featureBranch], execOpts);
|
|
82
|
-
// Branch exists locally — make sure we're on it
|
|
83
|
-
const current = await run('git', ['rev-parse', '--abbrev-ref', 'HEAD'], execOpts);
|
|
84
|
-
if (current !== featureBranch) {
|
|
85
|
-
await run('git', ['checkout', featureBranch], execOpts);
|
|
86
|
-
log.push(`Checked out ${featureBranch}`);
|
|
87
|
-
} else {
|
|
88
|
-
log.push(`Already on ${featureBranch}`);
|
|
53
|
+
if (ghToken) {
|
|
54
|
+
try {
|
|
55
|
+
const userJson = await run('gh', ['api', 'user', '-q', '{name: .name, login: .login, email: .email, id: .id}'], execOpts);
|
|
56
|
+
const user = JSON.parse(userJson);
|
|
57
|
+
const name = user.name || user.login;
|
|
58
|
+
const email = user.email || `${user.id}+${user.login}@users.noreply.github.com`;
|
|
59
|
+
await run('git', ['config', 'user.name', name], execOpts);
|
|
60
|
+
await run('git', ['config', 'user.email', email], execOpts);
|
|
61
|
+
log.push(`Git identity: ${name} <${email}>`);
|
|
62
|
+
} catch (err) {
|
|
63
|
+
console.error('[workspace-setup] Failed to set git identity:', err.message);
|
|
89
64
|
}
|
|
90
|
-
return log.join('\n');
|
|
91
|
-
} catch {
|
|
92
|
-
// Branch doesn't exist locally — check remote
|
|
93
65
|
}
|
|
94
66
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
} else {
|
|
102
|
-
// Create new branch and push
|
|
103
|
-
await run('git', ['checkout', '-b', featureBranch], execOpts);
|
|
104
|
-
const pushOut = await run('git', ['push', '-u', 'origin', featureBranch], execOpts);
|
|
105
|
-
log.push(`Created and pushed ${featureBranch}`);
|
|
106
|
-
if (pushOut) log.push(pushOut);
|
|
107
|
-
}
|
|
108
|
-
} catch (err) {
|
|
109
|
-
console.error('[workspace-setup] Feature branch error:', err.message);
|
|
110
|
-
throw err;
|
|
67
|
+
if (featureBranch && featureBranch !== branch) {
|
|
68
|
+
await run('git', ['checkout', '-b', featureBranch], execOpts);
|
|
69
|
+
log.push(`Created and checked out ${featureBranch}`);
|
|
70
|
+
} else {
|
|
71
|
+
await run('git', ['checkout', branch], execOpts);
|
|
72
|
+
log.push(`On ${branch}`);
|
|
111
73
|
}
|
|
112
74
|
|
|
113
75
|
return log.join('\n');
|