git-auto-fetch.plugin.zsh 812 Bytes
Newer Older
slavaGanzin's avatar
slavaGanzin committed
1
2
3
GIT_AUTO_FETCH_INTERVAL=${GIT_AUTO_FETCH_INTERVAL:=60}

function git-fetch-all {
slavaGanzin's avatar
slavaGanzin committed
4
5
6
  (`git rev-parse --is-inside-work-tree 2>/dev/null` &&
  dir=`git rev-parse --git-dir` &&
  [[ ! -f $dir/NO_AUTO_FETCH ]] &&
slavaGanzin's avatar
slavaGanzin committed
7
  (( `date +%s` - `date -r $dir/FETCH_LOG +%s 2>/dev/null || echo 0` > $GIT_AUTO_FETCH_INTERVAL )) &&
slavaGanzin's avatar
slavaGanzin committed
8
  git fetch --all &>! $dir/FETCH_LOG &)
slavaGanzin's avatar
slavaGanzin committed
9
10
11
}

function git-auto-fetch {
slavaGanzin's avatar
slavaGanzin committed
12
13
14
15
16
17
18
  `git rev-parse --is-inside-work-tree 2>/dev/null` || return
  guard="`git rev-parse --git-dir`/NO_AUTO_FETCH"

  (rm $guard 2>/dev/null &&
    echo "${fg_bold[green]}enabled${reset_color}") ||
  (touch $guard &&
    echo "${fg_bold[red]}disabled${reset_color}")
19
}
slavaGanzin's avatar
slavaGanzin committed
20

21
eval "override-git-auto-fetch-$(declare -f zle-line-init)"
slavaGanzin's avatar
slavaGanzin committed
22
23
24

function zle-line-init () {
  git-fetch-all
25
  override-git-auto-fetch-zle-line-init
slavaGanzin's avatar
slavaGanzin committed
26
27
}
zle -N zle-line-init