git-auto-fetch.plugin.zsh 979 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 )) &&
8
  git fetch --all 2>/dev/null &>! $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
22
23
24
25
26
27
28
29
30
31
32
33
34
# Override zle-line-init if it exists
if (( $+functions[zle-line-init] )); then
  eval "override-git-auto-fetch-$(declare -f zle-line-init)"
  
  function zle-line-init () {
    git-fetch-all
    override-git-auto-fetch-zle-line-init
  }
else
  function zle-line-init () {
    git-fetch-all
  }
fi
  
slavaGanzin's avatar
slavaGanzin committed
35
zle -N zle-line-init