Repository navigation
Closed
Description
Stash::apply() (and pop()) overwrite uncommitted changes in tracked files that the stash itself does not change. git refuses to apply a stash over local changes it would touch and keeps the others.
applyStashCommit() merges, per path, the stash base, the current HEAD and the stash tree, and writes the result for every path (
Lines 485 to 583 in 5beb97d
| private function applyStashCommit(Commit $stash): void | |
| { | |
| $headId = $this->refs->resolveHead(); | |
| $headCommit = $headId !== null ? $this->objects->read($headId) : null; | |
| $baseCommit = isset($stash->parents[0]) ? $this->objects->read($stash->parents[0]) : null; | |
| if (!$headCommit instanceof Commit || !$baseCommit instanceof Commit) { | |
| throw new \RuntimeException('Cannot apply stash without valid HEAD and base commits'); | |
| } | |
| $baseEntries = $this->flattenTree($baseCommit->tree); | |
| $currentEntries = $this->flattenTree($headCommit->tree); | |
| $stashEntries = $this->flattenTree($stash->tree); | |
| $allPaths = array_unique(array_merge( | |
| array_keys($baseEntries), | |
| array_keys($currentEntries), | |
| array_keys($stashEntries), | |
| )); | |
| sort($allPaths); | |
| $written = []; | |
| $deleted = []; | |
| $conflicts = []; | |
| foreach ($allPaths as $path) { | |
| $baseHash = $baseEntries[$path] ?? null; | |
| $currentHash = $currentEntries[$path] ?? null; | |
| $stashHash = $stashEntries[$path] ?? null; | |
| if ($currentHash === $stashHash) { | |
| if ($currentHash !== null) { | |
| $written[$path] = $this->readBlobContent(ObjectId::fromHex($currentHash)); | |
| } | |
| continue; | |
| } | |
| if ($baseHash === $currentHash) { | |
| if ($stashHash === null) { | |
| $deleted[] = $path; | |
| } else { | |
| $written[$path] = $this->readBlobContent(ObjectId::fromHex($stashHash)); | |
| } | |
| continue; | |
| } | |
| if ($baseHash === $stashHash) { | |
| if ($currentHash === null) { | |
| $deleted[] = $path; | |
| } else { | |
| $written[$path] = $this->readBlobContent(ObjectId::fromHex($currentHash)); | |
| } | |
| continue; | |
| } | |
| $baseContent = $baseHash !== null ? $this->readBlobContent(ObjectId::fromHex($baseHash)) : ''; | |
| $currentContent = $currentHash !== null ? $this->readBlobContent(ObjectId::fromHex($currentHash)) : ''; | |
| $stashContent = $stashHash !== null ? $this->readBlobContent(ObjectId::fromHex($stashHash)) : ''; | |
| $merged = ThreeWayMerge::merge( | |
| $baseContent, | |
| $currentContent, | |
| $stashContent, | |
| 'Updated upstream', | |
| 'Stashed changes', | |
| ); | |
| $written[$path] = $merged['content']; | |
| if (!$merged['clean']) { | |
| $conflicts[] = $path; | |
| } | |
| } | |
| foreach ($deleted as $path) { | |
| $fullPath = $this->workDir . '/' . $path; | |
| if (is_file($fullPath) || is_link($fullPath)) { | |
| unlink($fullPath); | |
| $this->removeEmptyParentDirectories(dirname($fullPath)); | |
| } | |
| } | |
| foreach ($written as $path => $content) { | |
| $fullPath = $this->workDir . '/' . $path; | |
| $dir = dirname($fullPath); | |
| if (!is_dir($dir)) { | |
| mkdir($dir, 0777, true); | |
| } | |
| file_put_contents($fullPath, $content); | |
| } | |
| if ($conflicts !== []) { | |
| throw new MergeConflictException($conflicts, 'Stash apply stopped due to conflicts'); | |
| } | |
| } |
Repro
pitmaster init && git config user.name T && git config user.email t@x
echo a > a.txt && echo b > b.txt && pitmaster add a.txt b.txt && pitmaster commit -m first
echo a2 > a.txt && pitmaster stash push
echo LOCAL > b.txt # uncommitted edit in a file the stash does not touch
pitmaster stash apply
cat b.txt # "b", the local edit is goneExpected
Local changes in paths the stash does not modify are kept; paths that would be overwritten raise an error before anything is written.
Metadata
Metadata
Assignees
Labels
No labels
Activity