Skip to content

Instantly share code, notes, and snippets.

@alb-i986
Last active August 29, 2015 14:07
Show Gist options
  • Save alb-i986/a4002f1ac50ce355278e to your computer and use it in GitHub Desktop.
Save alb-i986/a4002f1ac50ce355278e to your computer and use it in GitHub Desktop.
Script showing a bug in git: `stash save --untracked` makes an empty stash; it *looks* empty but it's not
#!/bin/bash
# Test bug in git
# if you stash untracked files only
# then the stash looks empty by `stash show -p`
# but actually `stash apply` restores the empty files.
#
REPO_DIR=/tmp/test-git-stash-bug-$( date +%F_%H%M%S )
gs() {
git status $@
}
pause_steps() {
echo
read -p "Type any key to continue to the next step"
echo -e "\n\n"
clear
}
step_header() {
echo -e "\t\t\t"$@
echo
}
define_first_step() {
clear
step_header $@
}
define_step() {
pause_steps
step_header $@
}
define_first_step "create new fresh git repo in $REPO_DIR"
git init $REPO_DIR
cd $REPO_DIR
define_step "create a couple of files/dirs and commit"
cat > file.txt <<-EOF
osijfd oijgioasg
dsgeigj et
sdgtrh
EOF
mkdir src
cat > src/MyClass.java <<-EOF
public class MyClass {
}
EOF
git add file.txt src/
gs
git commit -m "initial"
define_step "edit src/MyClass.java and commit"
cat >> src/MyClass.java <<-EOF
private class HiddenClass {
public void foo(){}
}
EOF
git add -u
gs
git commit -m "update MyClass"
define_step "create dir src/new-dir with one file inside"
mkdir src/new-dir
cat > src/new-dir/newfile-in-newdir.js <<-EOF
## aoijasiofjiofd
#sdjfoisdjf
EOF
define_step "edit file.txt and stage it"
cat >> file.txt <<-EOF
[EDIT]
dofjgih riuhg
oksjg eripoisdjg ';df;lpd
'
EOF
git add file.txt
gs
define_step "stash"
git stash
gs
define_step "stash untracked"
git stash save -u "buggy-stash"
gs
define_step "stash list"
git stash list
define_step "stash show untracked, i.e. \`git stash show stash@{0} -p\`"
STASH_SHOW_OUT=$( git stash show stash@{0} -p )
if [[ -z "$STASH_SHOW_OUT" ]] ; then
echo -e "\n\n\t\t>>>>>>>>>>>>>> BUG <<<<<<<<<<<<"
echo -e "\t\tthe stash looks empty\n\n"
fi
define_step "apply stash with untracked files"
STASH_APPLY=$( git stash apply | grep 'ntracked files' | wc -l )
if [[ "$STASH_APPLY" -eq 0 ]] ; then
echo -e "\n\n\t\t>>>>>>>>>>>>>> BUG <<<<<<<<<<<<"
echo -e "\t\tuntracked files are lost!\n\n"
else
echo -e "\n\n\t\t>>>>>>>>>>>>>> PASSED <<<<<<<<<<<<"
echo -e "\t\tuntracked files are NOT lost!\n\n"
fi
echo
echo "For more inspection: $REPO_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment