mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

svn-poll (2644B)


      1 #!/bin/bash
      2 
      3 # print svn log of changes in repository $URL (except those of $USER)
      4 # since the latest time svn-poll was called on that repository
      5 # once a log has been shown, svn-poll will shut up until $USER commits
      6 # or svn-poll-drop is called
      7 
      8 set -e
      9 
     10 # after this many lines of failure, complain
     11 MAX_ERRORS="100"
     12 DIFF_MAX="1000"
     13 # drop diff after that many lines
     14 DIFF_MAXB=$(($DIFF_MAX+1))
     15 
     16 URL="$1"
     17 USER="$2"
     18 BASE=$(base64 <<<"$URL" | tr -d '\n')
     19 DIR="$HOME/temp/svn-poll/$BASE"
     20 FILE="$DIR/rev"
     21 MUTEFILE="$DIR/muted"
     22 ERRORS="$DIR/errors"
     23 TEMP=$(mktemp)
     24 DATE=$(date)
     25 
     26 mkdir -p "$DIR"
     27 
     28 LATEST="$(svn info "$URL" 2>$TEMP | grep Revision | cut -d' ' -f2)"
     29 
     30 # http://stackoverflow.com/a/806923/414272
     31 re='^[0-9]+$'
     32 if ! [[ $LATEST =~ $re ]] ; then
     33   # importantly, don't save $LATEST then
     34   # log the error
     35   cat "$TEMP" >> "$ERRORS"
     36   rm "$TEMP"
     37   echo "$DATE: problem when fetching latest revision: got $LATEST" >>"$ERRORS"
     38   # now, should we whine...
     39   NERRORS=$(cat "$ERRORS" | wc -l)
     40   if [ $NERRORS -gt $MAX_ERRORS ]
     41   then
     42     # whine
     43     echo "errors encountered in the last $MAX_ERRORS attempts:" >&2
     44     cat "$ERRORS" >&2
     45     # reset the errors
     46     echo "" > "$ERRORS"
     47     exit 1
     48   fi
     49   # if we are here, we decided not to whine
     50   exit 0
     51 fi
     52 
     53 if [[ -f "$FILE" ]]
     54 then
     55   SEEN=$(cat "$FILE")
     56 else
     57   # the first time, shut up and just store the current revision
     58   echo "$LATEST" > "$FILE"
     59   echo "" > "$ERRORS"
     60   exit 0
     61 fi
     62 
     63 if [[ "$SEEN" == "$LATEST" ]]
     64 then
     65   # nothing to do!
     66   exit 0
     67 fi
     68 
     69 HEADER=0
     70 
     71 # ok, so we have revisions to go over...
     72 svn log --xml -r$(($SEEN+1)):$LATEST "$URL" |
     73   xmlstarlet sel -t -m '//log/logentry' \
     74   -v 'concat(@revision, " ", author/text())' -n |
     75   while read revision author
     76 do
     77   if [[ -f "$MUTEFILE" ]]
     78   then
     79     # we are muted (have notified a change already)
     80     # if we committed, we unmute
     81     if [[ "$author" == "$USER" ]]
     82     then
     83       rm -f "$MUTEFILE"
     84     fi
     85   else
     86     # we are not muted: if someone else than us committed,
     87     # we notify and mute
     88     if [[ "$author" != "$USER" ]]
     89     then
     90       if [ $HEADER -eq 0 ]
     91       then
     92         echo "### updates on $URL ($USER) ###"
     93         HEADER="1"
     94       fi
     95       # notify the revision
     96       svn log -v -r$revision "$URL"
     97       # show a diff with no colors
     98       # and truncated to the right number of lines
     99       svn diff --internal-diff -c$revision "$URL" | 
    100         sed "${DIFF_MAX}s/.*/... cut at line $DIFF_MAX .../;${DIFF_MAXB},\$d"
    101       # shut up until something new happens from $USER
    102       touch "$MUTEFILE"
    103     fi
    104   fi
    105 done
    106 
    107 # now that it's been processed, save it
    108 echo "$LATEST" > "$FILE"
    109 # and reset the errors
    110 echo "" > "$ERRORS"
    111