| 1 | import { exec, execSync } from "child_process"; |
| 2 | import { promisify } from "util"; |
| 3 | |
| 4 | const execAsync = promisify(exec); |
| 5 | const LARGE_STDIO_BUFFER_BYTES = 10 * 1024 * 1024; // bump buffer for large git output |
| 6 | |
| 7 | /** |
| 8 | * Get the git remote URL for the current repository |
| 9 | * @param remote The remote name (defaults to 'origin') |
| 10 | * @returns The remote URL or null if not found |
| 11 | */ |
| 12 | export function getGitRemoteUrl(remote: string = "origin"): string | null { |
| 13 | try { |
| 14 | const result = execSync(`git remote get-url ${remote}`, { |
| 15 | encoding: "utf-8", |
| 16 | cwd: process.cwd(), |
| 17 | stdio: "pipe", |
| 18 | }); |
| 19 | return result.trim(); |
| 20 | } catch { |
| 21 | return null; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Get the current git branch name |
| 27 | * @returns The current branch name or null if not found |
| 28 | */ |
| 29 | export function getGitBranch(): string | null { |
| 30 | try { |
| 31 | const result = execSync("git branch --show-current", { |
| 32 | encoding: "utf-8", |
| 33 | cwd: process.cwd(), |
| 34 | stdio: "pipe", |
| 35 | }); |
| 36 | return result.trim() || null; |
| 37 | } catch { |
| 38 | return null; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Check if current directory is a git repository |
| 44 | */ |
| 45 | export function isGitRepo(): boolean { |
| 46 | try { |
| 47 | execSync("git rev-parse --is-inside-work-tree", { |
| 48 | stdio: "ignore", |
| 49 | cwd: process.cwd(), |
| 50 | }); |
| 51 | return true; |
| 52 | } catch { |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check if running in GitHub Actions |
| 59 | */ |
| 60 | export function isGitHubActions(): boolean { |
| 61 | return process.env.GITHUB_ACTIONS === "true"; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Check if running in Continue remote agents |
| 66 | */ |
| 67 | export function isContinueRemoteAgent(): boolean { |
| 68 | return process.env.CONTINUE_REMOTE === "true"; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get repository URL from GitHub Actions environment variables |
| 73 | * @returns GitHub repository URL or null if not available |
| 74 | */ |
| 75 | export function getGitHubActionsRepoUrl(): string | null { |
| 76 | if (!isGitHubActions()) { |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | // GITHUB_REPOSITORY is in format "owner/repo" |
| 81 | const githubRepository = process.env.GITHUB_REPOSITORY; |
| 82 | if (!githubRepository) { |
| 83 | return null; |
| 84 | } |
| 85 | |
| 86 | // GITHUB_SERVER_URL defaults to https://github.com but can be different for GitHub Enterprise |
| 87 | const serverUrl = process.env.GITHUB_SERVER_URL || "https://github.com"; |
| 88 | |
| 89 | return `${serverUrl}/${githubRepository}`; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the current repository URL with enhanced detection for CI environments |
| 94 | * Priority order: |
| 95 | * 1. GitHub Actions environment variables (if running in GitHub Actions) |
| 96 | * 2. Git remote URL (if in a git repository) |
| 97 | * 3. Current working directory (fallback) |
| 98 | * @returns Repository URL or local path |
| 99 | */ |
| 100 | export function getRepoUrl(): string { |
| 101 | // First, check if we're in GitHub Actions and can get repo URL from env vars |
| 102 | const githubActionsUrl = getGitHubActionsRepoUrl(); |
| 103 | if (githubActionsUrl) { |
| 104 | return githubActionsUrl; |
| 105 | } |
| 106 | |
| 107 | // Then check if we're in a git repository |
| 108 | if (!isGitRepo()) { |
| 109 | return process.cwd(); |
| 110 | } |
| 111 | |
| 112 | // Try to get the remote URL from git |
| 113 | const remoteUrl = getGitRemoteUrl(); |
| 114 | const url = remoteUrl || process.cwd(); |
| 115 | return url.endsWith(".git") ? url.slice(0, -4) : url; |
| 116 | } |
| 117 | |
| 118 | export interface GitDiffSnapshot { |
| 119 | diff: string; |
| 120 | repoFound: boolean; |
| 121 | } |
| 122 | |
| 123 | function isExecError( |
| 124 | error: unknown, |
| 125 | ): error is { code?: number; stdout?: string } { |
| 126 | return ( |
| 127 | typeof error === "object" && |
| 128 | error !== null && |
| 129 | ("code" in error || "stdout" in error) |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | export async function getGitDiffSnapshot(): Promise<GitDiffSnapshot> { |
| 134 | try { |
| 135 | await execAsync("git rev-parse --git-dir", { |
| 136 | maxBuffer: LARGE_STDIO_BUFFER_BYTES, |
| 137 | }); |
| 138 | } catch (error) { |
| 139 | if (isExecError(error) && error.code === 128) { |
| 140 | return { diff: "", repoFound: false }; |
| 141 | } |
| 142 | throw error; |
| 143 | } |
| 144 | |
| 145 | try { |
| 146 | const { stdout } = await execAsync("git diff main", { |
| 147 | maxBuffer: LARGE_STDIO_BUFFER_BYTES, |
| 148 | }); |
| 149 | return { diff: stdout, repoFound: true }; |
| 150 | } catch (error) { |
| 151 | if (isExecError(error)) { |
| 152 | if (error.code === 1 && error.stdout) { |
| 153 | return { diff: error.stdout, repoFound: true }; |
| 154 | } |
| 155 | |
| 156 | if (error.code === 128) { |
| 157 | return { diff: "", repoFound: false }; |
| 158 | } |
| 159 | } |
| 160 | throw error; |
| 161 | } |
| 162 | } |