Diff
1diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh
2index e6a4a3d6955d34bbeebc2fdf0cf7a13d0bd8eb85..157b0cce2404dd2c9d7f134a48e3272a8577b022 100644
3--- a/tools/check_for_upgrade.sh
4+++ b/tools/check_for_upgrade.sh
5@@ -32,6 +32,41 @@ function current_epoch() {
6 echo $(( EPOCHSECONDS / 60 / 60 / 24 ))
7 }
8
9+function is_update_available() {
10+ local branch
11+ branch=${"$(git -C "$ZSH" config --local oh-my-zsh.branch)":-master}
12+
13+ local remote remote_url remote_repo
14+ remote=${"$(git -C "$ZSH" config --local oh-my-zsh.remote)":-origin}
15+ remote_url=$(git -C "$ZSH" config remote.$remote.url)
16+
17+ local repo
18+ case "$remote_url" in
19+ https://github.com/*) repo=${${remote_url#https://github.com/}%.git} ;;
20+ git@github.com:*) repo=${${remote_url#git@github.com:}%.git} ;;
21+ *)
22+ # If the remote is not using GitHub we can't check for updates
23+ # Let's assume there are updates
24+ return 0 ;;
25+ esac
26+
27+ # If the remote repo is not the official one, let's assume there are updates available
28+ [[ "$repo" = ohmyzsh/ohmyzsh ]] || return 0
29+ local api_url="https://api.github.com/repos/${repo}/commits/${branch}"
30+
31+ # Get local and remote HEADs and compare them. If we can't get either assume there are updates
32+ local local_head remote_head
33+ local_head=$(git -C "$ZSH" rev-parse $branch 2>/dev/null) || return 0
34+
35+ remote_head=$(curl -fsSL -H 'Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null) \
36+ || remote_head=$(wget -O- --header='Accept: application/vnd.github.v3.sha' $api_url 2>/dev/null) \
37+ || remote_head=$(HTTP_ACCEPT='Accept: application/vnd.github.v3.sha' fetch -o - $api_url 2>/dev/null) \
38+ || return 0
39+
40+ # Compare local and remote HEADs
41+ [[ "$local_head" != "$remote_head" ]]
42+}
43+
44 function update_last_updated_file() {
45 echo "LAST_EPOCH=$(current_epoch)" >! "${ZSH_CACHE_DIR}/.zsh-update"
46 }
47@@ -69,6 +104,8 @@ function update_ohmyzsh() {
48 # the shell actually exits what it's running.
49 trap "
50 ret=\$?
51+ unset update_mode
52+ unset -f current_epoch is_update_available update_last_updated_file update_ohmyzsh 2>/dev/null
53 command rm -rf '$ZSH/log/update.lock'
54 return \$ret
55 " EXIT INT QUIT
56@@ -86,6 +123,11 @@ function update_ohmyzsh() {
57 return
58 fi
59
60+ # Check if there are updates available before proceeding
61+ if ! is_update_available; then
62+ return
63+ fi
64+
65 # Ask for confirmation before updating unless in auto mode
66 if [[ "$update_mode" = auto ]]; then
67 update_ohmyzsh
68@@ -108,4 +150,4 @@ function update_ohmyzsh() {
69 }
70
71 unset update_mode
72-unset -f current_epoch update_last_updated_file update_ohmyzsh
73+unset -f current_epoch is_update_available update_last_updated_file update_ohmyzsh