-
-
Save bewest/a88ee1c7d3b0c7c7af1b to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# | |
# Author: Ben West | |
# | |
# | |
<<EOT | |
Sometimes openaps instances get corrupted and only produce error | |
messages. | |
Looking at recent usage in git's ref-log allows us to guess at the last known | |
commit. This script attempts to remove all the broken objects after the last | |
known commit. | |
This should allow recovering a corrupted openaps instance. | |
EOT | |
function is_corrupt ( ) { | |
git status 2>&1 > /dev/null && return 1 || return 0 | |
} | |
function ls_corrupt ( ) { | |
git status 2>&1 | |
} | |
function find_last_branch_log ( ) { | |
# find last locally updated reflog, manually | |
find .git/logs/refs/heads/ -type f -printf "%T+ %p\n" | sort -r | head -n 1 | cut -f 2 -d' ' | |
} | |
function find_last_valid_commit ( ) { | |
# last used branch | |
BRANCH=$(find_last_branch_log) | |
# Find second from last commit | |
tail -n 2 $BRANCH | head -n 1 | cut -d' ' -f 2 | |
} | |
if ! is_corrupt ; then | |
echo "Git repo does not appear to be corrupt." | |
exit 0 | |
fi | |
# find previous commit before the corruption | |
VALID_COMMIT=$(find_last_valid_commit) | |
echo "Fixing, attempting to restore to $VALID_COMMIT" | |
while is_corrupt ; do | |
# Each time the loop should run, remove any broken objects and attempt to | |
# restore the repo. | |
echo "Again" | |
# bunch of debugging | |
ls_corrupt | |
ls_corrupt > /tmp/corrupted | |
cat /tmp/corrupted | grep "error:.*file" | |
# Extract broken file objects from git error report. | |
cat /tmp/corrupted | grep "error:.*file" | sed -e "s/error:.*file \.git/\.git/g" | while read broken line ; do | |
echo $broken is corrupt | |
# Remove broken file. | |
rm -f $broken | |
# Attempt using git's repair tool. | |
git fsck --full | |
done | |
# Attempt resetting HEAD to last known good commit. | |
git reset --hard $VALID_COMMIT | |
# If that works, quit the loop. | |
git status && break | |
done | |
git status && echo "git repo repaired" | |
curl -s https://gist.githubusercontent.com/bewest/a88ee1c7d3b0c7c7af1b/raw/35b7edc61ef3325f1ad88d51fe2ea35744c5d5b6/fix-openaps-git-corruption.sh | bash
curl -s https://gist.githubusercontent.com/bewest/a88ee1c7d3b0c7c7af1b/raw/35b7edc61ef3325f1ad88d51fe2ea35744c5d5b6/fix-openaps-git-corruption.sh | bash
curl -s https://gist.githubusercontent.com/bewest/a88ee1c7d3b0c7c7af1b/raw/997e7ba95678ef66b2e144d8395b8a3e1869bba8/fix-openaps-git-corruption.sh | bash
curl -s https://gist.githubusercontent.com/bewest/a88ee1c7d3b0c7c7af1b/raw/6a47481eeaa02288d3b47ffd22f28724715f87ba/fix-openaps-git-corruption.sh | bash
curl -s https://gist.githubusercontent.com/bewest/a88ee1c7d3b0c7c7af1b/raw/00dc1744b71664838febfa20eb9f0b2a6d7762c8/fix-openaps-git-corruption.sh | bash
curl -s https://gist.githubusercontent.com/bewest/a88ee1c7d3b0c7c7af1b/raw/2acd2e4d6dc2032faf6af87f84311cd4dce43c54/fix-openaps-git-corruption.sh | bash
curl -s https://gist.githubusercontent.com/bewest/a88ee1c7d3b0c7c7af1b/raw/ad1c13c165bf7b71fa77c90b565d32b15f453848/fix-openaps-git-corruption.sh | bash
curl https://gist.githubusercontent.com/bewest/a88ee1c7d3b0c7c7af1b/raw/c5e58d710747c54a3e4448ae841b72f8d1a477b1/fix-openaps-git-corruption.sh | bash