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

login
register
mail settings
Submitter Zainab Zahid
Date Aug. 14, 2015, 6:15 p.m.
Message ID <44a9013fd24fd9a33ad5.1439576131@kevintjones-mbp.dhcp.thefacebook.com.DHCP.thefacebook.com>
Download mbox | patch
Permalink /patch/10213/
State Superseded
Commit 080276d377d9131c1fd4f538f7def95d669eba9f
Headers show

Comments

Zainab Zahid - Aug. 14, 2015, 6:15 p.m.
# HG changeset patch
# User Zainab Zahid <zzahid@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.
Matt Mackall - Aug. 14, 2015, 8:28 p.m.
On Fri, 2015-08-14 at 11:15 -0700, Zainab Zahid wrote:
> # HG changeset patch
> # User Zainab Zahid <zzahid@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

This patch looks ok but failed two tests:

test-check-code-hg.t   - stray semicolon and whitespace
test-check-config-hg.t - no docs for config option
Matt Mackall - Aug. 14, 2015, 8:30 p.m.
On Fri, 2015-08-14 at 11:15 -0700, Zainab Zahid wrote:
> # HG changeset patch
> # User Zainab Zahid <zzahid@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

This patch looks ok but failed two tests:

test-check-code-hg.t   - stray semicolon and whitespace
test-check-config-hg.t - no docs for config option

Patch

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)
 --------------------------------