#!/bin/sh -efu

[ -n "$*" ] || exit 0

enable -f /usr/lib/bash/lockf lockf
builtin lockf -v "$GIT_DIR"

echo -n 'Repacking repository... '
git prune
git repack -a -d
if [ -d "$GIT_DIR/refs/heads" ]; then
	f="$GIT_DIR/recent_committer"
	git for-each-ref "--format=%(committer)" --sort=-committerdate --count=1 refs/heads \
		>"$f"
	s="$(sed -n 's/.* \([1-9][0-9]\{9,\}\) [+-][0-9]\+$/\1/p' "$f")"
	if [ -n "$s" ]; then
		TZ=UTC touch --date="1970-01-01 $s seconds" -- "$f"
	else
		rm -f -- "$f"
	fi
fi
echo done
