Skip to content

Instantly share code, notes, and snippets.

@marvinborner
Created February 12, 2024 17:34
Show Gist options
  • Save marvinborner/765fe69cf633ee3ccddb1454e6a3cb07 to your computer and use it in GitHub Desktop.
Save marvinborner/765fe69cf633ee3ccddb1454e6a3cb07 to your computer and use it in GitHub Desktop.
hacky cgit github sync script
#!/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