script to pull your git commit logs for various date ranges, formatted for a timesheet
This file contains 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
#!/bin/bash | |
# Pulls git log for an author formatted for a timesheet | |
# Usage: | |
# gitlog = all commits today | |
# gitlog 48 = last 48 hours of commits | |
# gitlog 2022-05-31 = all commits on May 13, '22 (also supports 5/31/2022, 05-31-2022) | |
# 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="$(date +%Y-%m-%d) 00:00" | |
until="" | |
# Regex to match 2018-12-20 or 12-20-2018 | |
re_date='(^[0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2}$)|(^[0-9]{1,2}\-[0-9]{1,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="$(date -v-1d +%F) 00:00" | |
else | |
echo "Log since $args (also in clipboard):" | |
since="$args" | |
fi | |
else | |
# default | |
echo "Log for today (also in clipboard):" | |
fi | |
echo "----" | |
echo $args | |
echo $since | |
echo $until | |
# 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 "/^pedant$/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