Created
August 4, 2018 17:34
-
-
Save jjmalina/8a9baa4e8711f63116a33d4e9512a594 to your computer and use it in GitHub Desktop.
Get the first commit after a given date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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