Files
automaker/apps/server/src/routes/worktree/routes/push.ts
SuperComboGamer 6c669fbe6a refactor: replace git command for branch retrieval
- Updated multiple route handlers to use 'git symbolic-ref --short HEAD' instead of 'git rev-parse --abbrev-ref HEAD' for retrieving the current branch name, improving consistency and clarity in branch management across the application.
2026-01-06 00:04:52 -05:00

64 lines
1.7 KiB
TypeScript

/**
* POST /push endpoint - Push a worktree branch to remote
*
* Note: Git repository validation (isGitRepo, hasCommits) is handled by
* the requireValidWorktree middleware in index.ts
*/
import type { Request, Response } from 'express';
import { exec } from 'child_process';
import { promisify } from 'util';
import { getErrorMessage, logError } from '../common.js';
const execAsync = promisify(exec);
export function createPushHandler() {
return async (req: Request, res: Response): Promise<void> => {
try {
const { worktreePath, force } = req.body as {
worktreePath: string;
force?: boolean;
};
if (!worktreePath) {
res.status(400).json({
success: false,
error: 'worktreePath required',
});
return;
}
// Get branch name
const { stdout: branchOutput } = await execAsync('git symbolic-ref --short HEAD', {
cwd: worktreePath,
});
const branchName = branchOutput.trim();
// Push the branch
const forceFlag = force ? '--force' : '';
try {
await execAsync(`git push -u origin ${branchName} ${forceFlag}`, {
cwd: worktreePath,
});
} catch {
// Try setting upstream
await execAsync(`git push --set-upstream origin ${branchName} ${forceFlag}`, {
cwd: worktreePath,
});
}
res.json({
success: true,
result: {
branch: branchName,
pushed: true,
message: `Successfully pushed ${branchName} to origin`,
},
});
} catch (error) {
logError(error, 'Push worktree failed');
res.status(500).json({ success: false, error: getErrorMessage(error) });
}
};
}