Making custom git-remove-tree command

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I'm doing a script named git-remove-tree which removes the working tree
known to Git. It doesn't touch untracked files; it only deletes
directories if they are empty. The script seems to work, but because I'm
not very good at Git plumbing and there can be some corner cases which I
don't know about, I'd appreciate if more experienced users would have a
look. Is the following script safe?


#!/bin/sh
# git-remove-tree

is_wt=$(git rev-parse --is-inside-work-tree)
if [ "$is_wt" = false ]; then
	echo "You must run this inside a working tree."
	exit 1
elif [ -z "$is_wt" ]; then
	exit 1
fi

top=$(git rev-parse --show-toplevel)
cd "$top" || {
	echo "Can't go to top-level directory $top"
	exit 1
}

git ls-tree --name-only -r -z HEAD | xargs -0r -- sh -c '
for f in "$@"; do
	rm -f -- "$f"
	d=$(dirname -- "$f")
	[ "$d" = . ] || rmdir -p -- "$d" 2>/dev/null
done' dollar0_argument

echo 'Use "git reset --hard HEAD" to populate the working tree again.'
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]