Ensure directory exists before changing in popen

If the directory does not exist, we want to ensure that it does.

Forking repos will fail in some situations because of this issue.

Fixes the exact problems described in https://github.com/gitlabhq/gitlabhq/issues/4016 for me.