fix: clean git credentials before push to avoid double-auth URLs

Made-with: Cursor
This commit is contained in:
2026-02-26 15:01:42 -08:00
parent d3c3270309
commit 45bca13479
2 changed files with 32 additions and 14 deletions

22
dist/tools.js vendored
View File

@@ -377,13 +377,21 @@ async function gitCommitAndPush(message, ctx) {
try {
await execAsync('git add -A', { cwd });
await execAsync(`git commit -m "${message.replace(/"/g, '\\"')}"`, { cwd });
// Ensure remote has credentials
const remoteUrl = (await execAsync('git remote get-url origin', { cwd })).stdout.trim();
const authedUrl = remoteUrl.startsWith('https://')
? remoteUrl.replace('https://', `https://${username}:${apiToken}@`)
: remoteUrl;
await execAsync(`git push "${authedUrl}" HEAD`, { cwd, timeout: 60000 });
return { success: true, message };
// Get current remote URL, strip any existing credentials, re-inject cleanly
let remoteUrl = '';
try {
remoteUrl = (await execAsync('git remote get-url origin', { cwd })).stdout.trim();
}
catch { /* no remote yet */ }
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) {
const cleaned = (err.message || '').replace(new RegExp(apiToken, 'g'), '***');