~ecs/tm

ab0b841cc7308454752836acaef3f94315e32a00 — Ember Sawady 3 years ago c6cc396
update-tree: initial commit
5 files changed, 77 insertions(+), 9 deletions(-)

M lib.sh
M tm-cat
M tm-resolve-path
M tm-update-ref
A tm-update-tree
M lib.sh => lib.sh +3 -3
@@ 3,9 3,9 @@ export TM_DIR="${TM_DIR:-$PWD/.tm}"
export TM_WS="${TM_WS:-$TM_DIR/..}"
export TM_IGNORE="${TM_IGNORE:-$TM_WS/.tmignore}"
TMPDIR="${TMPDIR:-/tmp}"
TMPDIR="$TMPDIR/tm.$$.$(date)"
# TODO: replace this with something POSIX
TMPDIR="$(mktemp -d "$TMPDIR/tm.XXXXXXX")"
unset CDPATH
mkdir -- "$TMPDIR"
trap "rm -rf '$TMPDIR'; exit" INT
trap "rm -rf '$TMPDIR'" EXIT



@@ 41,7 41,7 @@ postfix() {
	len="$(printf "%s" "$1" | wc -c)"
	while read -r "line"; do
		linelen="$(printf "%s" "$line" | wc -c)"
		post="$(printf "%s" "$line" | cut -c"$((linelen - len + 1))-" 2>/dev/null)"
		post="$(printf "%s" "$line" | cut -c"$((linelen - len + 1))-" 2>/dev/null || true)"
		[ "z$post" = "z$1" ] && printf "%s\n" "$line"
	done
}

M tm-cat => tm-cat +1 -1
@@ 4,5 4,5 @@
if [ $# -ne 1 ]; then
	abort "usage: tm cat <ref>"
fi
ref="$(resolve_ref "$1" || abort "error: invalid ref")"
ref="$(resolve_ref "$1" || abort "error: invalid ref $1")"
cat "$TM_DIR/objects/$ref"

M tm-resolve-path => tm-resolve-path +35 -4
@@ 1,9 1,40 @@
#!/bin/sh -eu

usage() {
	abort "usage: tm resolve-path [-f <field>] <tree> <path>"
}

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
		;;
	?)
		usage
		;;
	esac
done

shift "$((OPTIND - 1))"

. "$(dirname -- "$0")/lib.sh"
[ $# -ne 2 ] && abort "usage: tm resolve-path <tree> <path>"
ref="$(resolve_ref "$1" || abort "error: invalid ref")"
[ $# -ne 2 ] && usage
ref="$(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")")"
dirref="$(tm resolve-path -- "$1" "$(dirname -- "$2")")"
[ "z$(objtype "$dirref")" != "ztree" ] && abort "error: not a directory: $2"
postfix " $(basename -- "$2")" <"$TM_DIR/objects/$dirref" | cut -f2 -d' '
tm cat "$dirref" | postfix " $(basename -- "$2")" | cut -f"$field" -d' '

M tm-update-ref => tm-update-ref +1 -1
@@ 13,5 13,5 @@ else
	printf "creating ref %s\n" "$1"
	out="$TM_DIR/refs/$1"
fi
ref="$(resolve_ref "$2" || abort "error: invalid ref")"
ref="$(resolve_ref "$2" || abort "error: invalid ref $2")"
printf "%s\n" "$ref" >"$out"

A tm-update-tree => tm-update-tree +37 -0
@@ 0,0 1,37 @@
#!/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")"
tmp="$TMPDIR/update-tree"
dirref="$(tm resolve-path "$1" "$(dirname -- "$2")" || exit 1)"
path="$(basename -- "$2")"
new="$(resolve_ref "$3" || abort "error: invalid ref $3")"
tm cat "$dirref" | awk -v"new=$mode $new $path" -v"path=$path" '{
	if ($3 == path) {
		print new;
	} else {
		print $0;
	}
}' >"$tmp"
[ "z$2" = "z/" ] || [ "z$2" = "z." ] && printf "%s\n" "$3" && exit
exec tm update-tree "$1" "$(dirname -- "$2")" "$(write <"$tmp")"