UNPKG

git-bolt

Version:

Git utilities for faster development

82 lines (75 loc) 2.58 kB
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 { askQuestion, closeReadlineInterface } from '../utils/input.js'; async function getCurrentBranch() { try { const currentBranch = await git.currentBranch({ fs, dir: '.', fullname: false }); return currentBranch || 'main'; } catch (error) { return 'main'; } } async function isMergeCommit() { try { const head = await git.resolveRef({ fs, dir: '.', ref: 'HEAD' }); const commit = await git.readCommit({ fs, dir: '.', oid: head }); return commit.commit.parent.length > 1; } catch (error) { return false; } } async function pushCommand(options) { try { // Get token let token = await getGitHubToken(); if (!token) { token = await askQuestion('Enter your GitHub personal access token: '); } const currentBranch = await getCurrentBranch(); console.log(chalk.blue(`Pushing to branch: ${currentBranch}`)); try { // First try normal push await git.push({ fs, http, dir: '.', remote: 'origin', ref: currentBranch, onAuth: () => ({ username: token, password: token }) }); console.log(chalk.green('Push completed successfully')); } catch (error) { // If push fails and we have a merge commit, try force push if (error.message.includes('fast-forward') && await isMergeCommit()) { console.log(chalk.yellow('Detected merge commit, using force push...')); await git.push({ fs, http, dir: '.', remote: 'origin', ref: currentBranch, force: true, onAuth: () => ({ username: token, password: token }) }); console.log(chalk.green('Force push completed successfully')); } else { throw error; } } } catch (error) { console.error(chalk.red('Error during push:'), error.message); process.exit(1); } finally { closeReadlineInterface(); } } export { pushCommand };