Files
orca/src/main/ipc/worktree-git-status-ref-watch.ts
T
Neil 583ab1601b refactor(shared): group worktree, github, and linear modules into folders (#14437)
`src/shared` is a flat directory of ~1,150 entries. The worktree, github, and
linear domains accounted for 71 of them, so finding the module you wanted meant
scanning a wall of same-prefixed filenames.

Move each domain into its own folder and drop the now-redundant prefix:

    src/shared/github-pr-types.ts    -> src/shared/github/pull-request-types.ts
    src/shared/worktree-id.ts        -> src/shared/worktree/id.ts
    src/shared/linear-links.ts       -> src/shared/linear/links.ts

This follows the existing `network/` and `new-workspace/` convention in the
same directory, which also drop the prefix inside the folder.

Whole clusters move, including tests. Foldering only part of a domain would be
worse than flat: a reader would have to check both `github/` and the flat
directory, and `github-auth-types.ts` / `github-project-types.ts` are type
modules that belong with the rest. No files with these prefixes remain flat.

Import specifiers were rewritten by resolving each one to an absolute path and
recomputing it, not by string substitution, so the `@/../../shared/...` alias
forms are handled correctly. 501 specifiers across 298 files.

Two things `tsc` cannot catch, handled explicitly:

- `github-project-types.ts` carries its own `max-lines` bypass, so its baseline
  entry is REPOINTED to the new path rather than pruned. Pruning would drop the
  bypass and then flag the new path as a fresh violation. Ratchet stays at 345.
- `mobile/` is outside `pnpm typecheck` and cannot be typechecked here
  (`mobile/node_modules` is empty). Instead every relative specifier in the repo
  was resolved against the filesystem: 174 unresolved before this change and 174
  after — identical, so nothing broke in mobile either.

The pinned `tests/e2e/.cross-version-checkouts` fixtures are deliberately NOT
rewritten; they are a snapshot of an older release and still reference the old
paths.

Verified: cold `tsc --noEmit` green on node, cli, and web (buildinfo deleted
first — these projects are `composite: true` and reuse stale caches).
2026-08-13 20:44:16 -07:00

199 lines
5.5 KiB
TypeScript

import { resolveRuntimePath } from '../../shared/cross-platform-path'
import { isSafeGitStatusUpstreamRef } from '../../shared/git-status-upstream-ref'
import { getRepoIdFromWorktreeId } from '../../shared/worktree/id'
import {
pathRelativeToWorktreeWatchRoot,
type WorktreeBaseWatchTarget
} from './worktree-base-directory-event-filter'
export type GitStatusRefWatchTarget = Pick<
WorktreeBaseWatchTarget,
'kind' | 'connectionId' | 'repos' | 'path'
> & {
gitStatusRefPaths: Set<string>
}
type ActiveGitStatusRefBinding = {
worktreeId: string
repoId: string
connectionId?: string
upstreamRef: string
}
type GitStatusRefResolution = {
key: string
worktreeId: string
repoId: string
connectionId?: string
controller: AbortController
promise: Promise<void>
retryAt: number
}
export type GitStatusRefBindingRequest = {
worktreeId: string
worktreePath: string
executionHostId: string
connectionId?: string
providerGeneration?: number
branch?: string
upstreamName?: string
}
const FAILED_RESOLUTION_RETRY_MS = 5 * 60_000
let activeBinding: ActiveGitStatusRefBinding | null = null
let activeResolution: GitStatusRefResolution | null = null
function watchMatchesBinding(
watch: GitStatusRefWatchTarget,
binding: ActiveGitStatusRefBinding
): boolean {
return (
watch.kind === 'git-common' &&
watch.connectionId === binding.connectionId &&
watch.repos.has(binding.repoId)
)
}
function watchMatchesResolution(
watch: GitStatusRefWatchTarget,
resolution: GitStatusRefResolution
): boolean {
return (
watch.kind === 'git-common' &&
watch.connectionId === resolution.connectionId &&
watch.repos.has(resolution.repoId)
)
}
export function applyActiveGitStatusRefBinding(watch: GitStatusRefWatchTarget): void {
watch.gitStatusRefPaths.clear()
if (activeBinding && watchMatchesBinding(watch, activeBinding)) {
watch.gitStatusRefPaths.add(resolveRuntimePath(watch.path, activeBinding.upstreamRef))
}
}
function applyBindingToWatches(watches: Iterable<GitStatusRefWatchTarget>): void {
for (const watch of watches) {
applyActiveGitStatusRefBinding(watch)
}
}
function clearResolutionForWorktree(
worktreeId: string,
getWatches: () => Iterable<GitStatusRefWatchTarget>
): void {
if (activeResolution?.worktreeId !== worktreeId) {
return
}
activeResolution.controller.abort()
activeResolution = null
activeBinding = null
applyBindingToWatches(getWatches())
}
function resolutionKey(args: GitStatusRefBindingRequest): string {
return [
args.executionHostId,
args.connectionId ?? '',
args.providerGeneration ?? 0,
args.worktreeId,
args.worktreePath,
args.branch,
args.upstreamName
].join('\0')
}
export function updateActiveGitStatusRefBinding(
args: GitStatusRefBindingRequest,
getWatches: () => Iterable<GitStatusRefWatchTarget>,
resolveUpstreamRef: (signal: AbortSignal) => Promise<string | undefined>
): Promise<void> {
if (!args.branch || !args.upstreamName) {
clearResolutionForWorktree(args.worktreeId, getWatches)
return Promise.resolve()
}
const key = resolutionKey(args)
if (activeResolution?.key === key && activeResolution.retryAt > Date.now()) {
applyBindingToWatches(getWatches())
return activeResolution.promise
}
activeResolution?.controller.abort()
activeBinding = null
applyBindingToWatches(getWatches())
const controller = new AbortController()
const resolution = {
key,
worktreeId: args.worktreeId,
repoId: getRepoIdFromWorktreeId(args.worktreeId),
...(args.connectionId ? { connectionId: args.connectionId } : {}),
controller,
promise: Promise.resolve(),
retryAt: Number.POSITIVE_INFINITY
}
resolution.promise = Promise.resolve()
.then(() => resolveUpstreamRef(controller.signal))
.then((upstreamRef) => {
if (activeResolution !== resolution) {
return
}
activeBinding =
upstreamRef && isSafeGitStatusUpstreamRef(upstreamRef)
? {
worktreeId: args.worktreeId,
repoId: getRepoIdFromWorktreeId(args.worktreeId),
...(args.connectionId ? { connectionId: args.connectionId } : {}),
upstreamRef
}
: null
applyBindingToWatches(getWatches())
})
.catch(() => {
if (activeResolution === resolution && !controller.signal.aborted) {
resolution.retryAt = Date.now() + FAILED_RESOLUTION_RETRY_MS
}
})
activeResolution = resolution
return resolution.promise
}
export function invalidateActiveGitStatusRefResolution(
changedWatch: GitStatusRefWatchTarget,
getWatches: () => Iterable<GitStatusRefWatchTarget>
): void {
if (!activeResolution || !watchMatchesResolution(changedWatch, activeResolution)) {
return
}
activeResolution.controller.abort()
activeResolution = null
activeBinding = null
applyBindingToWatches(getWatches())
}
export function invalidateGitStatusRefResolutionForPaths(
changedWatch: GitStatusRefWatchTarget,
paths: (string | undefined)[],
getWatches: () => Iterable<GitStatusRefWatchTarget>
): void {
const configChanged = paths.some((path) => {
if (!path || changedWatch.kind !== 'git-common') {
return false
}
const parts = pathRelativeToWorktreeWatchRoot(changedWatch.path, path)
return parts?.length === 1 && parts[0] === 'config'
})
if (configChanged) {
invalidateActiveGitStatusRefResolution(changedWatch, getWatches)
}
}
export function clearActiveGitStatusRefBinding(): void {
activeResolution?.controller.abort()
activeResolution = null
activeBinding = null
}