Created
October 7, 2026 10:47
-
-
Save lzap/709a1f5b087ccbcc365920a241987572 to your computer and use it in GitHub Desktop.
Sync GitHub repositories under $HOME or selected directories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # git-sync: sync GitHub repositories with their parent repositories. | |
| # | |
| # With no arguments, scan repositories directly under $HOME and one directory | |
| # below that. With directory arguments, process only those directories. | |
| # | |
| set -uo pipefail | |
| github_slug() { | |
| local url="$1" authority path rest | |
| case "$url" in | |
| *://*) | |
| rest="${url#*://}" | |
| [[ "$rest" == */* ]] || return 1 | |
| authority="${rest%%/*}" | |
| path="${rest#*/}" | |
| authority="${authority##*@}" | |
| authority="${authority%%:*}" | |
| ;; | |
| *@*:*|github.com:*) | |
| authority="${url%%:*}" | |
| authority="${authority##*@}" | |
| path="${url#*:}" | |
| ;; | |
| *) | |
| return 1 | |
| ;; | |
| esac | |
| [[ "${authority,,}" == github.com ]] || return 1 | |
| path="${path%%\?*}" | |
| path="${path%%#*}" | |
| path="${path%/}" | |
| path="${path%.git}" | |
| [[ -n "$path" && "$path" == */* && "$path" != */*/* && \ | |
| -n "${path%%/*}" && -n "${path#*/}" ]] || return 1 | |
| printf '%s\n' "$path" | |
| } | |
| process_repo() { | |
| local path="$1" repo origin_urls origin origin_slug="" | |
| if [[ ! -d "$path" ]]; then | |
| printf 'git-sync: not a directory: %s\n' "$path" >&2 | |
| return 1 | |
| fi | |
| if ! repo="$(git -C "$path" rev-parse --show-toplevel 2>/dev/null)"; then | |
| printf 'git-sync: not a Git repository: %s\n' "$path" >&2 | |
| return 1 | |
| fi | |
| if ! origin_urls="$(git -C "$repo" remote get-url --all origin 2>/dev/null)"; then | |
| return 0 | |
| fi | |
| while IFS= read -r origin; do | |
| if origin_slug="$(github_slug "$origin")"; then | |
| break | |
| fi | |
| done <<< "$origin_urls" | |
| if [[ -z "$origin_slug" ]]; then | |
| return 0 | |
| fi | |
| if ! command -v gh >/dev/null 2>&1; then | |
| printf 'git-sync: gh is not installed; cannot sync %s\n' "$repo" >&2 | |
| return 1 | |
| fi | |
| if (cd "$repo" && gh repo sync "$origin_slug"); then | |
| return 0 | |
| fi | |
| printf 'git-sync: sync failed for %s (%s)\n' "$repo" "$origin_slug" >&2 | |
| return 1 | |
| } | |
| main() { | |
| local failed=0 repo_dir git_dir | |
| local -a git_dirs=() | |
| if (( $# > 0 )); then | |
| for repo_dir in "$@"; do | |
| process_repo "$repo_dir" || failed=1 | |
| done | |
| else | |
| mapfile -d '' -t git_dirs < <( | |
| find "$HOME" -mindepth 2 -maxdepth 3 -name .git \ | |
| \( -type d -o -type f \) -print0 -prune | |
| ) | |
| for git_dir in "${git_dirs[@]}"; do | |
| repo_dir="${git_dir%/.git}" | |
| process_repo "$repo_dir" || failed=1 | |
| done | |
| fi | |
| return "$failed" | |
| } | |
| main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment