{"thread":{"id":"22519","subject":"Making custom git-remove-tree command","startedAt":"2010-02-04T09:03:34Z","lastAt":"2010-02-04T16:43:59Z","messageCount":4,"participants":["Teemu Likonen","Johannes Sixt"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"133594","messageId":"87iqad2x09.fsf@mithlond.arda","threadId":"22519","inReplyTo":null,"subject":"Making custom git-remove-tree command","fromName":"Teemu Likonen","fromEmail":"tlikonen@iki.fi","sentAt":"2010-02-04T09:03:34Z","receivedAt":"2010-02-04T09:03:34Z","isPatch":false,"sender":{"key":"tlikonen@iki.fi","avatar":null},"body":"I'm doing a script named git-remove-tree which removes the working tree\nknown to Git. It doesn't touch untracked files; it only deletes\ndirectories if they are empty. The script seems to work, but because I'm\nnot very good at Git plumbing and there can be some corner cases which I\ndon't know about, I'd appreciate if more experienced users would have a\nlook. Is the following script safe?\n\n\n#!/bin/sh\n# git-remove-tree\n\nis_wt=$(git rev-parse --is-inside-work-tree)\nif [ \"$is_wt\" = false ]; then\n\techo \"You must run this inside a working tree.\"\n\texit 1\nelif [ -z \"$is_wt\" ]; then\n\texit 1\nfi\n\ntop=$(git rev-parse --show-toplevel)\ncd \"$top\" || {\n\techo \"Can't go to top-level directory $top\"\n\texit 1\n}\n\ngit ls-tree --name-only -r -z HEAD | xargs -0r -- sh -c '\nfor f in \"$@\"; do\n\trm -f -- \"$f\"\n\td=$(dirname -- \"$f\")\n\t[ \"$d\" = . ] || rmdir -p -- \"$d\" 2>/dev/null\ndone' dollar0_argument\n\necho 'Use \"git reset --hard HEAD\" to populate the working tree again.'\n"},{"id":"133596","messageId":"4B6A9985.4050909@viscovery.net","threadId":"22519","inReplyTo":"87iqad2x09.fsf@mithlond.arda","subject":"Re: Making custom git-remove-tree command","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2010-02-04T09:55:17Z","receivedAt":"2010-02-04T09:55:17Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Teemu Likonen schrieb:\n> I'm doing a script named git-remove-tree which removes the working tree\n> known to Git. It doesn't touch untracked files; it only deletes\n> directories if they are empty. The script seems to work, but because I'm\n> not very good at Git plumbing and there can be some corner cases which I\n> don't know about, I'd appreciate if more experienced users would have a\n> look. Is the following script safe?\n> \n> \n> #!/bin/sh\n> # git-remove-tree\n> \n> is_wt=$(git rev-parse --is-inside-work-tree)\n> if [ \"$is_wt\" = false ]; then\n> \techo \"You must run this inside a working tree.\"\n> \texit 1\n> elif [ -z \"$is_wt\" ]; then\n> \texit 1\n> fi\n> \n> top=$(git rev-parse --show-toplevel)\n> cd \"$top\" || {\n> \techo \"Can't go to top-level directory $top\"\n> \texit 1\n> }\n> \n> git ls-tree --name-only -r -z HEAD | xargs -0r -- sh -c '\n> for f in \"$@\"; do\n> \trm -f -- \"$f\"\n> \td=$(dirname -- \"$f\")\n> \t[ \"$d\" = . ] || rmdir -p -- \"$d\" 2>/dev/null\n> done' dollar0_argument\n\nI think you should be able to reduce all of this to just\n\ngit read-tree --index-output=/tmp/empty.index -m -u \\\n\t4b825dc642cb6eb9a060e54bf8d69288fbee4904 # empty tree\nrm -f /tmp/empty.index\n\n(with the usual caveats about temporary files).\n\n> \n> echo 'Use \"git reset --hard HEAD\" to populate the working tree again.'\n\n-- Hannes\n"},{"id":"133610","messageId":"87tytxhsgc.fsf@mithlond.arda","threadId":"22519","inReplyTo":"4B6A9985.4050909@viscovery.net","subject":"Re: Making custom git-remove-tree command","fromName":"Teemu Likonen","fromEmail":"tlikonen@iki.fi","sentAt":"2010-02-04T16:32:51Z","receivedAt":"2010-02-04T16:32:51Z","isPatch":false,"sender":{"key":"tlikonen@iki.fi","avatar":null},"body":"* 2010-02-04 10:55 (+0100), Johannes Sixt wrote:\n\n> I think you should be able to reduce all of this to just\n>\n> git read-tree --index-output=/tmp/empty.index -m -u \\\n> \t4b825dc642cb6eb9a060e54bf8d69288fbee4904 # empty tree\n> rm -f /tmp/empty.index\n>\n> (with the usual caveats about temporary files).\n\nThanks, it works. It's also _much_ faster than my \"git ls-tree\" with\nshell loop thing. It prints ugly error, though:\n\n    fatal: unable to write new index file\n\nAlso, it doesn't leave any index output files around.\n"},{"id":"133612","messageId":"4B6AF94F.6070604@viscovery.net","threadId":"22519","inReplyTo":"87tytxhsgc.fsf@mithlond.arda","subject":"Re: Making custom git-remove-tree command","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2010-02-04T16:43:59Z","receivedAt":"2010-02-04T16:43:59Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Teemu Likonen schrieb:\n> * 2010-02-04 10:55 (+0100), Johannes Sixt wrote:\n> \n>> I think you should be able to reduce all of this to just\n>>\n>> git read-tree --index-output=/tmp/empty.index -m -u \\\n>> \t4b825dc642cb6eb9a060e54bf8d69288fbee4904 # empty tree\n>> rm -f /tmp/empty.index\n>>\n>> (with the usual caveats about temporary files).\n> \n> Thanks, it works. It's also _much_ faster than my \"git ls-tree\" with\n> shell loop thing. It prints ugly error, though:\n> \n>     fatal: unable to write new index file\n\nThen put the temporary index into your git-dir:\n\n   gitdir=$(git rev-parse --git-dir)\n   git read-tree --index-ouput=\"$gitdir/tmpindex\" ...\n   rm -f \"$gitdir/tmpindex\"\n\nThe purpose of this temporary index is only that your real index is not\ncleared. If you don't mind that your index is purged as well, then you can\njust drop the --index-output parameter.\n\n-- Hannes\n"}]}