fix(git-auto-fetch): background `git-fetch-all` and other fixes (#9468)
parent
d88887195f
commit
05e2956dc6
2 changed files with 84 additions and 40 deletions
@ -1,36 +1,61 @@ |
|||||||
GIT_AUTO_FETCH_INTERVAL=${GIT_AUTO_FETCH_INTERVAL:=60} |
# 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 { |
function git-fetch-all { |
||||||
(`command git rev-parse --is-inside-work-tree 2>/dev/null` && |
( |
||||||
dir=`command git rev-parse --git-dir` && |
# Get git root directory |
||||||
[[ ! -f $dir/NO_AUTO_FETCH ]] && |
if ! gitdir="$(command git rev-parse --git-dir 2>/dev/null)"; then |
||||||
(( `date +%s` - `date -r $dir/FETCH_LOG +%s 2>/dev/null || echo 0` > $GIT_AUTO_FETCH_INTERVAL )) && |
return 0 |
||||||
GIT_SSH_COMMAND="command ssh -o BatchMode=yes" \ |
fi |
||||||
command git fetch --all 2>/dev/null &>! $dir/FETCH_LOG &) |
|
||||||
|
# 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 { |
function git-auto-fetch { |
||||||
`command git rev-parse --is-inside-work-tree 2>/dev/null` || return |
# Do nothing if not in a git repository |
||||||
guard="`command git rev-parse --git-dir`/NO_AUTO_FETCH" |
command git rev-parse --is-inside-work-tree &>/dev/null || return 0 |
||||||
|
|
||||||
(rm $guard 2>/dev/null && |
# Remove or create guard file depending on its existence |
||||||
echo "${fg_bold[green]}enabled${reset_color}") || |
local guard="$(command git rev-parse --git-dir)/NO_AUTO_FETCH" |
||||||
(touch $guard && |
if [[ -f "$guard" ]]; then |
||||||
echo "${fg_bold[red]}disabled${reset_color}") |
command rm "$guard" && echo "${fg_bold[green]}enabled${reset_color}" |
||||||
|
else |
||||||
|
command touch "$guard" && echo "${fg_bold[red]}disabled${reset_color}" |
||||||
|
fi |
||||||
} |
} |
||||||
|
|
||||||
# Override zle-line-init if it exists |
# zle-line-init widget (don't redefine if already defined) |
||||||
if (( $+functions[zle-line-init] )); then |
(( ! ${+functions[_git-auto-fetch_zle-line-init]} )) || return 0 |
||||||
eval "override-git-auto-fetch-$(declare -f zle-line-init)" |
|
||||||
|
case "$widgets[zle-line-init]" in |
||||||
function zle-line-init () { |
# Simply define the function if zle-line-init doesn't yet exist |
||||||
git-fetch-all |
builtin|"") function _git-auto-fetch_zle-line-init() { |
||||||
override-git-auto-fetch-zle-line-init |
git-fetch-all |
||||||
} |
} ;; |
||||||
else |
# Override the current zle-line-init widget, calling the old one |
||||||
function zle-line-init () { |
user:*) zle -N _git-auto-fetch_orig_zle-line-init "${widgets[zle-line-init]#user:}" |
||||||
git-fetch-all |
function _git-auto-fetch_zle-line-init() { |
||||||
} |
git-fetch-all |
||||||
fi |
zle _git-auto-fetch_orig_zle-line-init -- "$@" |
||||||
|
} ;; |
||||||
zle -N zle-line-init |
esac |
||||||
|
|
||||||
|
zle -N zle-line-init _git-auto-fetch_zle-line-init |
||||||
|
|||||||
Loading…
Reference in new issue