#!/usr/bin/env bash
#
# Clean unknown files from the working tree.
# Copyright (c) Pavel Roskin, 2005
#
# Cleans file and directories that are not under version control.
# When run without arguments, files ignored by cg-status and directories
# are not removed.
#
# OPTIONS
# -------
# -d::
#	Also clean directories and their contents.
#
# -D::
#	Same as -d but try harder (change permissions first).
#
# -q::
#	Quiet - don't report what's being cleaned.
#
# -x::
#	Also clean files ignored by cg-status, such as object files.

USAGE="cg-clean [-d] [-D] [-q] [-x]"

. ${COGITO_LIB:-/usr/lib/cogito/}cg-Xlib || exit 1

noexclude=
cleandir=
cleandirhard=
quiet=
while optparse; do
	if optparse -d; then
		cleandir=1
	elif optparse -D; then
		cleandir=1
		cleandirhard=1
	elif optparse -q; then
		quiet=1
	elif optparse -x; then
		noexclude=1
	else
		optfail
	fi
done

[ "$ARGS" ] && usage


clean_dirs()
{
	dirlist=$(mktemp -t gitlsfiles.XXXXXX)
	git-ls-files --cached |
		sed -n 's|^'"$_git_relpath"'||p' |
		sed -n 's|/[^/]*$||p' |
		sort -u >"$dirlist"

	save_IFS="$IFS"
	IFS=$'\n'

	fpath=${_git_relpath-./}
	find "$fpath" -type d -print |
		sed 's|^'"$fpath"'||;/^$/d;/^\.git$/d;/^\.git\//d' |
		cat - "$dirlist" | sort -u |
		diff - "$dirlist" |
		sed -n 's/< //p' |
	for file in $(cat); do
		path="${_git_relpath}$file"
		if [ ! -d "$path" ]; then
			# Perhaps directory was removed with its parent
			continue
		fi
		if [ -z "$cleandir" ]; then
			echo "Not removing $file/"
			continue
		fi
		[ "$quiet" ] || echo "Removing $file/"
		if [ "$cleandirhard" ]; then
			chmod -R 700 "$path"
		fi
		rm -rf "$path"
		if [ -e "$path" -o -L "$path" ]; then
			echo "Cannot remove $file/"
		fi
	done

	IFS="$save_IFS"
	rm "$dirlist"
}

clean_files()
{
	xopt=
	[ "$noexclude" ] && xopt="-x"

	save_IFS="$IFS"
	IFS=$'\n'

	cg-status "$xopt" -w | sed -n 's/^? //p' |
	for file in $(cat); do
		path="${_git_relpath}$file"
		if [ -d "$path" ]; then
			# Sanity check, shouldn't happen
			echo "FATAL: cg-status reports directories (internal error)" >&2
			exit 1
		elif [ -e "$path" -o -L "$path" ]; then
			[ "$quiet" ] || echo "Removing $file"
			rm -f "$path"
			# rm would complain itself on failure
		else
			echo "File $file has disappeared!"
		fi
	done

	IFS="$save_IFS"
}


# Even if -d or -D is not specified, we want to tell user about
# directories that are not removed
if [ -z "$quiet" -o "$cleandir" ]; then
	clean_dirs
fi

clean_files
