Skip to content

fix(vcs): select the root directory a clone command was given - #252

Merged
HarshMN2345 merged 7 commits into
mainfrom
fix/vcs-clone-root-directory-sentinels
Sep 17, 2026
Merged

HarshMN2345 merged 7 commits into
mainfrom
fix/vcs-clone-root-directory-sentinels

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

The bug

generateCloneCommand() writes the root directory straight into .git/info/sparse-checkout, where git matches it gitignore-style. A ./ prefix looks for a directory literally named ., so the clone checks out nothing.

Reproduced against real git — bare repo, core.sparseCheckout, depth-1 pull:

pattern docs     -> checked out: [docs]
pattern ./docs   -> checked out: []
pattern .        -> checked out: []
pattern ./       -> checked out: []

. and ./ are the conventional spellings of the repository root, and a caller reading a path from a directory picker or config file routinely holds the ./ form. The failure is silent at clone time and surfaces later as a missing entrypoint — npm ENOENT ... package.json and the like.

Why it slipped through

Each adapter tested for the root itself, in two different shapes, and neither handled . or ./:

adapter test
GitHub.php:1192, Gitea.php:1060, Origin.php:894 $rootDirectory === '' || $rootDirectory === '0'
GitLab.php:981, Bitbucket.php:1508 \in_array($rootDirectory, ['', '0', '/'], true)

Meanwhile the package already answers this exact question correctly one file over: Git::normalizeRepositoryPath() has resolved these sentinels for the contents APIs since c2c60ae ("Resolve path sentinels the same way on every provider … a caller passing '.', './' or 'src//' gets the same answer everywhere"). It was applied to the 11 contents call sites and not to the one clone call site.

The change

Each adapter resolves the root directory through normalizeRepositoryPath() first, and its root test becomes === ''. That test is explicit rather than ?:, because '0' is a real directory name and PHP treats it as falsy.

'0' goes with it. That was an empty() leftover — a repository whose top-level directory is named 0 was treated as root and cloned whole. It now checks out that directory. Behaviour change, deliberate, called out in case anyone disagrees.

'/' still resolves to * on every adapter, so GitLab and Bitbucket keep their existing handling and the other three gain it.

Also in here

Gitea::$giteaUrl was declared without a default and assigned only in setEndpoint(), so an adapter built without one fataled on the first URL it tried to build — both generateCloneCommand() and getRepositoryUrl() read it. Each Gitea-shaped class now defaults it to the same host its own $endpoint already names.

That is what kept these cases off the shared suite: the test could not construct a Gitea, Gogs or Forgejo adapter far enough to build a clone command.

Tests

The sparse-checkout cases live on the shared unit base and run for every adapter: 60 cases over '', ., ./, /, docs, docs/, ./docs, ./docs/, ./astro/starter and 0. Against the old code 8 of the 10 forms fail per adapter — everything except '' and docs, the two it already handled.

phpunit --testsuite unit: 150 passing. pint and phpstan: clean.

Context

Found while tracing a root-directory bug in Appwrite (appwrite/appwrite#13574). Appwrite does not hit this today only because its builds worker canonicalizes one line before the call — other consumers are exposed.

generateCloneCommand writes the root directory into .git/info/sparse-checkout,
where git matches it gitignore-style. A './' prefix therefore looks for a
directory literally named '.', and the clone checks out an empty tree:

  pattern 'docs'    -> docs
  pattern './docs'  -> (nothing)
  pattern '.'       -> (nothing)
  pattern './'      -> (nothing)

'.' and './' are the conventional spellings of the repository root, and a
caller that reads a path from a directory picker or a config file routinely
holds the './' form. The five adapters each tested for the root themselves,
in two different shapes - '' or '0' on GitHub, Gitea and Origin, and
['', '0', '/'] on GitLab and Bitbucket - and none of them handled either.

Resolve the pattern through normalizeRepositoryPath instead, which the
contents APIs have used for these same sentinels since c2c60ae. The '0' test
went with it: that was an empty() leftover, and a repository whose top-level
directory is named '0' now checks out rather than silently cloning whole.

The clone command needs no provider, so the shared unit base covers it for
every adapter. Building one there also means the Gitea-family adapters must
carry an endpoint, as every other operation on them already assumes.
@greptile-apps

greptile-apps Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The implementation appears safe to merge, with only the existing non-blocking concern that its regression test mirrors generated command text instead of verifying the checkout result.

Fix All in Claude CodeFindings

  1. P2 Test Mirrors Command Text ▶
Fix with agent prompt
### Issue 1
packages/vcs/tests/Unit/Base.php:undefined-232
This test checks only that the shell-escaped pattern occurs somewhere in the generated command. It can therefore pass if the pattern is written to the wrong file, duplicated, or later overridden, and it never verifies the checkout tree that this regression actually broke. This violates the repository directive to test observable behavior instead of mirroring source or configuration. The requirement must be satisfied before merging by exercising the command against a local Git fixture and asserting the resulting files.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR normalizes sparse-checkout root paths consistently across all Git adapters before generating clone commands.

  • Treats ., ./, and / as repository-root selections.
  • Preserves 0 as a real directory name rather than a root sentinel.
  • Adds GitHub test cases for normalized sparse-checkout patterns, although the existing implementation-coupled-test finding remains unresolved.

Reviews (6) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..."

{
$command = $this->vcsAdapter->generateCloneCommand('owner', 'repo', 'main', Git::CLONE_TYPE_BRANCH, '/tmp/clone', $rootDirectory);

$this->assertStringContainsString(escapeshellarg($pattern), $command);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Test Mirrors Command Text

This test checks only that the shell-escaped pattern occurs somewhere in the generated command. It can therefore pass if the pattern is written to the wrong file, duplicated, or later overridden, and it never verifies the checkout tree that this regression actually broke. This violates the repository directive to test observable behavior instead of mirroring source or configuration. The requirement must be satisfied before merging by exercising the command against a local Git fixture and asserting the resulting files.

Context Used: Call out and harshly judge implementation-coupled ... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/vcs/tests/Unit/Base.php
Line: 232

Comment:
**Test Mirrors Command Text**

This test checks only that the shell-escaped pattern occurs somewhere in the generated command. It can therefore pass if the pattern is written to the wrong file, duplicated, or later overridden, and it never verifies the checkout tree that this regression actually broke. This violates the repository directive to test observable behavior instead of mirroring source or configuration. The requirement must be satisfied before merging by exercising the command against a local Git fixture and asserting the resulting files.

**Context Used:** Call out and harshly judge implementation-coupled ... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Codex

Each adapter resolves the root directory through normalizeRepositoryPath and
tests the result for the repository root itself, rather than calling a one-line
wrapper around it. The test is an explicit === '' instead of ?:, since '0' is a
real directory name and PHP treats it as falsy.
Comment thread packages/vcs/tests/Unit/ForgejoTest.php Resolved
The clone command reads $giteaUrl, which has no default and is assigned
only by setEndpoint, so an adapter must carry an endpoint before the shared
suite can build a command from it. Naming each provider's own host keeps a
Forgejo or Gogs adapter from appearing to point at Gitea.
Gitea assigns $giteaUrl only in setEndpoint and the property has no default,
so an adapter cannot build a clone command until an endpoint is set. Doing it
in the base the three Gitea-shaped adapters already share keeps each provider
test as it was, rather than naming a Gitea host inside Forgejo and Gogs.
…endpoint

Move the sparse-checkout pattern cases onto the GitHub suite. The shared base
is extended by the Gitea-shaped adapters, which cannot build a clone command
until an endpoint is set, and configuring one there is beside the point of
this change.
…rectory-sentinels

# Conflicts:
#	packages/vcs/tests/Unit/GitHubTest.php
$giteaUrl was declared without a default and assigned only in setEndpoint,
so an adapter built without one fataled on the first URL it built:
generateCloneCommand and getRepositoryUrl both read it. Each class now
defaults it to the same host its own $endpoint already names.

With that, the sparse-checkout cases move back to the shared unit base and
cover every adapter instead of GitHub alone.
@HarshMN2345
HarshMN2345 merged commit 1223252 into main Sep 17, 2026
6 of 8 checks passed

This branch had an error being deployed

1 failed deployment
vcs — 3045db1e Deployed Sep 16, 2026 by HarshMN2345 via test (vcs) #968
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants