Skip to content

On Windows, Rename() can be performed even if the destination is read-only#186

Draft
koron wants to merge 1 commit intogo-git:mainfrom
koron:windows-rename-to-readonly-file
Draft

On Windows, Rename() can be performed even if the destination is read-only#186
koron wants to merge 1 commit intogo-git:mainfrom
koron:windows-rename-to-readonly-file

Conversation

@koron
Copy link

@koron koron commented Feb 24, 2026

Unlike Linux, Windows os.Rename() fails if the destination file is read-only. Therefore, in this commit, we make the read-only file writable, then os.Rename() it, and restore the original permissions.

This fixes a problem in go-git/go-git where Worktree.Add() always fails on repositories checked out with the git command on Windows because the object files are read-only.

Unlike Linux, Windows os.Rename() fails if the destination file is
read-only. Therefore, in this commit, we make the read-only file
writable, then os.Rename() it, and restore the original permissions.

This fixes a problem in go-git/go-git where Worktree.Add() always fails
on repositories checked out with the git command on Windows because the
object files are read-only.

Signed-off-by: MURAOKA Taro <[email protected]>
@koron koron force-pushed the windows-rename-to-readonly-file branch from 0f67f88 to a74085a Compare February 24, 2026 13:03
@koron koron marked this pull request as draft February 24, 2026 15:38
@koron
Copy link
Author

koron commented Feb 24, 2026

In combination with go-git/go-git@v6, there were cases where it did not work, so I made it a draft.

@pjbgf
Copy link
Member

pjbgf commented Feb 24, 2026

@koron thank you for submitting this PR. This seems to be related to go-git/go-git#55.

I'm looking at that issue currently, on a first principles base, go-git should not try to rename nor override read-only files. Loose and packed object files are imutable as they are content-addressable. They can be created or deleted (during repacking for example), but not overwritten.

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.

2 participants