#!/usr/bin/env sh export GIT_TERMINAL_PROMPT=0 BASE=/mnt/ssd-data/repos/ pick_branch() { git -C "$1" for-each-ref --format='%(refname:short)' 'refs/remotes/origin/*' \ | sed 's#^origin/##' \ | grep -E '^(main|master|dev)$' \ | head -n1 } repos=$(2>1 ssh git.l-3.space | awk '/tslil\// {print $2}') echo Found $repos printf "\n\n" for repo in $repos; do REMOTE="ssh://git.l-3.space/$repo.git" DIR="$BASE/$repo" echo Looking at "$REMOTE" for "$DIR" if [ -d "$DIR/.git" ]; then echo "Syncing existing repo $DIR from $REMOTE ..." if git -C "$DIR" fetch -q origin \ && BRANCH="$(pick_branch "$DIR")" && [ -n "$BRANCH" ] \ && git -C "$DIR" reset --hard -q "origin/$BRANCH" \ && git -C "$DIR" clean -fdxq; then echo "OK: $DIR synced to remote from $BRANCH." else echo "Sync failed, removing $DIR and re-cloning..." rm -rf "$DIR" git clone -q "$REMOTE" "$DIR" echo "OK: $DIR re-cloned from $REMOTE." fi else echo "No repo at $DIR, cloning..." git clone -q "$REMOTE" "$DIR" echo "OK: $DIR cloned from $REMOTE." fi printf "\n\n" done