Files
orca/src/main/git/repo.ts
T
Jinwoo HongandOrca ea8ea08116 feat(orchestration): bundled improvements — check-wait, stale-base, preamble+ask, QoL (#1403)
* feat(orchestration): transport keepalive + delivered_at split for check --wait

Implements the four §3 fixes from the check-wait design doc:

- §3.1 Transport keepalive: long-poll RPCs (orchestration.check --wait) emit
  `{"_keepalive":true}` frames every 10s so neither server nor client tears
  the socket down on idle. A `longPoll` admission counter capped at 16 fails
  fast with `runtime_busy` when saturated; an AbortController wired through
  the RPC dispatcher cancels the inner waiter the moment the socket closes.
- §3.2 delivered_at split: push-on-idle now stamps `delivered_at` instead of
  flipping `read`, so the check caller remains the sole consumer of its
  queue. Adds a synchronous idempotent schema migration that hard-fails on
  error.
- §3.3 inbox/check parity: `orchestration inbox --terminal <handle>` and
  `orchestration check --all` agree on the same rows (sequence DESC, no
  mark-read). `check --unread=false` kept for one release as a compat shim.
- §3.4 CLI heartbeat: `orca orchestration check --wait` emits JSON heartbeat
  lines to stderr every 15s so Claude Code's Bash tool sees continuous
  output and doesn't auto-background the subprocess.

Tests: extends runtime-rpc, orca-runtime, envelope-schema, orchestration
method, and formatter suites; adds a subprocess test that spawns the built
CLI and verifies stderr line-flushing, heartbeat cadence, and stdout
cleanliness end-to-end.

Co-authored-by: Orca <help@stably.ai>

* feat(orchestration): preamble rules + heartbeat schema

- Preamble (#7, #15, #9): worker_done body ("3-sentence summary" + reportPath),
  BEHAVIOR RULE #1 forbidding AskUserQuestion, heartbeat every 5 minutes with
  taskId+dispatchId payload, AFTER YOU SEND grace window.
- Schema v2 migration: adds 'heartbeat' to messages.type CHECK, adds
  dispatch_contexts.last_heartbeat_at, gated by user_version PRAGMA with
  transactional rebuild + explicit CREATE INDEX to avoid silent perf regress.
- DB helpers: recordHeartbeat (dispatched-only), getStaleDispatches,
  getThreadMessagesFor (thread+handle scoped for ask).

Co-authored-by: Orca <help@stably.ai>

* feat(orchestration): coordinator heartbeat + stale detector

Handle incoming 'heartbeat' messages by calling recordHeartbeat keyed on
payload.dispatchId (strict — log-and-skip if missing, no taskId fallback so
a straggler heartbeat from a previously-failed dispatch cannot mask a hung
retry per §5.3.4). On every tick after the 10-minute threshold, emit one
log per stale dispatched row — no auto-fail.

Also threads dispatchId through buildDispatchPreamble so workers can
attribute their heartbeats back to the correct dispatch context.

Co-authored-by: Orca <help@stably.ai>

* feat(orchestration): orca orchestration ask verb

Adds a CLI verb that sends a decision_gate message and blocks on the
coordinator's reply, scoped to the outbound message's thread. Group
addresses (@all, @idle, …) are rejected — fan-out questions must use
send --type decision_gate explicitly.

--json emits bare single-line JSON (bypassing printResult) so workers can
pipe `orca orchestration ask … --json | jq -r .answer` without unwrapping
an RPC envelope; human mode prints just the answer. On timeout the verb
exits 1 and returns {answer: null, timedOut: true}.

This is the CLI surface BEHAVIOR RULE #1 in the dispatch preamble points
workers at instead of AskUserQuestion.

Co-authored-by: Orca <help@stably.ai>

* feat(orchestration): QoL bundle — preamble visibility, status enum, dispatch cross-ref, inbox --full

Addresses four items from ORCHESTRATOR_FEEDBACK:

- #5 preamble visibility: `dispatch-show --preamble` regenerates the preamble
  text for a task; `dispatch --inject --dry-run` previews without mutating
  state; `dispatch --return-preamble` echoes the injected preamble in the JSON
  response so coordinators can audit what a worker received.
- #6 status enum validation: CLI rejects unknown `task-update --status` values
  with `invalid status '<x>', expected one of: pending, ready, dispatched,
  completed, failed, blocked` before the RPC's generic Zod message. Valid
  statuses are listed under Notes in `task-update --help`.
- #13 task-list dispatch cross-ref: `task-list --json` now includes
  `assignee_handle` and `dispatch_id` for tasks in status=dispatched via a
  read-only LEFT JOIN on dispatch_contexts. Non-dispatched rows keep their
  legacy shape so existing consumers are unaffected.
- #14 inbox body visibility: `inbox --full` prints body + payload verbatim;
  default output is unchanged (id/from/to/subject only).

No DB migrations; join-only change on dispatch_contexts so the sibling
preamble PR's `last_heartbeat_at` column addition will not conflict.

Co-authored-by: Orca <help@stably.ai>

* fix(worktree): prevent stale-base worktree creation and dispatch

Addresses feedback #16 per DESIGN_DOC_STALE_BASE_FIX.md §0. Four v1
components coordinated by a single shared fetch cache on the runtime:

1. Concurrent-fetch-with-gate in UI create path: `createLocalWorktree`
   fires `git fetch` BEFORE the suffix loop / PR probe / path
   resolution, then awaits right before `addWorktree` so the new branch
   always spawns from a fresh remote tip. Renderer sees a two-phase
   spinner via the new `createWorktree:progress` IPC event. The cache
   is a `Map<repoPath::remote, Promise<void>>` + 30s success-only
   timestamp on `OrcaRuntimeService` (§7.1 — shared with dispatch).
2. Dispatch pre-flight drift guard in `Coordinator.dispatchTask`:
   probes `rev-list --left-right --count` against the target worktree
   and silently returns (preserves `ready`, no circuit-breaker burn)
   when `behind > 20` unless the task spec carries
   `allow-stale-base: true`. Parsing strips the flag so it never leaks
   into the worker's `--- TASK ---` block.
3. Preamble drift section: populated only when dispatch detected drift.
   Workers see `--- BASE DRIFT ---` with the N-most-recent subjects
   they don't have, so they can pull them in before running.
4. §3.3 Lifecycle: `.finally()` evicts Map entries on BOTH success and
   rejection; timestamp is written ONLY on success. Prevents a single
   DNS hiccup from wedging every future create on the repo until
   restart, and keeps the freshness window honest.

Defers the DB `allow_stale_base` column (§0.2) and the create-time
warn toast; both can layer in later without migration.

Tests: 35 new/updated unit tests covering drift preamble, dispatch
refusal, spec-text flag parsing, fetch Map eviction after rejection,
freshness-window short-circuit, and concurrent-caller serialization.

Co-authored-by: Orca <help@stably.ai>

* test(orchestration): seed v2 DB in migration hard-fail test

After consolidating the schema bump, fresh DBs are initialized directly at
v3 via createTables(), so the v2→v3 ALTER TABLE is skipped on new installs
and the prior test's stub never fired. Seed a v2-shape file on disk so the
guarded ALTER actually runs and the "simulated migration failure" stub
propagates as intended.

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-05-04 12:10:53 -07:00

620 lines
21 KiB
TypeScript

/* oxlint-disable max-lines */
import { execSync } from 'child_process'
import { existsSync, statSync } from 'fs'
import { join, basename } from 'path'
import hostedGitInfo from 'hosted-git-info'
import { gitExecFileSync, gitExecFileAsync } from './runner'
/**
* Ordered probe list used to resolve a repo's default base ref when no
* explicit origin/HEAD symbolic-ref is set. `returnAs` is the short-name
* format the UI expects (matches how `git for-each-ref --format=%(refname:short)`
* would render the ref).
*
* Why: shared between the local path (getDefaultBaseRefAsync) and the SSH
* relay path in src/main/ipc/repos.ts so both resolve identical defaults
* for equivalent repo states.
*/
export const DEFAULT_BASE_REF_PROBES: readonly { ref: string; returnAs: string }[] = [
{ ref: 'refs/remotes/origin/main', returnAs: 'origin/main' },
{ ref: 'refs/remotes/origin/master', returnAs: 'origin/master' },
{ ref: 'refs/heads/main', returnAs: 'main' },
{ ref: 'refs/heads/master', returnAs: 'master' }
]
/**
* Walk DEFAULT_BASE_REF_PROBES in order, returning the first ref whose
* existence is confirmed by `hasRef`. Returns null if none exist.
*
* Why: abstracts the "how do we test a ref exists" detail so the local
* path (hasGitRefAsync) and the SSH path (provider.exec rev-parse) can
* share a single authoritative probe ordering.
*/
async function resolveDefaultBaseRefFromProbes(
hasRef: (ref: string) => Promise<boolean>
): Promise<string | null> {
for (const { ref, returnAs } of DEFAULT_BASE_REF_PROBES) {
if (await hasRef(ref)) {
return returnAs
}
}
return null
}
/**
* Check if a path is a valid git repository (regular or bare).
*/
export function isGitRepo(path: string): boolean {
try {
if (!existsSync(path) || !statSync(path).isDirectory()) {
return false
}
// .git dir or file (for worktrees) or bare repo
if (existsSync(join(path, '.git'))) {
return true
}
// Might be a bare repo — ask git
const result = gitExecFileSync(['rev-parse', '--is-inside-work-tree'], {
cwd: path
}).trim()
return result === 'true'
} catch {
// Also check if it's a bare repo
try {
const result = gitExecFileSync(['rev-parse', '--is-bare-repository'], {
cwd: path
}).trim()
return result === 'true'
} catch {
return false
}
}
}
/**
* Get a human-readable name for the repo from its path.
*/
export function getRepoName(path: string): string {
const name = basename(path)
// Strip .git suffix from bare repos
return name.endsWith('.git') ? name.slice(0, -4) : name
}
/**
* Get the remote origin URL, or null if not set.
*/
export function getRemoteUrl(path: string): string | null {
try {
return gitExecFileSync(['remote', 'get-url', 'origin'], {
cwd: path
}).trim()
} catch {
return null
}
}
function getGitConfigValue(path: string, key: string): string {
try {
return gitExecFileSync(['config', '--get', key], {
cwd: path
}).trim()
} catch {
return ''
}
}
function normalizeUsername(value: string): string {
const trimmed = value.trim()
if (!trimmed) {
return ''
}
const localPart = trimmed.includes('@') ? trimmed.split('@')[0] : trimmed
return localPart.replace(/^\d+\+/, '')
}
let cachedGhLogin: string | undefined
function getGhLogin(): string {
if (cachedGhLogin !== undefined) {
return cachedGhLogin
}
try {
const apiLogin = execSync('gh api user -q .login', {
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe']
}).trim()
if (apiLogin) {
cachedGhLogin = normalizeUsername(apiLogin)
return cachedGhLogin
}
} catch {
// Fall through to auth status parsing
}
try {
// Why: gh auth status writes to stderr; redirect via shell so we can capture it.
// Use platform-appropriate shell — /bin/bash does not exist on Windows.
const output = execSync('gh auth status 2>&1', {
encoding: 'utf-8',
shell: process.platform === 'win32' ? process.env.ComSpec || 'cmd.exe' : '/bin/bash',
stdio: ['pipe', 'pipe', 'pipe']
})
const activeAccountMatch = output.match(
/Active account:\s+true[\s\S]*?account\s+([A-Za-z0-9-]+)/
)
if (activeAccountMatch?.[1]) {
cachedGhLogin = normalizeUsername(activeAccountMatch[1])
return cachedGhLogin
}
const accountMatch = output.match(/Logged in to github\.com account\s+([A-Za-z0-9-]+)/)
const login = normalizeUsername(accountMatch?.[1] ?? '')
if (login) {
cachedGhLogin = login
}
return login
} catch {
// Don't cache empty results on failure — allow retry on next call
return ''
}
}
/**
* Get the best username-style branch prefix for the repo.
*/
export function getGitUsername(path: string): string {
return normalizeUsername(
getGitConfigValue(path, 'github.user') ||
getGitConfigValue(path, 'user.username') ||
getGhLogin() ||
getGitConfigValue(path, 'user.email').split('@')[0] ||
getGitConfigValue(path, 'user.name')
)
}
function hasGitRef(path: string, ref: string): boolean {
try {
gitExecFileSync(['rev-parse', '--verify', ref], {
cwd: path
})
return true
} catch {
return false
}
}
/**
* Resolve the default base ref for new worktrees.
* Prefer the remote primary branch over a potentially stale local branch.
*
* Why: returns `null` when no candidate ref is resolvable. Previously this
* fell through to a hardcoded `'origin/main'` even when that ref did not
* exist, which silently handed `git worktree add` a bad ref and produced
* an opaque git error. Callers now fail loudly with a useful message, or
* degrade gracefully for non-creation uses (e.g. hosted URL building).
*/
export function getDefaultBaseRef(path: string): string | null {
try {
const ref = gitExecFileSync(['symbolic-ref', '--quiet', 'refs/remotes/origin/HEAD'], {
cwd: path
}).trim()
if (ref) {
return ref.replace(/^refs\/remotes\//, '')
}
} catch {
// Fall through to explicit remote branch probes.
}
// Why: walk the shared DEFAULT_BASE_REF_PROBES list so the sync path and the
// async/SSH paths cannot drift on which refs are tried or in what order.
for (const { ref, returnAs } of DEFAULT_BASE_REF_PROBES) {
if (hasGitRef(path, ref)) {
return returnAs
}
}
return null
}
export async function getBaseRefDefault(path: string): Promise<string | null> {
return getDefaultBaseRefAsync(path)
}
/**
* Return { ahead, behind } for localRef vs remoteRef, or null on git failure.
*
* Why: `rev-list --left-right --count A...B` emits `<ahead>\t<behind>` —
* ahead = commits on A not reachable from B; behind = commits on B not
* reachable from A. This is the merge-base-symmetric delta used by the
* stale-base dispatch guard (§3.1). Returning null on any failure (bad
* ref, corrupt repo, non-numeric output) lets callers degrade gracefully
* instead of failing dispatch on a probe error.
*/
export function getRemoteDrift(
repoPath: string,
localRef: string,
remoteRef: string
): { ahead: number; behind: number } | null {
try {
const stdout = gitExecFileSync(
['rev-list', '--left-right', '--count', `${localRef}...${remoteRef}`],
{ cwd: repoPath }
)
const [aheadStr, behindStr] = stdout.trim().split(/\s+/)
const ahead = Number(aheadStr)
const behind = Number(behindStr)
if (!Number.isFinite(ahead) || !Number.isFinite(behind)) {
return null
}
return { ahead, behind }
} catch {
return null
}
}
/**
* Up to `limit` commit subjects present on remoteRef but not localRef, in
* recency order. Returns [] on git failure.
*
* Why: powers the preamble drift section (§3.2) so a worker dispatched
* against an acknowledged-stale base can see at a glance whether the
* drift touches their task area.
*/
export function getRecentDriftSubjects(
repoPath: string,
localRef: string,
remoteRef: string,
limit: number
): string[] {
try {
const stdout = gitExecFileSync(
['log', '--format=%s', '-n', String(limit), `${localRef}..${remoteRef}`],
{ cwd: repoPath }
)
return stdout.split('\n').filter((s) => s.trim().length > 0)
} catch {
return []
}
}
/**
* Parse `git remote` stdout into a count of configured remotes.
*
* Why: shared between the local path and the SSH relay path so the
* count semantics cannot drift.
*/
export function parseRemoteCount(stdout: string): number {
return stdout.split('\n').filter((line) => line.trim().length > 0).length
}
/**
* Count the repo's configured remotes by shelling out `git remote`.
* Returns 0 on error — callers use 0 as "unknown / do not render the
* multi-remote hint", preserving today's no-hint behavior on failure.
*/
export async function getRemoteCount(path: string): Promise<number> {
try {
const { stdout } = await gitExecFileAsync(['remote'], { cwd: path })
return parseRemoteCount(stdout)
} catch (err) {
// Why: surface the failure for diagnostics; callers treat 0 as "unknown /
// do not render the multi-remote hint", but silently swallowing the error
// makes a missing hint impossible to debug.
console.warn('[getRemoteCount] git remote failed', { path, err })
return 0
}
}
/** Callback shape for a git exec function that yields stdout. */
export type GitExec = (argv: string[]) => Promise<{ stdout: string }>
/**
* Resolve the default base ref given a git exec callback. Prefers
* origin/HEAD's symbolic-ref target; falls back to DEFAULT_BASE_REF_PROBES.
*
* Why: shared between the local path (via gitExecFileAsync) and the SSH
* relay path (via provider.exec) so both paths return identical results
* for equivalent repo states. Accepting an exec callback avoids coupling
* this helper to either transport. Callers that want transport-level
* diagnostics should log inside their own exec callback before rethrowing —
* this helper swallows symbolic-ref's catch because a non-zero exit is the
* expected signal for "origin/HEAD is unset" and not distinguishable here
* from a genuine transport failure.
*/
export async function resolveDefaultBaseRefViaExec(exec: GitExec): Promise<string | null> {
try {
const { stdout } = await exec(['symbolic-ref', '--quiet', 'refs/remotes/origin/HEAD'])
const ref = stdout.trim()
if (ref) {
return ref.replace(/^refs\/remotes\//, '')
}
} catch {
// symbolic-ref returns non-zero when origin/HEAD is unset — expected.
// Fall through to probes.
}
return resolveDefaultBaseRefFromProbes(async (ref) => {
try {
await exec(['rev-parse', '--verify', '--quiet', ref])
return true
} catch {
return false
}
})
}
async function getDefaultBaseRefAsync(path: string): Promise<string | null> {
return resolveDefaultBaseRefViaExec((argv) => gitExecFileAsync(argv, { cwd: path }))
}
/**
* Build the argv for `git for-each-ref` used by ref search, given an
* already-normalized query string.
*
* Why: glob `refs/remotes/*\/*` (not `refs/remotes/origin/*`) so fork
* workflows can discover branches from any configured remote (e.g.
* `upstream/main`). The picker would otherwise structurally deny the
* correct answer for fork contributors — see docs/upstream-base-ref-design.md.
*
* Why two remote globs for a single-segment query: `git for-each-ref`
* uses fnmatch-style globs where `*` does NOT cross `/`. A single
* `refs/remotes/*\/*<q>*` pattern only matches when `<q>` appears in the
* branch-name segment, so typing `upstream` (a remote name) would return
* nothing. The extra `refs/remotes/*<q>*\/*` glob matches when the query
* appears in the remote-name segment, making remote-name filtering work.
*
* Why the multi-segment branch: the picker displays results as
* `upstream/main`, so users naturally retype that format. With a single
* glob, `upstream/main` becomes `refs/remotes/*upstream/main*\/*` — five
* path segments, zero matches. Splitting on `/` and emitting one
* `*<token>*` per ref segment maps directly to git's ref structure
* (`refs/remotes/<remote>/<branch>`, `refs/heads/<branch>`) and makes
* display-format queries actually find the ref on screen.
*
* Why shared: the local path and the SSH relay path must send the exact
* same argv so results cannot diverge between transports.
*/
export function buildSearchBaseRefsArgv(normalizedQuery: string): string[] {
const base = ['for-each-ref', '--format=%(refname)%00%(refname:short)', '--sort=-committerdate']
// Why: split on `/` so display-format queries (`upstream/main`) route
// each token to one git ref segment. Filter empty tokens so trailing
// (`upstream/`), leading (`/main`), or doubled (`upstream//main`)
// slashes don't produce empty `**` segments that degrade to useless
// patterns. A single remaining token means the user hasn't committed
// to a remote-plus-branch query yet — route through the widened
// single-segment globs below instead of pinning to one segment.
const tokens = normalizedQuery.split('/').filter((t) => t.length > 0)
if (tokens.length <= 1) {
const q = tokens[0] ?? ''
// Why three globs for a single-segment query: `git for-each-ref`
// uses fnmatch-style globs where `*` does NOT cross `/`. A single
// `refs/remotes/*\/*<q>*` only matches when `<q>` is in the branch
// segment, so typing `upstream` (a remote name) returns nothing.
// The extra `refs/remotes/*<q>*\/*` matches when the query lives in
// the remote segment, making remote-name filtering work.
return [...base, `refs/remotes/*${q}*/*`, `refs/remotes/*/*${q}*`, `refs/heads/*${q}*`]
}
// Why: multi-token queries like `upstream/main` map one `*token*` per
// ref segment, so each token is matched within a single git ref
// segment (fnmatch `*` cannot cross `/`). The picker displays results
// as `<remote>/<branch>`, so users naturally retype that format; this
// branch is what makes re-typing a visible result actually find it.
const segmented = tokens.map((token) => `*${token}*`).join('/')
return [...base, `refs/remotes/${segmented}`, `refs/heads/${segmented}`]
}
/**
* Resolve the default push remote for a repo.
* Order: remote configured on the current default branch → origin → the single
* remote when the repo has exactly one → error.
*/
export async function getDefaultRemote(path: string): Promise<string> {
const defaultRef = await getDefaultBaseRefAsync(path)
// Why: getDefaultBaseRefAsync returns null when no default branch can be
// detected (e.g. a brand-new repo with no commits on origin). Guard so we
// don't crash on .includes(); fall through to the remote-list heuristics.
const defaultBranch = defaultRef
? defaultRef.includes('/')
? defaultRef.split('/').slice(1).join('/')
: defaultRef
: null
if (defaultBranch) {
try {
const { stdout } = await gitExecFileAsync(
['config', '--get', `branch.${defaultBranch}.remote`],
{ cwd: path }
)
const value = stdout.trim()
if (value) {
return value
}
} catch {
// Fall through: branch has no explicit remote configured.
}
}
try {
const { stdout } = await gitExecFileAsync(['remote'], { cwd: path })
const remotes = stdout
.split('\n')
.map((line) => line.trim())
.filter(Boolean)
if (remotes.includes('origin')) {
return 'origin'
}
if (remotes.length === 1) {
return remotes[0]
}
if (remotes.length === 0) {
throw new Error('Repo has no configured git remotes.')
}
throw new Error(
`Repo has multiple remotes (${remotes.join(', ')}) and no default is configured. Set branch.<default>.remote.`
)
} catch (error) {
if (error instanceof Error) {
throw error
}
throw new Error('Failed to resolve default remote for repo.')
}
}
export async function searchBaseRefs(path: string, query: string, limit = 25): Promise<string[]> {
const normalizedQuery = normalizeRefSearchQuery(query)
if (!normalizedQuery) {
return []
}
try {
// Why: argv (including the two-remote-glob rationale) lives in
// buildSearchBaseRefsArgv so the SSH sibling cannot drift.
const { stdout } = await gitExecFileAsync(buildSearchBaseRefsArgv(normalizedQuery), {
cwd: path
})
return parseAndFilterSearchRefs(stdout, limit)
} catch (err) {
// Why: surface the failure for diagnostics; callers treat `[]` as "no
// matches", but silently swallowing the error makes a missing result
// set impossible to debug. Mirrors the SSH sibling in
// src/main/ipc/repos.ts.
console.warn('[searchBaseRefs] for-each-ref failed', { path, err })
return []
}
}
/**
* Parse `git for-each-ref --format=%(refname)%00%(refname:short)` stdout
* into a deduped list of short refs, filtering out `<remote>/HEAD`
* pseudo-refs, honoring a limit.
*
* Why: shared between the local `searchBaseRefs` and the SSH branch in
* `src/main/ipc/repos.ts` so both return identical, correctly-filtered
* results. The same bug class (wrong filter ordering, HEAD leaking into
* results, duplicate short refs) that motivated this helper originally
* lived in a single location; two copies double the regression surface.
*/
export function parseAndFilterSearchRefs(stdout: string, limit: number): string[] {
const seen = new Set<string>()
return (
stdout
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length > 0)
.map((line) => {
const nul = line.indexOf('\0')
if (nul < 0) {
// Why: defensive fallback for an unlikely %(refname) format change.
// Drop the entry — emitting a full refname as a "short" ref would
// hand callers a ref they can't use (and would bypass the HEAD
// filter below, since we could no longer tell a `<remote>/HEAD`
// pseudo-ref from a local branch named `foo/HEAD`).
return null
}
return { full: line.slice(0, nul), short: line.slice(nul + 1) }
})
.filter((entry): entry is { full: string; short: string } => entry !== null)
// Why: drop `refs/remotes/<remote>/HEAD` pseudo-refs. Uses `.+` (not
// `[^/]+`) because git allows slashes in remote names, so nested
// remotes like `refs/remotes/foo/bar/HEAD` also match. A local branch
// named `foo/HEAD` (rare but valid per git check-ref-format) is
// preserved because its `full` is `refs/heads/foo/HEAD`, which does
// not match this pattern.
.filter(({ full }) => !/^refs\/remotes\/.+\/HEAD$/.test(full))
.filter(({ short }) => {
if (seen.has(short)) {
return false
}
seen.add(short)
return true
})
.map(({ short }) => short)
// Why: `Math.max(0, limit)` — treat pathological `limit <= 0` as
// "zero results" rather than "at least 1". More honest than silently
// returning a single ref when the caller explicitly asked for none.
.slice(0, Math.max(0, limit))
)
}
export function normalizeRefSearchQuery(query: string): string {
return query.trim().replace(/[*?[\]\\]/g, '')
}
async function hasGitRefAsync(path: string, ref: string): Promise<boolean> {
try {
await gitExecFileAsync(['rev-parse', '--verify', ref], { cwd: path })
return true
} catch {
return false
}
}
export type BranchConflictKind = 'local' | 'remote'
export async function getBranchConflictKind(
path: string,
branchName: string
): Promise<BranchConflictKind | null> {
if (await hasGitRefAsync(path, `refs/heads/${branchName}`)) {
return 'local'
}
try {
const { stdout } = await gitExecFileAsync(
['for-each-ref', '--format=%(refname)', 'refs/remotes'],
{ cwd: path }
)
// Why: refs have the form refs/remotes/<remote>/<branch>. We strip the
// first three segments so that e.g. "feature/dashboard" only matches
// "refs/remotes/origin/feature/dashboard", not "refs/remotes/origin/other/feature/dashboard".
const hasRemoteConflict = stdout.split('\n').some((ref) => {
const parts = ref.trim().split('/')
return parts.slice(3).join('/') === branchName
})
return hasRemoteConflict ? 'remote' : null
} catch {
return null
}
}
/**
* Build a hosted URL (e.g. GitHub, GitLab, Bitbucket) for a specific file
* and line in the repo. Returns null when the remote isn't a recognized host.
*
* Why hosted-git-info: it handles SSH, HTTPS, and shorthand remote URLs
* across multiple providers, so we don't have to maintain our own URL parser.
*/
export function getRemoteFileUrl(
repoPath: string,
relativePath: string,
line: number
): string | null {
const remoteUrl = getRemoteUrl(repoPath)
if (!remoteUrl) {
return null
}
const info = hostedGitInfo.fromUrl(remoteUrl)
if (!info) {
return null
}
const defaultBaseRef = getDefaultBaseRef(repoPath)
if (!defaultBaseRef) {
return null
}
const defaultBranch = defaultBaseRef.replace(/^origin\//, '')
const browseUrl = info.browseFile(relativePath, { committish: defaultBranch })
if (!browseUrl) {
return null
}
// Why: hosted-git-info lowercases the fragment, but GitHub convention
// uses uppercase L for line links (e.g. #L42). Append manually.
return `${browseUrl}#L${line}`
}