Skip to content

Stash apply overwrites uncommitted changes in files the stash does not touch #11

Closed

Description

@dnnsjsk

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 (

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');
}
}
). The working tree file is never consulted, so a local edit is replaced by the HEAD content.

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 gone

Expected

Local changes in paths the stash does not modify are kept; paths that would be overwritten raise an error before anything is written.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions