Skip to content

Instantly share code, notes, and snippets.

@punitganshani
Created February 23, 2023 05:49
Show Gist options
  • Save punitganshani/2c78fb65a3c2933a5c857aa0633e18f1 to your computer and use it in GitHub Desktop.
Save punitganshani/2c78fb65a3c2933a5c857aa0633e18f1 to your computer and use it in GitHub Desktop.
PyGit: Last Commit with Age > 30d
# %%
#!/bin/env python3
import pygit2, os, datetime, json
from pygit2 import GIT_SORT_TOPOLOGICAL, GIT_SORT_REVERSE, Commit
# %%
repo = pygit2.Repository(pygit2.discover_repository(os.getcwd()))
time_now = datetime.datetime.now()
# %%
for branch in (repo.lookup_branch(b) for b in repo.listall_branches()):
last_commit_id = branch.target
i =0
for commit in repo.walk(last_commit_id, GIT_SORT_TOPOLOGICAL):
if (i == 0):
last_commit = commit
i+= 1
if (i> 0):
exit
commit_time = datetime.datetime.fromtimestamp(last_commit.commit_time)
age = time_now - commit_time
# age > 30d
if age > datetime.timedelta(days=30):
print("[-] {} {} {} {}".format(last_commit.author.email, branch.branch_name, commit_time, last_commit.message))
else:
print("[+] {} {} {} {}".format(last_commit.author.email, branch.branch_name, commit_time, last_commit.message))
#print(last_commit.commit_time, last_commit.message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment