~ecs/tm

ref: 0022137d89f12741ac3e6cf4173aa876cda5a726 tm/tm-insert -rwxr-xr-x 1.7 KiB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/sh -eu

usage() {
	abort "usage: tm insert [-t <type>] [<file>|<directory>]"
}

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

while getopts t: opt; do
	case "$opt" in
		t)
			type="$OPTARG"
			;;
		?)
			usage
			;;
	esac
done

shift "$((OPTIND - 1))"

[ $# -gt 1 ] && usage

isignored "$1" && exit 1

[ $# -eq 1 ] && [ -d "$1" ] && [ "z$type" = "zdefault" ] && type=tree
[ "z$type" = "zdefault" ] && type=blob

tmp="$TMPDIR/insert"
printf "%s\n" "$type" >"$tmp"
case "$type" in
blob)
	cat -- "$@" >>"$tmp"
	;;
tree)
	[ $# -eq 1 ] && cd "$1"
	IFS='
'
	# TODO: replace this with a find -type d followed by a find -type f
	for file in ..?* .[!.]* *; do
		[ ! -e "$file" ] && continue
		nf="$((8 + $(printf "%s" "$file" | awk '{print NF}')))"
		# We need to use ls -l to get the file mode; the only other way to get
		# it is with pax, and pax isn't even packaged on Alpine
		# shellcheck disable=SC2012
		mode="$(ls -lA | awk -v nf="$nf" '{if (NF = nf) print $0}' | postfix " $file" | cut -f1 -d' ')"
		case "$(ch 1 "$mode")" in
		b|c|l|p)
			abort "error: non-regular file %s\n" "$file"
		esac
		m=0
		[ "z$(ch 2 "$mode")" = "zr" ] && m="$((m + 400))"
		[ "z$(ch 3 "$mode")" = "zw" ] && m="$((m + 200))"
		[ "z$(ch 4 "$mode")" = "zx" ] && m="$((m + 100))"
		[ "z$(ch 5 "$mode")" = "zr" ] && m="$((m + 40))"
		[ "z$(ch 6 "$mode")" = "zw" ] && m="$((m + 20))"
		[ "z$(ch 7 "$mode")" = "zx" ] && m="$((m + 10))"
		[ "z$(ch 8 "$mode")" = "zr" ] && m="$((m + 4))"
		[ "z$(ch 9 "$mode")" = "zw" ] && m="$((m + 2))"
		[ "z$(ch 10 "$mode")" = "zx" ] && m="$((m + 1))"
		hash="$(tm insert -- "$file")" || true \
			&& printf "%s %s %s\n" "$m" "$hash" "$file" >>"$tmp"
	done	
	;;
commit)
	abort "TODO: implement this"
	;;
*)
	abort "invalid type"
esac
write <"$tmp"