Skip to content

Instantly share code, notes, and snippets.

@pongo
Created May 30, 2026 15:38
Show Gist options
  • Select an option

  • Save pongo/562d5b5e701599f882cc637f71685a22 to your computer and use it in GitHub Desktop.

Select an option

Save pongo/562d5b5e701599f882cc637f71685a22 to your computer and use it in GitHub Desktop.
import os
import stat
import github.Auth
from github import Github
import json
import subprocess
import time
from pathlib import Path
import shutil
TODAY = time.strftime("%Y%m%d")
def full_clone(gist, gist_dir):
subprocess.run(["git", "clone", gist.git_pull_url, gist_dir], check=True)
def shallow_clone(gist, gist_dir):
def _remove_ro(func, path, _):
os.chmod(path, stat.S_IWRITE)
func(path)
subprocess.run(["git", "clone", "--depth=1", gist.git_pull_url, gist_dir], check=True)
git_dir = gist_dir / ".git"
if git_dir.is_dir():
shutil.rmtree(git_dir, onerror=_remove_ro)
def main(token, save_git_history) -> None:
g = Github(auth=github.Auth.Token(token))
today_dir = Path("backup") / TODAY
today_dir.mkdir(parents=True, exist_ok=True)
gists = []
for gist in g.get_user().get_gists():
gists.append({
"id": gist.id,
"description": gist.description,
"public": gist.public,
"clone": gist.git_pull_url,
"updated": gist.updated_at.isoformat(),
"url": gist.url,
})
gist_dir = today_dir / gist.id
if save_git_history:
full_clone(gist, gist_dir)
else:
shallow_clone(gist, gist_dir)
with open(today_dir / "index.json", "w", encoding="utf-8") as f:
f.write(json.dumps(gists, indent=2, ensure_ascii=False) + "\n")
if __name__ == '__main__':
# pip install PyGithub
# create token with scope gist https://github.com/settings/tokens/new?scopes=gist
main("ghp_", save_git_history=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment