|
| 1 | +/* eslint-disable no-restricted-globals */ |
| 2 | +import { spawn } from 'child_process'; |
| 3 | +import * as fs from 'fs/promises'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +export class GitFixture { |
| 7 | + constructor(private readonly repoPath: string) {} |
| 8 | + |
| 9 | + get repoDir(): string { |
| 10 | + return this.repoPath; |
| 11 | + } |
| 12 | + |
| 13 | + async init(): Promise<void> { |
| 14 | + await fs.mkdir(this.repoPath, { recursive: true }); |
| 15 | + await this.git('init'); |
| 16 | + // Configure user for commits |
| 17 | + await this.git('config', 'user.email', '[email protected]'); |
| 18 | + await this.git('config', 'user.name', 'Your Name'); |
| 19 | + // Initial commit to have a HEAD |
| 20 | + await this.commit('Initial commit'); |
| 21 | + } |
| 22 | + |
| 23 | + async commit(message: string, fileName: string = 'test-file.txt', content: string = 'content'): Promise<void> { |
| 24 | + const filePath = path.join(this.repoPath, fileName); |
| 25 | + await fs.writeFile(filePath, content); |
| 26 | + await this.git('add', fileName); |
| 27 | + await this.git('commit', '-m', message); |
| 28 | + } |
| 29 | + |
| 30 | + async branch(name: string): Promise<void> { |
| 31 | + await this.git('branch', name); |
| 32 | + } |
| 33 | + |
| 34 | + async checkout(name: string, create: boolean = false): Promise<void> { |
| 35 | + if (create) { |
| 36 | + await this.git('checkout', '-b', name); |
| 37 | + } else { |
| 38 | + await this.git('checkout', name); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + async worktree(worktreePath: string, branch: string): Promise<void> { |
| 43 | + await this.git('worktree', 'add', worktreePath, branch); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Start an interactive rebase. This will open the rebase editor. |
| 48 | + * @param onto The commit or ref to rebase onto (e.g., 'HEAD~3') |
| 49 | + * @param env Additional environment variables |
| 50 | + */ |
| 51 | + async rebaseInteractive(onto: string, env?: Record<string, string>): Promise<void> { |
| 52 | + await this.git('rebase', '-i', onto, env); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Abort an in-progress rebase |
| 57 | + */ |
| 58 | + async rebaseAbort(): Promise<void> { |
| 59 | + await this.git('rebase', '--abort'); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get the current branch name |
| 64 | + */ |
| 65 | + async getCurrentBranch(): Promise<string> { |
| 66 | + return this.git('rev-parse', '--abbrev-ref', 'HEAD'); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Get the short SHA of a ref |
| 71 | + */ |
| 72 | + async getShortSha(ref: string = 'HEAD'): Promise<string> { |
| 73 | + return this.git('rev-parse', '--short', ref); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Check if a rebase is in progress |
| 78 | + */ |
| 79 | + async isRebaseInProgress(): Promise<boolean> { |
| 80 | + try { |
| 81 | + await this.git('rev-parse', '--verify', 'REBASE_HEAD'); |
| 82 | + return true; |
| 83 | + } catch { |
| 84 | + return false; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Reset the repository to a specific ref |
| 90 | + * @param ref The ref to reset to (default: HEAD) |
| 91 | + * @param mode The reset mode: 'soft', 'mixed', or 'hard' (default: 'hard') |
| 92 | + */ |
| 93 | + async reset(ref: string = 'HEAD', mode: 'soft' | 'mixed' | 'hard' = 'hard'): Promise<void> { |
| 94 | + await this.git('reset', `--${mode}`, ref); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Clean untracked files and directories |
| 99 | + */ |
| 100 | + async clean(): Promise<void> { |
| 101 | + await this.git('clean', '-fd'); |
| 102 | + } |
| 103 | + |
| 104 | + private async git(command: string, ...args: (string | Record<string, string> | undefined)[]): Promise<string> { |
| 105 | + // Separate command args from env options |
| 106 | + const cmdArgs: string[] = []; |
| 107 | + let envOverrides: Record<string, string> | undefined; |
| 108 | + |
| 109 | + for (const arg of args) { |
| 110 | + if (typeof arg === 'string') { |
| 111 | + cmdArgs.push(arg); |
| 112 | + } else if (typeof arg === 'object' && arg !== null) { |
| 113 | + envOverrides = arg; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + const fullArgs = [command, ...cmdArgs]; |
| 118 | + return new Promise((resolve, reject) => { |
| 119 | + const child = spawn('git', fullArgs, { |
| 120 | + cwd: this.repoPath, |
| 121 | + env: envOverrides ? { ...process.env, ...envOverrides } : process.env, |
| 122 | + }); |
| 123 | + let stdout = ''; |
| 124 | + let stderr = ''; |
| 125 | + |
| 126 | + child.stdout.on('data', (data: string | Buffer) => (stdout += data.toString())); |
| 127 | + child.stderr.on('data', (data: string | Buffer) => (stderr += data.toString())); |
| 128 | + |
| 129 | + child.on('close', code => { |
| 130 | + if (code === 0) { |
| 131 | + resolve(stdout.trim()); |
| 132 | + } else { |
| 133 | + reject(new Error(`Git command failed: git ${fullArgs.join(' ')}\n${stderr}`)); |
| 134 | + } |
| 135 | + }); |
| 136 | + }); |
| 137 | + } |
| 138 | +} |
0 commit comments