diff mbox series

git-p4: don't allow cloning into non-empty directories

Message ID 20220105140631.48252-1-jholdsworth@nvidia.com (mailing list archive)
State New, archived
Headers show
Series git-p4: don't allow cloning into non-empty directories | expand

Commit Message

Joel Holdsworth Jan. 5, 2022, 2:06 p.m. UTC
Previously, git-p4 would allow users to clone a Perforce repository into
a pre-existing git repository. When attempting this, git-p4 would
download the Perforce commits (a time consuming process), and would fail
at the end during the final git fast-import with a cryptic error
message.

This was easy to do inadvertently when running the same git-p4 clone
command more than once.

This patch changes the behaviour of git-p4.py so that it matches that
of git itself: disallowing all clones into non-empty directories.

Signed-off-by: Joel Holdsworth <jholdsworth@nvidia.com>
---
 git-p4.py | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/git-p4.py b/git-p4.py
index cb37545455..e15fbe1486 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -4099,12 +4099,19 @@  def run(self, args):
         if not self.cloneDestination:
             self.cloneDestination = self.defaultDestination(args)
 
-        print("Importing from %s into %s" % (', '.join(depotPaths), self.cloneDestination))
-
-        if not os.path.exists(self.cloneDestination):
+        if os.path.exists(self.cloneDestination):
+            if (not os.path.isdir(self.cloneDestination) or
+                os.listdir(self.cloneDestination)):
+                die(
+                    "fatal: destination path '{}' already exists and is not "
+                    "an empty directory.".format(self.cloneDestination))
+        else:
             os.makedirs(self.cloneDestination)
         chdir(self.cloneDestination)
 
+        print("Importing from {} into {}".format(
+            ', '.join(depotPaths), self.cloneDestination))
+
         init_cmd = [ "git", "init" ]
         if self.cloneBare:
             init_cmd.append("--bare")