Skip to content

Instantly share code, notes, and snippets.

@flyte
Created April 17, 2014 19:51
Show Gist options
  • Save flyte/11007668 to your computer and use it in GitHub Desktop.
Save flyte/11007668 to your computer and use it in GitHub Desktop.
Check all git repositories in a directory to see if they're clean or dirty.. Tested on: GitPython==0.1.7 argparse==1.2.1 prettytable==0.7.2
import argparse
import os
import git
from prettytable import PrettyTable
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument("base_dir")
args = p.parse_args()
repos = []
dirs = os.listdir(args.base_dir)
for path in [os.path.join(args.base_dir, d) for d in dirs]:
print "Loading repo %s" % path
try:
repos.append(git.Repo(path))
except git.InvalidGitRepositoryError:
pass
x = PrettyTable(("Path", "Clean"))
x.align["Path"] = "l"
clean = []
dirty = []
error = []
for repo in repos:
print "Checking repo %s" % repo.path
try:
if repo.is_dirty:
dirty.append(repo)
else:
clean.append(repo)
except git.errors.GitCommandError:
error.append(repo)
for repo in sorted(error, key=lambda x: x.path):
x.add_row((repo.path, "Error!"))
for repo in sorted(dirty, key=lambda x: x.path):
x.add_row((repo.path, "Dirty"))
for repo in sorted(clean, key=lambda x: x.path):
x.add_row((repo.path, "Clean"))
print x
print "%d errored, %d dirty, %d clean, %d total" % (
len(error),
len(dirty),
len(clean),
len(repos)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment