Suppress or prevent duplicate inotifywait events?

前端 未结 2 1244
臣服心动
臣服心动 2021-01-16 03:13

Currently inotifywait is watching git server folders. End it emits only when specific file modified. The problem is, when changes are pushed to git server,

2条回答
  •  花落未央
    2021-01-16 03:57

    Here is how I solved it for my own needs. I am monitoring a path so that it will auto-compile for syntax errors. And I was bothered with the duplicate events emitted by inotifywait. Add the syncheck function into your .bashrc:

    syncheck() {
      declare -A fileMap
      inotifywait --exclude .swp --format '%w%f' -e modify,create -m -r -q $* | \
      while read FILE; do
        es=$(date +'%s')
        case "$FILE" in
        *.hs)
        if [[ ${fileMap[$FILE]} != $es ]];then
            sdiff=$((es-${fileMap[$FILE]:-0}))
            fileMap[$FILE]=$es
            ((sdiff < 3)) && continue
            ghc -fno-code $FILE
            echo "---------------------------------------------"
        fi
        ;;
        esac
      done
    }
    

    How to use:

    cd ~/src/proj/
    . ~/.bashrc
    syncheck .
    

    In another terminal, modify or create a Haskell file in ~/src/proj/ location. The syncheck while-loop will detect this and auto-compile for syntax errors.

    The key idea to this duplicate events suppression solution is Unix`s epoch seconds and bash dictionary.

提交回复
热议问题