autoagent-cli
Version:
Run autonomous AI agents using Claude or Gemini for task execution
73 lines (72 loc) • 3.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitPushHook = void 0;
const git_js_1 = require("../utils/git.js");
class GitPushHook {
async execute(_data, config) {
const currentBranch = await (0, git_js_1.getCurrentBranch)();
if (currentBranch === null) {
return {
blocked: false,
output: 'Cannot push: Currently in detached HEAD state. Please checkout a branch first.',
};
}
const remote = config.remote ?? 'origin';
const validation = await (0, git_js_1.validateRemoteForPush)(remote);
if (!validation.isValid) {
const errorMessage = validation.errors.join('. ');
const suggestionsMessage = validation.suggestions.length > 0
? '\n\nSuggestions:\n' + validation.suggestions.map(s => ` - ${s}`).join('\n')
: '';
return {
blocked: false,
output: `Failed to push: ${errorMessage}${suggestionsMessage}`,
};
}
const pushResult = await (0, git_js_1.pushToRemote)({
remote,
branch: currentBranch,
setUpstream: true
});
if (pushResult.success) {
return {
blocked: false,
output: `Successfully pushed to ${pushResult.remote}/${pushResult.branch}`,
};
}
else {
let errorMessage = `Failed to push to ${remote}/${currentBranch}`;
if (pushResult.error !== undefined) {
if (pushResult.error.includes('Authentication failed') ||
pushResult.error.includes('Permission denied') ||
pushResult.error.includes('Could not read from remote repository')) {
errorMessage += ': Authentication failed. Please check your git credentials.';
}
else if (pushResult.error.includes('rejected') ||
pushResult.error.includes('non-fast-forward')) {
errorMessage += ': Push rejected. The remote contains work that you do not have locally.';
errorMessage += '\n\nTo resolve:\n';
errorMessage += ` 1. Pull the latest changes: git pull ${remote} ${currentBranch}\n`;
errorMessage += ' 2. Resolve any conflicts\n';
errorMessage += ' 3. Try pushing again';
}
else if (pushResult.error.includes('Could not resolve host') ||
pushResult.error.includes('unable to access') ||
pushResult.error.includes('Network is unreachable')) {
errorMessage += ': Network error. Please check your internet connection.';
}
else {
errorMessage += `: ${pushResult.error}`;
}
}
if (pushResult.stderr !== undefined) {
errorMessage += `\n\nGit output:\n${pushResult.stderr}`;
}
return {
blocked: false,
output: errorMessage,
};
}
}
}
exports.GitPushHook = GitPushHook;