Created
January 27, 2025 18:36
-
-
Save kitswas/c2baf5b57b688f1e9af6a4b0ec95c0ee to your computer and use it in GitHub Desktop.
Git maintenance
This file contains hidden or 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
| 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() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Git Maintenance
Runs the following git commands for housekeeping over a folder containing git repositories.
Verify filesystem integrity.
Garbage collect - compress and optimize the repository, remove unreachable objects.
Write a commit graph file, which speeds up operations like
git logandgit blameby improving commit traversal performance.