Write out hgrc properly.
authorBryan O'Sullivan <bos@serpentine.com>
Sun, 04 Sep 2005 15:47:59 -0700
changeset 1205 4003ea658693
parent 1204 b0f6053df539
child 1206 6512d352d6c1
Write out hgrc properly. Previously, we simply appended to the hgrc file, which meant that it ended up containing multiple "paths" sections. Now, we only modify "paths.default".
mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -10,7 +10,7 @@ from node import *
 demandload(globals(), "os re sys signal shutil imp")
 demandload(globals(), "fancyopts ui hg util lock revlog")
 demandload(globals(), "fnmatch hgweb mdiff random signal time traceback")
-demandload(globals(), "errno socket version struct atexit sets")
+demandload(globals(), "ConfigParser errno socket version struct atexit sets")
 
 class UnknownCommand(Exception):
     """Exception raised if command is not in the command table."""
@@ -622,9 +622,16 @@ def clone(ui, source, dest=None, **opts)
         repo = hg.repository(ui, dest, create=1)
         repo.pull(other)
 
-    f = repo.opener("hgrc", "a")
-    f.write("\n[paths]\n")
-    f.write("default = %s\n" % abspath)
+    cfg = ConfigParser.SafeConfigParser()
+    try:
+        fp = repo.opener('hgrc', 'r')
+        os.unlink(fp.name)
+        cfg.readfp(fp)
+    except IOError, inst:
+        if inst.errno != errno.ENOENT: raise
+    if not cfg.has_section('paths'): cfg.add_section('paths')
+    cfg.set('paths', 'default', abspath)
+    cfg.write(repo.opener('hgrc', 'w'))
 
     if not opts['noupdate']:
         update(ui, repo)