dirstate: use wlock and `dirstate.change_files` to scope the change in `mq`
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 14 Dec 2022 00:46:58 +0100
changeset 50103 3c431f7551dd
parent 50102 2264e775512b
child 50104 c3c8ac540513
dirstate: use wlock and `dirstate.change_files` to scope the change in `mq` This is the way.
hgext/mq.py
--- a/hgext/mq.py	Wed Jan 25 12:51:26 2023 +0100
+++ b/hgext/mq.py	Wed Dec 14 00:46:58 2022 +0100
@@ -791,7 +791,10 @@
         if self.added:
             qrepo = self.qrepo()
             if qrepo:
-                qrepo[None].add(f for f in self.added if f not in qrepo[None])
+                with qrepo.wlock(), qrepo.dirstate.changing_files(qrepo):
+                    qrepo[None].add(
+                        f for f in self.added if f not in qrepo[None]
+                    )
             self.added = []
 
     def removeundo(self, repo):
@@ -1129,7 +1132,8 @@
         if not keep:
             r = self.qrepo()
             if r:
-                r[None].forget(patches)
+                with r.wlock(), r.dirstate.changing_files(r):
+                    r[None].forget(patches)
             for p in patches:
                 try:
                     os.unlink(self.join(p))
@@ -1464,7 +1468,8 @@
                     p.close()
                     r = self.qrepo()
                     if r:
-                        r[None].add([patchfn])
+                        with r.wlock(), r.dirstate.changing_files(r):
+                            r[None].add([patchfn])
                 except:  # re-raises
                     repo.rollback()
                     raise
@@ -2760,18 +2765,19 @@
     r = q.init(repo, create)
     q.savedirty()
     if r:
-        if not os.path.exists(r.wjoin(b'.hgignore')):
-            fp = r.wvfs(b'.hgignore', b'w')
-            fp.write(b'^\\.hg\n')
-            fp.write(b'^\\.mq\n')
-            fp.write(b'syntax: glob\n')
-            fp.write(b'status\n')
-            fp.write(b'guards\n')
-            fp.close()
-        if not os.path.exists(r.wjoin(b'series')):
-            r.wvfs(b'series', b'w').close()
-        r[None].add([b'.hgignore', b'series'])
-        commands.add(ui, r)
+        with r.wlock(), r.dirstate.changing_files(r):
+            if not os.path.exists(r.wjoin(b'.hgignore')):
+                fp = r.wvfs(b'.hgignore', b'w')
+                fp.write(b'^\\.hg\n')
+                fp.write(b'^\\.mq\n')
+                fp.write(b'syntax: glob\n')
+                fp.write(b'status\n')
+                fp.write(b'guards\n')
+                fp.close()
+            if not os.path.exists(r.wjoin(b'series')):
+                r.wvfs(b'series', b'w').close()
+            r[None].add([b'.hgignore', b'series'])
+            commands.add(ui, r)
     return 0
 
 
@@ -3629,8 +3635,8 @@
     util.rename(q.join(patch), absdest)
     r = q.qrepo()
     if r and patch in r.dirstate:
-        wctx = r[None]
-        with r.wlock():
+        with r.wlock(), r.dirstate.changing_files(r):
+            wctx = r[None]
             if r.dirstate.get_entry(patch).added:
                 r.dirstate.set_untracked(patch)
                 r.dirstate.set_tracked(name)