~ecs/tm

1cc8f49c5c8bf344e0939690dd200cb52ae0e4a8 — Ember Sawady 3 years ago a0967d2
resolve-ref: initial commit

Replaces resolve_ref in lib.sh
7 files changed, 26 insertions(+), 23 deletions(-)

M lib.sh
M tm-cat
M tm-commit-tree
M tm-resolve-path
A tm-resolve-ref
M tm-update-ref
M tm-update-tree
M lib.sh => lib.sh +1 -15
@@ 9,20 9,6 @@ unset CDPATH
trap "rm -rf '$TMPDIR'; exit" INT
trap "rm -rf '$TMPDIR'" EXIT

# TODO: ref~X, ref^X
resolve_ref() {
	if [ -f "$TM_DIR/refs/$1" ]; then
		resolve_ref "$(cat "$TM_DIR/refs/$1")"
		return 0
	fi
	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
	return 1
}

write() {
	tmp="$TMPDIR/write"
	cat >"$tmp"


@@ 52,7 38,7 @@ abort() {
}

objtype() {
	ref="$(resolve_ref "$1")"
	ref="$(tm resolve-ref "$1")"
	tm cat "$ref" | head -n1
}


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 $1"
ref="$(tm resolve-ref "$1")" || abort "error: invalid ref $1"
cat "$TM_DIR/objects/$ref"

M tm-commit-tree => tm-commit-tree +4 -3
@@ 1,5 1,6 @@
#!/bin/sh -eu

. "$(dirname -- "$0")/lib.sh"
usage() {
	abort "usage: tm commit-tree [-p <parent>]... <tree>"
}


@@ 9,7 10,8 @@ parents=
while getopts p: opt; do
	case "$opt" in
	p)
		ref="$(resolve_ref "$OPTARG")" || abort "error: invalid ref $OPTARG"
		ref="$(tm resolve-ref "$OPTARG")" \
			|| abort "error: invalid ref $OPTARG"
		parents="$parents $ref"
		;;
	?)


@@ 21,7 23,6 @@ done
if [ $# -ne 1 ]; then
	usage
fi
. "$(dirname -- "$0")/lib.sh"
set +u
[ -z "$TM_AUTHOR" ] && abort "error: TM_AUTHOR unset"
set -u


@@ 30,7 31,7 @@ TM_COMMITTER="${TM_COMMITTER:-$TM_AUTHOR}"
TM_DATE="${TM_DATE:-$(date -u +'%s')}"
tmp="$TMPDIR/commit-tree"

ref="$(resolve_ref "$1")" || abort "error: invalid ref $1"
ref="$(tm resolve-ref "$1")" || abort "error: invalid ref $1"
printf "tree %s\n" "$ref" >"$tmp"
for parent in $parents; do
	printf "parent %s\n" "$parent" >>"$tmp"

M tm-resolve-path => tm-resolve-path +1 -1
@@ 33,7 33,7 @@ shift "$((OPTIND - 1))"

. "$(dirname -- "$0")/lib.sh"
[ $# -ne 2 ] && usage
ref="$(resolve_ref "$1")" || abort "error: invalid ref $1"
ref="$(tm 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")")"
[ "z$(objtype "$dirref")" != "ztree" ] && abort "error: not a directory: $2"

A tm-resolve-ref => tm-resolve-ref +16 -0
@@ 0,0 1,16 @@
#!/bin/sh -eu

. "$(dirname -- "$0")/lib.sh"
if [ $# -ne 1 ]; then
	abort "usage: tm resolve-ref <ref>"
fi

# TODO: ref~X, ref^X
if [ -f "$TM_DIR/refs/$1" ]; then
	tm resolve-ref "$(cat "$TM_DIR/refs/$1")"
elif [ "$(echo "$TM_DIR/objects/$1"* | awk '{print NF}')" -eq 1 ] \
		&& [ -f "$TM_DIR/objects/$1"* ]; then
	printf "%s\n" "$(basename "$TM_DIR/objects/$1"*)"
else
	abort "error: invalid ref $1"
fi

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

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


@@ 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 $2"
ref="$(tm resolve-ref "$2")" || abort "error: invalid ref $2"
printf "%s\n" "$ref" >"$out"

M tm-update-tree => tm-update-tree +1 -1
@@ 30,7 30,7 @@ if [ -z "$dirref" ]; then
	dirref="$(tm resolve-path "$dirref" "$(basename -- "$(dirname -- "$2")")")"
fi
path="$(basename -- "$2")"
new="$(resolve_ref "$3")" || abort "error: invalid ref $3"
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" }
	{