Styles

Thursday, December 1, 2022

Reporting on your After Hours using Git commit timestamps

Sometimes its hard to keep yourself accountable to the extra long hours you work, and most of the time you don't want to show off how late you've been working, but every now and then your manager would probably ask if you've been putting in the necessary hours.

If thats the case, git commits can give you a certain level of transparency provided you commit regularly.

The following bash script assumes you have all your git repositories cloned to a folder specified in GITFOLDER which should be updated to your local setup. It will run a simple "git log --author" command for a given USERNAME and list only the timestamps after the hours of 5pm to the hours of 6am:
#!/usr/bin/env sh

USERNAME="<username>"
GITFOLDER="${HOME}/dev"

cd ${GITFOLDER}
FOLDERS=$(ls)

while IFS= read -r FOLDER
do
    cd "${GITFOLDER}/${FOLDER}"

    TIME_LOGS=$(git log --author="${USERNAME}" | grep "Date:" | grep -E '\s1[7-9]+\:|\s2[0-3]+\:|\s0[0-6]+\:')
    
    while IFS= read -r TIME_LOG
    do
        if [ ! -z "$TIME_LOG" ]
        then
            COMMIT_DATE=$(echo ${TIME_LOG} | sed -e "s/Date: //g")
            echo "${COMMIT_DATE} ${FOLDER}"
        fi
    done <<< "${TIME_LOGS}"

done <<< "${FOLDERS}"