git: worktree, Don't delete local untracked files when resetting worktree#1540
git: worktree, Don't delete local untracked files when resetting worktree#1540pjbgf merged 4 commits intogo-git:mainfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR refines the worktree reset functionality so that local untracked files are preserved when resetting the worktree, addressing issue #53.
- Updates the test in worktree_test.go to verify that untracked files (e.g. "foo") remain unaltered.
- Modifies the ResetSparsely, resetIndex, and resetWorktree functions in worktree.go to track removed files and adjust the file checkout logic based on untracked status.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| worktree_test.go | Replaces a simple IsClean assertion with a loop to validate specific file statuses. |
| worktree.go | Returns removed file information from resetIndex and uses it in resetWorktree to decide which files to check out. |
worktree.go
Outdated
| // only checkout an untracked file if it is in the list of files | ||
| // a reset should leave untracked files alone | ||
| file := nameFromAction(&ch) | ||
| if status.File(file).Worktree != Untracked || inFiles(files, file) { |
There was a problem hiding this comment.
Can we use ch instead of status? I think something like
action, err := ch.Action()
if err != nil {
return err
}
if action != merkletrie.Insert {
// checkout the file
}Would work since "Untracked" would mean that the file appears in worktree but not in staging.
For reference, Status() also works this way internally.
Lines 93 to 118 in 93ed29d
This way we can avoid re-doing computations that we already have done.
There was a problem hiding this comment.
Hey @onee-only
Thanks for the reviews. I just realised that I don't need the check at all and I can directly do checkout.
The files being passed is always going to just include the files which need changes and no other files.
worktree.go
Outdated
| // only checkout an untracked file if it is in the list of files | ||
| // a reset should leave untracked files alone | ||
| file := nameFromAction(&ch) | ||
| if status.File(file).Worktree != Untracked || inFiles(files, file) { |
There was a problem hiding this comment.
Is there any specific reason that we check inFiles here? I think it is already done in block above.
I wasn't paying attention to the comment. Sorry for the confusion.
There was a problem hiding this comment.
Hey
You were right actually. I just removed the complete condition.
|
I have been looking for a solution to the same problem. I have tried these changes and they work in most cases, but when checking out to a branch with the same commit, so no files change, it still deletes all untracked files (including files that are in the gitignore). |
|
Hey @UnseenBook Thanks for looking into this and yes it does fail when reseting on to same commit. I am also discovering some more edge cases. I will take a look at it later again. |
|
@k-anshul I have created a PR to your branch with changes that I believe are what we need to solve this issue. The tests run successfully with this change. I hope this helps. |
Sounds good @UnseenBook. Just merged your changes in my PR. Thanks for looking into this. |
|
Thanks @k-anshul ! |
I could not reproduce the issues after your fix :) |
|
@k-anshul can you adjust the commit message to get this merged please? |
Done @jacksoncalvert |
|
@onee-only we are seeing this problem in multiple instances. can we get this merged please |
Unfortunately I'm not the maintainer of this project. Let's wait for maintainers to review this PR, though they appear to be inactive on go-git at the moment. |
|
@pjbgf Hi Paulo, can we please get this merged in. My team is using this package and this issue is hugely blocking for some of our tasks. Cheers |
|
If it's desired to have a test for this fix, I created a PR to add one: rilldata#4 @k-anshul If you merge the PR by using the rebase option, it should not create a merge commit that would fail the commit message check. |
|
Is there any chance this change could be incorporated in one of the upcoming v5.x.x releases? |
git: worktree, Don't delete local untracked files when resetting worktree
git: worktree, Don't delete local untracked files when resetting worktree
Fixes #53