~ecs/tm

47fd666bebe7f543f91ab221aba076b0844030a9 — Ember Sawady 3 years ago 09f2dc3
Fix handling of ambiguous ref prefixes
5 files changed, 17 insertions(+), 19 deletions(-)

M lib.sh
M tm-cat
M tm-init
M tm-insert
M tm-update-ref
M lib.sh => lib.sh +7 -3
@@ 16,9 16,8 @@ resolve_ref() {
		resolve_ref "$(cat "$TM_DIR/refs/$1")"
		return 0
	fi
	if [ -f "$TM_DIR/objects/$1"* ] \
			&& [ "$(printf "$TM_DIR/objects/$1"* \
			| awk '{print NF}')" -eq 1 ]; then
	if [ "$(echo "$TM_DIR/objects/$1"* | awk '{print NF}')" -eq 1 ] \
			&& [ -f "$TM_DIR/objects/$1"* ]; then
		printf "%s\n" "$(basename "$TM_DIR/objects/$1"*)"
		return 0
	fi


@@ 49,3 48,8 @@ postfix() {
		[ "z$post" = "z$1" ] && printf "%s\n" "$line"
	done
}

abort() {
	printf "%s\n" "$@" >&2
	exit 1
}

M tm-cat => tm-cat +3 -3
@@ 1,8 1,8 @@
#!/bin/sh -eu

if [ $# -ne 1 ]; then
	echo "usage: tm cat <ref>"
	exit 1
	abort "usage: tm cat <ref>"
fi
. "$(dirname -- "$0")/lib.sh"
cat "$TM_DIR/objects/$(resolve_ref "$1" || echo "error: invalid ref" >&2 || exit 1)"
ref="$(resolve_ref "$1" || abort "error: invalid ref")"
cat "$TM_DIR/objects/$ref"

M tm-init => tm-init +1 -2
@@ 1,8 1,7 @@
#!/bin/sh -eu

if [ $# -ne 0 ]; then
	echo "usage: tm init"
	exit 1
	abort "usage: tm init"
fi
. "$(dirname -- "$0")/lib.sh"
mkdir -p -- "$TM_DIR/objects" "$TM_DIR/refs"

M tm-insert => tm-insert +4 -8
@@ 1,8 1,7 @@
#!/bin/sh -eu

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

. "$(dirname -- "$0")/lib.sh"


@@ 40,8 39,7 @@ tree)
		mode="$(ls -lA | postfix " $file" | cut -f1 -d' ')"
		case "$(ch 1 "$mode")" in
		b|c|l|p)
			printf "error: non-regular file %s\n" "$file"
			exit
			abort "error: non-regular file %s\n" "$file"
		esac
		m=0
		[ "z$(ch 2 "$mode")" = "zr" ] && m="$((m + 400))"


@@ 58,11 56,9 @@ tree)
	done	
	;;
commit)
	echo "TODO: implement this" >&2
	exit 1
	abort "TODO: implement this"
	;;
*)
	echo "invalid type" >&2
	exit 1
	abort "invalid type"
esac
write <"$tmp"

M tm-update-ref => tm-update-ref +2 -3
@@ 1,8 1,7 @@
#!/bin/sh -eu

if [ $# -ne 2 ]; then
	echo "usage: tm update-ref <refname> <ref>"
	exit 1
	abort "usage: tm update-ref <refname> <ref>"
fi
. "$(dirname -- "$0")/lib.sh"
out=""


@@ 14,5 13,5 @@ else
	printf "creating ref %s\n" "$1"
	out="$TM_DIR/refs/$1"
fi
ref="$(resolve_ref "$2" || echo "error: invalid ref" >&2 || exit 1)"
ref="$(resolve_ref "$2" || abort "error: invalid ref")"
printf "%s\n" "$ref" >"$out"