[PATCH V2] revert: allow configuring the .orig file location

Zainab Zahid zzahid at fb.com
Fri Aug 14 18:15:31 UTC 2015


# HG changeset patch
# User Zainab Zahid <zzahid at fb.com>
# Date 1438967257 25200
#      Fri Aug 07 10:07:37 2015 -0700
# Node ID 44a9013fd24fd9a33ad511419e6d40545ec48515
# Parent  eabba9c75061254ff62827f92df0f32491c74b3d
revert: allow configuring the .orig file location

Adding support for a 'origbackuppath' entry under section [ui] in the configuration file.
It allows user to specify where .orig files should be stored relative to the repo.
In case of no origbackuppath entry, we fallback to the default behaviour of moving old
versions of a file to a file name <oldpath>.orig. This will prevent cluttering of the
 working copy.

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -3037,7 +3037,7 @@
                     xlist.append(abs)
                     if dobackup and (backup <= dobackup
                                      or wctx[abs].cmp(ctx[abs])):
-                            bakname = "%s.orig" % rel
+                            bakname = _getorigbackuppath(ui, repo, rel)
                             ui.note(_('saving current version of %s as %s\n') %
                                     (rel, bakname))
                             if not opts.get('dry_run'):
@@ -3069,6 +3069,26 @@
     finally:
         wlock.release()
 
+def _getorigbackuppath(ui, repo, filepath):
+    '''customize where .orig files are created
+
+    Fetch user defined path from config file: [ui] origbackuppath = <path>
+    Fall back to default (filepath) if not specified
+    '''
+    origbackuppath = ui.config('ui', 'origbackuppath', None)
+    if origbackuppath is None:
+        return filepath + ".orig"
+
+    filepathfromroot = os.path.relpath(filepath, start=repo.root)
+    origpath = repo.wjoin(origbackuppath, filepathfromroot);
+
+    origbackupdir = repo.vfs.dirname(origpath)
+    if not repo.vfs.exists(origbackupdir):
+        ui.note(_('creating directory: %s\n') % origbackupdir)
+        util.makedirs(origbackupdir)
+
+    return origpath + ".orig"
+
 def _revertprefetch(repo, ctx, *files):
     """Let extension changing the storage layer prefetch content"""
     pass
diff --git a/tests/test-revert.t b/tests/test-revert.t
--- a/tests/test-revert.t
+++ b/tests/test-revert.t
@@ -86,6 +86,23 @@
   saving current version of e as e.orig
   reverting e
 
+Test creation of backup (.orig) file in configured file location
+----------------------------------------------------------------
+ 
+  $ cat $HGRCPATH > hgrc_bak
+  $ cat >> $HGRCPATH << EOF
+  > [ui]
+  > origbackuppath= .hg/origbackups
+  > EOF
+  $ echo z > e
+  $ hg revert --all -v
+  creating directory: $TESTTMP/repo/.hg/origbackups
+  saving current version of e as $TESTTMP/repo/.hg/origbackups/e.orig
+  reverting e
+  $ cp $TESTTMP/repo/.hg/origbackups/e.orig .
+  $ cp hgrc_bak $HGRCPATH
+  $ rm hgrc_bak
+
 revert on clean file (no change)
 --------------------------------
 


More information about the Mercurial-devel mailing list