[PATCH] Ensure compatibility with tree 2.0.

Johannes Altmanninger aclopte at gmail.com
Sun Jan 16 17:46:49 UTC 2022


On Sun, Jan 16, 2022 at 05:50:28PM +0100, Marius Bakke wrote:
> Tree 2.0 and later will unconditionally ignore all options and write
> JSON data on file descriptor 3 when available, which causes problems
> for the test harness and other scripts that use FD 3.  Work around by
> always redirecting descriptor 3 to a temporary fd when invoking 'tree'.
> ---
>  src/password-store.sh | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> (previous email sent from the wrong address, trying again ...)
> 
> diff --git a/src/password-store.sh b/src/password-store.sh
> index aef8d72..a6d8469 100755
> --- a/src/password-store.sh
> +++ b/src/password-store.sh
> @@ -402,7 +402,13 @@ cmd_show() {
>  		else
>  			echo "${path%\/}"
>  		fi
> -		tree -N -C -l --noreport "$PREFIX/$path" | tail -n +2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' # remove .gpg at end of line, but keep colors
> +		# 'tree' 2.0 and later writes JSON output on fd3 when present,
> +		# so allocate a temporary fd and redirect file descriptor 3 to
> +		# ensure we get the stdout in case fd 3 happens to exist.
> +		tree -N -C -l --noreport "$PREFIX/$path" {tmp_fd}>&3- \

Looks good. I wonder if there is a difference between {varname}>&3- and 3>&-
I wasn't familiar with Bash's {varname}>&3- syntax.  It looked tricky at
first; I guess it's just a contraction of {varname}>&3 3>&-

Looks like the {varname} bit allows to keep a FD open across commands; but
in this case it's closed immediately, so I don't know if it makes a difference.

> +			| tail -n +2 \
> +			| sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g' \
> +			# remove .gpg at end of line, but keep colors

I'd write it with trailing pipes (also helps the comment)

	tree -N -C -l --noreport "$PREFIX/$path" {tmp_fd}>&3- |
		tail -n +2 |
		# remove .gpg at end of line, but keep colors
		sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'

>  	elif [[ -z $path ]]; then
>  		die "Error: password store is empty. Try \"pass init\"."
>  	else
> @@ -414,7 +420,10 @@ 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" | tail -n +2 | sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
> +	tree -N -C -l --noreport -P "${terms%|*}" --prune --matchdirs \
> +		--ignore-case "$PREFIX" {tmp_fd}>&3- \
> +		| tail -n +2 \
> +		| sed -E 's/\.gpg(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
>  }
>  
>  cmd_grep() {
> -- 
> 2.34.0
> 


More information about the Password-Store mailing list