#!/bin/sh
# $Id: gimme-git,v 1.9 2023/09/09 10:20:35 tom Exp $
# For the given directories, fetch current git

[ $# = 0 ] && exec "$0" .

unset DISPLAY
TOP=$(pwd)
for dir in "$@"
do
	cd "$TOP" || exit
	[ -d "$dir/.git" ] || continue
	cd "$dir" || continue
	rm -f git.log
	git fetch || continue
	git pull || continue

	TMP=$(mktemp -d)
	trap 'rm -rf $TMP' EXIT INT QUIT HUP TERM

	grep -r -F -l '<<<<<<<<' ./* >"$TMP/oops"
	if [ -s "$TMP/oops" ]
	then
		echo "OOPS"
		sed -e "s%^\.%>> $dir%" "$TMP/oops"
	fi
	git clean -f -x -d || continue
	git remote prune origin || continue
	git log >"$TMP/git.log"
	git-utimes
	REF=
	for file in *
	do
		[ -d "$file" ] && touch-dirs "$file"
		[ -z "$REF" ] && REF="$file"
		[ "$file" -nt "$REF" ] && REF="$file"
	done
	touch -r "$REF" "$TMP/git.log"
	mv "$TMP/git.log" .
	touch -r git.log .

	rm -rf "$TMP"
	trap 'echo done $dir' EXIT INT QUIT HUP TERM
done
