Skip to content

Instantly share code, notes, and snippets.

@kitswas
Created January 27, 2025 18:36
Show Gist options
  • Select an option

  • Save kitswas/c2baf5b57b688f1e9af6a4b0ec95c0ee to your computer and use it in GitHub Desktop.

Select an option

Save kitswas/c2baf5b57b688f1e9af6a4b0ec95c0ee to your computer and use it in GitHub Desktop.
Git maintenance
import os
import subprocess
import sys
def run_git_command(repo_path, command):
"""Run a git command in the given repository path."""
try:
subprocess.check_call(command, cwd=repo_path)
except subprocess.CalledProcessError as e:
print(f"Error while running '{' '.join(command)}' in {repo_path}: {e}")
return False
return True
def clean_repositories(repo_dir):
"""Iterate over all repositories and run git fsck and git gc."""
if not os.path.isdir(repo_dir):
print(f"Error: The directory {repo_dir} does not exist.")
return
# Iterate over each subdirectory in the given repo directory
for repo_name in os.listdir(repo_dir):
repo_path = os.path.join(repo_dir, repo_name)
if os.path.isdir(repo_path) and os.path.isdir(os.path.join(repo_path, ".git")):
print(f"\nProcessing repository: {repo_path}")
# Run git fsck --full
print("Running git fsck --full...")
if not run_git_command(repo_path, ["git", "fsck", "--full"]):
continue # Skip to the next repo if fsck fails
# Run git gc --aggressive --prune=now
print("Running git gc --aggressive --prune=now...")
if not run_git_command(
repo_path, ["git", "gc", "--aggressive", "--prune=now"]
):
continue # Skip to the next repo if gc fails
# Run git commit-graph write --reachable --changed-paths
print("Running git commit-graph write --reachable --changed-paths...")
if not run_git_command(
repo_path,
["git", "commit-graph", "write", "--reachable", "--changed-paths"],
):
continue # Skip to the next repo if commit-graph fails
print("Repository cleaned successfully.")
else:
print(f"Skipping non-Git directory: {repo_path}")
def main():
"""Main function."""
if len(sys.argv) != 2:
print("Usage: python clean_repos.py <path-to-repositories-folder>")
sys.exit(1)
repo_dir = sys.argv[1]
clean_repositories(repo_dir)
if __name__ == "__main__":
main()
@kitswas
Copy link
Author

kitswas commented Jan 27, 2025

Git Maintenance

Runs the following git commands for housekeeping over a folder containing git repositories.

Verify filesystem integrity.

git fsck --full

Garbage collect - compress and optimize the repository, remove unreachable objects.

git gc --aggressive --prune=now

Write a commit graph file, which speeds up operations like git log and git blame by improving commit traversal performance.

git commit-graph write --reachable --changed-paths

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment