All files / backend/src/embgit embgit.ts

3.86% Statements 7/181
0% Branches 0/52
4.76% Functions 1/21
3.86% Lines 7/181

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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523                                              2x                                                           9x 9x 9x 9x 9x   9x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
/**
 * Embgit - Embedded Git Client
 *
 * Wrapper around the embedded git binary for git operations.
 * The embedded git binary is a custom Go-based git client included with Quiqr.
 */
 
import path from 'path';
import fs from 'fs-extra';
import crypto from 'crypto';
import { execFile } from 'child_process';
import { promisify } from 'util';
import {
  quiqrSiteRepoInfoSchema,
  hugoThemeRepoInfoSchema,
  commitLogSchema,
  type QuiqrSiteRepoInfo,
  type HugoThemeRepoInfo,
  type CommitEntry
} from '@quiqr/types';
import type { PathHelper, EnvironmentInfo } from '../utils/path-helper.js';
import type { OutputConsole, AppInfoAdapter } from '../adapters/types.js';
 
const execFileAsync = promisify(execFile);
 
/**
 * User configuration for git operations
 */
export interface EmbgitUserConfig {
  email: string;
  name: string;
  machine: string;
  privateKey: string | null;
}
 
/**
 * Embgit class - Manages git operations using the embedded git binary
 */
export class Embgit {
  private userconf: EmbgitUserConfig;
  private pathHelper: PathHelper;
  private outputConsole: OutputConsole;
  private appInfo: AppInfoAdapter;
  private rootPath: string;
  private environmentInfo: EnvironmentInfo;
 
  constructor(
    pathHelper: PathHelper,
    outputConsole: OutputConsole,
    appInfo: AppInfoAdapter,
    rootPath: string,
    environmentInfo: EnvironmentInfo
  ) {
    this.pathHelper = pathHelper;
    this.outputConsole = outputConsole;
    this.appInfo = appInfo;
    this.rootPath = rootPath;
    this.environmentInfo = environmentInfo;
 
    this.userconf = {
      email: 'anonymous@quiqr.org',
      name: 'anonymous',
      machine: 'unknown-machine',
      privateKey: null,
    };
  }
 
  /**
   * Set user configuration for git operations
   */
  setUserConf(email: string, name: string): void {
    this.userconf.email = email;
    this.userconf.name = name;
  }
 
  /**
   * Set the path to the private key file
   */
  setPrivateKeyPath(keyPath: string): void {
    this.userconf.privateKey = keyPath;
  }
 
  /**
   * Create a temporary private key file from key contents
   */
  async createTemporaryPrivateKey(keyContents: string): Promise<string> {
    const tmpkeypathPrivate = path.join(this.pathHelper.getTempDir(), 'ghkey');
    await fs.writeFile(tmpkeypathPrivate, keyContents, 'utf-8');
    await fs.chmod(tmpkeypathPrivate, 0o600);
    return tmpkeypathPrivate;
  }
 
  /**
   * Get the path to the embedded git binary
   */
  getGitBin(): string {
    // Check for custom EMBGIT_PATH (for development)
    if (process.env.EMBGIT_PATH) {
      return process.env.EMBGIT_PATH;
    }
 
    let platform: string;
    let executable: string;
 
    switch (process.platform) {
      case 'linux':
        platform = 'linux';
        executable = 'embgit';
        break;
      case 'win32':
        platform = 'win';
        executable = 'embgit.exe';
        break;
      case 'darwin':
        platform = 'mac';
        executable = 'embgit';
        break;
      default:
        throw new Error('Platform not supported for embgit');
    }
 
    if (this.appInfo.isPackaged()) {
      return path.join(this.pathHelper.getApplicationResourcesDir(this.environmentInfo), platform, executable);
    } else {
      return path.join(this.rootPath, 'resources', platform, executable);
    }
  }
 
  /**
   * Execute embgit command
   */
  private async executeEmbgit(args: string[], logMessage?: string, cwd?: string): Promise<string> {
    const gitBinary = this.getGitBin();
 
    if (logMessage) {
      this.outputConsole.appendLine(logMessage);
    }
 
    try {
      const { stdout } = await execFileAsync(gitBinary, args, {
        windowsHide: true,
        timeout: 300000, // 5 minutes
        maxBuffer: 1024 * 1024 * 10, // 10MB
        cwd,
      });
      return stdout;
    } catch (error) {
      let errorMessage = "Unknown error";
 
      if (error instanceof Error) {
        errorMessage = error.message;
      }
 
      if (typeof error === "object" && error !== null) {
        if ("stderr" in error && (typeof error.stderr === "string" || Buffer.isBuffer(error.stderr))) {
          errorMessage = error.stderr.toString();
        } else if ("stdout" in error && (typeof error.stdout === "string" || Buffer.isBuffer(error.stdout))) {
          errorMessage = error.stdout.toString();
        }
      }
 
      this.outputConsole.appendLine(`Embgit error: ${errorMessage}`);
      throw error;
    }
  }
 
  /**
   * Reset hard - discard all local changes
   */
  async reset_hard(destination_path: string): Promise<boolean> {
    try {
      await this.executeEmbgit(
        ['reset_hard', destination_path],
        `Resetting hard: ${destination_path}`
      );
      this.outputConsole.appendLine('Reset success ...');
      return true;
    } catch (error) {
      this.outputConsole.appendLine(`Reset hard failed: ${destination_path}`);
      throw error;
    }
  }
 
  /**
   * Show repository information for a Quiqr site
   */
  async repo_show_quiqrsite(url: string): Promise<QuiqrSiteRepoInfo> {
    try {
      const output = await this.executeEmbgit(
        ['repo_show_quiqrsite', url],
        `Checking Quiqr site repo: ${url}`
      );
      const parsed = quiqrSiteRepoInfoSchema.safeParse(JSON.parse(output));
      if (!parsed.success) {
        this.outputConsole.appendLine(`Invalid response from repo_show_quiqrsite: ${parsed.error.message}`);
        throw new Error(`Invalid response from embgit repo_show_quiqrsite: ${parsed.error.message}`);
      }
      return parsed.data;
    } catch (error) {
      this.outputConsole.appendLine(`repo_show_quiqrsite failed: ${url}`);
      throw error;
    }
  }
 
  /**
   * Show repository information for a Hugo theme
   */
  async repo_show_hugotheme(url: string): Promise<HugoThemeRepoInfo> {
    try {
      const output = await this.executeEmbgit(
        ['repo_show_hugotheme', url],
        `Checking Hugo theme repo: ${url}`
      );
      const parsed = hugoThemeRepoInfoSchema.safeParse(JSON.parse(output));
      if (!parsed.success) {
        this.outputConsole.appendLine(`Invalid response from repo_show_hugotheme: ${parsed.error.message}`);
        throw new Error(`Invalid response from embgit repo_show_hugotheme: ${parsed.error.message}`);
      }
      return parsed.data;
    } catch (error) {
      this.outputConsole.appendLine(`repo_show_hugotheme failed: ${url}`);
      throw error;
    }
  }
 
  /**
   * Pull changes from remote repository
   */
  async pull(destination_path: string): Promise<boolean> {
    if (!this.userconf.privateKey) {
      throw new Error('Private key not set for git pull');
    }
 
    try {
      await this.executeEmbgit(
        ['pull', '-s', '-i', this.userconf.privateKey, destination_path],
        `Pulling from remote: ${destination_path}`
      );
      return true;
    } catch (error) {
      this.outputConsole.appendLine(`Git pull failed: ${destination_path}`);
      throw error;
    }
  }
 
  /**
   * Clone a repository from a public URL
   */
  async cloneFromPublicUrl(url: string, destination_path: string): Promise<boolean> {
    try {
      await this.executeEmbgit(
        ['clone', '-s', url, destination_path],
        `Cloning from public URL: ${url} to ${destination_path}`
      );
      this.outputConsole.appendLine('Clone success ...');
      return true;
    } catch (error) {
      this.outputConsole.appendLine(`Clone error: ${url}`);
      throw error;
    }
  }
 
  /**
   * Clone a private repository using a private key
   */
  async clonePrivateWithKey(
    url: string,
    destination_path: string,
    privateKey: string
  ): Promise<boolean> {
    const privateKeyPath = await this.createTemporaryPrivateKey(privateKey);
 
    try {
      await this.executeEmbgit(
        ['clone', '-s', '-i', privateKeyPath, url, destination_path],
        `Cloning private repo: ${url} to ${destination_path}`
      );
      this.outputConsole.appendLine('Clone success ...');
      return true;
    } catch (error) {
      this.outputConsole.appendLine(`Clone error: ${url}`);
      throw error;
    }
  }
 
  /**
   * Clone a repository using the configured private key
   */
  async cloneWithKey(url: string, destination_path: string): Promise<boolean> {
    if (!this.userconf.privateKey) {
      throw new Error('Private key not set for git clone');
    }
 
    try {
      await this.executeEmbgit(
        ['clone', '-s', '-i', this.userconf.privateKey, url, destination_path],
        `Cloning with key: ${url} to ${destination_path}`
      );
      this.outputConsole.appendLine('Clone success ...');
      return true;
    } catch (error) {
      this.outputConsole.appendLine(`Clone error: ${url}`);
      throw error;
    }
  }
 
  /**
   * Add all files to git staging area
   */
  async addAll(destination_path: string): Promise<boolean> {
    try {
      await this.executeEmbgit(
        ['add_all', destination_path],
        `Adding all files: ${destination_path}`
      );
      this.outputConsole.appendLine('Git add all success ...');
      return true;
    } catch (error) {
      this.outputConsole.appendLine(`Git add all failed: ${destination_path}`);
      throw error;
    }
  }
 
  /**
   * Commit changes
   */
  async commit(
    destination_path: string,
    message: string,
    authorName?: string,
    authorEmail?: string
  ): Promise<boolean> {
    const name = authorName || this.userconf.name;
    const email = authorEmail || this.userconf.email;
 
    try {
      await this.executeEmbgit(
        ['commit', '-a', '-n', name, '-e', email, '-m', message, destination_path],
        `Committing changes: ${destination_path}`
      );
      this.outputConsole.appendLine('Git commit success ...');
      return true;
    } catch (error) {
      this.outputConsole.appendLine(`Git commit failed: ${destination_path}`);
      throw error;
    }
  }
 
  /**
   * Push changes to remote repository
   */
  async push(destination_path: string, privateKeyPath?: string): Promise<boolean> {
    const keyPath = privateKeyPath || this.userconf.privateKey;
    if (!keyPath) {
      throw new Error('Private key not set for git push');
    }
 
    try {
      await this.executeEmbgit(
        ['push', '-s', '-i', keyPath, destination_path],
        `Pushing to remote: ${destination_path}`
      );
      this.outputConsole.appendLine('Git push success ...');
      return true;
    } catch (error) {
      this.outputConsole.appendLine(`Git push failed: ${destination_path}`);
      throw error;
    }
  }
 
  /**
   * Get remote commit history
   */
  async logRemote(url: string, privateKeyPath?: string): Promise<CommitEntry[]> {
    const keyPath = privateKeyPath || this.userconf.privateKey;
    if (!keyPath) {
      throw new Error('Private key not set for git log remote');
    }
 
    try {
      const output = await this.executeEmbgit(
        ['log_remote', '-s', '-i', keyPath, url],
        `Getting remote commits: ${url}`
      );
      const parsed = commitLogSchema.safeParse(JSON.parse(output));
      if (!parsed.success) {
        this.outputConsole.appendLine(`Invalid response from log_remote: ${parsed.error.message}`);
        throw new Error(`Invalid response from embgit log_remote: ${parsed.error.message}`);
      }
      return parsed.data;
    } catch (error) {
      this.outputConsole.appendLine(`Git log remote failed: ${url}`);
      throw error;
    }
  }
 
  /**
   * Get local commit history
   */
  async logLocal(destination_path: string): Promise<CommitEntry[]> {
    try {
      const output = await this.executeEmbgit(
        ['log_local', destination_path],
        `Getting local commits: ${destination_path}`
      );
      const parsed = commitLogSchema.safeParse(JSON.parse(output));
      if (!parsed.success) {
        this.outputConsole.appendLine(`Invalid response from log_local: ${parsed.error.message}`);
        throw new Error(`Invalid response from embgit log_local: ${parsed.error.message}`);
      }
      return parsed.data;
    } catch (error) {
      this.outputConsole.appendLine(`Git log local failed: ${destination_path}`);
      throw error;
    }
  }
 
  /**
   * Checkout a specific ref/commit
   */
  async checkout(ref: string, destination_path: string): Promise<boolean> {
    try {
      await this.executeEmbgit(
        ['checkout', '-r', ref, destination_path],
        `Checking out ref ${ref}: ${destination_path}`
      );
      this.outputConsole.appendLine('Git checkout success ...');
      return true;
    } catch (error) {
      this.outputConsole.appendLine(`Git checkout failed: ${destination_path}`);
      throw error;
    }
  }
 
  /**
   * Generate SSH key pair (ECDSA)
   */
  async generateKeyPair(): Promise<{ privateKey: string; publicKey: string }> {
    const tempDir = this.pathHelper.getTempDir();
 
    try {
      await this.executeEmbgit(['keygen_ecdsa'], `Generating SSH key pair`, tempDir);
 
      const privateKey = await fs.readFile(path.join(tempDir, 'id_ecdsa_quiqr'), 'utf-8');
      const publicKey = await fs.readFile(path.join(tempDir, 'id_ecdsa_quiqr.pub'), 'utf-8');
 
      // Clean up key files
      await fs.unlink(path.join(tempDir, 'id_ecdsa_quiqr'));
      await fs.unlink(path.join(tempDir, 'id_ecdsa_quiqr.pub'));
 
      this.outputConsole.appendLine('SSH key pair generated successfully');
      return { privateKey, publicKey };
    } catch (error) {
      this.outputConsole.appendLine('SSH key generation failed');
      throw error;
    }
  }
 
  /**
   * Derive public key from private key (ECDSA)
   * Returns the public key in OpenSSH format
   * Supports P-256 (nistp256), P-384 (nistp384), and P-521 (nistp521) curves
   */
  derivePublicKeyFromPrivate(privateKeyPem: string): string {
    try {
      // Create key object from private key PEM
      const privateKey = crypto.createPrivateKey(privateKeyPem);
 
      // Get the public key
      const publicKey = crypto.createPublicKey(privateKey);
 
      // Export as JWK to get the raw key data
      const jwk = publicKey.export({ format: 'jwk' }) as crypto.JsonWebKey;
 
      // Map JWK curve names to SSH curve names
      const curveMap: Record<string, { keyType: string; curve: string }> = {
        'P-256': { keyType: 'ecdsa-sha2-nistp256', curve: 'nistp256' },
        'P-384': { keyType: 'ecdsa-sha2-nistp384', curve: 'nistp384' },
        'P-521': { keyType: 'ecdsa-sha2-nistp521', curve: 'nistp521' },
      };
 
      const curveInfo = curveMap[jwk.crv!];
      if (!curveInfo) {
        throw new Error(`Unsupported curve: ${jwk.crv}`);
      }
 
      const { keyType, curve } = curveInfo;
 
      // Decode the x and y coordinates from base64url
      const x = Buffer.from(jwk.x!, 'base64url');
      const y = Buffer.from(jwk.y!, 'base64url');
 
      // Create uncompressed point (0x04 || x || y)
      const point = Buffer.concat([Buffer.from([0x04]), x, y]);
 
      // Build the SSH key blob
      // Format: [length][key-type][length][curve][length][point]
      const keyTypeLen = Buffer.alloc(4);
      keyTypeLen.writeUInt32BE(keyType.length);
 
      const curveLen = Buffer.alloc(4);
      curveLen.writeUInt32BE(curve.length);
 
      const pointLen = Buffer.alloc(4);
      pointLen.writeUInt32BE(point.length);
 
      const blob = Buffer.concat([
        keyTypeLen,
        Buffer.from(keyType),
        curveLen,
        Buffer.from(curve),
        pointLen,
        point,
      ]);
 
      return `${keyType} ${blob.toString('base64')}`;
    } catch (error) {
      this.outputConsole.appendLine(`Failed to derive public key: ${error}`);
      throw error;
    }
  }
}