record: add an option to backup all wc modifications
authorIdan Kamara <idankk86@gmail.com>
Tue, 24 May 2011 19:17:04 +0300
changeset 14425 e89534504fb9
parent 14424 4eb88d296f63
child 14426 1df64ccef23e
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)
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