Repository navigation
Description
Repository::checkout() overwrites uncommitted changes in tracked files that the branch switch does not touch. git keeps those changes (git checkout prints M\ta.txt and carries the edit over). Staged changes are lost the same way. The same happens in merge() (fast-forward and merge commit), cherryPick() and the other operations that rebuild the working tree through resetWorktree().
No error is raised and the edit is gone.
Repro (v0.2.11, PHP 8.4)
mkdir t && cd t
pitmaster init
git config user.name T && git config user.email t@x
printf 'a\n' > a.txt && printf 'b\n' > b.txt
pitmaster add a.txt b.txt && pitmaster commit -m init
pitmaster branch feature && pitmaster checkout feature
printf 'b2\n' > b.txt && pitmaster add b.txt && pitmaster commit -m b2
pitmaster checkout main
printf 'a-local\n' >> a.txt # uncommitted edit in a file both branches share
pitmaster status # modified: a.txt
pitmaster checkout feature # exit 0, "Switched to 'feature'"
cat a.txt # "a" (the local line is gone)With git in the same state, git checkout feature keeps a-local in a.txt.
The same loss happens with pitmaster merge feature and pitmaster cherry-pick <sha> when an unrelated tracked file has local edits, and when the edit was staged with add before the checkout.
Expected
Changes in paths that are identical in the source and target trees are carried over to the new checkout (as git does). Changes in paths that do differ keep raising the existing "would be overwritten" error.
Actual
assertSafeCheckout() only inspects the paths that differ between the two trees (
Lines 4984 to 5004 in 5beb97d
| private function assertSafeCheckout(ObjectId $targetId): void | |
| { | |
| if ($this->isBare) { | |
| return; | |
| } | |
| $headId = $this->refs->resolveHead(); | |
| $currentTree = $headId !== null ? $this->flattenTreeEntries($this->getCommitTree($headId)) : []; | |
| $targetTree = $this->flattenTreeEntries($this->getCommitTree($targetId)); | |
| $currentEntries = $this->index()->entries(); | |
| $pathsChanging = []; | |
| foreach (array_keys(array_merge($currentTree, $targetTree)) as $path) { | |
| if (($currentTree[$path] ?? null) !== ($targetTree[$path] ?? null)) { | |
| $pathsChanging[$path] = true; | |
| } | |
| } | |
| if ($pathsChanging === []) { | |
| return; | |
| } |
resetWorktree() then rebuilds the index from the target tree and rewrites every tracked path (Lines 2725 to 2790 in 5beb97d
| private function resetWorktree(ObjectId $commitId, array $pathsToPrune = [], array $preserveRefs = []): void | |
| { | |
| $commit = $this->objects->read($commitId); | |
| if (!$commit instanceof Commit) { | |
| return; | |
| } | |
| $this->clearOperationState($preserveRefs); | |
| $treeEntries = $this->flattenTreeEntries($commit->tree); | |
| $materializedEntries = $this->materializedTreeEntries($treeEntries, $this->workDir); | |
| $this->pruneMissingPaths( | |
| array_map( | |
| static fn (array $entry): string => $entry['hash'], | |
| $materializedEntries, | |
| ), | |
| $this->workDir, | |
| $pathsToPrune, | |
| ); | |
| $currentEntries = $this->index()->entries(); | |
| $index = new Index($this->objectHashBytes()); | |
| $sparse = new SparseCheckout($this->gitDir); | |
| $sparseEnabled = $sparse->isEnabled(); | |
| $scanTimeSec = $this->isBare ? null : time(); | |
| $nextEntries = []; | |
| foreach ($treeEntries as $path => $treeEntry) { | |
| $extendedFlags = $sparseEnabled && !$sparse->includes($path) | |
| ? IndexEntry::EXTENDED_SKIP_WORKTREE | |
| : 0; | |
| $currentEntry = $currentEntries[$path] ?? null; | |
| if ($this->canReuseResetEntry($currentEntry, $treeEntry, $extendedFlags, $scanTimeSec)) { | |
| $nextEntries[] = $this->copyIndexEntryWithExtendedFlags($currentEntry, $extendedFlags); | |
| continue; | |
| } | |
| $blob = $this->objects->read(ObjectId::fromHex($treeEntry['hash'])); | |
| if (!$blob instanceof Blob) { | |
| continue; | |
| } | |
| if (!isset($materializedEntries[$path])) { | |
| $nextEntries[] = IndexEntry::create( | |
| $path, | |
| $blob->id, | |
| $treeEntry['mode'], | |
| strlen($blob->content), | |
| 0, | |
| $extendedFlags, | |
| ); | |
| continue; | |
| } | |
| $fullPath = $this->workDir . '/' . $path; | |
| $dir = dirname($fullPath); | |
| if (!is_dir($dir)) { | |
| mkdir($dir, 0777, true); | |
| } | |
| file_put_contents($fullPath, $blob->content); | |
| $nextEntries[] = IndexEntry::fromStat($path, $blob->id, $fullPath, $extendedFlags); | |
| } |
checkout() at Line 687 in 5beb97d
| $this->resetWorktree($branchId, $trackedPaths); |
Line 703 in 5beb97d
| $this->resetWorktree($id, $trackedPaths); |
merge() at Line 2278 in 5beb97d
| $this->resetWorktree($theirsId, $trackedPaths); |
Line 2326 in 5beb97d
| $this->resetWorktree($commitId, $trackedPaths); |
cherryPick() at Line 1193 in 5beb97d
| $this->resetWorktree($commitId, $trackedPaths); |
Activity