Skip to content

Instantly share code, notes, and snippets.

@digitalWestie
Last active December 20, 2024 18:05
Show Gist options
  • Select an option

  • Save digitalWestie/9ae3cad0a524b10d495f81d622f0d596 to your computer and use it in GitHub Desktop.

Select an option

Save digitalWestie/9ae3cad0a524b10d495f81d622f0d596 to your computer and use it in GitHub Desktop.
Visualise your commits

gitvis - A git log visualiser in your terminal

image

import sys
import argparse
from collections import defaultdict
from datetime import datetime
DESCRIPTION = """
Visualize git commit patterns in the terminal.
This script provides two visualisation modes:
1. Timeline view (default): Shows when commits occurred during each day using a 24-hour timeline
- █ indicates a commit occurred in that 30-minute block
- ░ indicates no commits in that 30-minute block
2. Count view (-c): Shows the number of commits per day using dots
- Each • represents one commit
Usage examples:
# Show timeline of commits for author "Rory"
git log | grep "Author: Rory" -A1 | python gitvis.py
# Show commit counts for author "Rory"
git log | grep "Author: Rory" -A1 | python gitvis.py -c
# Show timeline for all commits
git log | grep "Date:" -A1 | python gitvis.py
Happy coding!
Author: Rory Gianni
MIT License
"""
def visualize_commit_counts():
# Group commits by date
commits_by_date = defaultdict(int)
for line in sys.stdin:
if line.startswith('Date:'):
# Parse the date string (removing "Date:" prefix and whitespace)
date_str = line[5:].strip()
# Parse the git date format
date = datetime.strptime(date_str, '%a %b %d %H:%M:%S %Y %z')
# Convert to YYYY-MM-DD format
date_key = date.strftime('%Y-%m-%d')
commits_by_date[date_key] += 1
# Print visualization
for date in sorted(commits_by_date.keys()):
dots = '•' * commits_by_date[date]
print(f"{date} {dots}")
def visualize_commits():
# Group commits by date and half hour
commits_by_date_halfhour = defaultdict(set) # Using set to track which half hours have commits
for line in sys.stdin:
if line.startswith('Date:'):
# Parse the date string
date_str = line[5:].strip()
date = datetime.strptime(date_str, '%a %b %d %H:%M:%S %Y %z')
# Get date, hour, and calculate half hour block (0 or 1)
date_key = date.strftime('%Y-%m-%d')
half_hour_block = date.hour * 2 + (1 if date.minute >= 30 else 0)
commits_by_date_halfhour[date_key].add(half_hour_block)
# Print hour labels (showing even hours for readability)
print(" 0 12 24")
print(" | | |")
# Print visualization
for date in sorted(commits_by_date_halfhour.keys()):
blocks_with_commits = commits_by_date_halfhour[date]
timeline = ''.join('█' if block in blocks_with_commits else '░'
for block in range(48))
print(f"{date} {timeline}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=DESCRIPTION,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument('-c', '--count', action='store_true',
help='Show commit counts instead of timeline')
args = parser.parse_args()
if args.count:
visualize_commit_counts()
else:
visualize_commits()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment