You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.1 KiB

  1. #!/usr/bin/env bash
  2. # Requires all the repositories you want to monitor to exist in the same directory
  3. # I usually create a directory in my home dir and move my repositories in there
  4. DIRS="/home/user/projects/* "
  5. # Can be your name, but much easier if you make this your email
  6. USERNAME="user@mail.dcu.ie"
  7. # You can change the format of the date lwhcih gets logged if you want
  8. DATE=`date +%Y_%m_%d`
  9. # Add who this get's mailed to. If you're confident on your workflow, send it to your boss.
  10. RECIPIENT="other@gmail.com"
  11. # ased on parameter passed, returns a string which will be created as a directory
  12. periodToString() {
  13. case $opt in
  14. w)
  15. echo "weekly_logs"
  16. ;;
  17. m)
  18. echo "monthly_logs"
  19. ;;
  20. y)
  21. echo "annual_logs"
  22. ;;
  23. esac
  24. }
  25. # Checks each file in DIRS, checks if it a dierectory and if '.git' exists inside it.
  26. # If so, a git report directory is created in the users home directory, which will have a subdirectory based on what the user passed as a paramter
  27. # Gets the commit history of each directory for the desired period of time where the author is the username passed above
  28. # This is reversed so as to be in chronological order. Any blank lines are removed using sed and a report is generated.
  29. # This report is email to an email of the user's choice, as set above. I usually email it to myself when this is running as a cronjob.
  30. reporter() {
  31. for D in $DIRS; do
  32. if [ -d "${D}" ]; then
  33. NAME=$(echo $D | cut -d/ -f5-)
  34. cd "${D}"
  35. if [ -e ".git" ]; then
  36. PTS=$(periodToString)
  37. mkdir -p /home/${USER}/gitreports/${PTS}/${NAME}
  38. touch /home/${USER}/gitreports/${PTS}/${NAME}//${NAME}_${DATE}.log
  39. echo -e "My ${NAME} workflow since ${PERIOD}:\n" > /home/${USER}/gitreports/${PTS}/${NAME}/${NAME}_${DATE}.log
  40. git log --author="$USERNAME" --format="%aD --> %B" --since=="${PERIOD}" --reverse | sed '/^$/d' >> /home/${USER}/gitreports/${PTS}/${NAME}/${NAME}_${DATE}.report
  41. mailx -s "${PTS} review: ${NAME}" ${RECIPIENT} < /home/${USER}/gitreports/${PTS}/${NAME}/${NAME}_${DATE}.report
  42. fi
  43. fi
  44. done
  45. }
  46. # A help message displayed when the user passes an incorrect parameter or `-h`
  47. usage() {
  48. cat <<EOF
  49. Report
  50. Description: A program to report back your weekly, monthly, or yearly git workflow through email.
  51. Usage: report [-h] [-w] [-m] [-y]
  52. -h Show this help message
  53. -w Report workflow over the past week.
  54. -m Report workflow over the past month.
  55. -y Report workflow over the past year.
  56. Examples:
  57. report -h
  58. report -w
  59. EOF
  60. }
  61. # Accepts only w, m, y, and h
  62. # Anything else results in the help message
  63. while getopts ":wmyh" opt; do
  64. case "${opt}" in
  65. h)
  66. usage
  67. ;;
  68. w)
  69. PERIOD="7 days ago"
  70. reporter
  71. ;;
  72. m)
  73. PERIOD="1 month ago"
  74. reporter
  75. ;;
  76. y)
  77. PERIOD="1 year ago"
  78. reporter
  79. ;;
  80. *)
  81. usage
  82. exit 0
  83. ;;
  84. esac
  85. done