Script to display Git commits for creating time sheet.

27 / Feb / 2013 by Puneet Behl 0 comments

We use “git log” many times a day while using Git in our projects. Sometimes we need to list Git commits between specific dates. Although, Git provides command to do this but the syntax is too long to type.

So I created a script named timesheet, which facilitates us to display Git commits between two specific dates or after a specified date in prettified one line format for a specific author via very simple syntax. Below is the timesheet script:

   #!/bin/bash

   TODAY=`date +%Y%m%d`
   EXPECTED_ARGS=1
   E_BADARGS=65
   AUTHOR_NAME_REGEX=".*"
   DATE_REGEX="[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]"
   DAY_REGEX="[0-3][0-9]"
   if [ $# -lt $EXPECTED_ARGS ]
   then
       git log --pretty="short" --format=%Cblue%h%' 'Cgreen%an[%ad]%' 'Creset%s%n --date=short
   fi

   if [[ $# -eq $EXPECTED_ARGS && $1 =~ $AUTHOR_NAME_REGEX ]]
   then
       git log --pretty="short" --format=%Cblue%h%' 'Cgreen%an[%ad]%' 'Creset%s%n --date=short --author=$1
   fi

   if [[ $# -eq $EXPECTED_ARGS && $1 =~ $DATE_REGEX ]]
   then
       git log --pretty="short" --format=%Cblue%h%' 'Cgreen%an[%ad]%' 'Creset%s%n --date=short --after={$1}
   fi

   if [[ $# -gt $EXPECTED_ARGS && $# -eq 2 && $1 =~ $DATE_REGEX && $2 =~ $AUTHOR_NAME_REGEX ]]
   then
       git log --pretty="short" --format=%Cblue%h%' 'Cgreen%an[%ad]%' 'Creset%s%n --date=short --after={$1} --author=$2
   fi

   if [[ $# -gt $EXPECTED_ARGS && $# -eq 2 && $1 =~ $DATE_REGEX && $2 =~ $DATE_REGEX ]]
   then
       git log --pretty="short" --format=%Cblue%h%' 'Cgreen%an[%ad]%' 'Creset%s%n --date=short --after={$1} --before={$2}
   fi

   if [[ $# -gt $EXPECTED_ARGS && $# -eq 3 && $1 =~ $DATE_REGEX && $2 =~ $DATE_REGEX && $3 =~ $AUTHOR_NAME_REGEX ]]
   then
       git log --pretty="short" --format=%Cblue%h%' 'Cgreen%an[%ad]%' 'Creset%s%n --date=short --after={$1} --before=${2} --author=$3
   fi

   if [[ $# -eq $EXPECTED_ARGS && $1 =~ $DAY_REGEX ]]
   then
       git log --pretty="short" --format=%Cblue%h%' 'Cgreen%an[%ad]%' 'Creset%s%n --date=short --after={$1-%m-%Y}
   fi

   if [[ $# -gt $EXPECTED_ARGS && $# -eq 2 && $1 =~ $DAY_REGEX && $2 =~ $DAY_REGEX ]]
   then
       git log --pretty="short" --format=%Cblue%h%' 'Cgreen%an[%ad]%' 'Creset%s%n --date=short --after={$1-%m-%Y} --before={$2-%m-%Y}
   fi

   if [[ $# -gt $EXPECTED_ARGS && $# -eq 2 && $1 =~ $DAY_REGEX && $2 =~ $AUTHOR_NAME_REGEX ]]
   then
       git log --pretty="short" --format=%Cblue%h%' 'Cgreen%an[%ad]%' 'Creset%s%n --date=short --after={$1-%m-%Y} --author=$2
   fi

   if [[ $# -gt $EXPECTED_ARGS && $# -eq 3 && $1 =~ $DAY_REGEX && $2 =~ $DAY_REGEX && $3 =~ $AUTHOR_NAME_REGEX ]]
   then
       git log --pretty="short" --format=%Cblue%h%' 'Cgreen%an[%ad]%' 'Creset%s%n --date=short --after={$1-%m-%Y} --before={$2-%m-%Y} --author=$3
   fi

Installation
In case of Ubuntu

  1. Open the terminal.
  2. Open the file .gitconfig in your home directory using your favorite text editor. (e.g: gedit, vim, vi)
  3. Check that the script file has executable permissions. (if not change via chmod command)
  4. Create an alias for the script in .gitconfig as shown below:
    [alias]
    timesheet = ! <PATH_TO_SCRIPT_FILE>/timesheet
     

Usage
Some of the usages are given below:

   git timesheet
   git timesheet <START_DAY_OF_CURRENT_MONTH>
   git timesheet <START_DAY> <END_DAY>
   git timesheet <START_DATE>
   git timesheet <START_DATE> <END_DATE>
   git timesheet <AUTHOR_NAME>
   git timesheet <START_DAY> <AUTHOR_NAME>
   git timesheet <START_DAY> <END_DAY> <AUTHOR_NAME>
   git timesheet <START_DATE> <AUTHOR_NAME>
   git timesheet <START_DATE> <END_DATE> <AUTHOR_NAME>

Example

git timesheet 15 17
or
git timesheet 2013-02-15 2013-02-17
or
git timesheet 15 17 <AUTHOR_NAME>

Note: One can specify dates as 15 or 17 only for current month otherwise dates should be in yyyy-mm-dd (2013-02-15) format.

Please share your feedback and suggestions.


Read more about timesheet script on GitHub »

Tag -

FOUND THIS USEFUL? SHARE IT

Leave a comment -