Skip to content

Instantly share code, notes, and snippets.

@jjmalina
Created August 4, 2018 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jjmalina/8a9baa4e8711f63116a33d4e9512a594 to your computer and use it in GitHub Desktop.
Save jjmalina/8a9baa4e8711f63116a33d4e9512a594 to your computer and use it in GitHub Desktop.
Get the first commit after a given date
#! /usr/bin/env python
import sys
from datetime import date
# pip install gitpython
from git import Repo
def get_earliest_commit_since(repo, dt):
earliest_commit = None
count = 0
while not earliest_commit:
for commit in repo.iter_commits('master', skip=count):
count += 1
if commit.committed_datetime.date() < dt:
return commit
return earliest_commit
def main(args):
dt = date(*[int(i) for i in args[1].split('-')])
repo = Repo()
commit = get_earliest_commit_since(repo, dt)
if commit:
print(
"%s %s %s %s" % (
commit.hexsha,
commit.committed_datetime,
commit.author,
commit.message
)
)
if __name__ == '__main__':
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment