Skip to content

Instantly share code, notes, and snippets.

@cwvh
Created May 31, 2014 22:57
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 cwvh/1f644b8da0464a887fcf to your computer and use it in GitHub Desktop.
Save cwvh/1f644b8da0464a887fcf to your computer and use it in GitHub Desktop.
""" pip install GitPython --upgrade --pre """
import git
import sys
def pretty_date(time=False):
"http://stackoverflow.com/questions/1551382/user-friendly-time-format-in-python"
from datetime import datetime
now = datetime.now()
if type(time) is int:
diff = now - datetime.fromtimestamp(time)
elif isinstance(time,datetime):
diff = now - time
elif not time:
diff = now - now
second_diff = diff.seconds
day_diff = diff.days
if day_diff < 0:
return ''
if day_diff == 0:
if second_diff < 10:
return "just now"
if second_diff < 60:
return str(second_diff) + " seconds ago"
if second_diff < 120:
return "a minute ago"
if second_diff < 3600:
return str( second_diff / 60 ) + " minutes ago"
if second_diff < 7200:
return "an hour ago"
if second_diff < 86400:
return str( second_diff / 3600 ) + " hours ago"
if day_diff == 1:
return "Yesterday"
if day_diff < 7:
return str(day_diff) + " days ago"
if day_diff < 31:
return str(day_diff/7) + " weeks ago"
if day_diff < 365:
return str(day_diff/30) + " months ago"
return str(day_diff/365) + " years ago"
def generate_last_commits(path):
repo = git.Repo(path)
tree = {}
fmt = "{:<30} {:<15} {}"
for commit in repo.iter_commits():
new = []
for path in commit.stats.files:
# The commits are ordered in time-descending order so
# if a pathspec is in our tree, no further commits could
# have been made in earlier time than the first one seen.
if not path in tree:
# Save the commit summary and timestamp for a pathspec
# that has not yet been seen. The ordering on `iter_items`
# guarantees that subsequent commit timestamps are strictly
# less than.
summary, timestamp = commit.summary, commit.committed_date
if len(summary) > 10:
summary = summary[:7] + '...'
new.append(fmt.format(path, summary, pretty_date(timestamp)))
tree[path] = True
for commit in new:
print(commit)
if __name__ == '__main__':
repo_path = './ruby/' if len(sys.argv) < 2 else sys.argv[1]
generate_last_commits(repo_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment