From 8c4f64cf9650b4e76156fc00f2bcaadca705b215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Val=C3=AD=C4=8Dek=20=28YCNet=29?= Date: Thu, 22 Feb 2018 22:09:11 +0100 Subject: [PATCH] Testing git runner --- checker/detector-lib-cfg | 29 +++++++++++++++++++++ checker/detector-lib-git | 37 +++++++++++++++++++++++++++ checker/notify-commit | 11 ++++++++ checker/notify-tag | 11 ++++++++ checker/run-checker | 54 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 checker/detector-lib-cfg create mode 100644 checker/detector-lib-git create mode 100755 checker/notify-commit create mode 100755 checker/notify-tag diff --git a/checker/detector-lib-cfg b/checker/detector-lib-cfg new file mode 100644 index 0000000..d29b6e6 --- /dev/null +++ b/checker/detector-lib-cfg @@ -0,0 +1,29 @@ +#!/bin/bash + + +# check if varriable is set +[ -n "$CONFIG_DIR" ] || die "ConfigDir is not set - exit!" + + +function detectorRunCapable(){ + if [ -f $CONFIG_DIR/detector.cfg ] + then + return 0 + else + return 1 + fi +} + +function detectorLoadConfig(){ + # load config + source $CONFIG_DIR/detector.cfg + + # create cache dirs + DET_DIR=$CCLONE_CACHE/detector + DET_TAGS=$DET_DIR/tags + DET_BRANCHES=$DET_DIR/branches + [ -d $DET_TAGS ] || mkdir -p $DET_TAGS + [ -d $DET_BRANCHES ] || mkdir -p $DET_BRANCHES +} + + diff --git a/checker/detector-lib-git b/checker/detector-lib-git new file mode 100644 index 0000000..d5416a5 --- /dev/null +++ b/checker/detector-lib-git @@ -0,0 +1,37 @@ +#!/bin/bash + + +function detectorSum(){ + local dir=$1 + [ -f $dir/FETCH_HEAD ] || touch $dir/FETCH_HEAD + # use md5sum - it is in busybox + cat $dir/FETCH_HEAD | md5sum | cut -f1 -d' ' +} + +function detectorSumPersist(){ + detectorSum $1 > $CCLONE_CACHE/detectorSum +} + +function detectorCheckFetchHead(){ + # check if repo fetch_head changed, if so, return 1 + local dir=$1 + + [ -f $CCLONE_CACHE/detectorSum ] || touch $CCLONE_CACHE/detectorSum + + newSum=$(detectorSum $dir) + oldSum=$(cat $CCLONE_CACHE/detectorSum) + + if [ "x$oldSum" = "x$newSum" ] + then + return 0 + else + return 1 + fi +} + + +function gitListTags(){ + local dir=$1 + git --no-paginate --git-dir="$dir" tag -l + +} diff --git a/checker/notify-commit b/checker/notify-commit new file mode 100755 index 0000000..de17493 --- /dev/null +++ b/checker/notify-commit @@ -0,0 +1,11 @@ +#!/bin/bash +# notify on commit push +# +# Parameters (via env) +# +# COMMIT - hash of commit +# COMMIT_AUTHOR - author of commit +# COMMIT_BRANCH - name of branch +# PROJECT_NAME - project name specified in cloner.cfg +echo "Commit: ($COMMIT: (AUTHOR=$COMMIT_AUTHOR; BRANCH=$COMMIT_BRANCH), Project: $PROJECT_NAME)" + diff --git a/checker/notify-tag b/checker/notify-tag new file mode 100755 index 0000000..11aeb92 --- /dev/null +++ b/checker/notify-tag @@ -0,0 +1,11 @@ +#!/bin/bash +# notify on tag push (new tag or change) +# +# Parameters (via env) +# +# TAG_HASH - hash of tagged commit +# TAG_NAME - tag label +# TAG_AUTHOR - who authored the tag - if available +# PROJECT_NAME - name of project specified in cloner.cfg +echo "TAG: (Name=$TAG_NAME; AUTHOR=$TAG_AUTHOR; SHA: $TAG_HASH), PROJECT: $PROJECT_NAME" + diff --git a/checker/run-checker b/checker/run-checker index 80f44ac..733df57 100755 --- a/checker/run-checker +++ b/checker/run-checker @@ -7,6 +7,7 @@ mydir=$(dirname $(realpath $0)) source $mydir/cloner-lib-general source $mydir/cloner-lib-auth source $mydir/cloner-lib-cfg +source $mydir/detector-lib-cfg # interval - in minutes @@ -46,4 +47,57 @@ fi date +"%s" > $stampfile +# if detector is not enabled, quit quietly +if ! detectorRunCapable +then + exit 0 +fi + +source $mydir/detector-lib-git + +detectorLoadConfig + +repodir=$(gen-mirror-path $repo) + +if detectorCheckFetchHead $repodir +then + # nothing changed, just die + exit 0 +fi + +# now, the fun begins! + +# first, solve commits + +# solve tags - remove nonexistent refs +find $DET_TAGS -type f | sort | while read tagname +do + tag=$(basename $tagname) + if ! git --git-dir $repodir tag -l | grep -q "^$tag$" + then + echo "Removing tag: $tag (was [$(cat $tagname)])" + rm $tagname + fi +done + +# tags that changed or was pushed as new +git --git-dir $repodir tag -l | while read tagname +do + [ -f $DET_TAGS/$tagname ] || touch $DET_TAGS/$tagname + oldsha=$(cat $DET_TAGS/$tagname) + newsha=$(git --git-dir $repodir show-ref $tagname | cut -d' ' -f1) + if ! [ "$oldsha" = "$newsha" ] + then + # TAG_HASH = $newsha + # TAG_AUTHOR + git --git-dir $repodir log $newsha -1 --pretty=format:"%an" + + fi + +done + + + +# save hash to keep things clear +#detectorSumPersist $repodir