All files / lib git-ops.js

93.33% Statements 84/90
73.52% Branches 50/68
100% Functions 9/9
93.25% Lines 83/89

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234        1x             7x 7x 4x   3x                   24x                 18x 18x     13x   5x                         7x 2x     5x 1x 1x       4x 4x 1x     3x 3x       2x   1x 1x                           6x 2x     4x 1x 1x     3x 3x 1x     2x   2x 2x 2x 2x 3x 3x   1x 1x     2x   1x 1x 1x           1x 1x                           4x 1x 1x 1x     3x 3x 1x     2x 2x 2x                             5x 1x 1x 1x     4x 4x 1x     3x 3x 3x 2x   1x 1x                       6x 1x 1x     5x 5x 1x     4x   4x     4x       4x       3x 3x     1x 1x            
import { execFile } from 'child_process';
import { promisify } from 'util';
import chalk from 'chalk';
 
const execFileAsync = promisify(execFile);
 
/**
 * Validates that the git binary is available on PATH.
 * @returns {Promise<boolean>} True if git is available
 */
export async function validateGitAvailable() {
  try {
    await execFileAsync('git', ['--version']);
    return true;
  } catch {
    return false;
  }
}
 
/**
 * Validates that a URL is an HTTPS git URL.
 * @param {string} url - The URL to validate
 * @returns {boolean} True if valid HTTPS URL
 */
export function isValidHttpsGitUrl(url) {
  return typeof url === 'string' && url.startsWith('https://');
}
 
/**
 * Checks if a path is a valid git repository.
 * @param {string} path - The directory path to check
 * @returns {Promise<boolean>} True if path is a git repo
 */
export async function isGitRepository(path) {
  try {
    const { stdout } = await execFileAsync('git', ['rev-parse', '--is-inside-work-tree'], {
      cwd: path
    });
    return stdout.trim() === 'true';
  } catch {
    return false;
  }
}
 
/**
 * Clones a git repository to the specified destination.
 * @param {string} url - HTTPS git URL to clone
 * @param {string} dest - Destination directory path
 * @param {Object} options - Optional settings (dryRun)
 * @returns {Promise<string>} Output from git clone
 */
export async function gitClone(url, dest, options = {}) {
  // Validation: allow HTTPS or file:// URLs
  if (!isValidHttpsGitUrl(url) && !url.startsWith('file://')) {
    throw new Error(chalk.red(`Invalid git URL: "${url}". Must start with https:// or file://`));
  }
 
  if (options.dryRun) {
    console.log(chalk.yellow(`[Dry-run] Would clone: ${url} to ${dest}`));
    return `Dry-run: git clone ${url} ${dest}`;
  }
 
  // Check git availability
  const gitAvailable = await validateGitAvailable();
  if (!gitAvailable) {
    throw new Error(chalk.red('Git is not installed or not available on PATH.'));
  }
 
  try {
    const { stdout } = await execFileAsync('git', ['clone', url, dest], {
      timeout: 300000, // 5 minute timeout
      maxBuffer: 10 * 1024 * 1024 // 10MB buffer
    });
    return stdout;
  } catch (err) {
    const errorMsg = err.stderr || err.message || 'Unknown git clone error';
    throw new Error(chalk.red(`Git clone failed: ${errorMsg}`));
  }
}
 
/**
 * Adds a remote to an existing git repository.
 * @param {string} path - Path to existing git repository
 * @param {string} name - Remote name (e.g., 'origin', 'upstream')
 * @param {string} url - HTTPS git URL for the remote
 * @param {Object} options - Optional settings (dryRun)
 * @returns {Promise<string>} Output from git remote add
 */
export async function gitAddRemote(path, name, url, options = {}) {
  // Validation: allow HTTPS or file:// URLs
  if (!isValidHttpsGitUrl(url) && !url.startsWith('file://')) {
    throw new Error(chalk.red(`Invalid git URL: "${url}". Must start with https:// or file://`));
  }
 
  if (options.dryRun) {
    console.log(chalk.yellow(`[Dry-run] Would add remote "${name}": ${url} to ${path}`));
    return `Dry-run: git remote add ${name} ${url}`;
  }
 
  const repoExists = await isGitRepository(path);
  if (!repoExists) {
    throw new Error(chalk.red(`Path is not a git repository: "${path}"`));
  }
 
  try {
    // Check if remote already exists; update if it does
    try {
      const existingRemotes = await execFileAsync('git', ['remote', '-v'], { cwd: path });
      const remoteLines = existingRemotes.stdout.split('\n');
      const remoteExists = remoteLines.some(line => {
        const parts = line.split('\t');
        if (parts[0] !== name) return false;
        // parts[1] contains URL followed by optional ' (fetch)' or ' (push)'
        const remoteUrl = parts[1] ? parts[1].split(' ')[0] : '';
        return remoteUrl === url;
      });
 
      if (remoteExists) {
        // Update remote URL
        const { stdout } = await execFileAsync('git', ['remote', 'set-url', name, url], { cwd: path });
        console.log(chalk.blue(`Remote "${name}" updated to ${url}`));
        return stdout;
      }
    } catch {
      // remote -v failed, continue to add
    }
 
    const { stdout } = await execFileAsync('git', ['remote', 'add', name, url], { cwd: path });
    return stdout;
  } catch (err) {
    const errorMsg = err.stderr || err.message || 'Unknown git remote add error';
    throw new Error(chalk.red(`Git remote add failed: ${errorMsg}`));
  }
}
 
/**
 * Gets the current commit hash of a git repository.
 * @param {string} path - Path to git repository
 * @param {Object} options - Optional settings (dryRun)
 * @returns {Promise<string>} Current commit hash
 */
export async function gitCurrentCommit(path, options = {}) {
  if (options.dryRun) {
    const mockHash = 'abc1234';
    console.log(chalk.yellow(`[Dry-run] Would get current commit: ${mockHash}`));
    return mockHash;
  }
 
  const repoExists = await isGitRepository(path);
  if (!repoExists) {
    throw new Error(chalk.red(`Path is not a git repository: "${path}"`));
  }
 
  try {
    const { stdout } = await execFileAsync('git', ['rev-parse', 'HEAD'], { cwd: path });
    return stdout.trim();
  } catch (err) {
    const errorMsg = err.stderr || err.message || 'Unknown git rev-parse error';
    throw new Error(chalk.red(`Failed to get current commit: ${errorMsg}`));
  }
}
 
/**
 * Fetches updates from a remote in a git repository.
 * @param {string} path - Path to git repository
 * @param {string} [remoteName] - Optional remote name (defaults to all remotes)
 * @param {Object} options - Optional settings (dryRun)
 * @returns {Promise<string>} Output from git fetch
 */
export async function gitFetch(path, remoteName, options = {}) {
  if (options.dryRun) {
    const target = remoteName ? `remote "${remoteName}"` : 'all remotes';
    console.log(chalk.yellow(`[Dry-run] Would fetch from ${target} in ${path}`));
    return `Dry-run: git fetch ${remoteName ? remoteName : ''}`;
  }
 
  const repoExists = await isGitRepository(path);
  if (!repoExists) {
    throw new Error(chalk.red(`Path is not a git repository: "${path}"`));
  }
 
  try {
    const args = remoteName ? ['fetch', remoteName] : ['fetch'];
    const { stdout } = await execFileAsync('git', args, { cwd: path });
    return stdout;
  } catch (err) {
    const errorMsg = err.stderr || err.message || 'Unknown git fetch error';
    throw new Error(chalk.red(`Git fetch failed: ${errorMsg}`));
  }
}
 
/**
 * Gets the number of commits the local branch is behind the remote.
 * @param {string} path - Path to git repository
 * @param {string} remoteName - Remote name to compare against
 * @param {Object} options - Optional settings (dryRun)
 * @returns {Promise<number>} Number of commits behind
 */
export async function gitCommitsBehind(path, remoteName, options = {}) {
  if (options.dryRun) {
    console.log(chalk.yellow(`[Dry-run] Would check commits behind ${remoteName}`));
    return 0;
  }
 
  const repoExists = await isGitRepository(path);
  if (!repoExists) {
    throw new Error(chalk.red(`Path is not a git repository: "${path}"`));
  }
 
  try {
    // First, get the current branch name
    const { stdout: branch } = await execFileAsync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
      cwd: path
    });
    const currentBranch = branch.trim();
 
    // Get the number of commits behind
    // git rev-list --count HEAD..remote/branch
    const { stdout } = await execFileAsync('git', ['rev-list', '--count', `HEAD..${remoteName}/${currentBranch}`], {
      cwd: path
    });
 
    const count = parseInt(stdout.trim(), 10);
    return isNaN(count) ? 0 : count;
  } catch (err) {
    // If the error is about the remote branch not existing, return 0
    Eif (err.stderr && err.stderr.includes('unknown revision or path not in the working tree')) {
      return 0;
    }
    const errorMsg = err.stderr || err.message || 'Unknown git rev-list error';
    throw new Error(chalk.red(`Failed to check commits behind: ${errorMsg}`));
  }
}