patch: add lexists() to backends, use it in selectfile()
authorPatrick Mezard <pmezard@gmail.com>
Tue, 17 May 2011 23:46:38 +0200
changeset 14351 d54f9bbcc640
parent 14350 00da6624e167
child 14352 077cdf172580
patch: add lexists() to backends, use it in selectfile() At this point, all applydiff() filesystem calls should pass through fsbackend.
hgext/mq.py
mercurial/patch.py
--- a/hgext/mq.py	Tue May 17 23:46:38 2011 +0200
+++ b/hgext/mq.py	Tue May 17 23:46:38 2011 +0200
@@ -1178,7 +1178,7 @@
                 if wcfiles:
                     for patchname in s:
                         pf = os.path.join(self.path, patchname)
-                        patchfiles = patchmod.changedfiles(pf, strip=1)
+                        patchfiles = patchmod.changedfiles(self.ui, repo, pf)
                         if wcfiles.intersection(patchfiles):
                             self.localchangesfound(self.applied)
             elif mergeq:
--- a/mercurial/patch.py	Tue May 17 23:46:38 2011 +0200
+++ b/mercurial/patch.py	Tue May 17 23:46:38 2011 +0200
@@ -394,6 +394,9 @@
         """
         raise NotImplementedError
 
+    def exists(self, fname):
+        raise NotImplementedError
+
 class fsbackend(abstractbackend):
     def __init__(self, ui, basedir):
         super(fsbackend, self).__init__(ui)
@@ -461,6 +464,9 @@
                     % dst)
         util.copyfile(abssrc, absdst)
 
+    def exists(self, fname):
+        return os.path.lexists(fname)
+
 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1
 unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@')
 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)')
@@ -970,16 +976,16 @@
         count -= 1
     return path[:i].lstrip(), path[i:].rstrip()
 
-def selectfile(afile_orig, bfile_orig, hunk, strip):
+def selectfile(backend, afile_orig, bfile_orig, hunk, strip):
     nulla = afile_orig == "/dev/null"
     nullb = bfile_orig == "/dev/null"
     abase, afile = pathstrip(afile_orig, strip)
-    gooda = not nulla and os.path.lexists(afile)
+    gooda = not nulla and backend.exists(afile)
     bbase, bfile = pathstrip(bfile_orig, strip)
     if afile == bfile:
         goodb = gooda
     else:
-        goodb = not nullb and os.path.lexists(bfile)
+        goodb = not nullb and backend.exists(bfile)
     createfunc = hunk.createfile
     missing = not goodb and not gooda and not createfunc()
 
@@ -1176,7 +1182,7 @@
                 rejects += current_file.close()
             afile, bfile, first_hunk = values
             try:
-                current_file, missing = selectfile(afile, bfile,
+                current_file, missing = selectfile(backend, afile, bfile,
                                                    first_hunk, strip)
                 current_file = patcher(ui, current_file, backend,
                                        missing=missing, eolmode=eolmode)
@@ -1347,7 +1353,8 @@
     except PatchError, err:
         raise util.Abort(str(err))
 
-def changedfiles(patchpath, strip=1):
+def changedfiles(ui, repo, patchpath, strip=1):
+    backend = fsbackend(ui, repo.root)
     fp = open(patchpath, 'rb')
     try:
         changed = set()
@@ -1356,7 +1363,7 @@
                 continue
             elif state == 'file':
                 afile, bfile, first_hunk = values
-                current_file, missing = selectfile(afile, bfile,
+                current_file, missing = selectfile(backend, afile, bfile,
                                                    first_hunk, strip)
                 changed.add(current_file)
             elif state == 'git':