Skip to content

Instantly share code, notes, and snippets.

@kirikaza
Created August 7, 2013 21:08
Show Gist options
  • Save kirikaza/6178695 to your computer and use it in GitHub Desktop.
Save kirikaza/6178695 to your computer and use it in GitHub Desktop.
Fixes unaccessable packfiles in the git repo, i.e. errors like "packfile .git/objects/pack/pack-<hash>.pack cannot be accessed". If there are no such packfiles, the script just does "git gc". It is safe enough. See http://stackoverflow.com/a/14571150/421146
#!/bin/bash
packlist=`mktemp`
git gc 2>&1 |
grep '^warning: packfile .* cannot be accessed$' |
cut -d' ' -f3 |
sort -u > $packlist
packs_count=`wc -l < $packlist`
temp=`mktemp`
backup=`mktemp -d`
rm $temp
unpacked=0
backed=0
while read pack ; do
mv $pack $temp || break
if ! git unpack-objects < $temp ; then
echo >&2 "error: unpacking has failed, restoring packfile \`$pack'..."
if ! mv $temp $pack ; then
echo >&2 "error: cannot restore the packfile from \`$temp'"
fi
break
fi
let ++unpacked
mkdir -p $backup/`dirname $pack`
if ! mv $temp $backup/$pack ; then
echo >&2 "error: unpacking finished well, but cannot backup \`$temp' to \`$backup/$pack/'"
break
fi
let ++backed
done < <(head $packlist)
echo >&2 "$unpacked of $packs_count are unpacked, $backed of them are backed up to \`$backup/.git'"
rm $packlist || exit 1
if [ $backed -eq $packs_count ] ; then
echo >&2 'good work!'
else
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment