-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-forgotten.py
executable file
·120 lines (96 loc) · 2.79 KB
/
git-forgotten.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
"""
Find 'git' repos that you've forgotten:
* Uncommited code
* Commits that you haven't pushed out
* Files you've fogetten to add to the repo
TODO Try using `git status --porcelain` to capture more possible states
"""
import colorama
import contextlib
import logging
import os
import pathlib
import subprocess
import sys
logger = logging.getLogger(__name__)
@contextlib.contextmanager
def chdir(folder):
"""
Context manager to temporarily change the current working directory.
"""
curdir = os.getcwd()
os.chdir(folder)
yield
os.chdir(curdir)
def git_repo_folders(parent):
"""
Yield folders containing git repos.
This is any regular folder containing in turn the folder '.git'.
"""
logger.debug("Looking for Git repos under: %s", parent)
parent = str(parent)
examined = 0
found = 0
for root, dirs, files in os.walk(parent, topdown=True):
examined += 1
dirs.sort()
if '.git' in dirs:
found += 1
yield root
# Don't look any further inside repo
dirs.clear()
logger.info(f"{found:,} Git repos found. {examined:,} folders searched.")
class MultiGit:
def __init__(self, parent, args):
"""
Initialiser.
"""
self.args = args if args else ['diff', '--stat', '--color']
self.encoding = 'utf-8'
self.parent = self.clean_path(parent)
def clean_path(self, path):
"""
Return cannonical version of given path.
"""
path = pathlib.Path(path)
path = path.expanduser()
path = path.resolve()
return path
def run(self):
"""
Main script.
"""
for repo in git_repo_folders(self.parent):
with chdir(repo):
self.run_git(repo, self.args)
def run_git(self, repo, args):
"""
Run git 'inside' repo and print output, if not empty.
"""
args = ['git'] + args
process = subprocess.run(args, stdout=subprocess.PIPE)
if process.stdout:
heading = f"{repo:<80}"
print(colorama.Style.BRIGHT + colorama.Back.BLUE + heading)
stdout = process.stdout.decode(self.encoding).strip()
print(stdout)
print()
def setup_logging(level):
logging.basicConfig(
datefmt="%d-%b-%Y %H:%M:%S",
format='{message}',
level=level,
style='{'
)
if __name__ == '__main__':
colorama.init(autoreset=True)
setup_logging(logging.INFO)
if len(sys.argv) > 1:
parent = sys.argv[1]
else:
logger.debug('No parent folder given, defaulting to home folder.')
parent = '~'
args = sys.argv[2:]
multi_git = MultiGit(parent, args)
sys.exit(multi_git.run())