summaryrefslogtreecommitdiff
path: root/plugins/git-auto-fetch/git-auto-fetch.plugin.zsh
blob: 0da84f2f5addf7cc1bb95092296e06dd2775294d (plain)
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
# Default auto-fetch interval: 60 seconds
: ${GIT_AUTO_FETCH_INTERVAL:=60}

# Necessary for the git-fetch-all function
zmodload zsh/datetime zsh/stat

function git-fetch-all {
  (
    # Get git root directory
    if ! gitdir="$(command git rev-parse --git-dir 2>/dev/null)"; then
      return 0
    fi

    # Do nothing if auto-fetch disabled
    if [[ -z "$gitdir" || -f "$gitdir/NO_AUTO_FETCH" ]]; then
      return 0
    fi

    # Get time (seconds) when auto-fetch was last run
    lastrun="$(zstat +mtime "$gitdir/FETCH_LOG" 2>/dev/null || echo 0)"
    # Do nothing if not enough time has passed since last auto-fetch
    if (( EPOCHSECONDS - lastrun < $GIT_AUTO_FETCH_INTERVAL )); then
      return 0
    fi

    # Fetch all remotes (avoid ssh passphrase prompt)
    GIT_SSH_COMMAND="command ssh -o BatchMode=yes" \
      command git fetch --all 2>/dev/null &>! "$gitdir/FETCH_LOG"
  ) &|
}

function git-auto-fetch {
  # Do nothing if not in a git repository
  command git rev-parse --is-inside-work-tree &>/dev/null || return 0

  # Remove or create guard file depending on its existence
  local guard="$(command git rev-parse --git-dir)/NO_AUTO_FETCH"
  if [[ -f "$guard" ]]; then
    command rm "$guard" && echo "${fg_bold[green]}enabled${reset_color}"
  else
    command touch "$guard" && echo "${fg_bold[red]}disabled${reset_color}"
  fi
}

# zle-line-init widget (don't redefine if already defined)
(( ! ${+functions[_git-auto-fetch_zle-line-init]} )) || return 0

case "$widgets[zle-line-init]" in
  # Simply define the function if zle-line-init doesn't yet exist
  builtin|"") function _git-auto-fetch_zle-line-init() {
      git-fetch-all
    } ;;
  # Override the current zle-line-init widget, calling the old one
  user:*) zle -N _git-auto-fetch_orig_zle-line-init "${widgets[zle-line-init]#user:}"
    function _git-auto-fetch_zle-line-init() {
      git-fetch-all
      zle _git-auto-fetch_orig_zle-line-init -- "$@"
    } ;;
esac

zle -N zle-line-init _git-auto-fetch_zle-line-init