Skip to content

adopt-project.py: a source symlink whose target begins with a NUL byte is check's plan-empty-symlink, which execute refuses before any leg, so \0spec.md no longer fails the first clone after both legs exist, or verifies over a spec leg no clone can check out (#388) - #720

Merged
brettheap merged 2 commits into
mainfrom
fix/388-adopt-nul-symlink-target
Oct 16, 2026

Conversation

@brettheap

@brettheap brettheap commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Lane: openreposhape-1 (openRepoShape-1)
Claim: #388 (comment)

What

A symlink in the source whose target begins with a NUL byte is now check's plan-empty-symlink, as an empty target has been since #303. execute refuses it before any leg exists, and plan notes it while it still writes the plan.

  • Read the targets once. plan and check read the target of every source symlink whose blob is not empty, with one git cat-file --batch over the blob ids Source.tree() already lists (symlink_targets). Each id is read once, however many links share it. Each object is cut by the size git prints, so a target that holds a line feed is read whole. A link of size 0 is known to be empty without reading it, and a source with no symlink of size 1 or more starts no process for this.

  • One finding. The new link uses the same code, worded for its blob. A target that begins with NUL is empty to Linux, it fails the same way at the same place, and its remediation is the same:

    FINDING plan-empty-symlink: `docs/odd-link` is a symlink whose target begins with a NUL byte at the source commit; a symlink's target ends at its first NUL, so this one is empty too, and Linux cannot make a symlink to nothing. There, where git makes symlinks by default, each clone `execute` makes of the source stops at it after both leg repositories were created, whatever the plan answers for it; and where `core.symlinks` is false the clone holds it as a plain file, and the leg or root the plan keeps it in then cannot be cloned on Linux, where the root's own `validate` workflow runs.
    

    An empty target's sentence is unchanged. Past the twentieth link, the count line now reads … and N more symlinks whose target is empty or begins with a NUL byte.

  • One link, one finding, in the root too. A NUL-led link the plan keeps in the root at or above a path the shape writes (.github, CLAUDE.md) gets this finding alone. On main it was refused only as plan-root-symlink, "leads to \x00ci, which is not in the source tree", whose fix ("send it to a leg or drop it") does not help it with symlinks on. The walk of materialize_assembly_root writes the shape files through a root-kept symlink to anywhere on disk, and crashes with a traceback on a link loop, after both legs are pushed #183 (_PlannedRoot._through) now takes a NUL-led target as it takes an empty one, so _root_symlink_findings leaves the link to plan-empty-symlink, as test_adopt_root_symlinks.py already asserts for an empty target.

Why (reproduced on main da48256)

The issue's script, with docs/odd-link a 120000 blob holding \0spec.md, every repository local, the stub gh first on PATH:

120000 blob da51ee712ecf519313363bb25342bb6dc80f4b8d       8	docs/odd-link
plan exit 0
check exit 0: plan ok
execute exit 2:
REFUSED extracting the spec leg. THE EXACT COMMAND WAS:
    git -c protocol.file.allow=always clone -q <T>/Thing <T>/work/Northwind-spec
    exit 128
--- output ---
error: unable to create symlink docs/odd-link: No such file or directory
fatal: unable to checkout working tree
...
NOTHING has been rolled back: `<T>/remotes/Northwind-spec.git` and `<T>/remotes/Northwind-code.git` exist, and neither leg is pushed. ...
leg repositories left: Northwind-code.git Northwind-spec.git

With core.symlinks=false for the whole run, on the same source:

check exit 0: plan ok
execute exit 0: adoption verified: every source path is in exactly one place
the spec leg holds: 120000 blob da51ee712ecf519313363bb25342bb6dc80f4b8d	docs/odd-link
a clone of the spec leg: exit 128
error: unable to create symlink docs/odd-link: No such file or directory
fatal: unable to checkout working tree

The same runs on this branch (939623b). Symlinks on and off print the same finding, and no leg is made:

plan exit 0              (and the NOTE above, in `plan`'s words)
check exit 1             (FINDING plan-empty-symlink: `docs/odd-link` is a symlink whose target begins with a NUL byte ...)
execute exit 2
REFUSED plan-empty-symlink: 1 finding(s) in <T>/plan.yaml, as `check` prints them:
Remediation: no leg repository was created and nothing was pushed. ...
leg repositories left:

The control, docs/odd-link -> spec.md (7 bytes), still checks plan ok and ends adoption verified, with symlinks on and off.

How

Tests

tests/test_adopt_empty_symlinks.py: 28 cases on main, 53 on this branch. Five tests are now parametrized over both targets, empty and nul-led: the plan NOTE, check in each place and with core.symlinks=false, and execute in-process and end to end. test_each_link_is_named and test_it_comes_after_the_unsafe_paths_and_before_coverage keep the empty target. A new place in PLACES, .github kept in the root, runs the #183 walk.

  • New tests. test_a_symlink_whose_target_begins_with_nul_is_empty_too covers a file whose blob begins with NUL, a link to a name and an unread link. test_the_finding_says_where_a_nul_led_target_ends checks the words. test_the_targets_are_read_in_one_pass_and_only_where_there_is_one expects exactly [["git", "cat-file", "--batch"]], two links sharing one blob, and a target holding a line feed. test_a_source_whose_links_are_all_empty_reads_no_target expects no process at all.
  • New cases. nul-led runs of the plan NOTE, of check in six places, of check with core.symlinks=false, and of execute in-process in six places. The end-to-end execute runs with symlinks on and off, docs/ and top-level drop. The control test_the_link_given_a_target_adopts_end_to_end now runs with symlinks on and off.
  • Existing tests. Three in-memory tests pass {} (no target read) for the new argument. test_a_source_with_none_has_no_finding_and_no_remediation hands in its link's target, spec.md. test_past_twenty_the_rest_are_counted makes its last link NUL-led and expects the widened count line.

Revert table. Each change was reverted alone in adopt-project.py, with the final tests, and tests/test_adopt_empty_symlinks.py run in full (git-filter-repo on PATH):

change reverted result what fails
the whole adopt-project.py change (main's file) 27 failed, 26 passed every nul-led case (18), the 4 new tests, test_past_twenty_the_rest_are_counted, and the 4 existing in-memory tests (only for the new argument)
R1 the rule: empty_symlinks by size alone 21 failed every nul-led case (18), ..._begins_with_nul_is_empty_too, ..._says_where_a_nul_led_target_ends, test_past_twenty_the_rest_are_counted
R2 the read: symlink_targets returns {} 19 failed every nul-led case (18), ..._read_in_one_pass_...
R3 reading size-0 links too 2 failed ..._read_in_one_pass_..., ..._all_empty_reads_no_target
R4 the walk: _through tests not target alone 2 failed test_check_reports_what_it_passed[above-a-shape-path-kept-in-the-root-nul-led], test_execute_refuses_it_before_any_leg_exists[above-a-shape-path-kept-in-the-root-nul-led]
R5 the words: the empty sentence for every link 19 failed ..._says_where_a_nul_led_target_ends and every nul-led case that reads the words (18)
R6 the count line: "whose target is empty" 1 failed test_past_twenty_the_rest_are_counted

Three new cases pass with the change reverted, by design. They are test_check_reports_what_it_passed and test_execute_refuses_it_before_any_leg_exists at above-a-shape-path-kept-in-the-root-empty, which #303 already handles, and the control test_the_link_given_a_target_adopts_end_to_end[symlinks-off].

Targeted runs. These covered every test file that runs or imports adopt-project.py (grep -l -E 'adopt-project|adopt_project|\bADOPT\b' tests/*.py: 86 files on da48256), test_shape_doctor.py and test_scaffold_leg_path_collisions.py among them. They ran on head 939623b with dirty 0, with git-filter-repo on PATH and with it hidden:

  • with git-filter-repo: 6392 passed, 13 skipped, 0 failed (EXIT=0, 1:05:16)
  • without: 6043 passed, 362 skipped, 0 failed (EXIT=0, 0:52:46)

Windows

CI is Linux only (#597). The tests still run anywhere. Every link is made in the index from bytes on git hash-object --stdin, so no symlink is made on a disk to build a source. The only test that checks a link out with symlinks on is the control's symlinks-on case, which keeps its needs_disk_symlinks skip. Every path a test makes is legal on NTFS. The line\nbreak target lives only in a blob and is never checked out. No test counts bytes of a file a helper wrote with write_text.

Not in this PR

Known limits

Closes #388

🤖 Generated with Claude Code

Summary by Sourcery

Reject source symlinks with empty or NUL-terminated targets before adoption can create unusable leg repositories.

New Features:

  • Detect symlinks whose targets begin with a NUL byte and report them as empty symlink findings during planning and validation.

Bug Fixes:

  • Prevent execution from creating leg repositories before refusing source symlinks that Linux cannot check out.
  • Ensure root-kept NUL-led symlinks receive the appropriate empty-symlink finding instead of an unrelated root-symlink finding.

Enhancements:

  • Read non-empty symlink targets in a single deduplicated batch operation and tailor diagnostics for NUL-led targets.

Tests:

  • Expand empty-symlink coverage across planning, checking, execution, root traversal, symlink settings, and end-to-end adoption scenarios.

…symlink whose blob is not empty, in one `git cat-file --batch`, and report one that begins with a NUL byte as `plan-empty-symlink`, so `\0spec.md` no longer checks ok and then fails `execute`'s first clone after both legs exist, or verifies over a spec leg no clone can check out (#388)

#303's `plan-empty-symlink` found an empty symlink by its blob's size alone.
A `120000` blob that begins with a NUL byte (`\0spec.md`, 8 bytes) is not
empty, but git hands the target to `symlink(2)` as a C string, which ends at
its first NUL, so Linux reads it as the empty target. On main da48256,
`check` said `plan ok`; with symlinks on, `execute` created both leg
repositories and failed at its first clone of the source (`unable to create
symlink docs/odd-link: No such file or directory`); with `core.symlinks`
false it said `adoption verified` over a spec leg whose own clone exits 128.

- `symlink_targets(source, tree)`: one `git cat-file --batch` over the blob
  ids `Source.tree()` lists for every symlink of size 1 or more, each id
  once, cut by the size git prints, so a line feed in a target is read
  whole; a link of size 0 is not read, and a source with none starts no
  process.
- `empty_symlinks(tree, targets)`: size 0, or a target that begins with
  NUL. Its callers, `_source_path_findings` (so `check` and `execute`'s
  `_refuse_what_check_finds`) and `plan`'s `_empty_symlink_notes`, pass the
  targets. The finding is the same code, worded for its blob ("begins with
  a NUL byte ...; a symlink's target ends at its first NUL, so this one is
  empty too", held "as a plain file" where `core.symlinks` is false), and
  the "... and N more" tail now says "empty or begins with a NUL byte".
- The walk of #183 (`_PlannedRoot._through`) takes a NUL-led target as it
  takes an empty one, so a link kept in the root at or above a shape path
  is this one finding and not also `plan-root-symlink`, whose fix (send it
  to a leg, or `drop` it) is none for it. On main that link was refused
  only as `plan-root-symlink`.

A NUL later in the target (`spec\0.md`) and a target too long for the
platform are not this change's; both still fail after both legs exist.

Lane: openreposhape-1 (openRepoShape-1)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Oct 16, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR makes source symlinks whose blob begins with a NUL byte fail early as plan-empty-symlink, by reading non-empty symlink blobs in one deduplicated batch and applying the result consistently to plan notes, check findings, execute refusal, and root traversal; extensive tests cover diagnostics, I/O behavior, placement, platform modes, and regression controls.

Sequence diagram for batched symlink validation

sequenceDiagram
    participant Plan
    participant Source
    participant Git
    participant Findings

    Plan->>Source: tree()
    Plan->>Git: cat-file --batch(blob ids)
    Git-->>Plan: symlink target bytes
    Plan->>Findings: empty_symlinks(tree, targets)
    Findings-->>Plan: plan-empty-symlink notes

    participant Check
    Check->>Source: tree()
    Check->>Git: cat-file --batch(blob ids)
    Git-->>Check: deduplicated target bytes
    Check->>Findings: _empty_symlink_findings(tree, targets)
    Findings-->>Check: findings and remediation
Loading

State diagram for root traversal of NUL-led symlinks

stateDiagram-v2
    [*] --> FollowingSymlink
    FollowingSymlink --> EmptyTarget: target is empty
    FollowingSymlink --> EmptyTarget: target starts with NUL
    EmptyTarget --> PlanEmptySymlink: _root_symlink_findings
    PlanEmptySymlink --> [*]
Loading

Flow diagram for early NUL-led symlink refusal

flowchart TD
    A[Source tree contains symlink] --> B{Blob size is zero?}
    B -- Yes --> D[plan-empty-symlink]
    B -- No --> C[Read target with cat-file --batch]
    C --> E{Target begins with NUL?}
    E -- Yes --> D
    E -- No --> F[Continue normal planning]
    D --> G[check fails]
    G --> H[execute refuses before creating leg repositories]
Loading

File-Level Changes

Change Details Files
Detect NUL-led symlink targets as empty during planning and validation, using deduplicated batched blob reads.
  • Add symlink_targets to read non-empty symlink blobs once via git cat-file --batch, preserving blob sizes and handling missing objects.
  • Extend empty-symlink detection to targets beginning with NUL while retaining size-zero detection without reads.
  • Pass target data through plan, check, and execute refusal paths, with tailored findings, notes, remediation, and aggregate wording.
adopt-project.py
Treat NUL-led symlinks kept in the root as the same empty-symlink finding during root path traversal.
  • Stop root symlink walks on targets beginning with NUL, matching empty-target behavior.
  • Prevent duplicate or misleading root-symlink findings for these links.
adopt-project.py
Expand coverage for NUL-led symlink behavior, batched reads, and unchanged valid-target behavior.
  • Parametrize empty-symlink tests over empty and NUL-led targets across plan, check, execute, root placement, and symlink configuration modes.
  • Verify one batched read per unique blob, no reads for size-zero links, newline-containing targets, missing/unread targets, and precise finding text.
  • Run the valid-target end-to-end control with symlinks enabled and disabled.
tests/test_adopt_empty_symlinks.py

Assessment against linked issues

Issue Objective Addressed Explanation
#388 Detect source symlinks whose target begins with a NUL byte during plan/check and report them as empty-target symlinks, including when such a link is kept in the root. ✅
#388 Have execute refuse plans containing such links before creating any leg repositories, preventing both the failed-clone case and verification of an uncheckoutable leg when core.symlinks=false. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai

sourcery-ai Bot commented Oct 16, 2026

Copy link
Copy Markdown

Sorry @brettheap, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 4 hours and 14 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@sonarqubecloud

Copy link
Copy Markdown

@brettheap
brettheap merged commit 5c99070 into main Oct 16, 2026
4 of 5 checks passed
@brettheap
brettheap deleted the fix/388-adopt-nul-symlink-target branch October 16, 2026 19:13
brettheap pushed a commit that referenced this pull request Oct 17, 2026
main gained 25 pull requests since de92614: #634, #699, #671, #719,
#701, #717, #700, #702, #722, #720, #725, #721, #728, #705, #727, #718,
#744, #743, #749, #740, #737, #734, #742, #751 and #736. No conflict:
AGENTS.md, adopt-project.py and docs/cli.md merged as text, and
README.md and docs/handbook.html are unchanged by the merge. README.md
is 1473 lines, its cap; AGENTS.md 494, its cap; every handbook marker is
current; `render-cli-reference.py --check` is ok.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
brettheap pushed a commit that referenced this pull request Oct 17, 2026
…es every reason THIS checkout cannot answer, and the notes are current to main bf1f099 with the 25 pull requests since de92614 (#563, #683)

- W683 and RV-f's N4 on #742: README's exit-3 list (Keeping a project's
  shape current, :1208-1220) and its handbook twin (`doctor`) named only a
  pin whose commit this checkout does not carry. They now say, as
  shape-doctor.py's EXIT CODES does: a pin's commit it lacks, a partial
  clone missing what is compared (#683), a read that fails
  (`update-upstream-unreadable`), a crashed validator of its own (#278,
  #410), or its own naming policy not loading (#678). +1 line, with
  tests/readme-cap.d/683.txt; README.md is 1474, its cap (1422 + 2 + 5 +
  12 + 22 + 10 + 1). The two `Keeping a project's shape current` markers
  move: `doctor` text and marker, `current` marker only.
- The notes, "What changed since v1.0.1", one entry per pull request from
  `git log --first-parent de92614..bf1f099`, each from its squash commit and
  the issues GitHub says it closes:
  - adopt-project.py: #699, #719, #749, #727, #717, #728 (with #621's
    hint), #743 + #720 + #718, #722, #721;
  - update-shape.py, bump-leg.py and a project's own scripts: #702 (with
    the pinned validators), #634;
  - scaffold, setup, family and the shared readers: #725, #671, #744;
  - shape-doctor.py: #742, #701, #737, #751, #736, #734, #700 + #705,
    #740;
  - install, CI and the docs: the exit-3 sentence above.
- Counts (99 pull requests since bfed143; 109 of 135 milestone issues
  closed at 10:28Z), the "current to main" sha and the open list (26) are
  at main bf1f099, still placeholders for tag time.

Left for round B: lane 5's eight lines (#630 issuecomment-6227129879),
whose pull requests #726, #747, #682, #724, #746, #745 and #738 are open
drafts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant