tweaks
[infrastructure.git] / scripts / git-mirror.sh
index 4cd6d34..5a324d6 100755 (executable)
@@ -19,6 +19,11 @@ RUNNING_FILE=$LOCAL_MIRROR_DIR/RUNNING_MIRROR
 # global list - errors to report
 FAILED_CMDS=()
 
+VERBOSE=
+function verbose () {
+    [ -n "$VERBOSE" ] && echo "--------------------" "$@"
+}
+
 function msg () {
     [ -n "$QUIET" ] || echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" "$@"
 }
@@ -33,12 +38,16 @@ function error () {
     local REPO=$1; shift
     local SUBJECT=$1; shift
     local BODY=$1; shift
+    verbose "error" $SUBJECT
 
     # already notified within a half day ?
     GRACE=$((60*12))
-    is_old=$(find $REPO/$NOTIFIED_FILE -mmin +$GRACE 2> /dev/null)
+    is_recent=$(find $REPO/$NOTIFIED_FILE -mmin -$GRACE 2> /dev/null)
     # not there or less than 12 our old: don't notify
-    [ -z "$is_old" ] && return
+    if [ -n "$is_recent" ] ; then
+       verbose "skipping recent notification -- $SUBJECT"
+       return
+    fi
 
     for admin in $ADMINS; do
         echo -e "$BODY" | mail -s "$SUBJECT" $admin
@@ -66,6 +75,7 @@ function run () {
        $COMMAND || ok=
        TORECORD="$COMMAND"
     fi
+    
     # failed ?
     if [ -z "$ok" ]; then
        # let's record the failure unless ignore is set
@@ -73,6 +83,7 @@ function run () {
             FAILED_CMDS=("${FAILED_CMDS[@]}" "$TORECORD")
        fi
     fi
+    verbose "after run with $COMMAND" "ok=$ok" "#failed=${#FAILED_CMDS[@]}"
     popd > /dev/null
 }
 
@@ -129,11 +140,12 @@ function mirror_repo () {
        run ${REPO_DIR} git fetch origin --tags
        run ${REPO_DIR} git fetch origin
        merge_all_branches $REPO_DIR $NAME origin
-       if [ $? -ne 0 ]; then
+       if [ -n "$FAILED_CMDS" ]; then
            # format mail body
            body="Can not fetch from ${MASTER_REPO}\n\n------------\n FAILED COMMANDS:\n"
            for line in "${FAILED_CMDS[@]}"; do body="$body$line\n"; done
             error $REPO_DIR "git-mirror.sh failed to merge remote with module ${NAME}" "$body"
+           return
        fi
     else
         msg "mirroring ${NAME} for the first time"
@@ -157,7 +169,7 @@ function mirror_repo () {
     fi
     run ${REPO_DIR} git push --tags local_master
     push_all_branches $REPO_DIR $NAME local_master origin 
-    if [ $? -ne 0 ]; then
+    if [ -n "$FAILED_CMDS" ]; then
        # format mail body
        body="FAILED COMMANDS:\n"
        for line in "${FAILED_CMDS[@]}"; do body="$body$line\n"; done
@@ -169,19 +181,21 @@ function mirror_repo () {
 }
 
 function usage () {
-    echo "Usage $COMMAND [-a admin-mails] [-r remote-git-url] [-q] REPONAME*"
+    echo "Usage $COMMAND [-a admin-mails] [-r remote-git-url] [-q] [-v] REPONAME*"
     exit 1
 }
 
-while getopts "a:r:qh" opt; do
+while getopts "a:r:qvh" opt; do
   case $opt in
       a) ADMINS=$OPTARG ;;
       r) REMOTE_GIT=$OPTARG ;;
       q) QUIET=true ;;
+      v) VERBOSE=true ;;
       h) usage ;;
       \?) echo "Invalid option: -$opt" >&2 ;;
   esac
 done
+shift $((OPTIND-1))
 
 # is the stamp older than an hour ? 
 # in minutes
@@ -199,7 +213,6 @@ fi
 
 trap failure INT
 
-shift $((OPTIND-1))
 date > $RUNNING_FILE
 for gitrepo in "$@"; do mirror_repo $gitrepo ; done
 rm -f $RUNNING_FILE