{"thread":{"id":"57188","subject":"[PATCH] git-p4: don't allow cloning into non-empty directories","startedAt":"2022-01-05T14:07:13Z","lastAt":"2022-01-05T14:07:13Z","messageCount":1,"participants":["Joel Holdsworth"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"445522","messageId":"20220105140631.48252-1-jholdsworth@nvidia.com","threadId":"57188","inReplyTo":null,"subject":"[PATCH] git-p4: don't allow cloning into non-empty directories","fromName":"Joel Holdsworth","fromEmail":"jholdsworth@nvidia.com","sentAt":"2022-01-05T14:06:31Z","receivedAt":"2022-01-05T14:07:13Z","isPatch":true,"sender":{"key":"jholdsworth@nvidia.com","avatar":"https://avatars.githubusercontent.com/u/1449493?v=4"},"body":"Previously, git-p4 would allow users to clone a Perforce repository into\na pre-existing git repository. When attempting this, git-p4 would\ndownload the Perforce commits (a time consuming process), and would fail\nat the end during the final git fast-import with a cryptic error\nmessage.\n\nThis was easy to do inadvertently when running the same git-p4 clone\ncommand more than once.\n\nThis patch changes the behaviour of git-p4.py so that it matches that\nof git itself: disallowing all clones into non-empty directories.\n\nSigned-off-by: Joel Holdsworth <jholdsworth@nvidia.com>\n---\n git-p4.py | 13 ++++++++++---\n 1 file changed, 10 insertions(+), 3 deletions(-)\n\ndiff --git a/git-p4.py b/git-p4.py\nindex cb37545455..e15fbe1486 100755\n--- a/git-p4.py\n+++ b/git-p4.py\n@@ -4099,12 +4099,19 @@ def run(self, args):\n         if not self.cloneDestination:\n             self.cloneDestination = self.defaultDestination(args)\n \n-        print(\"Importing from %s into %s\" % (', '.join(depotPaths), self.cloneDestination))\n-\n-        if not os.path.exists(self.cloneDestination):\n+        if os.path.exists(self.cloneDestination):\n+            if (not os.path.isdir(self.cloneDestination) or\n+                os.listdir(self.cloneDestination)):\n+                die(\n+                    \"fatal: destination path '{}' already exists and is not \"\n+                    \"an empty directory.\".format(self.cloneDestination))\n+        else:\n             os.makedirs(self.cloneDestination)\n         chdir(self.cloneDestination)\n \n+        print(\"Importing from {} into {}\".format(\n+            ', '.join(depotPaths), self.cloneDestination))\n+\n         init_cmd = [ \"git\", \"init\" ]\n         if self.cloneBare:\n             init_cmd.append(\"--bare\")\n-- \n2.34.1\n\n"}]}