Last active
July 31, 2021 04:56
-
-
Save bjoseru/11edada1cc8501e496a44a380488719a to your computer and use it in GitHub Desktop.
Print a list of all git repositories found below your home directory along with their remotes
This file contains 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
#!/usr/bin/env python3 | |
# coding: utf-8 | |
# written for Python 3.9.4 | |
# Copyright (C) 2021 by Björn Rüffer | |
# | |
"Print a list of all git repositories found below your home directory along with their remotes." | |
import glob | |
import itertools | |
import pathlib | |
import re | |
import git | |
import tqdm | |
from rich import print | |
def is_repo(dir_name: pathlib.Path): | |
"Is DIR a git repository? (Judge by existence of .git subdirectory.)" | |
try: | |
return (dir_name / ".git").is_dir() | |
except PermissionError: | |
print( | |
f"[red]Cannot access [bold]{dir_name}[/bold]. Permission denied.[/red]") | |
return False | |
def traverse_dir(dir_name: pathlib.Path): | |
"Scan DIR for git repositories and traverse recursively." | |
directory_entries = [ | |
path_entry | |
for entry in itertools.chain( | |
glob.iglob(str(dir_name) + "/.*"), # check hidden directories | |
glob.iglob(str(dir_name) + "/*"), | |
) | |
if (path_entry := pathlib.Path(entry)).is_dir() | |
# there are some places where we don't want to look: | |
if not re.search( | |
"Library/(Application Support|ApplicationSupport|Caches|" + | |
"Containers|Developer|Group Containers|Logs|Python)", | |
str(path_entry), | |
) | |
and path_entry.name not in [".cache", ".bundle", ".pyenv", ".sage"] | |
] | |
next_list = [] | |
for subdir in tqdm.tqdm(directory_entries, leave=False, | |
desc=str(dir_name.name)): | |
if is_repo(subdir): | |
print(f"repo [bold blue]{subdir}[/bold blue]") | |
repo = git.Repo(subdir) | |
for remote in repo.remotes: | |
for url in remote.urls: | |
print(f" {remote} -> [bold magenta]{url}[/bold magenta]") | |
else: | |
next_list.append(subdir) | |
for subdir in next_list: | |
traverse_dir(subdir) | |
if __name__ == "__main__": | |
print("[bold]Here's a list of all your git repositories and their remotes:[/bold]") | |
traverse_dir(pathlib.Path.home()) |
This file contains 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
git-python==1.0.3 | |
rich==10.6.0 | |
tqdm==4.62.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment