summaryrefslogtreecommitdiff
path: root/oh-my-zsh/plugins/git-auto-fetch
diff options
context:
space:
mode:
Diffstat (limited to 'oh-my-zsh/plugins/git-auto-fetch')
-rw-r--r--oh-my-zsh/plugins/git-auto-fetch/README.md50
-rw-r--r--oh-my-zsh/plugins/git-auto-fetch/git-auto-fetch.plugin.zsh65
2 files changed, 115 insertions, 0 deletions
diff --git a/oh-my-zsh/plugins/git-auto-fetch/README.md b/oh-my-zsh/plugins/git-auto-fetch/README.md
new file mode 100644
index 0000000..e96ab42
--- /dev/null
+++ b/oh-my-zsh/plugins/git-auto-fetch/README.md
@@ -0,0 +1,50 @@
+# Git auto-fetch
+
+Automatically fetches all changes from all remotes while you are working in a git-initialized directory.
+
+To use it, add `git-auto-fetch` to the plugins array in your zshrc file:
+
+```shell
+plugins=(... git-auto-fetch)
+```
+
+## Usage
+
+Every time the command prompt is shown all remotes will be fetched in the background. By default,
+`git-auto-fetch` will be triggered only if the last auto-fetch was done at least 60 seconds ago.
+You can change the fetch interval in your .zshrc:
+
+```sh
+GIT_AUTO_FETCH_INTERVAL=1200 # in seconds
+```
+
+A log of `git fetch --all` will be saved in `.git/FETCH_LOG`.
+
+## Toggle auto-fetch per folder
+
+If you are using a mobile connection or for any other reason you can disable git-auto-fetch
+for any folder:
+
+```shell
+$ cd to/your/project
+$ git-auto-fetch
+disabled
+$ git-auto-fetch
+enabled
+```
+
+## Caveats
+
+Automatically fetching all changes defeats the purpose of `git push --force-with-lease`,
+and makes it behave like `git push --force` in some cases. For example:
+
+Consider that you made some changes and possibly rebased some stuff, which means you'll
+need to use `--force-with-lease` to overwrite the remote history of a branch. Between the
+time when you make the changes (maybe do a `git log`) and the time when you `git push`,
+it's possible that someone else updates the branch you're working on.
+
+If `git-auto-fetch` triggers then, you'll have fetched the remote changes without knowing
+it, and even though you're running the push with `--force-with-lease`, git will overwrite
+the recent changes because you already have them in your local repository. The
+[`git push --force-with-lease` docs](https://git-scm.com/docs/git-push) talk about possible
+solutions to this problem.
diff --git a/oh-my-zsh/plugins/git-auto-fetch/git-auto-fetch.plugin.zsh b/oh-my-zsh/plugins/git-auto-fetch/git-auto-fetch.plugin.zsh
new file mode 100644
index 0000000..2df34bb
--- /dev/null
+++ b/oh-my-zsh/plugins/git-auto-fetch/git-auto-fetch.plugin.zsh
@@ -0,0 +1,65 @@
+# Default auto-fetch interval: 60 seconds
+: ${GIT_AUTO_FETCH_INTERVAL:=60}
+
+# Necessary for the git-fetch-all function
+zmodload zsh/datetime
+zmodload -F zsh/stat b:zstat # only zstat command, not stat command
+
+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 is disabled or don't have permissions
+ if [[ ! -w "$gitdir" || -f "$gitdir/NO_AUTO_FETCH" ]] ||
+ [[ -f "$gitdir/FETCH_LOG" && ! -w "$gitdir/FETCH_LOG" ]]; 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)
+ date -R &>! "$gitdir/FETCH_LOG"
+ GIT_SSH_COMMAND="command ssh -o BatchMode=yes" \
+ GIT_TERMINAL_PROMPT=0 \
+ 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