Abstract git change calculator

This commit is contained in:
Zeva Rose 2022-05-01 15:09:50 -06:00
parent b1d5415625
commit 3bafd25cab

View file

@ -57,6 +57,54 @@ def get_all_commands(module: ModuleType = jarvis.cogs) -> Dict[str, Callable]:
return {k: v for k, v in commands.items() if v}
def get_git_changes() -> dict:
"""Get all Git changes"""
repo = git.Repo(".")
current_hash = repo.head.object.hexsha
origin = repo.remotes.origin
changes = origin.fetch()
file_changes = {}
for change in changes:
if change.commit.hexsha == current_hash:
break
files = change.commit.stats.files
for file, stats in files.items():
if file not in file_changes:
file_changes[file] = {"insertions": 0, "deletions": 0, "lines": 0}
for k, v in stats.items():
file_changes[file][k] += v
table = Table(title="File Changes")
table.add_column("File", justify="left", style="white", no_wrap=True)
table.add_column("Insertions", justify="center", style="green")
table.add_column("Deletions", justify="center", style="red")
table.add_column("Lines", justify="center", style="magenta")
i_total = 0
d_total = 0
l_total = 0
for file, stats in file_changes.items():
i_total += stats["insertions"]
d_total += stats["deletions"]
l_total += stats["lines"]
table.add_row(
file,
str(stats["insertions"]),
str(stats["deletions"]),
str(stats["lines"]),
)
table.add_row("Total", str(i_total), str(d_total), str(l_total))
return {
"table": table,
"inserted_lines": i_total,
"deleted_lines": d_total,
"total_lines": l_total,
}
async def update(bot: "Snake", logger: "Logger" = None) -> Optional[UpdateResult]:
"""
Update JARVIS and return an UpdateResult.
@ -79,9 +127,9 @@ async def update(bot: "Snake", logger: "Logger" = None) -> Optional[UpdateResult
if current_hash != remote_hash:
logger.info(f"Updating from {current_hash} to {remote_hash}")
current_commands = get_all_commands()
changes = get_git_changes()
changes = origin.pull()
logger.info(f"Pulled {len(changes)} changes")
origin.pull()
await asyncio.sleep(3)
new_commands = get_all_commands()
@ -139,49 +187,12 @@ async def update(bot: "Snake", logger: "Logger" = None) -> Optional[UpdateResult
bot.reload_extension(module)
reloaded.append(module)
file_changes = {}
for change in sorted(changes, key=lambda x: x.commit.committed_datetime, reverse=True):
if change.commit.hexsha == current_hash:
break
files = change.commit.stats.files
for file, stats in files.items():
if file not in file_changes:
file_changes[file] = {"insertions": 0, "deletions": 0, "lines": 0}
for k, v in stats.items():
file_changes[file][k] += v
table = Table(title="File Changes")
table.add_column("File", justify="left", style="white", no_wrap=True)
table.add_column("Insertions", justify="center", style="green")
table.add_column("Deletions", justify="center", style="red")
table.add_column("Lines", justify="center", style="magenta")
i_total = 0
d_total = 0
l_total = 0
for file, stats in file_changes.items():
i_total += stats["insertions"]
d_total += stats["deletions"]
l_total += stats["lines"]
table.add_row(
file,
str(stats["insertions"]),
str(stats["deletions"]),
str(stats["lines"]),
)
table.add_row("Total", str(i_total), str(d_total), str(l_total))
return UpdateResult(
table=table,
old_hash=current_hash,
new_hash=remote_hash,
added=loaded,
removed=unloaded,
changed=reloaded,
inserted_lines=i_total,
deleted_lines=d_total,
total_lines=l_total,
**changes,
)
return None