git-bolt
Version:
Git utilities for faster development
70 lines (61 loc) • 2.21 kB
JavaScript
import chalk from 'chalk';
import fs from 'fs';
import git from 'isomorphic-git';
import http from 'isomorphic-git/http/node/index.js';
import { getGitHubToken } from '../utils/auth.js';
import { closeReadlineInterface } from '../utils/input.js';
async function pullCommand(options) {
try {
// Load GitHub token
const token = getGitHubToken();
if (!token) {
console.error(chalk.red('Error: GitHub token not found'));
console.log('Set it using:');
console.log(' - The GITHUB_TOKEN environment variable');
console.log(' - A .env file with GITHUB_TOKEN=your_token');
process.exit(1);
}
// Get current branch or use the branch specified as option
const branch = options.branch || await git.currentBranch({ fs, dir: '.' });
if (!branch) {
console.error(chalk.red('Error: Could not determine current branch and no branch specified'));
console.log('Use: npx git-bolt pull -b <branch-name>');
process.exit(1);
}
console.log(chalk.blue(`Pulling from branch: ${branch}`));
// Use isomorphic-git's built-in pull function
try {
await git.pull({
fs,
http,
dir: '.',
ref: branch,
remote: 'origin',
fastForwardOnly: true, // Only allow fast-forward merges
onAuth: () => ({ username: token }),
singleBranch: true,
tags: true,
depth: Infinity,
prune: true
});
console.log(chalk.green('✓ Pull completed successfully'));
} catch (pullError) {
if (pullError.code === 'MergeNotSupportedError') {
console.error(chalk.red('Error: Fast-forward merge failed.'));
console.log(chalk.yellow('This pull command only supports fast-forward merges.'));
} else {
console.error(chalk.red('Error during pull:'), pullError.message);
}
process.exit(1);
}
} catch (error) {
console.error(chalk.red('Error during pull:'), error.message);
process.exit(1);
} finally {
// Close any readline interfaces if they were opened
if (typeof closeReadlineInterface === 'function') {
closeReadlineInterface();
}
}
}
export { pullCommand };