Elffuss BraidHub

kikocis/continue public

c1b3fd06027c · 2.6 KiBRawPermalink
1import { execSync } from "child_process";
2
3/**
4 * Get the git remote URL for the current repository
5 * @param remote The remote name (defaults to 'origin')
6 * @returns The remote URL or null if not found
7 */
8export function getGitRemoteUrl(remote: string = "origin"): string | null {
9 try {
10 const result = execSync(`git remote get-url ${remote}`, {
11 encoding: "utf-8",
12 cwd: process.cwd(),
13 stdio: "pipe",
14 });
15 return result.trim();
16 } catch {
17 return null;
18 }
19}
20
21/**
22 * Get the current git branch name
23 * @returns The current branch name or null if not found
24 */
25export function getGitBranch(): string | null {
26 try {
27 const result = execSync("git branch --show-current", {
28 encoding: "utf-8",
29 cwd: process.cwd(),
30 stdio: "pipe",
31 });
32 return result.trim() || null;
33 } catch {
34 return null;
35 }
36}
37
38/**
39 * Check if current directory is a git repository
40 */
41export function isGitRepo(): boolean {
42 try {
43 execSync("git rev-parse --is-inside-work-tree", {
44 stdio: "ignore",
45 cwd: process.cwd(),
46 });
47 return true;
48 } catch {
49 return false;
50 }
51}
52
53/**
54 * Check if running in GitHub Actions
55 */
56export function isGitHubActions(): boolean {
57 return process.env.GITHUB_ACTIONS === "true";
58}
59
60/**
61 * Get repository URL from GitHub Actions environment variables
62 * @returns GitHub repository URL or null if not available
63 */
64export function getGitHubActionsRepoUrl(): string | null {
65 if (!isGitHubActions()) {
66 return null;
67 }
68
69 // GITHUB_REPOSITORY is in format "owner/repo"
70 const githubRepository = process.env.GITHUB_REPOSITORY;
71 if (!githubRepository) {
72 return null;
73 }
74
75 // GITHUB_SERVER_URL defaults to https://github.com but can be different for GitHub Enterprise
76 const serverUrl = process.env.GITHUB_SERVER_URL || "https://github.com";
77
78 return `${serverUrl}/${githubRepository}`;
79}
80
81/**
82 * Get the current repository URL with enhanced detection for CI environments
83 * Priority order:
84 * 1. GitHub Actions environment variables (if running in GitHub Actions)
85 * 2. Git remote URL (if in a git repository)
86 * 3. Current working directory (fallback)
87 * @returns Repository URL or local path
88 */
89export function getRepoUrl(): string {
90 // First, check if we're in GitHub Actions and can get repo URL from env vars
91 const githubActionsUrl = getGitHubActionsRepoUrl();
92 if (githubActionsUrl) {
93 return githubActionsUrl;
94 }
95
96 // Then check if we're in a git repository
97 if (!isGitRepo()) {
98 return process.cwd();
99 }
100
101 // Try to get the remote URL from git
102 const remoteUrl = getGitRemoteUrl();
103 const url = remoteUrl || process.cwd();
104 return url.endsWith(".git") ? url.slice(0, -4) : url;
105}