Repository navigation
fix(host-service): stop workspace↔PR/diff state drifting from GitHub #5455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
08840ca
64f6fc6
7e6db86
270a958
2f71991
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,55 @@ async function refExists(git: SimpleGit, fullRef: string): Promise<boolean> { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Enumerate branch refnames as git stores them. Used instead of `rev-parse` | ||
| * probes because on a case-insensitive filesystem (macOS default) probing | ||
| * `refs/heads/foo` resolves the `refs/heads/Foo` file, hiding case drift. | ||
| */ | ||
| async function listBranchShortNames( | ||
| git: SimpleGit, | ||
| remote: string, | ||
| ): Promise<{ local: string[]; remoteTracking: string[] }> { | ||
| const local: string[] = []; | ||
| const remoteTracking: string[] = []; | ||
| let raw: string; | ||
| try { | ||
| // A real git failure must not be masked as "no branches" — that hides an | ||
| // existing branch and lets a case-twin be created. (An empty repo isn't | ||
| // a failure: for-each-ref exits 0 with no output.) | ||
| raw = await git.raw([ | ||
| "for-each-ref", | ||
| "--format=%(refname)", | ||
| "refs/heads/", | ||
| `refs/remotes/${remote}/`, | ||
| ]); | ||
| } catch (error) { | ||
| console.warn("[host-service:git] for-each-ref failed in resolveRef", { | ||
| remote, | ||
| error, | ||
| }); | ||
| throw error; | ||
| } | ||
| const remotePrefix = `refs/remotes/${remote}/`; | ||
| for (const refname of raw.trim().split("\n").filter(Boolean)) { | ||
| if (refname.startsWith("refs/heads/")) { | ||
| local.push(refname.slice("refs/heads/".length)); | ||
| } else if (refname.startsWith(remotePrefix)) { | ||
| const name = refname.slice(remotePrefix.length); | ||
| if (name !== "HEAD") remoteTracking.push(name); | ||
| } | ||
| } | ||
| return { local, remoteTracking }; | ||
| } | ||
|
|
||
| function findCaseInsensitiveMatch( | ||
| names: string[], | ||
| input: string, | ||
| ): string | null { | ||
| const lower = input.toLowerCase(); | ||
| return names.find((name) => name.toLowerCase() === lower) ?? null; | ||
| } | ||
|
|
||
| export interface ResolveRefOptions { | ||
| /** | ||
| * Remote name to probe for remote-tracking refs. Defaults to "origin". | ||
|
|
@@ -94,38 +143,60 @@ export async function resolveRef( | |
| return options.headFallback ? { kind: "head" } : null; | ||
| } | ||
|
|
||
| // Local always wins: if a local branch literally named `<input>` exists | ||
| // (including names like `origin/foo`), it takes precedence. Probes against | ||
| // the full refname so the structural prefix removes any ambiguity. | ||
| const localRef = asLocalRef(trimmed); | ||
| if (await refExists(git, localRef)) { | ||
| return { kind: "local", fullRef: localRef, shortName: trimmed }; | ||
| } | ||
| // Match against enumerated refnames so casing is authoritative. Exact | ||
| // matches keep precedence (local > remote-tracking); a case-insensitive | ||
| // match is a fallback tier that adopts the existing branch's canonical | ||
| // casing rather than minting a case-twin sharing its loose-ref file. | ||
| const branches = await listBranchShortNames(git, remote); | ||
|
|
||
| // For the remote probe, accept both bare names (`foo`) and the natural | ||
| // For the remote form, accept both bare names (`foo`) and the natural | ||
| // short form (`origin/foo`). Strip the `<remote>/` prefix only if it's | ||
| // present in the input — without this, `origin/foo` would probe | ||
| // present in the input — without this, `origin/foo` would look up | ||
| // `refs/remotes/origin/origin/foo` and miss. | ||
| const remotePrefix = `${remote}/`; | ||
| const remoteShortName = trimmed.startsWith(remotePrefix) | ||
| ? trimmed.slice(remotePrefix.length) | ||
| : trimmed; | ||
| const remoteRef = asRemoteRef(remote, remoteShortName); | ||
| if (await refExists(git, remoteRef)) { | ||
| return { | ||
| kind: "remote-tracking", | ||
| fullRef: remoteRef, | ||
| shortName: remoteShortName, | ||
| remote, | ||
| remoteShortName: `${remote}/${remoteShortName}`, | ||
| }; | ||
|
|
||
| const asLocal = (name: string): ResolvedRef => ({ | ||
| kind: "local", | ||
| fullRef: asLocalRef(name), | ||
| shortName: name, | ||
| }); | ||
| const asRemoteTracking = (name: string): ResolvedRef => ({ | ||
| kind: "remote-tracking", | ||
| fullRef: asRemoteRef(remote, name), | ||
| shortName: name, | ||
| remote, | ||
| remoteShortName: `${remote}/${name}`, | ||
| }); | ||
|
|
||
| if (branches.local.includes(trimmed)) { | ||
| return asLocal(trimmed); | ||
| } | ||
|
|
||
| if (branches.remoteTracking.includes(remoteShortName)) { | ||
| return asRemoteTracking(remoteShortName); | ||
| } | ||
|
|
||
| const tagRef: `refs/tags/${string}` = `refs/tags/${trimmed}`; | ||
| if (await refExists(git, tagRef)) { | ||
| return { kind: "tag", fullRef: tagRef, shortName: trimmed }; | ||
| } | ||
|
Comment on lines
182
to
185
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🧩 Analysis chain🌐 Web query:
💡 Result: Yes, git rev-parse --verify can resolve loose refs (such as tags) case-insensitively when running on a case-insensitive filesystem like macOS APFS [1][2][3]. This occurs because Git's ref resolution mechanism relies on the underlying filesystem to locate loose ref files [1][2][3]. On case-insensitive filesystems, the operating system treats files with different casing as the same file, allowing Git to inadvertently "find" a ref even if the provided name does not match the actual casing of the file on disk [1][2][3]. This behavior is not a deliberate feature of Git's revision parsing logic, but rather an artifact of the interaction between Git's storage architecture and the filesystem [1][2]. Key technical consequences include: 1. Ambiguity and Misresolution: In complex scenarios—most notably with linked worktrees—this can lead to critical errors [2][4]. For example, if a user attempts to resolve a ref like "head" (lowercase) on a case-insensitive system, it may inadvertently resolve to the wrong ref (such as the main worktree's HEAD instead of a linked worktree's specific HEAD) because the OS successfully opens the file path for the main HEAD [2][4]. 2. Inconsistency: While Git generally treats refs as case-sensitive internally, the reliance on the filesystem for loose refs means that operations may behave differently depending on whether the ref is loose (on disk) or packed (in the packed-refs file) [1][2]. Packed-refs typically require exact, case-sensitive matching because Git performs the search against the contents of the packed-refs file rather than the filesystem [1][2]. 3. Recommended Practice: Because of these risks, it is widely recommended to avoid creating refs that differ only by case and to always use the exact casing (e.g., "HEAD" rather than "head") when referencing them [1][5][3]. Note that while some specific commands like git tag --list allow for explicit case-insensitive sorting or filtering [6][7], this is an application-level feature and distinct from the filesystem-induced behavior observed during general ref resolution [1]. Citations:
🏁 Script executed: #!/bin/bash
set -euo pipefail
# Locate the relevant symbols and surrounding code.
rg -n "refExists|listBranchShortNames|planBranchSource|refs/tags|refs/heads" packages/host-service/src/runtime/git/refs.ts packages/host-service/src/runtime/git/workspaces.ts
echo
echo "---- refs.ts outline ----"
ast-grep outline packages/host-service/src/runtime/git/refs.ts --view expanded
echo
echo "---- workspaces.ts outline ----"
ast-grep outline packages/host-service/src/runtime/git/workspaces.ts --view expandedRepository: superset-sh/superset Length of output: 1820 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Inspect the specific implementations around the tag/branch probes.
sed -n '1,260p' packages/host-service/src/runtime/git/refs.ts
echo
sed -n '280,360p' packages/host-service/src/runtime/git/workspaces.tsRepository: superset-sh/superset Length of output: 7671 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Find the call site and tag handling paths in the current tree.
rg -n "BAD_REQUEST|kind: \"tag\"|resolveRef\\(|resolveBranch|resolve.*Ref|checkout" packages -g '*.ts' -g '*.tsx'
echo
echo "---- files mentioning resolveRef ----"
rg -n "resolveRef\\(" packages -g '*.ts' -g '*.tsx'Repository: superset-sh/superset Length of output: 27650 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Inspect the workspace call site and the ref tests for tag/branch edge cases.
sed -n '290,345p' packages/host-service/src/trpc/router/workspaces/workspaces.ts
echo
sed -n '1,240p' packages/host-service/src/runtime/git/refs.test.tsRepository: superset-sh/superset Length of output: 9015 🏁 Script executed: #!/bin/bash
set -euo pipefail
# Check whether tag enumeration is already used anywhere else and whether refs.md documents tags.
rg -n "for-each-ref|refs/tags/|tag" packages/host-service/src/runtime/git packages/host-service/src/trpc/router/workspaces -g '*.ts' -g '*.md'Repository: superset-sh/superset Length of output: 2484 🏁 Script executed: #!/bin/bash
set -euo pipefail
sed -n '290,345p' packages/host-service/src/trpc/router/workspaces/workspaces.ts
echo
sed -n '1,240p' packages/host-service/src/runtime/git/refs.test.tsRepository: superset-sh/superset Length of output: 9015 Tag lookup should avoid loose-ref probing
🤖 Prompt for AI Agents |
||
|
|
||
| const localTwin = findCaseInsensitiveMatch(branches.local, trimmed); | ||
| if (localTwin) { | ||
| return asLocal(localTwin); | ||
| } | ||
|
|
||
| const remoteTwin = findCaseInsensitiveMatch( | ||
| branches.remoteTracking, | ||
| remoteShortName, | ||
| ); | ||
| if (remoteTwin) { | ||
| return asRemoteTracking(remoteTwin); | ||
| } | ||
|
|
||
| return options.headFallback ? { kind: "head" } : null; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.