Adapt to recent changes in Hg (locks handling, dirstate.update gone)
authorChristian Ebert <blacktrash@gmx.net>
Sun, 22 Jul 2007 14:56:13 +0200
changeset 209 430837dbe7f4
parent 208 5afdcec8a01f
child 210 304f9ac35869
Adapt to recent changes in Hg (locks handling, dirstate.update gone)
hgkw/keyword.py
tests/test-keyword
tests/test-keyword.out
--- a/hgkw/keyword.py	Sat Jul 21 21:44:33 2007 +0200
+++ b/hgkw/keyword.py	Sun Jul 22 14:56:13 2007 +0200
@@ -184,6 +184,8 @@
         expand = self.t is not None
         action = ('shrinking', 'expanding')[expand]
         notify = (self.ui.note, self.ui.debug)[commit]
+        # backwards compatibility: older versions have dirstate.update
+        nodirstateupdate = not hasattr(self.repo.dirstate, 'update')
         files = []
         for f in candidates:
             fp = self.repo.file(f, kwexp=expand, kwcnt=True)
@@ -195,7 +197,10 @@
                 except AttributeError:
                     # older versions want file descriptor as 3. optional arg
                     self.repo.wwrite(f, data)
-                files.append(f)
+                if nodirstateupdate:
+                    self.repo.dirstate.normal(f)
+                else:
+                    files.append(f)
         if files:
             self.repo.dirstate.update(files, 'n')
 
@@ -248,9 +253,10 @@
 
 def _overwrite(ui, repo, files, expand):
     '''Expands/shrinks keywords in working directory.'''
-    wlock = None
+    wlock = lock = None
     try:
         wlock = repo.wlock()
+        lock = repo.lock()
         bail_if_changed(repo)
         ctx = repo.changectx()
         if not ctx:
@@ -264,16 +270,19 @@
             files = [f for f in files if f in m.keys()]
         else:
             files = m.keys()
-        files = [f for f in files if kwfmatcher(f) and not os.path.islink(f)]
+        if hasattr(repo, '_link'):
+            files = [f for f in files if kwfmatcher(f) and not repo._link(f)]
+        else:
+            files = [f for f in files if kwfmatcher(f)
+                     and not os.path.islink(repo.wjoin(f))]
         if not files:
             ui.warn(_('files not configured for expansion or untracked\n'))
             return
         commit = False
         kwt = kwtemplater(ui, repo, expand, node=ctx.node())
         kwt.overwrite(files, m, commit)
-        wlock = None
     finally:
-        del wlock
+        del wlock, lock
 
 
 def shrink(ui, repo, *args):
@@ -392,31 +401,54 @@
                    match=util.always, force=False, lock=None, wlock=None,
                    force_editor=False, p1=None, p2=None, extra={}):
             try:
-                if not wlock:
-                    wlock = self.wlock()
-                removed = self.status(node1=p1, node2=p2, files=files,
-                                      match=match, wlock=wlock)[2]
-
-                node = super(kwrepo,
-                             self).commit(files=files, text=text, user=user,
-                                          date=date, match=match, force=force,
-                                          lock=lock, wlock=wlock,
-                                          force_editor=force_editor,
-                                          p1=p1, p2=p2, extra=extra)
+                kwlock = lock
+                kwwlock = wlock
+                del wlock, lock
+            except NameError:
+                # (w)lock arguments removed in recent Hg versions
+                kwwlock = kwlock = None
+            try:
+                if not kwwlock:
+                    kwwlock = self.wlock()
+                if not kwlock:
+                    kwlock = self.lock()
+                try:
+                    removed = self.status(node1=p1, node2=p2, files=files,
+                                          match=match, wlock=kwwlock)[2]
+                    node = super(kwrepo,
+                                 self).commit(files=files, text=text,
+                                              user=user, date=date,
+                                              match=match, force=force,
+                                              lock=kwlock, wlock=kwwlock,
+                                              force_editor=force_editor,
+                                              p1=p1, p2=p2, extra=extra)
+                except TypeError:
+                    removed = self.status(node1=p1, node2=p2,
+                                          files=files, match=match)[2]
+                    node = super(kwrepo,
+                                 self).commit(files=files, text=text,
+                                              user=user, date=date,
+                                              match=match, force=force,
+                                              force_editor=force_editor,
+                                              p1=p1, p2=p2, extra=extra)
                 if node is not None:
                     cl = self.changelog.read(node)
-                    candidates = [f for f in cl[3] if kwfmatcher(f)
-                                  and f not in removed
-                                  and not os.path.islink(self.wjoin(f))]
+                    if hasattr(self, '_link'):
+                        candidates = [f for f in cl[3] if kwfmatcher(f)
+                                      and f not in removed
+                                      and not self._link(f)]
+                    else:
+                        candidates = [f for f in cl[3] if kwfmatcher(f)
+                                      and f not in removed
+                                      and not os.path.islink(self.wjoin(f))]
                     if candidates:
-                        m = self.manifest.read(cl[0])
+                        mn = self.manifest.read(cl[0])
                         expand = commit = True
                         kwt = kwtemplater(ui, self, expand, node=node)
-                        kwt.overwrite(candidates, m, commit)
-                wlock = None
+                        kwt.overwrite(candidates, mn, commit)
                 return node
             finally:
-                del wlock
+                del kwwlock, kwlock
 
     repo.__class__ = kwrepo
 
--- a/tests/test-keyword	Sat Jul 21 21:44:33 2007 +0200
+++ b/tests/test-keyword	Sun Jul 22 14:56:13 2007 +0200
@@ -9,6 +9,7 @@
 EOF
 
 echo % help
+# sed for backwards compatibility
 hg help keyword \
 | sed -e '/^list of commands/d' -e '/hg -v help keyword/d' -e '/^$/d'
 
@@ -92,7 +93,8 @@
 EOF
 
 echo % interrupted commit
-HGEDITOR=false hg commit
+# redirection and sed for backwards compatibility
+HGEDITOR=false hg commit 2>&1 | sed -e '/edit failed:/d'
 echo % status
 hg status
 
@@ -115,7 +117,7 @@
 default = ../Test
 EOF
 echo % incoming
-# remove path to temp dir
+# remove path to temp dir for backwards compatibility
 hg incoming | sed -e '/^comparing with/d'
 
 echo % switch off expansion
--- a/tests/test-keyword.out	Sat Jul 21 21:44:33 2007 +0200
+++ b/tests/test-keyword.out	Sun Jul 22 14:56:13 2007 +0200
@@ -112,7 +112,6 @@
 expand $Id: a 65cbcc9534b0 Thu, 01 Jan 1970 00:00:00 +0000 user $
 ignore $Id$
 % interrupted commit
-abort: edit failed: false exited with status 1
 transaction abort!
 rollback completed
 % status