56cfcb44e7ff730c10c5a00b58f38c33c984c52e

Author
Marc Cornellà <marc@mcornella.com>
Committer
Marc Cornellà <marc@mcornella.com>
Date

Message

fix(updater): abort update if `$ZSH` is not a git repository

Fixes #12298

Diff

 1diff --git a/lib/cli.zsh b/lib/cli.zsh
 2index aa36a6ab57ab4d842a2bdab8020258d1538061fc..4a8d4d12785a647c48081702816675716758c772 100644
 3--- a/lib/cli.zsh
 4+++ b/lib/cli.zsh
 5@@ -773,7 +773,17 @@ function _omz::theme::use {
 6 }
 7 
 8 function _omz::update {
 9-  local last_commit=$(builtin cd -q "$ZSH"; git rev-parse HEAD)
10+  # Check if git command is available
11+  (( $+commands[git] )) || {
12+    _omz::log error "git is not installed. Aborting..."
13+    return 1
14+  }
15+
16+  local last_commit=$(builtin cd -q "$ZSH"; git rev-parse HEAD 2>/dev/null)
17+  [[ $? -eq 0 ]] || {
18+    _omz::log error "\`$ZSH\` is not a git directory. Aborting..."
19+    return 1
20+  }
21 
22   # Run update script
23   zstyle -s ':omz:update' verbose verbose_mode || verbose_mode=default
24diff --git a/tools/check_for_upgrade.sh b/tools/check_for_upgrade.sh
25index 1cc193bde045bdb911570b8f8ceb41c4601a5d1b..a5d2112f650c6ec99437a1932b4dc294cb62dba9 100644
26--- a/tools/check_for_upgrade.sh
27+++ b/tools/check_for_upgrade.sh
28@@ -20,14 +20,16 @@ zstyle -s ':omz:update' mode update_mode || {
29 }
30 
31 # Cancel update if:
32-# - the automatic update is disabled.
33-# - the current user doesn't have write permissions nor owns the $ZSH directory.
34+# - the automatic update is disabled
35+# - the current user doesn't have write permissions nor owns the $ZSH directory
36 # - is not run from a tty
37-# - git is unavailable on the system.
38+# - git is unavailable on the system
39+# - $ZSH is not a git repository
40 if [[ "$update_mode" = disabled ]] \
41    || [[ ! -w "$ZSH" || ! -O "$ZSH" ]] \
42    || [[ ! -t 1 ]] \
43-   || ! command git --version 2>&1 >/dev/null; then
44+   || ! command git --version 2>&1 >/dev/null
45+   || (builtin cd -q "$ZSH"; ! command git rev-parse --is-inside-work-tree &>/dev/null); then
46   unset update_mode
47   return
48 fi