Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/repo-master. A GitHub CLI for PRs, issues, and reproduction.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import { readFile } from 'node:fs/promises'import { homedir } from 'node:os'import path from 'node:path'import { parse } from 'yaml'import { simpleGit } from 'simple-git'// @ts-expect-errorimport parseGhUrl from 'parse-github-url'
export const git = simpleGit(process.cwd())
export async function getAuth(): Promise<{ user: string; auth: string }> { const filePath = path.join(homedir(), '.config/gh/hosts.yml') const hosts = parse(await readFile(filePath, 'utf-8')) const { user, oauth_token } = hosts['github.com'] return { user, auth: oauth_token }}
export async function gitRemoteRepo(name = 'origin') { let origin = (await git.remote(['get-url', name]))?.trim() if (!origin) { throw new Error('No remote repository found') } if (origin.endsWith('.git')) { origin = origin.slice(0, -4) }
return origin}
export async function getGitHubRepo(): Promise<{ owner: string repo: string}> { const upstream = await gitRemoteRepo(await getUpstream()) const { owner, name } = parseGhUrl(upstream) return { owner, repo: name }}
export async function getUpstream(remotes?: string[]) { if (!remotes) remotes = (await git.remote([]))!.trim().split('\n') return remotes.includes('upstream') ? 'upstream' : 'origin'}
export async function getMainBranches(branches: string[]) { if (!branches) branches = await git.branchLocal().then((b) => b.all) if (branches.includes('main')) return 'main' if (branches.includes('dev')) return 'dev' if (branches.includes('master')) return 'master' throw new Error('No main or master branch found')}