Skip to content

Commit

Permalink
#2 Fix index deleted file recovering in fs
Browse files Browse the repository at this point in the history
  • Loading branch information
askirmas committed Nov 28, 2019
1 parent 2cfe1e7 commit 0a4c8b2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
14 changes: 5 additions & 9 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
#!/bin/bash

# TODO: Recursive for submodules?

MY_DIR=$(dirname "$(realpath "$0")")
HOOKS_DIR=$(cat $MY_DIR/hooks_dir)
HOOK="$HOOKS_DIR/pre-commit"

test -e $HOOK
if [ $? -ne 0 ]
then
echo "No pre-commit hook '$HOOK'- skipped";
echo -e "\033[31mNo pre-commit hook '$HOOK'- skipped\033[0m";
exit 0;
fi

echo -e "\033[1;33m<pre-commit> \033[0m$HOOK"
STASH_NAME="pre-commit.$(date +%F.%T)"
# TODO: Recursive for submodules?
git stash push -q --keep-index --include-untracked -m $STASH_NAME;
../scripts/stash_untracked.sh "$STASH_NAME"
STASH_TITLE="On $(git rev-parse --abbrev-ref HEAD): ${STASH_NAME}"

# git clean -f ? issue#2

_unstash() {
STASH_LAST=$(git stash list -1 --pretty=%B)

if [ "$STASH_LAST" = "$STASH_TITLE" ]
then
# TODO: Deleted files afterwards are somehow in index as untracked
git stash pop > /dev/null
else
echo -e "\033[31mLast stash is not mine!\033[0m"
Expand All @@ -36,10 +34,8 @@ trap _unstash KILL
trap _unstash INT

RETURN=$($HOOK)
RESULT="$?"
RESULT=$?

# During tests updated snapshots can be produced and track it in your pre-commit hook with something like
# git add -n? -f? --all
_unstash
if [ $RESULT != 0 ]
then
Expand Down
4 changes: 4 additions & 0 deletions scripts/stash_untracked.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
git stash push -q --keep-index --include-untracked -m "$1" && \
git clean -f
exit $?
50 changes: 37 additions & 13 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,57 @@ then
_failed "$test"
fi

test="delete file"
rm -rf file
_it "$test"
git commit -avm "$test" && (test -e file; [ $? == 1 ])
result=$?
if [ $result != 0 ]
then
_failed "$test: $result"
fi

_commit "recover file"

cp -rf "$MY_DIR/tests/exit1.sh" "$HOOKS/pre-commit" || exit 1;
git commit -anm "bad pre-commit"
test="pre-commit exit1"
_it "$test"
_commit "$test"
if [ "$?" = 0 ]
if [ $? == 0 ]
then
_failed "$test $?"
_failed "$test"
fi

#it stash push;
#it commit interuption;
test="stash pop"
_it "$test"
touch "tostash"
_commit "$test"
if [ "$?" = 0 ]
if [ "$?" == 0 ]
then
_failed "$test"
else
test="check appearance"
_it "$test"
test -e "tostash"
if [ "$?" != 0 ]
then
_failed "$test"
fi
fi

test="check appearance"
_it "$test"
test -e "tostash"
if [ "$?" != 0 ]
then
_failed "$test"
fi



test="delete file"
rm -rf file
_it "$test"
git add file && git commit -avm "$test"
test -e file
if [ $? != 1 ]
then
_failed "$test"
fi

#it commit interuption;

echo -e "\n\033[1;30;42m DONE \033[0m"

0 comments on commit 0a4c8b2

Please sign in to comment.