Skip to content

Instantly share code, notes, and snippets.

@thecist
Created June 29, 2025 03:09
Show Gist options
  • Select an option

  • Save thecist/ba0f3f5dd0e6924b6b4bd588fc191812 to your computer and use it in GitHub Desktop.

Select an option

Save thecist/ba0f3f5dd0e6924b6b4bd588fc191812 to your computer and use it in GitHub Desktop.
Code for archiving thecist's repos
#!/usr/bin/env python3
import os
import sys
import subprocess
# --- Configuration ---
# You'll have to update the BASE_REPO_DIR to match yours
BASE_REPO_DIR = os.path.expanduser("~/thecist")
REMOTE_NAME = "origin"
BRANCH_PREFIX = "v"
# --- Input ---
if len(sys.argv) != 2:
print("Usage: python create_push_delete_branch.py <repo-name>")
sys.exit(1)
repo_name = sys.argv[1]
repo_path = os.path.join(BASE_REPO_DIR, repo_name)
if not os.path.isdir(repo_path):
print(f"Repo '{repo_name}' not found in {BASE_REPO_DIR}")
sys.exit(1)
def run(cmd, cwd=None, capture_output=False):
return subprocess.run(
cmd,
cwd=cwd,
shell=True,
check=True,
stdout=subprocess.PIPE if capture_output else None,
text=True,
)
# --- Find available branch name on remote ---
def find_available_branch():
result = run(f"git ls-remote --heads {REMOTE_NAME}", cwd=repo_path, capture_output=True)
remote_branches = result.stdout
i = 1
while True:
candidate = f"{BRANCH_PREFIX}{i}"
full_ref = f"refs/heads/{candidate}"
if full_ref not in remote_branches:
return candidate
i += 1
try:
# Get a branch name that doesn't exist on origin
branch_name = find_available_branch()
print(f"Using new branch name: {branch_name}")
# Make sure local main is not ahead of origin/main
result = run("git rev-list origin/main..main", cwd=repo_path, capture_output=True)
if result.stdout.strip():
print("Local main is ahead of origin/main. Please push or reset --soft to make sure your archive is clean.")
sys.exit(1)
# Create the branch locally
print("Creating artifacts for archive...")
run(f"git checkout -b {branch_name} main", cwd=repo_path)
# Push to remote
print("Archiving...")
run(f"git push {REMOTE_NAME} {branch_name}", cwd=repo_path)
# Switch back and delete local branch
print("Deleting artifacts...")
run("git checkout main", cwd=repo_path)
run(f"git branch -D {branch_name}", cwd=repo_path)
print(f"Repo '{repo_name}' has been archived successfully.")
except subprocess.CalledProcessError:
print("An error occurred during git operations.")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment