All files / src/models git.js

81.67% Statements 156/191
58.33% Branches 7/12
35.29% Functions 6/17
81.67% Lines 156/191

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 192193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 10x 10x 203x 203x 203x 203x 203x 203x 203x 10x 10x 10x     10x 10x 10x 10x 10x 10x 10x 203x 203x 203x 203x 203x 203x 203x 203x 25x 25x 25x 203x 203x 203x 203x 203x 203x 203x 25x 25x 25x     25x 203x 203x 203x 203x 203x 203x 203x 203x 203x 203x 25x 25x 25x 25x 25x     25x 203x 203x 203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x                   203x 203x 203x 203x 203x 203x     203x 203x 203x 203x 203x 203x     203x  
import fs from 'node:fs';
import path from 'node:path';
 
import { isFeatureEnabled } from '../config/features.js';
import { Logger } from '../logger.js';
import { findPath } from './fs-utils.js';
 
/**
 * Abstract base class for git operations.
 * Implementations: GitIsomorphic (JS-based) and GitSystem (system git command)
 */
export class Git {
  /** @type {Git | null} */
  static #instance = null;
 
  /** @type {string} */
  #name;
 
  /**
   * @param {string} name - Backend name for logging purposes
   */
  constructor(name) {
    this.#name = name;
  }
 
  /**
   * Get the git implementation based on the feature flag.
   * Returns a cached instance if available.
   * @returns {Promise<Git>}
   */
  static async get() {
    if (Git.#instance == null) {
      const useSystemGit = await isFeatureEnabled('system-git');
      if (useSystemGit) {
        const { GitSystem } = await import('./git-system.js');
        Git.#instance = new GitSystem();
      } else {
        const { GitIsomorphic } = await import('./git-isomorphic.js');
        Git.#instance = new GitIsomorphic();
      }
    }
    return Git.#instance;
  }
 
  /**
   * Log a debug message for git operations
   * @protected
   * @param {string} operation - The operation name
   * @param {...string} args - Additional arguments to log
   */
  _debug(operation, ...args) {
    const argsStr = args.length > 0 ? ` ${args.join(' ')}` : '';
    Logger.debug(`git(${this.#name}): ${operation}${argsStr}`);
  }
 
  /**
   * Get the repository directory
   * @protected
   * @returns {Promise<string>}
   */
  async _getRepoDir() {
    try {
      return await findPath('.', '.git');
    } catch {
      throw new Error('Could not find the .git folder');
    }
  }
 
  /**
   * Check if the repository is a linked git worktree.
   * In a linked worktree (created with `git worktree add`), the top-level `.git`
   * is a file holding a `gitdir:` pointer instead of a directory.
   * @protected
   * @param {string} [dir] - Repository directory (resolved automatically when omitted)
   * @returns {Promise<boolean>}
   */
  async _isLinkedWorktree(dir) {
    const repoDir = dir ?? (await this._getRepoDir());
    try {
      const stats = await fs.promises.stat(path.join(repoDir, '.git'));
      return stats.isFile();
    } catch {
      return false;
    }
  }
 
  /**
   * Add a remote to the repository
   * @param {string} remoteName
   * @param {string} url
   * @returns {Promise<void>}
   */
  async addRemote(remoteName, url) {
    throw new Error('Not implemented');
  }
 
  /**
   * Resolve a short commit ID to its full form
   * @param {string | null} commitId
   * @returns {Promise<string | null>}
   */
  async resolveFullCommitId(commitId) {
    throw new Error('Not implemented');
  }
 
  /**
   * Get the commit SHA of the master branch on a remote
   * @param {string} remoteUrl
   * @returns {Promise<string | undefined>}
   */
  async getRemoteCommit(remoteUrl) {
    throw new Error('Not implemented');
  }
 
  /**
   * Get the full ref name for a branch
   * @param {string} branchName - Branch name, or empty string for current branch
   * @returns {Promise<string>}
   */
  async getFullBranch(branchName) {
    throw new Error('Not implemented');
  }
 
  /**
   * Get the commit SHA for a branch or tag
   * @param {string} refspec
   * @returns {Promise<string>}
   */
  async getBranchCommit(refspec) {
    throw new Error('Not implemented');
  }
 
  /**
   * Check if a tag exists
   * @param {string} tag
   * @returns {Promise<boolean>}
   */
  async isExistingTag(tag) {
    throw new Error('Not implemented');
  }
 
  /**
   * Push to a remote repository
   * @param {string} remoteUrl
   * @param {string} branchRefspec
   * @param {boolean} force
   * @returns {Promise<object>}
   */
  async push(remoteUrl, branchRefspec, force) {
    throw new Error('Not implemented');
  }
 
  /**
   * List local branches (for autocompletion)
   * @returns {Promise<string[]>}
   */
  async completeBranches() {
    throw new Error('Not implemented');
  }
 
  /**
   * Check if the repository is a shallow clone
   * @returns {Promise<boolean>}
   */
  async isShallow() {
    this._debug('isShallow');
    const dir = await this._getRepoDir();
    try {
      await fs.promises.access(path.join(dir, '.git', 'shallow'));
      return true;
    } catch {
      return false;
    }
  }
 
  /**
   * Check if the current directory is inside a git repository
   * @returns {Promise<boolean>}
   */
  async isInsideGitRepo() {
    throw new Error('Not implemented');
  }
 
  /**
   * Check if the git working directory is clean (no uncommitted changes)
   * @returns {Promise<boolean>}
   */
  async isGitWorkingDirectoryClean() {
    throw new Error('Not implemented');
  }
}