DEV Community

Cover image for I put GitHub's contribution graph in my terminal, for any repo
ccarcaci
ccarcaci

Posted on

I put GitHub's contribution graph in my terminal, for any repo

GitHub's contribution graph is... only on GitHub.

You don't say?

(Image credits to Know Your Meme)

Check this out!

https://gist.github.com/ccarcaci/83b00ef0bd8f7d5434ad022562271d13

It provides an immediate-to-use sh script to show the contribution graph in your console.

Self-hosted GitLab, Bitbucket, private repositories... none of them has contributions shown as calendar. Plus, programmers are lazy and they don't want to switch from terminal to the browser.

On the same flavour, git log shows you only a list of commits with no clue about the distributions of them through your working days.

This script, half vibe-coded, in 60 lines, brings the contribution graph in your terminal.

Just:

./contribution-calendar.sh ~/code/some-repo
Enter fullscreen mode Exit fullscreen mode

Twelve months of history, 52 columns wide, greens scaled against the busiest day in the window. Same squares, same shape, in your terminal.

How it works

Three moving parts, and the first one does most of the work.

Pick a Sunday. Today minus the day-of-week gives the most recent Sunday; another 364 days back gives a Sunday 52 weeks earlier. Once the date list starts on a Sunday, the calendar is just a flat array read as a grid grid[col * 7 + row], columns are weeks, rows are weekdays. There's no calendar maths anywhere else in the script.

Count the commits. One pipeline turns history into date count pairs:

git log --since="$start" --date=short --format=%ad | sort | uniq -c | awk '{print $2, $1}'
Enter fullscreen mode Exit fullscreen mode

Draw it. A single awk pass loads the counts into a hash, walks the dates in order, and prints each day as two spaces with a 256-colour background. The month labels along the top get spliced into a blank string with substr, so they land above the right column without any cursor juggling.

Running it

It uses BSD date, so it works out of the box on macOS. On Linux, swap the two date calls:

start=$(date -d "-$((weeks * 7 + dow)) days" +%Y-%m-%d)
d=$(date -d "$d +1 day" +%Y-%m-%d)
Enter fullscreen mode Exit fullscreen mode

You'll want a 256-colour terminal and about 110 columns of width.

Make it yours

Add --author="$(git config user.email)" to the git log line to see only your own squares. Bump weeks past 52 for a longer view. Change the four colour codes if green isn't your palette. Alias it to git-cal and forget it exists until the day you want to know what a year of work actually looked like.

It's short enough to read in one sitting, fork it and bend it.

Why this is different?

Very similar solutions already exist, but they require dependencies installation, some are no longer updated for a very long time, or they provide an articulated set of parameters.

This script is ready for just being integrated in your terminal configuration with an alias.

Top comments (0)