~ecs/tm

ref: 95cb618f450695312ff6849e7c6e86709e985ae9 tm/tm-insert -rwxr-xr-x 1.4 KiB
95cb618fEmber Sawady init: use commit instead of commit-tree 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
#!/bin/sh -eu

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

. "$(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='
'
	for file in $(ls -A); do
		mode="$(ls -lA | 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))"
		set +e
		hash="$(tm insert -- "$file")" && printf "%s %s %s\n" "$m" "$hash" "$file" >>"$tmp"
		set -e
	done	
	;;
commit)
	abort "TODO: implement this"
	;;
*)
	abort "invalid type"
esac
write <"$tmp"