Closed
Description
Summary
The pattern of editing a file in a sparse-checkouted branch and doing a commit -> push worked.
However, when I run switch branch from the sparse-checkouted branch, I get the error worktree contains unstaged changes.
If I check the status in worktree.Status(), I see that the unchecked out files are marked as deleted.
When I run the git status command on the sparse-checkouted directory in go-git, I get working tree clean. I think the cause is that go-git's worktree.Status does not take sparse-checkout into account.
Test Repository
I have prepared a Repository with files arranged as follows.
[go-git-test(main =)] $ tree
.
├── README.md
├── dir1
│ └── test.txt
├── dir2
│ └── test.txt
├── dir3
│ └── test.txt
└── root.txt
4 directories, 5 files
[go-git-test(main =)] $
Pattern of running switch branch from sparse-checkouted branch
test code
https://go.dev/play/p/1Cd4nSqS97v
execution log
$ go run main.go
dir1
└── test.txt
panic: worktree contains unstaged changes
goroutine 1 [running]:
main.main()
/Users/takumi.kaneda/asobiba/main.go:46 +0x3bc
exit status 2
$
The error occurred in the following code section
// switch branch
if err := w.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName("branch1"),
Create: true,
}); err != nil {
panic(err)
}
status check test code
https://go.dev/play/p/9-o8v2HPujF
execution log
$ go run main.go
dir1
└── test.txt
dirty
README.md D
dir2/test.txt D
dir3/test.txt D
root.txt D
dir1/test.txt D
$
Result of git status command execution
$ tree
.
└── dir1
└── test.txt
2 directories, 1 file
$
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
$