Speedup pass on macOS and brew

Amir Yalon quoiceehoh-20180826 at yxejamir.net
Tue Oct 23 12:24:58 CEST 2018


Subject: darwin: Avoid slow operation `brew --prefix <formula>` if possible

In order to avoid the costly extra work that `brew --prefix gnu-getopt` does
behind the scenes, try an educated guess based on `brew --prefix` first, and
fall back as necessary.

This change also falls back to /usr/local, if getopt is not installed by
Homebrew or Macports.
---

On Tue, Oct 23, 2018, at 06:54, Allan Odgaard wrote:
> The elif and else branches look wrong, as we are not in a command context, so
> shouldn’t prefix with echo, plus including the which line is redundant when
> the elif already test for the presence of port.

> I also find it cleaner to test the exit status of command instead of testing
> against non-zero output.

> Here’s my revised proposal for the relevant code to setup GETOPT:

> If we find neither brew nor port then we use GETOPT_PREFIX before falling back
> to /usr/local.

Both of your suggestions assume that if Homebrew or Macports is installed, then
getopt must be installed by that tool, which may be wrong. The original code
currently has a slightly different set of assumptions, that may also be wrong.
In my suggested patch below, it always falls back to /usr/local if it can’t find
getopt by other means.

diff --git a/src/platform/darwin.sh b/src/platform/darwin.sh
index 342ecce..d805f4c 100644
--- a/src/platform/darwin.sh
+++ b/src/platform/darwin.sh
@@ -43,6 +43,16 @@ qrcode() {
 	fi
 }
 
-GETOPT="$(brew --prefix gnu-getopt 2>/dev/null || { which port &>/dev/null && echo /opt/local; } || echo /usr/local)/bin/getopt"
+if command -v brew >/dev/null; then
+    GETOPT="$(brew --prefix 2>/dev/null)/opt/gnu-getopt/bin/getopt"
+    if ! [ -x "$GETOPT" ]; then
+        GETOPT="$(brew --prefix gnu-getopt 2>/dev/null)/bin/getopt"
+    fi
+elif command -v port >/dev/null; then
+    GETOPT=/opt/local/bin/getopt
+fi
+if ! [ -x "$GETOPT" ]; then
+    GETOPT=/usr/local/bin/getopt
+fi
 SHRED="srm -f -z"
 BASE64="openssl base64"
-- 
2.19.1



More information about the Password-Store mailing list