Skip to content

Instantly share code, notes, and snippets.

@sushichan044
Last active November 24, 2025 18:10
Show Gist options
  • Select an option

  • Save sushichan044/7d18b5a8f70e538ec8b28f761a78e398 to your computer and use it in GitHub Desktop.

Select an option

Save sushichan044/7d18b5a8f70e538ec8b28f761a78e398 to your computer and use it in GitHub Desktop.
#!/usr/bin/env zsh
FETCH_INTERVAL_SECONDS=900 # 15 minutes
_should_fetch() {
# Do nothing if not inside a git repository
command git rev-parse --is-inside-work-tree &>/dev/null || return 1
local git_dir="$(realpath "$(git rev-parse --git-dir 2>/dev/null)")"
local last_fetch_file="${git_dir}/SUSHICHAN044_last_auto_fetch_time"
local current_time="$(date +%s)"
if [[ -f $last_fetch_file ]]; then
local last_fetch_time="$(cat "$last_fetch_file")"
local elapsed_seconds=$((current_time - last_fetch_time))
if ((elapsed_seconds < FETCH_INTERVAL_SECONDS)); then
return 1
fi
fi
echo "$current_time" >|"$last_fetch_file"
return 0
}
_git_auto_fetch() {
if _should_fetch; then
echo "Auto-fetching git repositories..."
command git fetch --all --prune &>/dev/null &
fi
}
# Run once after zshrc is loaded
autoload -Uz add-zsh-hook
add-zsh-hook zshrc _git_auto_fetch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment