mirror of
https://github.com/akelge/zsh
synced 2026-02-24 03:10:47 +00:00
Changed async git fetch
This commit is contained in:
@ -22,20 +22,58 @@ autoload -Uz async && async
|
|||||||
# Async Git update
|
# Async Git update
|
||||||
autoload -Uz vcs_info
|
autoload -Uz vcs_info
|
||||||
|
|
||||||
_vbe_vcs_info_done() {
|
# 2. Callback: cosa fare quando il fetch finisce
|
||||||
local stdout=$3
|
git_callback() {
|
||||||
vcs_info_msg_0_=$stdout
|
# Aggiorna il prompt solo dopo che il fetch è terminato
|
||||||
# echo $(date +"%Y-%m-%d %H:%M:%S") $PWD info $vcs_info_msg_0_ $vcs_info_msg_1_ >> ~/vcs.log
|
# Ricarica vcs_info o il tuo sistema di prompt
|
||||||
zle reset-prompt
|
# vcs_info 2>/dev/null
|
||||||
|
zle reset-prompt
|
||||||
}
|
}
|
||||||
|
|
||||||
_vbe_vcs_precmd() {
|
# 3. Funzione di inizializzazione worker
|
||||||
# echo $(date +"%Y-%m-%d %H:%M:%S") $PWD pre >> ~/vcs.log
|
init_vcs_worker() {
|
||||||
async_flush_jobs vcs_info
|
# -n: crea solo se non esiste
|
||||||
async_job vcs_info _vbe_vcs_info $PWD
|
# -u: permette l'invio di job unici (evita sovrapposizioni)
|
||||||
|
async_start_worker vcs_info -u -n
|
||||||
|
async_register_callback vcs_info git_callback
|
||||||
}
|
}
|
||||||
|
|
||||||
async_init
|
# 2. Funzione per verificare se è necessario il fetch
|
||||||
async_start_worker vcs_info
|
should_fetch() {
|
||||||
async_register_callback vcs_info _vbe_vcs_info_done
|
local last_fetch_file=".git/FETCH_HEAD"
|
||||||
add-zsh-hook precmd _vbe_vcs_precmd
|
|
||||||
|
# Se il file non esiste (mai fatto fetch), procedi
|
||||||
|
[[ ! -f "$last_fetch_file" ]] && return 0
|
||||||
|
|
||||||
|
# Calcola la differenza in secondi (5 min = 300 sec)
|
||||||
|
local last_mod=$(date -r "$last_fetch_file" +%s)
|
||||||
|
local now=$(date +%s)
|
||||||
|
local diff=$(( now - last_mod ))
|
||||||
|
local threshold=$(( FETCH_INTERVAL_MIN * 60 ))
|
||||||
|
|
||||||
|
if [[ $diff -gt $threshold ]]; then
|
||||||
|
return 0 # È passato abbastanza tempo
|
||||||
|
else
|
||||||
|
return 1 # Troppo presto
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 3. Funzione Core aggiornata
|
||||||
|
check_git_status_async() {
|
||||||
|
# Verifica se siamo in un git repo
|
||||||
|
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||||
|
|
||||||
|
# Controlla il timer prima di stressare il worker
|
||||||
|
if should_fetch; then
|
||||||
|
# Se il worker è morto, lo riavviamo
|
||||||
|
if ! async_job vcs_info "git fetch --quiet" 2>/dev/null; then
|
||||||
|
init_vcs_worker
|
||||||
|
async_job vcs_info "git fetch --quiet"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# 5. Hook: Esegui la funzione ogni volta che cambi directory
|
||||||
|
autoload -Uz add-zsh-hook
|
||||||
|
add-zsh-hook chpwd check_git_status_async
|
||||||
|
|||||||
Reference in New Issue
Block a user