Skip to content

Instantly share code, notes, and snippets.

@konstruktoid
Last active May 2, 2024 20:58
Show Gist options
  • Save konstruktoid/a9f2d65603faa6313d64e2eef3e7b62d to your computer and use it in GitHub Desktop.
Save konstruktoid/a9f2d65603faa6313d64e2eef3e7b62d to your computer and use it in GitHub Desktop.
Clone all repositories for a GitHub user and push to origin from upstream.
#!/bin/bash
set -eu -o pipefail
USER=""
PAGE=1
REPOTMP="$(mktemp)"
PUSH=0
curl -s "https://api.github.com/users/${USER}/repos?page=${PAGE}&per_page=100" |\
grep 'ssh_url' | cut -d \" -f 4 > "${REPOTMP}"
grep "\.git" "${REPOTMP}" | while read -r repo; do
for name in $(echo "${repo}" | sed -e 's/.*\///g' -e 's/\.git//g'); do
if [ -d "${name}" ]; then
cd "${name}" || exit
if git remote -v | grep -qi 'upstream'; then
echo "${name} exists with upstream."
if git branch -a | grep -qi upstream/main; then
echo "${name} got upstream/main."
git checkout main
git fetch -v upstream
git rebase -v upstream/main
PUSH=1
fi
if git branch -a | grep -qi upstream/master; then
echo "${name} got upstream/master."
git checkout master
git fetch -v upstream
git rebase -v upstream/master
PUSH=1
fi
if git branch -a | grep -qi upstream/devel; then
if ! echo "${name}" | grep -qiE 'ansible-lint|molecule'; then
echo "${name} got upstream/devel."
git checkout devel
git fetch -v upstream
git rebase -v upstream/devel
PUSH=1
fi
fi
if git branch -a | grep -qi "upstream/dev$"; then
if ! echo "${name}" | grep -qiE 'ansible-lint|molecule'; then
echo "${name} got upstream/dev."
git checkout dev
git fetch -v upstream
git rebase -v upstream/dev
PUSH=1
fi
fi
if git branch -a | grep -qi upstream/public; then
echo "${name} got upstream/public."
git checkout public
git fetch -v upstream
git rebase -v upstream/public
PUSH=1
fi
if [ $PUSH = 1 ]; then
git gc --auto
git push -v
fi
if git branch --merged | grep -q -Ev 'main|master|dev|public'; then
git branch --merged | grep -Ev 'main|master|dev|public' | xargs git branch -d
fi
git remote prune origin
cd .. || exit
else
echo "${name} dont have upstream. Pulling."
git pull
git gc --auto
if git branch --merged | grep -q -Ev 'main|master|dev|public'; then
git branch --merged | grep -Ev 'main|master|dev|public' | xargs git branch -d
fi
git remote prune origin
cd .. || exit
fi
else
if [ ! -d "${name}" ]; then
echo "${name} doesn't exist. Cloning"
git clone "${repo}"
else
echo "${name} exists, but is not a directory."
fi
fi
done
done
rm "$REPOTMP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment