fix: clean git credentials before push to avoid double-auth URLs
Made-with: Cursor
This commit is contained in:
22
dist/tools.js
vendored
22
dist/tools.js
vendored
@@ -377,13 +377,21 @@ async function gitCommitAndPush(message, ctx) {
|
|||||||
try {
|
try {
|
||||||
await execAsync('git add -A', { cwd });
|
await execAsync('git add -A', { cwd });
|
||||||
await execAsync(`git commit -m "${message.replace(/"/g, '\\"')}"`, { cwd });
|
await execAsync(`git commit -m "${message.replace(/"/g, '\\"')}"`, { cwd });
|
||||||
// Ensure remote has credentials
|
// Get current remote URL, strip any existing credentials, re-inject cleanly
|
||||||
const remoteUrl = (await execAsync('git remote get-url origin', { cwd })).stdout.trim();
|
let remoteUrl = '';
|
||||||
const authedUrl = remoteUrl.startsWith('https://')
|
try {
|
||||||
? remoteUrl.replace('https://', `https://${username}:${apiToken}@`)
|
remoteUrl = (await execAsync('git remote get-url origin', { cwd })).stdout.trim();
|
||||||
: remoteUrl;
|
}
|
||||||
await execAsync(`git push "${authedUrl}" HEAD`, { cwd, timeout: 60000 });
|
catch { /* no remote yet */ }
|
||||||
return { success: true, message };
|
const cleanUrl = remoteUrl.replace(/https:\/\/[^@]+@/, 'https://');
|
||||||
|
const baseUrl = cleanUrl || apiUrl;
|
||||||
|
const authedUrl = baseUrl.replace('https://', `https://${username}:${apiToken}@`);
|
||||||
|
await execAsync(`git remote set-url origin "${authedUrl}"`, { cwd }).catch(async () => {
|
||||||
|
await execAsync(`git remote add origin "${authedUrl}"`, { cwd });
|
||||||
|
});
|
||||||
|
const branch = (await execAsync('git rev-parse --abbrev-ref HEAD', { cwd })).stdout.trim();
|
||||||
|
await execAsync(`git push -u origin "${branch}"`, { cwd, timeout: 60000 });
|
||||||
|
return { success: true, message, branch };
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
const cleaned = (err.message || '').replace(new RegExp(apiToken, 'g'), '***');
|
const cleaned = (err.message || '').replace(new RegExp(apiToken, 'g'), '***');
|
||||||
|
|||||||
24
src/tools.ts
24
src/tools.ts
@@ -372,14 +372,24 @@ async function gitCommitAndPush(message: string, ctx: ToolContext): Promise<unkn
|
|||||||
await execAsync('git add -A', { cwd });
|
await execAsync('git add -A', { cwd });
|
||||||
await execAsync(`git commit -m "${message.replace(/"/g, '\\"')}"`, { cwd });
|
await execAsync(`git commit -m "${message.replace(/"/g, '\\"')}"`, { cwd });
|
||||||
|
|
||||||
// Ensure remote has credentials
|
// Get current remote URL, strip any existing credentials, re-inject cleanly
|
||||||
const remoteUrl = (await execAsync('git remote get-url origin', { cwd })).stdout.trim();
|
let remoteUrl = '';
|
||||||
const authedUrl = remoteUrl.startsWith('https://')
|
try {
|
||||||
? remoteUrl.replace('https://', `https://${username}:${apiToken}@`)
|
remoteUrl = (await execAsync('git remote get-url origin', { cwd })).stdout.trim();
|
||||||
: remoteUrl;
|
} catch { /* no remote yet */ }
|
||||||
await execAsync(`git push "${authedUrl}" HEAD`, { cwd, timeout: 60_000 });
|
|
||||||
|
|
||||||
return { success: true, message };
|
const cleanUrl = remoteUrl.replace(/https:\/\/[^@]+@/, 'https://');
|
||||||
|
const baseUrl = cleanUrl || apiUrl;
|
||||||
|
const authedUrl = baseUrl.replace('https://', `https://${username}:${apiToken}@`);
|
||||||
|
|
||||||
|
await execAsync(`git remote set-url origin "${authedUrl}"`, { cwd }).catch(async () => {
|
||||||
|
await execAsync(`git remote add origin "${authedUrl}"`, { cwd });
|
||||||
|
});
|
||||||
|
|
||||||
|
const branch = (await execAsync('git rev-parse --abbrev-ref HEAD', { cwd })).stdout.trim();
|
||||||
|
await execAsync(`git push -u origin "${branch}"`, { cwd, timeout: 60_000 });
|
||||||
|
|
||||||
|
return { success: true, message, branch };
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
const cleaned = (err.message || '').replace(new RegExp(apiToken, 'g'), '***');
|
const cleaned = (err.message || '').replace(new RegExp(apiToken, 'g'), '***');
|
||||||
return { error: `Git operation failed: ${cleaned}` };
|
return { error: `Git operation failed: ${cleaned}` };
|
||||||
|
|||||||
Reference in New Issue
Block a user