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.
Whitespace fixes to patch
Merge pull request #5294 from dalehamel/master

Ensure directory exists before changing in popen