[PATCH 1/1] Add support for minimal systems without 'tree'

Trevor Vaughan peiriannydd at gmail.com
Sat Jan 20 00:03:25 UTC 2024


In minimal environments, `tree` may not exist. This
patch adds support for using `find` instead.

The `tests/setup.sh` file was also modified to detect and fail if the
`git` and `diff` commands were not present on the system running tests.

Signed-off-by: Trevor Vaughan <peiriannydd at gmail.com>
---
 src/password-store.sh | 13 +++++++++++--
 tests/t0500-find.sh   |  6 +++++-
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/password-store.sh b/src/password-store.sh
index 22e818f..a3847cf 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -402,7 +402,12 @@ cmd_show() {
         else
             echo "${path%\/}"
         fi
-        tree -N -C -l --noreport "$PREFIX/$path" 3>&- | tail -n +2 |
sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' # remove .gpg at end of
line, but keep colors
+
+        if hash -t tree >& /dev/null; then
+            tree -N -C -l --noreport "$PREFIX/$path" 3>&- | tail -n
+2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' # remove .gpg at
end of line, but keep colors
+        else
+            find "$PREFIX/$path" -type f -name "*.gpg" -exec bash -c
'x="${0##${1}/}"; x="${x%%.gpg}"; echo "${x//\// >> }" ' {} "$PREFIX"
\;
+        fi
     elif [[ -z $path ]]; then
         die "Error: password store is empty. Try \"pass init\"."
     else
@@ -414,7 +419,11 @@ cmd_find() {
     [[ $# -eq 0 ]] && die "Usage: $PROGRAM $COMMAND pass-names..."
     IFS="," eval 'echo "Search Terms: $*"'
     local terms="*$(printf '%s*|*' "$@")"
-    tree -N -C -l --noreport -P "${terms%|*}" --prune --matchdirs
--ignore-case "$PREFIX" 3>&- | tail -n +2 | sed -E
's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
+    if hash -t tree >& /dev/null; then
+        tree -N -C -l --noreport -P "${terms%|*}" --prune --matchdirs
--ignore-case "$PREFIX" 3>&- | tail -n +2 | sed -E
's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
+    else
+        find "$PREFIX/$path" -type f -ipath "${terms%|*}" -exec bash
-c 'x="${0##${1}/}"; x="${x%%.gpg}"; echo "${x//\// >> }" ' {}
"$PREFIX" \;
+    fi
 }

 cmd_grep() {
diff --git a/tests/t0500-find.sh b/tests/t0500-find.sh
index 3cf6815..50d370f 100755
--- a/tests/t0500-find.sh
+++ b/tests/t0500-find.sh
@@ -12,7 +12,11 @@ test_expect_success 'Make sure find resolves correct files' '
     "$PASS" generate Fishthings 122 &&
     "$PASS" generate Fishies/stuff 21 &&
     "$PASS" generate Fishies/otherstuff 1234 &&
-    [[ $("$PASS" find fish | sed "s/^[ \`|-]*//g;s/$(printf
\\x1b)\\[[0-9;]*[a-zA-Z]//g" | tr "\\n" -) == "Search Terms:
fish-Fish-Fishies-otherstuff-stuff-Fishthings-" ]]
+    x=$("$PASS" find fish | sed "s/^[ \`|-]*//g;s/$(printf
\\x1b)\\[[0-9;]*[a-zA-Z]//g" | tr "\\n" -) &&
+    (
+        [[ "$x" == "Search Terms:
fish-Fish-Fishies-otherstuff-stuff-Fishthings-" ]] ||
+        [[ "$x" == "Search Terms: fish-Fishies >> stuff-Fishies >>
otherstuff-Fishthings-Fish-" ]]
+    )
 '

 test_done
-- 
2.39.3


More information about the Password-Store mailing list