-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
137 lines (93 loc) · 4 KB
/
main.py
File metadata and controls
137 lines (93 loc) · 4 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import os
from typing import Generator
import git
from git_wrapper import GitRepo
from contributions_heatmap import ContributionsHeatmap
from terminal_image import Image
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.color import Color
console = Console()
# [https://stackoverflow.com/a/74677730]
def scan_tree_for_git_dirs(path: str) -> Generator[os.DirEntry]:
"""Recursively yield DirEntry objects for given directory."""
for entry in os.scandir(path):
try:
if entry.is_dir(follow_symlinks=False):
yield entry
if entry.name != ".git":
yield from scan_tree_for_git_dirs(entry.path)
except PermissionError:
continue
# [https://stackoverflow.com/a/6227623]
path = os.path.expanduser("~\\Documents")
folder = " "
while folder != "" and not os.path.isdir(folder):
folder = console.input(f"folder to scan ({path}): ")
folder = folder or path
username = console.input("your Git username (empty for all repos): ")
repos: list[GitRepo] = []
with console.status("finding repos ..."):
for entry in scan_tree_for_git_dirs(folder):
if entry.name == ".git":
try:
repo = GitRepo(path=entry.path, search_parent_directories=True)
if username != "":
# only repos i contributed to
if not any( username == author.name for author in repo.authors ):
continue
# skip exact duplicate repos
if repo.commits in [ repo.commits for repo in repos]:
console.log(f"skipping repeat repo at: {entry.path}")
continue
repos.append( repo )
console.log(f"found repo at: {entry.path}")
except git.InvalidGitRepositoryError:
console.log(f"skipping invalid repo at: {entry.path}")
console.log(f"found: {len(repos)} repos!")
console.log("Generating report ...")
common_prefix = os.path.commonprefix([ repo.working_dir for repo in repos ])
# get all MY contributions
commits = sum([ repo.commits for repo in repos ], [])
commits = list(set(commits)) # exclude repeats
commits = [ commit for commit in commits if (not username) or commit.author.name == username ]
heatmap = ContributionsHeatmap(all_commits=commits, console=console)
#region render git icon
# image from: https://git-scm.com/community/logos
git_image = Image(image="assets/Git-Icon.png", size=(16, 8), background_color=(12, 12, 12))
#endregion
contributions_table = Table(
box=None,
expand=False,
show_header=False,
show_edge=False,
pad_edge=False
)
contributions_table.add_row(git_image, Panel(heatmap, title="Contributions heatmap"))
console.print( contributions_table )
#region render stats table
table = Table(title="Statistics")
table.add_column("repo name", style="cyan", no_wrap=True)
table.add_column("path", style="bright_black", no_wrap=False)
table.add_column("remotes", style="cyan", no_wrap=False)
table.add_column("commits", style="green", no_wrap=True)
table.add_column("branches", style="orange3", no_wrap=True)
table.add_column("contributors", style="white", no_wrap=False)
repos.sort(key=lambda repo: len(repo.commits), reverse=True)
for repo in repos:
# sort the authors by commit count
authors = list(repo.authors)
authors.sort(key=lambda author: author.commits, reverse=True)
names = [ author.name for author in authors if author.name is not None ]
remotes = list(repo.remotes)
table.add_row(
repo.name,
os.path.dirname(repo.working_dir).replace(common_prefix, "…\\"),
"\n".join(f"[link={remote.url}]{remote.name}[/]" for remote in remotes[:4]) if any(remotes) else "None",
str(len(repo.commits)),
str(len(repo.branches)),
", ".join(names[:4]) + ( ", …" if len(names) > 4 else "" ) if any(names) else "None"
)
console.print(Panel(table, title="Table view"))
#endregion