Skip to content

Commit 2d0328e

Browse files
committed
🔧 Improve performances
1 parent f95074b commit 2d0328e

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

‎lib/function/self_updated.sh‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22

33
laptop_self_updated() {
44
local current_branch
5-
local behind_count
5+
local remote_sha
6+
local local_sha
67

78
# Homebrew install: check if a newer formula version is available
9+
# HOMEBREW_NO_AUTO_UPDATE=1 avoids slow tap updates when only checking one formula
810
if [[ -n "$LAPTOP_INSTALL_BREW_PACKAGE" ]] && brew list "$LAPTOP_INSTALL_BREW_PACKAGE" &>/dev/null; then
9-
if brew outdated --quiet "$LAPTOP_INSTALL_BREW_PACKAGE" 2>/dev/null | grep -q .; then
11+
if HOMEBREW_NO_AUTO_UPDATE=1 brew outdated --quiet "$LAPTOP_INSTALL_BREW_PACKAGE" 2>/dev/null | grep -q .; then
1012
return 1 # Outdated
1113
fi
1214
return 0 # Up-to-date
1315
fi
1416

1517
# Git install (e.g. zinit or manual clone): check if remote is ahead
18+
# Use ls-remote instead of fetch so we don't download objects on every check
1619
if [[ -d "$LAPTOP_HOME/.git" ]]; then
1720
current_branch=$(git -C "$LAPTOP_HOME" rev-parse --abbrev-ref HEAD 2>/dev/null)
21+
[[ -z "$current_branch" ]] && return 0
1822

19-
# Fetch updates from the remote
20-
git -C "$LAPTOP_HOME" --git-dir="$LAPTOP_HOME/.git" fetch origin >/dev/null 2>&1 || true
23+
remote_sha=$(git -C "$LAPTOP_HOME" ls-remote origin "$current_branch" 2>/dev/null | cut -f1)
24+
[[ -z "$remote_sha" ]] && return 0 # Can't tell, assume up-to-date
2125

22-
# Check if the working directory is outdated
23-
behind_count=$(git -C "$LAPTOP_HOME" rev-list --left-right --count HEAD...origin/"$current_branch" 2>/dev/null | awk '{print $2}')
24-
25-
# If behind_count is greater than 0, the repo is outdated
26-
if [[ "$behind_count" -gt 0 ]]; then
26+
local_sha=$(git -C "$LAPTOP_HOME" rev-parse HEAD 2>/dev/null)
27+
# Behind = local is ancestor of remote and not equal
28+
if [[ -n "$local_sha" ]] && git -C "$LAPTOP_HOME" merge-base --is-ancestor HEAD "$remote_sha" 2>/dev/null && [[ "$local_sha" != "$remote_sha" ]]; then
2729
return 1 # Outdated
2830
fi
2931
fi

0 commit comments

Comments
 (0)