git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Making custom git-remove-tree command
@ 2010-02-04  9:03 Teemu Likonen
  2010-02-04  9:55 ` Johannes Sixt
  0 siblings, 1 reply; 4+ messages in thread
From: Teemu Likonen @ 2010-02-04  9:03 UTC (permalink / raw)
  To: git

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.'

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Making custom git-remove-tree command
  2010-02-04  9:03 Making custom git-remove-tree command Teemu Likonen
@ 2010-02-04  9:55 ` Johannes Sixt
  2010-02-04 16:32   ` Teemu Likonen
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Sixt @ 2010-02-04  9:55 UTC (permalink / raw)
  To: Teemu Likonen; +Cc: git

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Making custom git-remove-tree command
  2010-02-04  9:55 ` Johannes Sixt
@ 2010-02-04 16:32   ` Teemu Likonen
  2010-02-04 16:43     ` Johannes Sixt
  0 siblings, 1 reply; 4+ messages in thread
From: Teemu Likonen @ 2010-02-04 16:32 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: git

* 2010-02-04 10:55 (+0100), Johannes Sixt wrote:

> 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).

Thanks, it works. It's also _much_ faster than my "git ls-tree" with
shell loop thing. It prints ugly error, though:

    fatal: unable to write new index file

Also, it doesn't leave any index output files around.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Making custom git-remove-tree command
  2010-02-04 16:32   ` Teemu Likonen
@ 2010-02-04 16:43     ` Johannes Sixt
  0 siblings, 0 replies; 4+ messages in thread
From: Johannes Sixt @ 2010-02-04 16:43 UTC (permalink / raw)
  To: Teemu Likonen; +Cc: git

Teemu Likonen schrieb:
> * 2010-02-04 10:55 (+0100), Johannes Sixt wrote:
> 
>> 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).
> 
> Thanks, it works. It's also _much_ faster than my "git ls-tree" with
> shell loop thing. It prints ugly error, though:
> 
>     fatal: unable to write new index file

Then put the temporary index into your git-dir:

   gitdir=$(git rev-parse --git-dir)
   git read-tree --index-ouput="$gitdir/tmpindex" ...
   rm -f "$gitdir/tmpindex"

The purpose of this temporary index is only that your real index is not
cleared. If you don't mind that your index is purged as well, then you can
just drop the --index-output parameter.

-- Hannes

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2010-02-04 16:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-02-04  9:03 Making custom git-remove-tree command Teemu Likonen
2010-02-04  9:55 ` Johannes Sixt
2010-02-04 16:32   ` Teemu Likonen
2010-02-04 16:43     ` Johannes Sixt

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).