~ecs/tm

ref: 95cb618f450695312ff6849e7c6e86709e985ae9 tm/tm-update-tree -rwxr-xr-x 1.1 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
#!/bin/sh -eu

usage() {
	abort "usage: tm update-tree [-m <mode>] <tree> <path> <new-ref>"
}

mode="default"
while getopts m: opt; do
	case "$opt" in
	m)
		mode="$OPTARG"
		;;
	?)
		usage
		;;
	esac
done

shift "$((OPTIND - 1))"

. "$(dirname -- "$0")/lib.sh"
[ $# -ne 3 ] && usage

[ "z$mode" = "zdefault" ] \
	&& mode="$(tm resolve-path -f mode -- "$1" "$2" 2>/dev/null || echo 755)"
tmp="$TMPDIR/update-tree"
dirref="$(tm resolve-path "$1" "$(dirname -- "$2")" || true)"
if [ -z "$dirref" ]; then
	dirref="$(tm update-tree "$1" "$(dirname -- "$2")" "$(echo tree | write)")"
	dirref="$(tm resolve-path "$dirref" "$(basename -- "$(dirname -- "$2")")")"
fi
path="$(basename -- "$2")"
new="$(tm resolve-ref "$3")" || abort "error: invalid ref $3"
tm cat "$dirref" | awk -v"new=$mode $new $path" -v"path=$path" '
	BEGIN { found="0" }
	{
		if ($3 == path) {
			print new;
			found="1";
		} else {
			print $0;
		}
	}
	END {
		if (found == "0") {
			print new;
		}
	}
' >"$tmp"
[ "z$2" = "z/" ] || [ "z$2" = "z." ] && printf "%s\n" "$3" && exit
exec tm update-tree "$1" "$(dirname -- "$2")" "$(write <"$tmp")"