Skip to content

Instantly share code, notes, and snippets.

@natebeaty

natebeaty/gitlog

Last active Mar 7, 2021
Embed
What would you like to do?
script to pull your git commit logs for various date ranges, formatted for a timesheet
#!/bin/bash
# Pulls git log for an author formatted for a timesheet
# Usage:
# gitlog = last 8 hours of commits
# gitlog 48 = last 48 hours of commits
# gitlog 2018-12-20 = all commits on 12/20/2018
# gitlog yesterday = all commits yesterday
# gitlog 1 week ago = all commits since a week ago
# Your username
user="Nate"
# Default values if no args are sent
since="8.hours.ago"
until=""
# Regex to match 2018-12-20 or 12-20-2018
re_date='(^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$)|(^[0-9]{2}\-[0-9]{2}\-[0-9]{4}$)'
# Regex to match 5, 15, or 150
re_hours='^[0-9]{1,3}$'
# Concatenate all args into single string (to allow for "1 week ago" without quotes)
args="$*"
# replace / with - for alternate date formats e.g. 11/28/2020
args=${args//\//\-}
if [ -n "$args" ]
then
if [[ $args =~ $re_date ]]
then
echo "Log for $args (also in clipboard):"
since="$args 00:00"
until="$args 23:59"
elif [[ $args =~ $re_hours ]]
then
echo "Log for last $args hours (also in clipboard):"
since="$args.hours.ago"
elif [[ $args == "yesterday" ]]
then
until="$(date +%Y-%m-%d) 00:00"
echo "Log for $args (also in clipboard):"
since="$args"
else
echo "Log since $args (also in clipboard):"
since="$args"
fi
else
# default
echo "Log for last 8 hours (also in clipboard):"
fi
echo "----"
# Store output to var so we can spit it out to console then shove it in the clipboard
OUTPUT=$(git log --author=$user --since="$since" --until="$until" --all-match --format="%s" | sed "/Merge branch/d" | sed "/^compiled assets$/d" | sed "/^typo$/d" | tail -r | tr "\n" "😂" | sed -e "s/😂/; /g" | sed -e "s/; $//g" | perl -pe 's/(; \d+\.\d+\.\d+)//g')
echo $OUTPUT
echo $OUTPUT | tr -d '\n' | pbcopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment