Patchwork [1,of,7,V4] revert: allow configuring the .orig file location

login
register
mail settings
Submitter Christian Delahousse
Date Nov. 11, 2015, 12:59 a.m.
Message ID <5ee6aa63a694b3f8b4ab.1447203592@dev4253.prn1.facebook.com>
Download mbox | patch
Permalink /patch/11353/
State Accepted
Headers show

Comments

Christian Delahousse - Nov. 11, 2015, 12:59 a.m.
# HG changeset patch
# User Christian Delahousse <cdelahousse@fb.com>
# Date 1447194103 28800
#      Tue Nov 10 14:21:43 2015 -0800
# Node ID 5ee6aa63a694b3f8b4aba1c7b23561fba77dd78b
# Parent  8b2fbe3f59b1b969878691cb472369ad0067f165
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 the working copy.

The cmdutil._origpath function will continue to used in this series of patches.
Matt Mackall - Nov. 12, 2015, 11:05 p.m.
On Tue, 2015-11-10 at 16:59 -0800, cdelahousse@fb.com wrote:
> # HG changeset patch
> # User Christian Delahousse <cdelahousse@fb.com>
> # Date 1447194103 28800
> #      Tue Nov 10 14:21:43 2015 -0800
> # Node ID 5ee6aa63a694b3f8b4aba1c7b23561fba77dd78b
> # Parent  8b2fbe3f59b1b969878691cb472369ad0067f165
> 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 the working copy.
> 
> The cmdutil._origpath function will continue to used in this series
> of patches.

This series looks good in total but the first few patches are messy.
I've rearranged them thusly:

patch 1: introduce function with final name and docs
patch 2: add a user
patch 3: add a user
...

-- 
Mathematics is the supreme nostalgia of our time.

Patch

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -3082,7 +3082,7 @@ 
                     xlist.append(abs)
                     if dobackup and (backup <= dobackup
                                      or wctx[abs].cmp(ctx[abs])):
-                            bakname = "%s.orig" % rel
+                            bakname = _origpath(ui, repo, rel)
                             ui.note(_('saving current version of %s as %s\n') %
                                     (rel, bakname))
                             if not opts.get('dry_run'):
@@ -3114,6 +3114,26 @@ 
     finally:
         wlock.release()
 
+def _origpath(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)
+    fullorigpath = repo.wjoin(origbackuppath, filepathfromroot)
+
+    origbackupdir = repo.vfs.dirname(fullorigpath)
+    if not repo.vfs.exists(origbackupdir):
+        ui.note(_('creating directory: %s\n') % origbackupdir)
+        util.makedirs(origbackupdir)
+
+    return fullorigpath + ".orig"
+
 def _revertprefetch(repo, ctx, *files):
     """Let extension changing the storage layer prefetch content"""
     pass
diff --git a/mercurial/help/config.txt b/mercurial/help/config.txt
--- a/mercurial/help/config.txt
+++ b/mercurial/help/config.txt
@@ -1488,6 +1488,10 @@ 
     markers is different from the encoding of the merged files,
     serious problems may occur.
 
+``origbackuppath``
+    The path to a directory used to store generated .orig files. If the path is
+    not a directory, one will be created.
+
 ``patch``
     An optional external tool that ``hg import`` and some extensions
     will use for applying patches. By default Mercurial uses an
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,16 @@ 
   saving current version of e as e.orig
   reverting e
 
+Test creation of backup (.orig) file in configured file location
+----------------------------------------------------------------
+
+  $ echo z > e
+  $ hg revert --all -v --config 'ui.origbackuppath=.hg/origbackups'
+  creating directory: $TESTTMP/repo/.hg/origbackups
+  saving current version of e as $TESTTMP/repo/.hg/origbackups/e.orig
+  reverting e
+  $ rm -rf .hg/origbackups
+
 revert on clean file (no change)
 --------------------------------