gitcheck 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/bash
  2. # Show actions needed for a list of git directories
  3. #
  4. # Usage: gitcheck [-v] [DIR [DIR]]
  5. #
  6. set -e; set -u
  7. # Helper functions
  8. PROG=`basename "$0" | tr -d '\n'`
  9. function info() { echo `date +%c` ${PROG}\: info: "$@" 1>&2; }
  10. function warn() { echo `date +%c` ${PROG}\: warning: "$@" 1>&2; }
  11. function error() { echo `date +%c` ${PROG}\: error: "$@" 1>&2; }
  12. function debug() { [[ -v DEBUG ]] && echo `date +%c` ${PROG}\: debug: "$@" 1>&2 || true ; }
  13. function die() { echo `date +%c` ${PROG}\: fatal: "$@" 1>&2 && exit 1; }
  14. # Global Variables
  15. declare csv_row
  16. csv_row="" # build up a one line output for each repo
  17. function isGitPushOrPullNeeded() {
  18. # Show if git push or pull is needed to upstream
  19. #
  20. # Source: http://stackoverflow.com/questions/3258243/git-check-if-pull-needed
  21. #
  22. # TO DOs:
  23. # TODO collect all info in a CSV by default, put put one summary line per repo
  24. # TODO add number of modified, untracked, etc files from "git ls-files [--optione]"
  25. #
  26. # Here are the "git branch --format" options
  27. #
  28. # $ git branch --format "refname %(refname) objecttype %(objecttype) objectsize %(objectsize) objectname %(objectname) deltabase %(deltabase) upstream %(upstream) push %(push) HEAD %(HEAD) symref %(symref) worktreepath %(worktreepath) contents:subject %(contents:subject) contents:body %(contents:body) contents:signature %(contents:signature)"
  29. #
  30. # They are likely useful for getting info
  31. # Fetch so we can compare local state with upstream without pull
  32. git fetch --all
  33. LOCAL=$(git rev-parse @)
  34. REMOTE=$(git rev-parse @{u})
  35. BASE=$(git merge-base @ @{u})
  36. push_or_pull_needed=":"
  37. if [ $LOCAL = $REMOTE ]; then
  38. push_or_pull_needed="UP-TO-DATE"
  39. elif [ $LOCAL = $BASE ]; then
  40. push_or_pull_needed="pull"
  41. elif [ $REMOTE = $BASE ]; then
  42. push_or_pull_needed="push"
  43. else
  44. push_or_pull_needed="DIVERGED"
  45. fi
  46. info "push_or_pull_needed: $push_or_pull_needed"
  47. csv_row="${csv_row}:${push_or_pull_needed}"
  48. }
  49. function gitChecks() {
  50. # Perform various checks of status of git in currenct directory
  51. # Show any actions needed
  52. DIR=${1:-`pwd`} # get directory name
  53. if [ ! -d "$DIR/.git" ]; then
  54. warn "$DIR is not a git repo. Skipping."
  55. echo
  56. return
  57. fi
  58. pushd ${DIR} > /dev/null # go there
  59. info "Checking ${DIR}"
  60. csv_row="${DIR}:"
  61. isGitPushOrPullNeeded
  62. echo
  63. info git status for $DIR
  64. git status .
  65. echo
  66. popd > /dev/null # back to original directory
  67. }
  68. ARGC=$# # Number of args, not counting $0
  69. if [ "$ARGC" == "0" ]; then
  70. set -- `pwd` "$@" # add pwd if no args
  71. fi
  72. i=1 # Used as argument index
  73. while true; do
  74. DIR=${1:-""}
  75. if [ "$DIR" ]; then
  76. gitChecks "$DIR"
  77. shift
  78. else
  79. break # Stop loop when no more args.
  80. fi
  81. i=$((i+1))
  82. done
  83. echo
  84. info "Done."