git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Making custom git-remove-tree command

From
Johannes Sixt <j.sixt@viscovery.net>
Date
Feb 4, 2010, 09:55 UTC
Message-ID
<4B6A9985.4050909@viscovery.net>
In-Reply-To
<87iqad2x09.fsf@mithlond.arda>
Teemu Likonen schrieb:
Show 31 quoted lines
> 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
I think you should be able to reduce all of this to just
git read-tree --index-output=/tmp/empty.index -m -u \
	4b825dc642cb6eb9a060e54bf8d69288fbee4904 # empty tree
rm -f /tmp/empty.index
(with the usual caveats about temporary files).
> 
> echo 'Use "git reset --hard HEAD" to populate the working tree again.'
-- Hannes
Previous: Teemu LikonenNext: Teemu Likonen
Message 2 of 4 in “Making custom git-remove-tree command”
  1. Teemu LikonenFeb 4, 2010
  2. Johannes SixtFeb 4, 2010
  3. Teemu LikonenFeb 4, 2010
  4. Johannes SixtFeb 4, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.