From b3b64a3fbdd6545610da51a36179dae164fae2a3 Mon Sep 17 00:00:00 2001 From: michaeldfallen Date: Thu, 5 Feb 2015 23:30:32 +0000 Subject: orchestrate async fetching --- git-base.sh | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.sh | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100755 git-base.sh create mode 100755 test.sh diff --git a/git-base.sh b/git-base.sh new file mode 100755 index 0000000..d0f349d --- /dev/null +++ b/git-base.sh @@ -0,0 +1,65 @@ +set -e + +debug_print() { + debug=$1 + message=$2 + if [[ $debug == "debug" ]]; then + echo $message + fi +} + +dot_git() { + if [ -d .git ]; then + echo ".git" + else + echo "$(git rev-parse --git-dir)" + fi +} + +git_root() { + if [ -d .git ]; then + echo "." + else + echo "$(git rev-parse --show-toplevel)" + fi +} + +record_timestamp() { + touch "$(dot_git)/lastupdatetime" +} + +timestamp() { + echo "$(stat -f%m "$(dot_git)/lastupdatetime")" +} + +time_now() { + echo "$(date +%s)" +} + +time_to_update() { + timesincelastupdate="$(($(time_now) - $(timestamp)))" + fiveminutes="$((5 * 60))" + if (( "$timesincelastupdate" > "$5minutes" )); then + # time to update return 0 (which is false) + return 0 + else + # not time to update return 1 (which is true) + return 1 + fi +} + +fetch_async() { + debug="$1" + if time_to_update; then + debug_print $debug "Starting fetch" + fetch $debug & + else + debug_print $debug "Didn't fetch" + fi +} + +fetch() { + debug="$1" + git fetch + debug_print $debug "Finished fetch" +} diff --git a/test.sh b/test.sh new file mode 100755 index 0000000..bb8170d --- /dev/null +++ b/test.sh @@ -0,0 +1,38 @@ +set -e + +scriptDir="$( dirname "$0" )" + +source "$scriptDir/git-base.sh" + +echo "\nTest: Root of this git repo" +echo "$(git_root)" + +echo "\nTest: Location of .git" +echo "$(dot_git)" + +echo "\nTest: Record the timestamp" +record_timestamp +echo "Timestamp = $(timestamp)" +echo "Time now = $(time_now)" + +echo "\nTest: Time to update when just recorded" +if time_to_update; then + echo "time to update" +else + echo "not time yet" +fi + +echo "\nTest: Don't fetch if it's not time to update" +fetch_async "debug" + +echo "\nTest: Time to update when timestamp 5 mins ago" +touch -A "-010000" "$(dot_git)/lastupdatetime" +if time_to_update; then + echo "time to update" +else + echo "not time yet" +fi + +echo "\nTest: Do a non-blocking git fetch" +fetch_async "debug" +echo "Did I block?" -- cgit v1.2.3