-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit.js
More file actions
195 lines (182 loc) · 5.94 KB
/
git.js
File metadata and controls
195 lines (182 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import simpleGit from 'simple-git';
import { join } from 'path';
import { homedir } from 'os';
import { mkdirSync } from 'fs';
import { getLogger } from '../utils/logger.js';
function getWorkspaceDir(config) {
const dir = config.claude_code?.workspace_dir || join(homedir(), '.kernelbot', 'workspaces');
mkdirSync(dir, { recursive: true });
return dir;
}
/**
* Get the auth header value for GitHub HTTPS operations.
* Uses extraheader instead of embedding credentials in the URL,
* preventing token leaks in git remote -v, error messages, and process listings.
*/
function getGitAuthEnv(config) {
const token = config.github?.token || process.env.GITHUB_TOKEN;
if (!token) return null;
const base64 = Buffer.from(`x-access-token:${token}`).toString('base64');
return `AUTHORIZATION: basic ${base64}`;
}
/**
* Configure a simple-git instance with auth via extraheader (not URL embedding).
*/
function configureGitAuth(git, config) {
const authHeader = getGitAuthEnv(config);
if (authHeader) {
git.env('GIT_CONFIG_COUNT', '1');
git.env('GIT_CONFIG_KEY_0', 'http.extraheader');
git.env('GIT_CONFIG_VALUE_0', authHeader);
}
return git;
}
export const definitions = [
{
name: 'git_clone',
description: 'Clone a git repository. Accepts "org/repo" shorthand (uses GitHub) or a full URL.',
input_schema: {
type: 'object',
properties: {
repo: {
type: 'string',
description: 'Repository — "org/repo" or full git URL',
},
dest: {
type: 'string',
description: 'Destination directory name (optional, defaults to repo name)',
},
},
required: ['repo'],
},
},
{
name: 'git_checkout',
description: 'Checkout an existing branch or create a new one.',
input_schema: {
type: 'object',
properties: {
dir: { type: 'string', description: 'Repository directory path' },
branch: { type: 'string', description: 'Branch name' },
create: { type: 'boolean', description: 'Create the branch if it doesn\'t exist (default false)' },
},
required: ['dir', 'branch'],
},
},
{
name: 'git_commit',
description: 'Stage all changes and create a commit.',
input_schema: {
type: 'object',
properties: {
dir: { type: 'string', description: 'Repository directory path' },
message: { type: 'string', description: 'Commit message' },
},
required: ['dir', 'message'],
},
},
{
name: 'git_push',
description: 'Push the current branch to the remote.',
input_schema: {
type: 'object',
properties: {
dir: { type: 'string', description: 'Repository directory path' },
force: { type: 'boolean', description: 'Force push (default false)' },
},
required: ['dir'],
},
},
{
name: 'git_diff',
description: 'Get the diff of current uncommitted changes.',
input_schema: {
type: 'object',
properties: {
dir: { type: 'string', description: 'Repository directory path' },
},
required: ['dir'],
},
},
];
export const handlers = {
git_clone: async (params, context) => {
const { repo, dest } = params;
const workspaceDir = getWorkspaceDir(context.config);
let url = repo;
if (!repo.includes('://') && !repo.startsWith('git@')) {
url = `https://github.com/${repo}.git`;
}
const repoName = dest || repo.split('/').pop().replace('.git', '');
// Prevent path traversal — dest must not escape workspace directory
if (repoName.includes('..') || repoName.startsWith('/')) {
return { error: 'Invalid destination: path traversal is not allowed' };
}
const targetDir = join(workspaceDir, repoName);
if (!targetDir.startsWith(workspaceDir)) {
return { error: 'Invalid destination: path escapes workspace directory' };
}
try {
const git = configureGitAuth(simpleGit(), context.config);
await git.clone(url, targetDir);
return { success: true, path: targetDir };
} catch (err) {
getLogger().error(`git_clone failed for ${params.repo}: ${err.message}`);
return { error: err.message };
}
},
git_checkout: async (params) => {
const { dir, branch, create = false } = params;
try {
const git = simpleGit(dir);
if (create) {
await git.checkoutLocalBranch(branch);
} else {
await git.checkout(branch);
}
return { success: true, branch };
} catch (err) {
getLogger().error(`git_checkout failed for branch ${params.branch}: ${err.message}`);
return { error: err.message };
}
},
git_commit: async (params) => {
const { dir, message } = params;
try {
const git = simpleGit(dir);
await git.add('.');
const result = await git.commit(message);
return { success: true, commit: result.commit, summary: result.summary };
} catch (err) {
getLogger().error(`git_commit failed: ${err.message}`);
return { error: err.message };
}
},
git_push: async (params, context) => {
const { dir, force = false } = params;
try {
// Use extraheader auth instead of modifying remote URLs
const git = configureGitAuth(simpleGit(dir), context.config);
const branch = (await git.branchLocal()).current;
const options = ['-u'];
if (force) options.push('--force');
await git.push('origin', branch, options);
return { success: true, branch };
} catch (err) {
getLogger().error(`git_push failed: ${err.message}`);
return { error: err.message };
}
},
git_diff: async (params) => {
const { dir } = params;
try {
const git = simpleGit(dir);
const diff = await git.diff();
const staged = await git.diff(['--cached']);
return { unstaged: diff || '(no changes)', staged: staged || '(no staged changes)' };
} catch (err) {
getLogger().error(`git_diff failed: ${err.message}`);
return { error: err.message };
}
},
};