threads / bug / 44328

[BUG] git-stash fails when tracked file is replaced with directory

Subject: [BUG] git-stash fails when tracked file is replaced with directory

## tl;dr

One message between Oct 19, 2016 and Oct 19, 2016.

replies: 0people: 1as markdown or json

Alex C. Reed, IV· Oct 19, 2016, 15:34 UTC · lore

When a file is deleted from Git and replaced with a directory+file(s), git-stash has two unexpected behaviors. This is tested against versions 2.8.1 and 2.10.1:

1) Old file is removed from index and newly-added directory+file(s)
are added to index.
In this scenario, 'git stash' fails with the message:
    error: <directory>: is a directory - add individual files instead
    fatal: Unable to process path <directory>
    Cannot save the current work tree state

The expected result would be that Git properly records the state currently stored in index.

2) Old file is removed and new-added directory+file(s) are present
only in worktree, but not added to the index.
In this scenario, 'git stash -u' works fine, but 'git stash apply'
fails with the message:
    fatal: cannot create directory at '<directory>': File exists
    Could not restore untracked files from stash

The expected result would be that Git removes 'file' and replaces with the untracked contents recorded in the original stash.

It is worth noting that Git does properly handle the scenario where the stash operation of (1) is replaced with a commit to a temporary branch, so this quirk seems to be restricted to stashes only.

I found a similar issue reported 22 April 2016 titled "possible bug of git stash deleting uncommitted files in corner case". The thread-view of the message is here:

    http://marc.info/?t=146132568600002&r=1&w=2

Here's a quick-and-dirty bash script to re-create all three scenarios (1, 2, and 'commit to branch'):

===== BEGIN stash-test.sh #!/bin/bash

set -x export GIT_TRACE=1

setup() {
  repo=$1
  # Prepare repo
  rm -rf $repo
  git init $repo
  cd $repo
  # Add initial file (symlink to external assets)
  ln -fs /external/dir dir
  git add dir
  git commit -m "Add symlink to /external/dir"
  # Replace symlink with local copy of assets
  rm dir
  mkdir -p dir
  touch dir/local_copy_of_dir_files
}
{
  ( setup stash-test-unstaged
    git stash -u
    git stash apply )
  ( setup stash-test-staged
    git add .
    git stash )
  ( setup stash-test-commit
    git add .
    git co -b stash-branch
    git commit -m "commit to branch instead of stash" )

} 2>&1 | tee stash-test.log ===== END stash-test.sh

Thanks, -Alex Reed

← back to recent threads