Skip to content

checkout discards uncommitted changes in tracked files the switch does not touch #2

Closed

Description

@dnnsjsk

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 (

pitmaster/src/Repository.php

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;
}
), which is correct for the safety check, but resetWorktree() then rebuilds the index from the target tree and rewrites every tracked path (

pitmaster/src/Repository.php

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);
}
), called from checkout() at
$this->resetWorktree($branchId, $trackedPaths);
and
$this->resetWorktree($id, $trackedPaths);
, from merge() at
$this->resetWorktree($theirsId, $trackedPaths);
and
$this->resetWorktree($commitId, $trackedPaths);
, and from cherryPick() at
$this->resetWorktree($commitId, $trackedPaths);
(also revert, rebase, octopus merge and bisect).

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