summaryrefslogtreecommitdiffstats
path: root/git-base.sh
diff options
context:
space:
mode:
authormichaeldfallen <michaeldfallen@gmail.com>2015-02-05 23:30:32 +0000
committermichaeldfallen <michaeldfallen@gmail.com>2015-02-05 23:30:32 +0000
commitb3b64a3fbdd6545610da51a36179dae164fae2a3 (patch)
treefcc52633765b5d5aebdd37ac809391c802223adb /git-base.sh
downloadgit-sonar-b3b64a3fbdd6545610da51a36179dae164fae2a3.tar.gz
git-sonar-b3b64a3fbdd6545610da51a36179dae164fae2a3.zip
orchestrate async fetching
Diffstat (limited to 'git-base.sh')
-rwxr-xr-xgit-base.sh65
1 files changed, 65 insertions, 0 deletions
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"
+}