[PATCH] Clean up destination directory if a clone fails.
authormpm@selenic.com
Tue, 28 Jun 2005 00:59:59 -0800
changeset 500 ebc4714a7632
parent 499 81c563a254be
child 501 7ea1c88792bf
[PATCH] Clean up destination directory if a clone fails. -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 [PATCH] Clean up destination directory if a clone fails. From: Bryan O'Sullivan <bos@serpentine.com> On Mon, 2005-06-27 at 16:27 +0800, Soh Tk-r28629 wrote: > Hg clone left the dest directory if the source isn't valid. > I think it should either validate the source before mkdir, or do a > rmdir to clean it up. Good suggestion, thanks. As validating the source in advance isn't really practical, I opted for the latter. manifest hash: 1508afddd2402142f9d8b2387652d4cb28ccc1f5 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCwRGPywK+sNU5EO8RApBOAJ9Fj22MP3pYJhxWsj8BYbcgWzIOKwCff9yC lX87RrDKicM1W61lBpQl20w= =efck -----END PGP SIGNATURE-----
mercurial/commands.py
--- a/mercurial/commands.py	Tue Jun 28 00:55:59 2005 -0800
+++ b/mercurial/commands.py	Tue Jun 28 00:59:59 2005 -0800
@@ -234,7 +234,7 @@
                 name = name[:f]
             bcache[rev] = name
             return name
-    
+
     bcache = {}
     opmap = [['user', getname], ['number', str], ['changeset', getnode]]
     if not ops['user'] and not ops['changeset']:
@@ -273,42 +273,53 @@
 
     if source in paths: source = paths[source]
 
+    created = False
+
     if dest is None:
         dest = os.getcwd()
     elif not os.path.exists(dest):
-        os.makedirs(dest)
+        os.mkdir(dest)
+        created = True
+
+    try:
+        dest = os.path.realpath(dest)
 
-    link = 0
-    if not source.startswith("http://"):
-        source = os.path.realpath(source)
-        d1 = os.stat(dest).st_dev
-        d2 = os.stat(source).st_dev
-        if d1 == d2: link = 1
+        link = 0
+        if not source.startswith("http://"):
+            source = os.path.realpath(source)
+            d1 = os.stat(dest).st_dev
+            d2 = os.stat(source).st_dev
+            if d1 == d2: link = 1
 
-    os.chdir(dest)
+        os.chdir(dest)
 
-    if link:
-        ui.debug("copying by hardlink\n")
-        os.system("cp -al %s/.hg .hg" % source)
-        try:
-            os.remove(".hg/dirstate")
-        except: pass
+        if link:
+            ui.debug("copying by hardlink\n")
+            os.system("cp -al %s/.hg .hg" % source)
+            try:
+                os.remove(".hg/dirstate")
+            except: pass
 
-        repo = hg.repository(ui, ".")
+            repo = hg.repository(ui, ".")
 
-    else:
-        repo = hg.repository(ui, ".", create=1)
-        other = hg.repository(ui, source)
-        cg = repo.getchangegroup(other)
-        repo.addchangegroup(cg)
+        else:
+            repo = hg.repository(ui, ".", create=1)
+            other = hg.repository(ui, source)
+            cg = repo.getchangegroup(other)
+            repo.addchangegroup(cg)
 
-    f = repo.opener("hgrc", "w")
-    f.write("[paths]\n")
-    f.write("default = %s\n" % source)
+        f = repo.opener("hgrc", "w")
+        f.write("[paths]\n")
+        f.write("default = %s\n" % source)
 
-    if not opts['no-update']:
-        update(ui, repo)
-    
+        if not opts['no-update']:
+            update(ui, repo)
+    except:
+        if created:
+            import shutil
+            shutil.rmtree(dest, True)
+        raise
+
 def commit(ui, repo, *files, **opts):
     """commit the specified files or all outstanding changes"""
     text = opts['text']
@@ -459,7 +470,7 @@
         pass
 
     patches = (patch1,) + patches
-    
+
     d = opts["base"]
     strip = opts["strip"]
 
@@ -543,7 +554,7 @@
         source = paths[source]
 
     ui.status('pulling from %s\n' % (source))
-    
+
     other = hg.repository(ui, source)
     cg = repo.getchangegroup(other)
     r = repo.addchangegroup(cg)
@@ -562,7 +573,7 @@
         paths[name] = path
 
     if dest in paths: dest = paths[dest]
-    
+
     if not dest.startswith("ssh://"):
         ui.warn("abort: can only push to ssh:// destinations currently\n")
         return 1
@@ -607,9 +618,9 @@
         files += open(rc['files']).read().splitlines()
 
     rc['parent'] = map(repo.lookup, rc['parent'])
-    
+
     repo.rawcommit(files, text, rc['user'], rc['date'], *rc['parent'])
- 
+
 def recover(ui, repo):
     """roll back an interrupted transaction"""
     repo.recover()
@@ -626,7 +637,7 @@
     """export the repository via HTTP"""
     hgweb.server(repo.root, opts["name"], opts["templates"],
                  opts["address"], opts["port"])
-    
+
 def status(ui, repo):
     '''show changed files in the working directory
 
@@ -645,7 +656,7 @@
 
 def tag(ui, repo, name, rev = None, **opts):
     """add a tag for the current tip or a given revision"""
-    
+
     if name == "tip":
 	ui.warn("abort: 'tip' is a reserved name!\n")
 	return -1
@@ -755,7 +766,7 @@
     "log": (log, [], 'hg log <file>'),
     "manifest": (manifest, [], 'hg manifest [rev]'),
     "parents": (parents, [], 'hg parents [node]'),
-    "pull": (pull, 
+    "pull": (pull,
                   [('u', 'update', None, 'update working directory')],
 		  'hg pull [options] [source]'),
     "push": (push, [], 'hg push <destination>'),