Repository navigation
fix(desktop): confirm the repo root when the picked folder isn't one #6262
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
a37ffc5
f4d8644
cd218a9
ff12eba
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 |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import { | |
| realpathSync, | ||
| rmSync, | ||
| statSync, | ||
| symlinkSync, | ||
| writeFileSync, | ||
| } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
|
|
@@ -15,9 +16,11 @@ import { | |
| branchExistsOnRemote, | ||
| createWorktree, | ||
| getCurrentBranch, | ||
| getGitRoot, | ||
| getWorktreeCreatedAt, | ||
| hasUnpushedCommits, | ||
| isUnbornHeadError, | ||
| NotGitRepoError, | ||
| parsePorcelainStatusV2, | ||
| parsePrUrl, | ||
| } from "./git"; | ||
|
|
@@ -1134,3 +1137,159 @@ describe("parsePrUrl", () => { | |
| ).toBe(null); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getGitRoot", () => { | ||
| beforeEach(() => { | ||
| mkdirSync(TEST_DIR, { recursive: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| if (existsSync(TEST_DIR)) { | ||
| rmSync(TEST_DIR, { recursive: true, force: true }); | ||
| } | ||
| }); | ||
|
|
||
| test("reports the repo root itself as the root", async () => { | ||
| const repoPath = createTestRepo("root"); | ||
| seedCommit(repoPath); | ||
|
|
||
| const result = await getGitRoot(repoPath); | ||
|
|
||
| expect(result.root).toBe(repoPath); | ||
|
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. P3: The new getGitRoot tests compare the git-reported root against the locally joined Prompt for AI agents
Author
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. Leaving this as is for now. CI is Worth noting the production code is separator-agnostic — the root check is |
||
| expect(result.isRoot).toBe(true); | ||
| }); | ||
|
|
||
| // Guards the untrimmed read of `--show-toplevel`: a directory name may end | ||
| // in whitespace, and trimming it hands callers a path that doesn't exist. | ||
| test("preserves a root whose directory name ends in whitespace", async () => { | ||
| const repoPath = createTestRepo("trailing space "); | ||
| seedCommit(repoPath); | ||
|
|
||
| const result = await getGitRoot(repoPath); | ||
|
|
||
| expect(result.root).toBe(repoPath); | ||
| expect(result.root.endsWith(" ")).toBe(true); | ||
| expect(existsSync(result.root)).toBe(true); | ||
| expect(result.isRoot).toBe(true); | ||
| }); | ||
|
|
||
| // Git terminates the line with LF and round-trips a trailing CR that is part | ||
| // of the name, so the terminator strip must not consume it. | ||
| test("preserves a root whose directory name ends in a carriage return", async () => { | ||
| const repoPath = createTestRepo("trailing-cr\r"); | ||
| seedCommit(repoPath); | ||
|
|
||
| const result = await getGitRoot(repoPath); | ||
|
|
||
| expect(result.root).toBe(repoPath); | ||
| expect(result.root.endsWith("\r")).toBe(true); | ||
| expect(existsSync(result.root)).toBe(true); | ||
| expect(result.isRoot).toBe(true); | ||
| }); | ||
|
|
||
| // The walk-up is deliberate and stays: a subdirectory still resolves to the | ||
| // repo it belongs to. Only isRoot tells the caller the user picked something | ||
| // other than that root. | ||
| test("walks up from a subdirectory and reports it is not the root", async () => { | ||
| const repoPath = createTestRepo("subdir"); | ||
| seedCommit(repoPath); | ||
| const nested = join(repoPath, "packages", "app"); | ||
| mkdirSync(nested, { recursive: true }); | ||
|
|
||
| const result = await getGitRoot(nested); | ||
|
|
||
| expect(result.root).toBe(repoPath); | ||
| expect(result.isRoot).toBe(false); | ||
| }); | ||
|
|
||
| test("reports an untracked folder inside a repo as not the root", async () => { | ||
| const repoPath = createTestRepo("untracked-child"); | ||
| seedCommit(repoPath); | ||
| const scratch = join(repoPath, "scratch"); | ||
| mkdirSync(scratch, { recursive: true }); | ||
|
|
||
| const result = await getGitRoot(scratch); | ||
|
|
||
| expect(result.root).toBe(repoPath); | ||
| expect(result.isRoot).toBe(false); | ||
| }); | ||
|
|
||
| test("throws NotGitRepoError when no ancestor is a repo", async () => { | ||
| const plainPath = join(TEST_DIR, "plain"); | ||
| mkdirSync(plainPath, { recursive: true }); | ||
|
|
||
| await expect(getGitRoot(plainPath)).rejects.toBeInstanceOf(NotGitRepoError); | ||
| }); | ||
|
|
||
| // A worktree root keeps `.git` as a FILE, not a directory, so a filesystem | ||
| // check would misclassify it and offer to initialize a repo Superset made. | ||
| test("treats a linked worktree root as a root", async () => { | ||
| const repoPath = createTestRepo("worktree-main"); | ||
| seedCommit(repoPath); | ||
| const worktreePath = join(TEST_DIR, "worktree-linked"); | ||
| execSync(`git worktree add -b wt-branch "${worktreePath}"`, { | ||
| cwd: repoPath, | ||
| stdio: "ignore", | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| expect(statSync(join(worktreePath, ".git")).isFile()).toBe(true); | ||
|
|
||
| const result = await getGitRoot(worktreePath); | ||
|
|
||
| expect(result.root).toBe(worktreePath); | ||
| expect(result.isRoot).toBe(true); | ||
| }); | ||
|
|
||
| test("resolves a subdirectory of a worktree to the worktree", async () => { | ||
| const repoPath = createTestRepo("worktree-sub-main"); | ||
| seedCommit(repoPath); | ||
| const worktreePath = join(TEST_DIR, "worktree-sub"); | ||
| execSync(`git worktree add -b wt-sub-branch "${worktreePath}"`, { | ||
| cwd: repoPath, | ||
| stdio: "ignore", | ||
| }); | ||
| const nested = join(worktreePath, "src"); | ||
| mkdirSync(nested, { recursive: true }); | ||
|
|
||
| const result = await getGitRoot(nested); | ||
|
|
||
| expect(result.root).toBe(worktreePath); | ||
| expect(result.isRoot).toBe(false); | ||
| }); | ||
|
|
||
| // Git reports the real path, so a symlinked repo root would fail any | ||
| // comparison of the requested path against the reported root. | ||
| test("treats a symlink to a repo root as a root", async () => { | ||
| const repoPath = createTestRepo("symlink-target"); | ||
| seedCommit(repoPath); | ||
| const linkPath = join(TEST_DIR, "symlink-to-repo"); | ||
| symlinkSync(repoPath, linkPath); | ||
|
|
||
| const result = await getGitRoot(linkPath); | ||
|
|
||
| expect(result.root).toBe(repoPath); | ||
| expect(result.isRoot).toBe(true); | ||
| }); | ||
|
|
||
| // Bare repos and `.git` directories fail with "must be run in a work tree", | ||
| // not "not a git repository". They have no `.git` child, so classifying them | ||
| // as NotGitRepoError would offer to initialize a repo inside a repo. | ||
| test("does not classify a bare repo as NotGitRepoError", async () => { | ||
| const barePath = join(TEST_DIR, "bare.git"); | ||
| mkdirSync(barePath, { recursive: true }); | ||
| execSync("git init --bare", { cwd: barePath, stdio: "ignore" }); | ||
|
|
||
| await expect(getGitRoot(barePath)).rejects.not.toBeInstanceOf( | ||
| NotGitRepoError, | ||
| ); | ||
| }); | ||
|
|
||
| test("does not classify a .git directory as NotGitRepoError", async () => { | ||
| const repoPath = createTestRepo("dotgit"); | ||
| seedCommit(repoPath); | ||
|
|
||
| await expect(getGitRoot(join(repoPath, ".git"))).rejects.not.toBeInstanceOf( | ||
| NotGitRepoError, | ||
| ); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.