record: move copystat() hack out of util.copyfile() and into record
authorBrodie Rao <brodie@bitheap.org>
Tue, 07 Dec 2010 20:03:05 +1100
changeset 13099 a08b49d2f116
parent 13098 f7d6750dcd01
child 13101 37d0fe7a14da
record: move copystat() hack out of util.copyfile() and into record b2410ed2cbe9 updated copyfile to also copy over atimes and mtimes. That behavior is specifically to trick editors into thinking files that hg record has modified haven't changed. We don't really care about preserving times in the general case.
hgext/record.py
mercurial/util.py
--- a/hgext/record.py	Tue Dec 07 16:03:42 2010 +0100
+++ b/hgext/record.py	Tue Dec 07 20:03:05 2010 +1100
@@ -10,7 +10,7 @@
 from mercurial.i18n import gettext, _
 from mercurial import cmdutil, commands, extensions, hg, mdiff, patch
 from mercurial import util
-import copy, cStringIO, errno, os, re, tempfile
+import copy, cStringIO, errno, os, re, shutil, tempfile
 
 lines_re = re.compile(r'@@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)')
 
@@ -475,6 +475,7 @@
                 os.close(fd)
                 ui.debug('backup %r as %r\n' % (f, tmpname))
                 util.copyfile(repo.wjoin(f), tmpname)
+                shutil.copystat(repo.wjoin(f), tmpname)
                 backups[f] = tmpname
 
             fp = cStringIO.StringIO()
@@ -521,6 +522,14 @@
                 for realname, tmpname in backups.iteritems():
                     ui.debug('restoring %r to %r\n' % (tmpname, realname))
                     util.copyfile(tmpname, repo.wjoin(realname))
+                    # Our calls to copystat() here and above are a
+                    # hack to trick any editors that have f open that
+                    # we haven't modified them.
+                    #
+                    # Also note that this racy as an editor could
+                    # notice the file's mtime before we've finished
+                    # writing it.
+                    shutil.copystat(tmpname, repo.wjoin(realname))
                     os.unlink(tmpname)
                 os.rmdir(backupdir)
             except OSError:
--- a/mercurial/util.py	Tue Dec 07 16:03:42 2010 +0100
+++ b/mercurial/util.py	Tue Dec 07 20:03:05 2010 +1100
@@ -452,7 +452,7 @@
     else:
         try:
             shutil.copyfile(src, dest)
-            shutil.copystat(src, dest)
+            shutil.copymode(src, dest)
         except shutil.Error, inst:
             raise Abort(str(inst))