GeezerVison is showing github.com — *: Bound reference and worktree names to what git can lock by hiddeco · Pull Request #2401 · go-git/go-git
Conversation
hiddeco
commented
Sep 16, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Bound a name where it becomes a path, so go-git stops creating references and
worktrees canonical Git cannot operate on.
Git's files backend updates refs/heads/main by creating refs/heads/main.lock
beside it, so the longest reference it can move is that suffix short of the
longest one a directory entry holds. go-git writes the file in place, needs no
lock, and took the longer name — storing a reference only go-git can move.
storage: filesystem/dotgit
validReferenceNameLength bounds each component of a new reference name at 255
bytes and the last one at 250, applied from validNewReferenceName, so SetRef
and ReflogWriter refuse a name Git could not have created either. Reads and
deletes are unchanged, so a repository already holding such a name can still be
repaired.
The rule is not in ReferenceName.Validate: that mirrors check_refname_format,
which has no length rule. This is what storing a reference as a path costs, not
something the name itself is.
Rejections wrap plumbing.ErrInvalidReferenceName, so a fetch drops the one
reference and keeps the rest. They do not wrap ErrReferenceNameEscape, since a
name this long reaches no path it should not; the docs on ReferenceStorage and
on that sentinel are updated to say so. Lengths are reported rather than the name
quoted.
x: plumbing/worktree
Reject worktree names longer than 255 bytes before constructing metadata paths.
Previously, oversized names could produce filesystem errors or, when the gitdir
path exceeded the 1024-byte read limit, a misleading repository does not exist
error.
Add, Remove and Init now share validateName. Length is checked before
syntax, and invalid names are quoted with bounded output.
Add bounds an attached name at 250, five short of the directory limit, because
that name also becomes a branch. The reference storage refuses it too, but only
during the checkout, by which point Add has written the metadata directory and
the worktree's .git file; the preflight rejects it before anything is written.
WithDetachedHead creates no branch and keeps 255.
Behaviour changes
- Length and syntax errors wrap the new
ErrInvalidWorktreeName.
- Oversized names are reported by byte length rather than included in the error.
Remove rejects oversized names before attempting a lookup.
SetRef and ReflogWriter reject a reference name holding a component over
255 bytes, or a last component over 250.
The worktree limit applies to the directory entry under .git/worktrees. On the
tested filesystem, Git 2.55 accepts a 255-character name and rejects 256 with
File name too long, and refuses git worktree add -b at a 251-byte branch name
with cannot lock ref ... '<name>.lock': File name too long.
Known limitations
- The existing name regexp rejects some IDs Git creates, including
my_wt and
my.repo. This still affects Remove and Init; List and Open do not
validate names.
- The bound applies where go-git creates a reference.
Refs still enumerates an
over-long name already on disk, and Ref/RemoveRef still reach it, so a
repository holding one can be repaired.
Validation
go test -race ./...: one umask-dependent filemode failure in
x/plumbing/worktree, also present on main; remaining tests pass.
Checked against Git 2.55. A 250-byte worktree name created by go-git is one
git update-ref, git status in the worktree and git worktree list all
accept; a 250-byte reference written through SetRef is one git update-ref
and git branch --list accept. Both are refused at 251.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Add can leave an orphaned worktree for 251–255-byte names when branch creation fails; add branch-specific preflight or cleanup and boundary coverage.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This PR adds bounded worktree-name validation to prevent oversized metadata paths and limit error output.
Changes:
- Adds shared 255-byte validation for
Add, Remove, and Init.
- Adds
ErrInvalidWorktreeName and bounded error quoting.
- Adds boundary and validation tests.
File summaries
| File |
Summary |
x/plumbing/worktree/worktree.go |
Implements shared name validation and error handling. |
x/plumbing/worktree/worktree_test.go |
Tests validation boundaries and bounded errors. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
A loose reference is a file at .git/<name>, so every component of the
name is a directory entry and stops where directory entries stop. Git
stops sooner still on the last one: it updates refs/heads/main by
creating refs/heads/main.lock beside it, so the longest reference it can
move is that suffix short of the longest one a filesystem will hold.
go-git writes the file in place and needs no lock file, so it took the
longer name and stored it without complaint. That is the worse of the
two failures available: not a reference that refuses to store, but one
only go-git can move. git update-ref, git branch -f and a checkout of a
worktree on that branch all fail with ENAMETOOLONG, and nothing says so
until one of them runs.
validReferenceNameLength bounds a component at 255 and the last one at
250, and validNewReferenceName applies it, so SetRef and ReflogWriter
refuse a name canonical Git could not have created either. Reads and
deletes are unchanged: a repository already holding such a name still
needs a way to be rid of it.
The rule belongs here rather than in ReferenceName.Validate, which
mirrors check_refname_format, and check_refname_format has no length
rule. This is what storing a reference as a path costs, not something
the name itself is.
The rejection wraps plumbing.ErrInvalidReferenceName, which is what a
fetch tests to drop one reference and keep the rest, and not
ErrReferenceNameEscape: a name this long reaches no path it should not,
and calling it an escape would leave that sentinel meaning nothing in
particular. It reports lengths rather than quoting the name, which at
this length would be most of the message.
Assisted-by: Claude Opus 5 (1M context)
Signed-off-by: Hidde Beydals <[email protected]>
hiddeco
changed the title
x: plumbing/worktree, Bound the length of a worktree name
*: Bound reference and worktree names to what git can lock
Sep 17, 2026
A worktree name becomes a directory entry under .git/worktrees, so it
cannot be longer than a single path component, and nothing checked that.
Add joined an arbitrarily long name into five metadata paths before the
filesystem refused it, and past about a kilobyte the failure stopped
being legible at all: the worktree's .git file is read back through a
1024 byte limit, so a longer gitdir path was truncated and Add reported
"repository does not exist" for a name it had just written itself.
validateName rejects anything longer than 255 bytes, and Add, Remove and
Init share it rather than each repeating the name test. That is the
limit a path component has on the filesystems go-git runs on, and git
refuses a longer name with ENAMETOOLONG, so nothing that could have
become a worktree is lost.
An attached worktree takes its name as a branch name as well, and there
git stops five bytes sooner: it updates refs/heads/<name> through a
sibling <name>.lock, so the longest branch it can carry is the longest
directory entry less that suffix. The reference storage now refuses such
a name, but it refuses it during the checkout, by which point Add has
written the metadata directory and the worktree's .git file — leaving
exactly the half-made worktree a name check is there to avoid. Add
bounds a name it turns into a branch at 250 before it writes anything,
and keeps 255 for a detached one, which creates no branch to outgrow.
Checking the length before the shape also keeps an over-long name out of
the message describing it, and what still reaches that message is quoted
bounded: quoting expands a byte up to fourfold, so a name at the limit
made an error four times its size.
Both rejections wrap ErrInvalidWorktreeName, so a caller can tell a name
it chose badly from a filesystem that refused one. A name rejected for
its length is reported by that length rather than its contents, so a
name that is both too long and malformed no longer names the byte that
offended. Remove refuses a name it would previously have looked up,
which can only matter for a directory git could not have created.
Assisted-by: Claude Opus 5 (1M context)
Signed-off-by: Hidde Beydals <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
pjbgf
pjbgf approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|