05e2956dc61198d4767b96d97c5d10c93cedd6e3

Author
Marc Cornellà <marc.cornella@live.com>
Committer
GitHub <noreply@github.com>
Date

Message

fix(git-auto-fetch): background `git-fetch-all` and other fixes (#9468)

Diff

  1diff --git a/plugins/git-auto-fetch/README.md b/plugins/git-auto-fetch/README.md
  2index 35f3c2f716f0482f149556f4e8241386b6e61faf..e96ab42a3c77238e3ec3db3342a702449877c04e 100644
  3--- a/plugins/git-auto-fetch/README.md
  4+++ b/plugins/git-auto-fetch/README.md
  5@@ -1,26 +1,29 @@
  6 # Git auto-fetch
  7 
  8-Automatically fetches all changes from all remotes while you are working in git-initialized directory.
  9+Automatically fetches all changes from all remotes while you are working in a git-initialized directory.
 10 
 11-#### Usage
 12-
 13-Add `git-auto-fetch` to the plugins array in your zshrc file:
 14+To use it, add `git-auto-fetch` to the plugins array in your zshrc file:
 15 
 16 ```shell
 17 plugins=(... git-auto-fetch)
 18 ```
 19 
 20-Every time you launch a command in your shell all remotes will be fetched in background.
 21-By default autofetch will be triggered only if last fetch was done at least 60 seconds ago.
 22-You can change fetch interval in your .zshrc:
 23-```
 24-GIT_AUTO_FETCH_INTERVAL=1200 #in seconds
 25+## Usage
 26+
 27+Every time the command prompt is shown all remotes will be fetched in the background. By default,
 28+`git-auto-fetch` will be triggered only if the last auto-fetch was done at least 60 seconds ago.
 29+You can change the fetch interval in your .zshrc:
 30+
 31+```sh
 32+GIT_AUTO_FETCH_INTERVAL=1200 # in seconds
 33 ```
 34-Log of `git fetch --all` will be saved into `.git/FETCH_LOG`
 35 
 36+A log of `git fetch --all` will be saved in `.git/FETCH_LOG`.
 37+
 38+## Toggle auto-fetch per folder
 39 
 40-#### Toggle auto fetch per folder
 41-If you are using mobile connection or for any other reason you can disable git-auto-fetch for any folder:
 42+If you are using a mobile connection or for any other reason you can disable git-auto-fetch
 43+for any folder:
 44 
 45 ```shell
 46 $ cd to/your/project
 47@@ -29,3 +32,19 @@ disabled
 48 $ git-auto-fetch
 49 enabled
 50 ```
 51+
 52+## Caveats
 53+
 54+Automatically fetching all changes defeats the purpose of `git push --force-with-lease`,
 55+and makes it behave like `git push --force` in some cases. For example:
 56+
 57+Consider that you made some changes and possibly rebased some stuff, which means you'll
 58+need to use `--force-with-lease` to overwrite the remote history of a branch. Between the
 59+time when you make the changes (maybe do a `git log`) and the time when you `git push`,
 60+it's possible that someone else updates the branch you're working on.
 61+
 62+If `git-auto-fetch` triggers then, you'll have fetched the remote changes without knowing
 63+it, and even though you're running the push with `--force-with-lease`, git will overwrite
 64+the recent changes because you already have them in your local repository. The
 65+[`git push --force-with-lease` docs](https://git-scm.com/docs/git-push) talk about possible
 66+solutions to this problem.
 67diff --git a/plugins/git-auto-fetch/git-auto-fetch.plugin.zsh b/plugins/git-auto-fetch/git-auto-fetch.plugin.zsh
 68index 5c42c21a78b14bc05b273513194beb1b1118c697..0da84f2f5addf7cc1bb95092296e06dd2775294d 100644
 69--- a/plugins/git-auto-fetch/git-auto-fetch.plugin.zsh
 70+++ b/plugins/git-auto-fetch/git-auto-fetch.plugin.zsh
 71@@ -1,36 +1,61 @@
 72-GIT_AUTO_FETCH_INTERVAL=${GIT_AUTO_FETCH_INTERVAL:=60}
 73+# Default auto-fetch interval: 60 seconds
 74+: ${GIT_AUTO_FETCH_INTERVAL:=60}
 75+
 76+# Necessary for the git-fetch-all function
 77+zmodload zsh/datetime zsh/stat
 78 
 79 function git-fetch-all {
 80-  (`command git rev-parse --is-inside-work-tree 2>/dev/null` &&
 81-  dir=`command git rev-parse --git-dir` &&
 82-  [[ ! -f $dir/NO_AUTO_FETCH ]] &&
 83-  (( `date +%s` - `date -r $dir/FETCH_LOG +%s 2>/dev/null || echo 0` > $GIT_AUTO_FETCH_INTERVAL )) &&
 84-  GIT_SSH_COMMAND="command ssh -o BatchMode=yes" \
 85-    command git fetch --all 2>/dev/null &>! $dir/FETCH_LOG &)
 86+  (
 87+    # Get git root directory
 88+    if ! gitdir="$(command git rev-parse --git-dir 2>/dev/null)"; then
 89+      return 0
 90+    fi
 91+
 92+    # Do nothing if auto-fetch disabled
 93+    if [[ -z "$gitdir" || -f "$gitdir/NO_AUTO_FETCH" ]]; then
 94+      return 0
 95+    fi
 96+
 97+    # Get time (seconds) when auto-fetch was last run
 98+    lastrun="$(zstat +mtime "$gitdir/FETCH_LOG" 2>/dev/null || echo 0)"
 99+    # Do nothing if not enough time has passed since last auto-fetch
100+    if (( EPOCHSECONDS - lastrun < $GIT_AUTO_FETCH_INTERVAL )); then
101+      return 0
102+    fi
103+
104+    # Fetch all remotes (avoid ssh passphrase prompt)
105+    GIT_SSH_COMMAND="command ssh -o BatchMode=yes" \
106+      command git fetch --all 2>/dev/null &>! "$gitdir/FETCH_LOG"
107+  ) &|
108 }
109 
110 function git-auto-fetch {
111-  `command git rev-parse --is-inside-work-tree 2>/dev/null` || return
112-  guard="`command git rev-parse --git-dir`/NO_AUTO_FETCH"
113+  # Do nothing if not in a git repository
114+  command git rev-parse --is-inside-work-tree &>/dev/null || return 0
115 
116-  (rm $guard 2>/dev/null &&
117-    echo "${fg_bold[green]}enabled${reset_color}") ||
118-  (touch $guard &&
119-    echo "${fg_bold[red]}disabled${reset_color}")
120+  # Remove or create guard file depending on its existence
121+  local guard="$(command git rev-parse --git-dir)/NO_AUTO_FETCH"
122+  if [[ -f "$guard" ]]; then
123+    command rm "$guard" && echo "${fg_bold[green]}enabled${reset_color}"
124+  else
125+    command touch "$guard" && echo "${fg_bold[red]}disabled${reset_color}"
126+  fi
127 }
128 
129-# Override zle-line-init if it exists
130-if (( $+functions[zle-line-init] )); then
131-  eval "override-git-auto-fetch-$(declare -f zle-line-init)"
132-  
133-  function zle-line-init () {
134-    git-fetch-all
135-    override-git-auto-fetch-zle-line-init
136-  }
137-else
138-  function zle-line-init () {
139-    git-fetch-all
140-  }
141-fi
142-  
143-zle -N zle-line-init
144+# zle-line-init widget (don't redefine if already defined)
145+(( ! ${+functions[_git-auto-fetch_zle-line-init]} )) || return 0
146+
147+case "$widgets[zle-line-init]" in
148+  # Simply define the function if zle-line-init doesn't yet exist
149+  builtin|"") function _git-auto-fetch_zle-line-init() {
150+      git-fetch-all
151+    } ;;
152+  # Override the current zle-line-init widget, calling the old one
153+  user:*) zle -N _git-auto-fetch_orig_zle-line-init "${widgets[zle-line-init]#user:}"
154+    function _git-auto-fetch_zle-line-init() {
155+      git-fetch-all
156+      zle _git-auto-fetch_orig_zle-line-init -- "$@"
157+    } ;;
158+esac
159+
160+zle -N zle-line-init _git-auto-fetch_zle-line-init