summaryrefslogtreecommitdiffstats
path: root/git-base.sh
blob: d0f349dfa47e371707f202e6b8e46863401f5df3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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"
}