#!/usr/bin/env bash
#
# Cancels current edits in the working tree.
# Copyright (c) Petr Baudis, 2005
#
# This script reverts the working tree to a consistent state before
# any changes to it (including merges etc) were done.
#
# Basically, this is the opposite of cg-commit in some sense.
#
# Takes no arguments and the evil changes from the tree.

. cg-Xlib

# Undo seek?
branch=
[ -s .git/blocked ] && branch=$(grep '^seeked from ' .git/blocked | sed 's/^seeked from //')
if [ "$branch" ]; then
	echo "Unseeking: $(cat .git/HEAD) -> $(cat ".git/refs/heads/$branch")"
	if [ -s ".git/refs/heads/$branch" ]; then
		rm .git/HEAD
		ln -s "refs/heads/$branch" .git/HEAD
	else
		echo "ERROR: Unknown branch $branch! Preserving HEAD." >&2
	fi
fi

rm -f .git/blocked .git/merging .git/merging-sym .git/merge-base
read-tree -m $(tree-id) || read-tree $(tree-id)

checkout-cache -f -a
update-cache --refresh
