Skip to content

git: worktree, fix Add silently failing for absolute paths - #1949

Merged
pjbgf merged 1 commit into
go-git:mainfrom
tmchow:osc/1460-fix-worktree-add-absolute-path
Apr 2, 2026
Merged

pjbgf merged 1 commit into
go-git:mainfrom
tmchow:osc/1460-fix-worktree-add-absolute-path

Conversation

@tmchow

@tmchow tmchow commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Description

Worktree.Add() silently fails when given an absolute path. No error is returned, but the file is not added to the index because the index stores relative paths.

The fix converts absolute paths to worktree-relative paths using filepath.Rel() before updating the index. If the path is outside the worktree root, an error is returned.

Link to tracking issue

Fixes #1460

Testing

Added TestAddAbsolutePath which:

  1. Creates a repo with a real filesystem
  2. Writes a file
  3. Calls w.Add() with the absolute path
  4. Verifies the index contains the relative path
  5. Verifies status shows the file as staged

go test ./... and go vet ./... pass.

Documentation

N/A - behavior now matches git add /absolute/path.

This contribution was developed with AI assistance (Codex).

Convert absolute paths to worktree-relative paths in doAdd() before
updating the index. Return an error if the path is outside the worktree
root. Previously, absolute paths passed through filepath.Clean but were
never relativized, causing the file to not be tracked.

Fixes #1460

@pjbgf pjbgf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@tmchow thanks for working on this. 🙇

@pjbgf
pjbgf merged commit 7aeb877 into go-git:main Apr 2, 2026
23 of 24 checks passed
@tmchow

tmchow commented Apr 2, 2026

Copy link
Copy Markdown
Contributor Author

@tmchow thanks for working on this. 🙇

You bet!

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.

Worktree.Add() silently fails for absolute paths

2 participants