revert: properly back up added files with local modification
authorPierre-Yves David <pierre-yves.david@fb.com>
Sun, 31 Aug 2014 13:01:00 +0200
changeset 22611 2ff28e07d7d6
parent 22610 0f323ed8effd
child 22612 fdfa40ee75cf
revert: properly back up added files with local modification These files were previously not backed up because the backup mechanism was not smart enough. This leads to data lose for the user since uncommitted contents were discarded. We now properly move the modified version to <filename>.orig before deleting it. We have to use a small hack to do a different action if "--no-backup" is specified. This is needed because the backup process is actually a move (not a copy) so the file is already missing when we backup. The internet kitten is a bit disapointed about that, but such is life. This patch concludes the "lets refactor revert" phases. We can now open the "Lets find stupid bug with renames and merge" phases. I'm sure that now that the code is clearer we could do it in another simpler way, but I consider the current improvement good enough for now.
mercurial/cmdutil.py
tests/test-revert.t
--- a/mercurial/cmdutil.py	Sat Aug 30 02:39:08 2014 +0200
+++ b/mercurial/cmdutil.py	Sun Aug 31 13:01:00 2014 +0200
@@ -2640,6 +2640,10 @@
         if opts.get('no_backup'):
             backup = check = discard
 
+        backupanddel = actions['remove']
+        if not opts.get('no_backup'):
+            backupanddel = actions['drop']
+
         disptable = (
             # dispatch table:
             #   file state
@@ -2658,7 +2662,7 @@
             # Added in working directory
             (dsadded,       actions['forget'],   discard),
             # Added since target, have local modification
-            (modadded,      actions['remove'],   discard),
+            (modadded,      backupanddel,        backup),
             # Added since target but file is missing in working directory
             (deladded,      actions['drop'],   discard),
             # Removed since  target, before working copy parent
--- a/tests/test-revert.t	Sat Aug 30 02:39:08 2014 +0200
+++ b/tests/test-revert.t	Sun Aug 31 13:01:00 2014 +0200
@@ -940,16 +940,12 @@
 The diff is filtered to include change only. The only difference should be
 additional `.orig` backup file when applicable.
 
-Misbehavior:
-
-- no backup for
-| - added_wc (DATA LOSS)
-
   $ python ../dircontent.py > ../content-base-all.txt
   $ cd ..
   $ diff -U 0 -- content-base.txt content-base-all.txt | grep _
   +parent added_untracked-clean
   +wc     added_untracked-wc
+  +wc     added_wc.orig
   +wc     clean_untracked-wc.orig
   +wc     clean_wc.orig
   +wc     missing_untracked-wc