Patchwork D5851: scmutil: introduce a new backuppath() to replace origpath()

login
register
mail settings
Submitter phabricator
Date Feb. 6, 2019, 11:59 p.m.
Message ID <78256af936cf14f8b86359b34482d4ce@localhost.localdomain>
Download mbox | patch
Permalink /patch/38487/
State Not Applicable
Headers show

Comments

phabricator - Feb. 6, 2019, 11:59 p.m.
martinvonz updated this revision to Diff 13856.
martinvonz edited the summary of this revision.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D5851?vs=13815&id=13856

REVISION DETAIL
  https://phab.mercurial-scm.org/D5851

AFFECTED FILES
  mercurial/scmutil.py

CHANGE DETAILS




To: martinvonz, #hg-reviewers
Cc: mercurial-devel

Patch

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -838,6 +838,41 @@ 
         return None
     return vfs.vfs(repo.wvfs.join(origbackuppath))
 
+def backuppath(ui, repo, filepath):
+    '''customize where working copy backup files (.orig files) are created
+
+    Fetch user defined path from config file: [ui] origbackuppath = <path>
+    Fall back to default (filepath with .orig suffix) if not specified
+
+    filepath is repo-relative
+
+    Returns an absolute path
+    '''
+    origvfs = getorigvfs(ui, repo)
+    if origvfs is None:
+        return repo.wjoin(filepath + ".orig")
+
+    origbackupdir = origvfs.dirname(filepath)
+    if not origvfs.isdir(origbackupdir) or origvfs.islink(origbackupdir):
+        ui.note(_('creating directory: %s\n') % origvfs.join(origbackupdir))
+
+        # Remove any files that conflict with the backup file's path
+        for f in reversed(list(util.finddirs(filepath))):
+            if origvfs.isfileorlink(f):
+                ui.note(_('removing conflicting file: %s\n')
+                        % origvfs.join(f))
+                origvfs.unlink(f)
+                break
+
+        origvfs.makedirs(origbackupdir)
+
+    if origvfs.isdir(filepath) and not origvfs.islink(filepath):
+        ui.note(_('removing conflicting directory: %s\n')
+                % origvfs.join(filepath))
+        origvfs.rmtree(filepath, forcibly=True)
+
+    return origvfs.join(filepath)
+
 def origpath(ui, repo, filepath):
     '''customize where .orig files are created