git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Sixt <j.sixt@viscovery.net>
To: Teemu Likonen <tlikonen@iki.fi>
Cc: git@vger.kernel.org
Subject: Re: Making custom git-remove-tree command
Date: Thu, 04 Feb 2010 10:55:17 +0100	[thread overview]
Message-ID: <4B6A9985.4050909@viscovery.net> (raw)
In-Reply-To: <87iqad2x09.fsf@mithlond.arda>

Teemu Likonen schrieb:
> 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

  reply	other threads:[~2010-02-04  9:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-02-04  9:03 Making custom git-remove-tree command Teemu Likonen
2010-02-04  9:55 ` Johannes Sixt [this message]
2010-02-04 16:32   ` Teemu Likonen
2010-02-04 16:43     ` Johannes Sixt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B6A9985.4050909@viscovery.net \
    --to=j.sixt@viscovery.net \
    --cc=git@vger.kernel.org \
    --cc=tlikonen@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).