From 56787489b5beb74b6d9a1b46a6e4465be9e1260f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 18 Dec 2015 15:08:04 +0200 Subject: Add script that checks repository for various useful things --- check | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100755 check diff --git a/check b/check new file mode 100755 index 0000000..6cbdfa6 --- /dev/null +++ b/check @@ -0,0 +1,117 @@ +#! /usr/bin/env bash + +# Check git repository for various things: +# +# master - check the current branch is master +# clean - check for untracked files/uncommitted changes +# synced - check that local and origin and in sync (push/pull) +# submodule - check submodules are synced +# +# Usage: check [--master] [--clean] [--synced] [--submodule] [] +# +usage="usage: $0 [--master] [--clean] [--synced] [--submodule] []" + +trap 'exit 1' ERR + +function info () { echo "$*" 1>&2; } +function error () { info "$*"; exit 1; } + +dir=. +master=n +submod=n +clean=n +synced=n + +while [ $# -gt 0 ]; do + case $1 in + --master) + master=y + shift + ;; + --submodule) + submod=y + shift + ;; + --clean) + clean=y + shift + ;; + --synced) + synced=y + shift + ;; + *) + dir=$1 + break + ;; + esac +done + +function branch () # +{ + git -C $1 rev-parse --symbolic-full-name --abbrev-ref HEAD +} + +function synced () # +{ + git -C $1 fetch + + local r=`git -C $1 rev-parse --verify $2` + local or=`git -C $1 rev-parse --verify origin/$2` + + if [ "$r" != "$or" ]; then + return 1 + fi +} + +r=0 + +if [ "$master" = "y" ]; then + br=`branch $dir` + + if [ "$br" != "master" ]; then + info "current branch in $dir is $br, not master" + r=1 + fi +fi + +if [ "$submod" = "y" ]; then + sms=`git -C $dir submodule foreach --quiet --recursive 'echo $path'` + for sm in $sms; do + smd=$dir/$sm + br=`branch $smd` + + if ! synced $smd $br; then + info "submodule $smd is not updated" + r=1 + fi + done +fi + +if [ "$clean" = "y" ]; then + if git -C $dir status --porcelain | grep -q "^?? "; then + info "untracked files in $dir" + r=1 + fi + + if git -C $dir status --porcelain | grep -q "^ M "; then + info "unstaged changes in $dir" + r=1 + fi + + if git -C $dir status --porcelain | grep -q "^M "; then + info "staged changes in $dir" + r=1 + fi +fi + +if [ "$synced" = "y" ]; then + br=`branch $dir` + + if ! synced $dir $br; then + info "$br and origin/$br in $dir differ, not synced" + r=1 + fi +fi + +exit $r -- cgit v1.1