git-auto-fetch.plugin.zsh 760 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` > $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

slavaGanzin's avatar
slavaGanzin committed
21
22
23
24
25
26
27
eval "original-$(declare -f zle-line-init)"

function zle-line-init () {
  git-fetch-all
  original-zle-line-init
}
zle -N zle-line-init