Why does `rm -rf` behave differently when used in a git post-receive hook as opposed to shell?

I'm using this example on publishing a website w/ git post receive hooks.

The hook pretty much clones the bare repo into a temporary directory, and after generating the site, removes that temporary git clone.

#!/bin/sh
# clone a repo, generate site etc
# done generating site, remove the TMP_GIT_CLONE
rm -rf $TMP_GIT_CLONE

When I do the push, all the other tasks fine, but doesn't remove all the files.

I get the follwing errors:

remote: rm: <TMP_GIT_CLONE>/.git/objects/pack: Directory not empty
remote: rm: <TMP_GIT_CLONE>/.git/objects: Directory not emppty

...

You get the idea

However, when I invoke the post-receive script directly from the command line, the rm behaves as expected.

Why?

Note: I've looked at Post-hook receive act's differently to shell, where the asker's problem had to do with being in a bare repo instead of a work-tree.

Answers


use

which rm

to get rm's path eg. /bin/rm

then replace it with /bin/rm to get another try. Sometimes, it is occurs by your shell script's start scripts.


I don't know if you ever found a way around this, but I had the exact same problem. I found that the /git/objects/pack directory was the one not emptying. I suspect that whatever process is used to track files runs slower or in a different manner when you're either in ssh or post-receive.

The solutions:

One way is to manually remove those directories. This kind of worked for me, but I didn't want to depend on that structure being the same. I tried removing just the .git directory first. That produced the odd behavior of complaining about non-empty directories but eventually emptying them.

A better approach is to avoid the unnecessary git files in the first place. The answers to this question may provide some insight. I replaced the 'git clone' line with:

mkdir -p $TMP_GIT_CLONE
git archive master --format=tar | tar -x -f - -C $TMP_GIT_CLONE

I think it's safe to git rid of $GIT_REPO. Since you're in a post-receive hook, you know you're in the repo you want to clone anyway.


Need Your Help

mongodb get/post hangs (or is so slow it seems hung)

mongodb express mongoose

I continue apace in banging my head on mongoose/mongodb/express/node app. Today, it's the ultimate slowness cage match. We're talking snails would be ashamed to go this slow. I've looked through the

Increase PHP performance PHP INI

php ini

The server is hosted by JustHost and on there are a few separate domains. I am uploading files to wordpress blog on one of the domains and my import only gets through 1332 records each time.