~ecs/tm

ref: 0022137d89f12741ac3e6cf4173aa876cda5a726 tm/tm-resolve-path -rwxr-xr-x 936 bytes
0022137dEmber Sawady Fix shellcheck warnings 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/sh -eu

# shellcheck source=./lib.sh
. "$(dirname -- "$0")/lib.sh"

field=2
while getopts f: opt; do
	case "$opt" in
	f)
		case "$OPTARG" in
		1|mode)
			field=1;
			;;
		2|hash)
			field=2;
			;;
		3|path)
			field=3;
			;;
		*)
			abort "invalid field, must be one of mode, hash, or path"
			;;
		esac
		;;
	?)
		abort "usage: tm resolve-path [-f <field>] <tree> <path>"
		;;
	esac
done

shift "$((OPTIND - 1))"

[ $# -ne 2 ] &&  abort "usage: tm resolve-path [-f <field>] <tree> <path>"
ref="$(tm resolve-ref "$1")" || abort "error: invalid ref $1"
[ "z$2" = "z/" ] || [ "z$2" = "z." ] && printf "%s\n" "$ref" && exit
dirref="$(tm resolve-path -- "$1" "$(dirname -- "$2")")"
[ "z$(tm object-type "$dirref")" != "ztree" ] && abort "error: not a directory: $2"
line="$(tm cat "$dirref" | postfix " $(basename -- "$2")" | cut -f"$field" -d' ' | head -n1)"
[ -z "$line" ] && abort "error: file $2 not found"
printf "%s\n" "$line"