Created
February 12, 2024 17:34
-
-
Save marvinborner/765fe69cf633ee3ccddb1454e6a3cb07 to your computer and use it in GitHub Desktop.
hacky cgit github sync script
This file contains 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
#!/bin/bash | |
gh repo list "$1" --limit 1000 --json "nameWithOwner,isPrivate,description,pushedAt" | jq -r ".[]|[.nameWithOwner,.isPrivate,.description,.pushedAt] | @tsv" | | |
while IFS=$'\t' read -r name private description pushedAt; do | |
if [ -f "$name"/.git/age ]; then | |
currentAge="$(date -d "$(cat "$name"/.git/age)" +%s)" | |
newAge="$(date -d "$pushedAt" +%s)" | |
if [ "$currentAge" -ge "$newAge" ]; then | |
continue | |
fi | |
fi | |
echo "$name has new push" | |
if [ -d "$name" ]; then | |
cd "$name" | |
if git pull -r; then | |
echo "$name pulled" | |
cd - | |
else | |
echo "$name pull failed, cloning fresh" | |
cd - | |
rm -rf "$name" | |
gh repo clone "$name" "$name" | |
fi | |
else | |
gh repo clone "$name" "$name" | |
echo "$name cloned" | |
fi | |
# cgit metadata | |
echo "$name" >"$name/.git/name" | |
if [ "$private" = "true" ]; then | |
echo "<private>" >"$name/.git/description" | |
else | |
echo "$description" >"$name/.git/description" | |
fi | |
echo "$private" >"$name/.git/private" | |
echo "$pushedAt" >"$name/.git/age" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment