f761b2a7b96c04174737ff9a68127a7f89e76fea

Author
Jongwook Choi <wookayin@gmail.com>
Committer
Jongwook Choi <wookayin@gmail.com>
Date

Message

Fix stat command usage to support macOS/FreeBSD

In macOS or FreeBSD systems with the stat command different from
GNU coreutils, stat does not support the '-c' flag. To support
those systems, we can use different flag and format strings.

Diff

 1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 2index c87b1fbdc178d32cea541c6dd844941471b7ea80..57f878f02eb1d35a7bfe03120b2adb80ca2d3696 100644
 3--- a/autoswitch_virtualenv.plugin.zsh
 4+++ b/autoswitch_virtualenv.plugin.zsh
 5@@ -38,10 +38,18 @@ function _check_venv()
 6 
 7         SWITCH_TO=""
 8 
 9+        # Get the .venv file, scanning parent directories
10         venv_path=$(_check_venv_path "$PWD")
11         if [[ -n "$venv_path" ]]; then
12-          file_owner="$(stat -c %u "$venv_path")"
13-          file_permissions="$(stat -c %a "$venv_path")"
14+
15+          stat --version &> /dev/null
16+          if [[ $? -eq 0 ]]; then   # Linux, or GNU stat
17+            file_owner="$(stat -c %u "$venv_path")"
18+            file_permissions="$(stat -c %a "$venv_path")"
19+          else                      # macOS, or FreeBSD stat
20+            file_owner="$(stat -f %u "$venv_path")"
21+            file_permissions="$(stat -f %OLp "$venv_path")"
22+          fi
23 
24           if [[ "$file_owner" != "$(id -u)" ]]; then
25             echo "AUTOSWITCH WARNING: Virtualenv will not be activated"