~ecs/tm

a0967d2956ec1d4272e1f97a041fcc048468d07d — Ember Sawady 3 years ago ab0b841
update-tree: create nonexistent files/directories
5 files changed, 31 insertions(+), 15 deletions(-)

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

M tm-commit-tree => tm-commit-tree +2 -2
@@ 9,7 9,7 @@ parents=
while getopts p: opt; do
	case "$opt" in
	p)
		ref="$(resolve_ref "$OPTARG" || abort "error: invalid ref $OPTARG")"
		ref="$(resolve_ref "$OPTARG")" || abort "error: invalid ref $OPTARG"
		parents="$parents $ref"
		;;
	?)


@@ 30,7 30,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="$(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 +4 -2
@@ 33,8 33,10 @@ shift "$((OPTIND - 1))"

. "$(dirname -- "$0")/lib.sh"
[ $# -ne 2 ] && usage
ref="$(resolve_ref "$1" || abort "error: invalid ref $1")"
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")")"
[ "z$(objtype "$dirref")" != "ztree" ] && abort "error: not a directory: $2"
tm cat "$dirref" | postfix " $(basename -- "$2")" | cut -f"$field" -d' '
line="$(tm cat "$dirref" | postfix " $(basename -- "$2")" | cut -f"$field" -d' ' | head -n1)"
[ -z "$line" ] && abort "error: file $2 not found"
printf "%s\n" "$line"

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

M tm-update-tree => tm-update-tree +23 -9
@@ 21,17 21,31 @@ shift "$((OPTIND - 1))"
. "$(dirname -- "$0")/lib.sh"
[ $# -ne 3 ] && usage

[ "z$mode" = "zdefault" ] && mode="$(tm resolve-path -f mode -- "$1" "$2")"
[ "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")" || exit 1)"
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="$(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;
new="$(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;
		}
	}
}' >"$tmp"
	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")"