#!/usr/bin/env bash

# On Android, I have this issue... When too many repos are being
# cloned, it fails. Then I have to go over them one by one and either
# remove them or checkout the main branch after fetching. So this is a
# possible workaround. In the future, elpaca-queue-limit should take
# care of this on new devices I will use.

checkout()
{
  if git show-ref --verify --quiet refs/remotes/origin/main; then
    git checkout -b main origin/main
  elif git show-ref --verify --quiet refs/remotes/origin/master; then
    git checkout -b master origin/master
  else
    echo "ERROR: No main or master branch found in $repo" >&2
  fi
}

for repo in ~/.emacs.d/stateful/elpaca/repos/*; do
    count=$(ls "$repo" | wc -l)

    if [[ $count -eq 0 ]]; then
      echo "Found, $repo"

      (cd "$repo" && git fetch --all && checkout)
    fi
done
