[PATCH 4 of 7] record: add an option to backup all wc modifications

Idan Kamara idankk86 at gmail.com
Tue May 24 11:26:10 CDT 2011


# HG changeset patch
# User Idan Kamara <idankk86 at gmail.com>
# Date 1306253824 -10800
# Node ID 4527996659c1e45193021261345fdfea5ba1a8bf
# Parent  793e7db95eac686eafc179e0ca7aefb0af3b5dfb
record: add an option to backup all wc modifications

Also, don't create a backup dir if we have no files to backup.

This is essential for qrefresh --interactive. Since we can't
select individual files to qrefresh without eliminating already
present changes, we have to backup all changes in the working
copy to avoid refreshing unaccepted hunks.

(thanks to Patrick for the idea)

diff -r 793e7db95eac -r 4527996659c1 hgext/record.py
--- a/hgext/record.py	Tue May 24 19:17:02 2011 +0300
+++ b/hgext/record.py	Tue May 24 19:17:04 2011 +0300
@@ -374,7 +374,7 @@
 
     This command is not available when committing a merge.'''
 
-    dorecord(ui, repo, commands.commit, 'commit', *pats, **opts)
+    dorecord(ui, repo, commands.commit, 'commit', False, *pats, **opts)
 
 
 def qrecord(ui, repo, patch, *pats, **opts):
@@ -395,10 +395,9 @@
         opts['checkname'] = False
         mq.new(ui, repo, patch, *pats, **opts)
 
-    dorecord(ui, repo, committomq, 'qnew', *pats, **opts)
+    dorecord(ui, repo, committomq, 'qnew', False, *pats, **opts)
 
-
-def dorecord(ui, repo, commitfunc, cmdsuggest, *pats, **opts):
+def dorecord(ui, repo, commitfunc, cmdsuggest, backupall, *pats, **opts):
     if not ui.interactive():
         raise util.Abort(_('running non-interactively, use %s instead') %
                          cmdsuggest)
@@ -450,18 +449,22 @@
         modified = set(changes[0])
 
         # 2. backup changed files, so we can restore them in the end
+        if backupall:
+            tobackup = changed
+        else:
+            tobackup = [f for f in newfiles if f in modified]
+
         backups = {}
-        backupdir = repo.join('record-backups')
-        try:
-            os.mkdir(backupdir)
-        except OSError, err:
-            if err.errno != errno.EEXIST:
-                raise
+        if tobackup:
+            backupdir = repo.join('record-backups')
+            try:
+                os.mkdir(backupdir)
+            except OSError, err:
+                if err.errno != errno.EEXIST:
+                    raise
         try:
             # backup continues
-            for f in newfiles:
-                if f not in modified:
-                    continue
+            for f in tobackup:
                 fd, tmpname = tempfile.mkstemp(prefix=f.replace('/', '_')+'.',
                                                dir=backupdir)
                 os.close(fd)
@@ -522,7 +525,8 @@
                     # writing it.
                     shutil.copystat(tmpname, repo.wjoin(realname))
                     os.unlink(tmpname)
-                os.rmdir(backupdir)
+                if tobackup:
+                    os.rmdir(backupdir)
             except OSError:
                 pass
 


More information about the Mercurial-devel mailing list