[PATCH 05 of 19] tag: lock before tagging

Mads Kiilerich mads at kiilerich.com
Thu Jan 12 19:32:39 CST 2012


# HG changeset patch
# User Mads Kiilerich <mads at kiilerich.com>
# Date 1326413947 -3600
# Node ID fb6701592b245272e8fa918f0bcea5770f627bdd
# Parent  d153ef199ab47c4f2723eb00ccca425058eb3991
tag: lock before tagging

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -5501,62 +5501,68 @@
 
     Returns 0 on success.
     """
-
-    rev_ = "."
-    names = [t.strip() for t in (name1,) + names]
-    if len(names) != len(set(names)):
-        raise util.Abort(_('tag names must be unique'))
-    for n in names:
-        if n in ['tip', '.', 'null']:
-            raise util.Abort(_("the name '%s' is reserved") % n)
-        if not n:
-            raise util.Abort(_('tag names cannot consist entirely of whitespace'))
-    if opts.get('rev') and opts.get('remove'):
-        raise util.Abort(_("--rev and --remove are incompatible"))
-    if opts.get('rev'):
-        rev_ = opts['rev']
-    message = opts.get('message')
-    if opts.get('remove'):
-        expectedtype = opts.get('local') and 'local' or 'global'
+    wlock = lock = None
+    try:
+        wlock = repo.wlock()
+        lock = repo.lock()
+        rev_ = "."
+        names = [t.strip() for t in (name1,) + names]
+        if len(names) != len(set(names)):
+            raise util.Abort(_('tag names must be unique'))
         for n in names:
-            if not repo.tagtype(n):
-                raise util.Abort(_("tag '%s' does not exist") % n)
-            if repo.tagtype(n) != expectedtype:
-                if expectedtype == 'global':
-                    raise util.Abort(_("tag '%s' is not a global tag") % n)
-                else:
-                    raise util.Abort(_("tag '%s' is not a local tag") % n)
-        rev_ = nullid
+            if n in ['tip', '.', 'null']:
+                raise util.Abort(_("the name '%s' is reserved") % n)
+            if not n:
+                raise util.Abort(_('tag names cannot consist entirely of '
+                                   'whitespace'))
+        if opts.get('rev') and opts.get('remove'):
+            raise util.Abort(_("--rev and --remove are incompatible"))
+        if opts.get('rev'):
+            rev_ = opts['rev']
+        message = opts.get('message')
+        if opts.get('remove'):
+            expectedtype = opts.get('local') and 'local' or 'global'
+            for n in names:
+                if not repo.tagtype(n):
+                    raise util.Abort(_("tag '%s' does not exist") % n)
+                if repo.tagtype(n) != expectedtype:
+                    if expectedtype == 'global':
+                        raise util.Abort(_("tag '%s' is not a global tag") % n)
+                    else:
+                        raise util.Abort(_("tag '%s' is not a local tag") % n)
+            rev_ = nullid
+            if not message:
+                # we don't translate commit messages
+                message = 'Removed tag %s' % ', '.join(names)
+        elif not opts.get('force'):
+            for n in names:
+                if n in repo.tags():
+                    raise util.Abort(_("tag '%s' already exists "
+                                       "(use -f to force)") % n)
+        if not opts.get('local'):
+            p1, p2 = repo.dirstate.parents()
+            if p2 != nullid:
+                raise util.Abort(_('uncommitted merge'))
+            bheads = repo.branchheads()
+            if not opts.get('force') and bheads and p1 not in bheads:
+                raise util.Abort(_('not at a branch head (use -f to force)'))
+        r = scmutil.revsingle(repo, rev_).node()
+
         if not message:
             # we don't translate commit messages
-            message = 'Removed tag %s' % ', '.join(names)
-    elif not opts.get('force'):
-        for n in names:
-            if n in repo.tags():
-                raise util.Abort(_("tag '%s' already exists "
-                                   "(use -f to force)") % n)
-    if not opts.get('local'):
-        p1, p2 = repo.dirstate.parents()
-        if p2 != nullid:
-            raise util.Abort(_('uncommitted merge'))
-        bheads = repo.branchheads()
-        if not opts.get('force') and bheads and p1 not in bheads:
-            raise util.Abort(_('not at a branch head (use -f to force)'))
-    r = scmutil.revsingle(repo, rev_).node()
-
-    if not message:
-        # we don't translate commit messages
-        message = ('Added tag %s for changeset %s' %
-                   (', '.join(names), short(r)))
-
-    date = opts.get('date')
-    if date:
-        date = util.parsedate(date)
-
-    if opts.get('edit'):
-        message = ui.edit(message, ui.username())
-
-    repo.tag(names, r, message, opts.get('local'), opts.get('user'), date)
+            message = ('Added tag %s for changeset %s' %
+                       (', '.join(names), short(r)))
+
+        date = opts.get('date')
+        if date:
+            date = util.parsedate(date)
+
+        if opts.get('edit'):
+            message = ui.edit(message, ui.username())
+
+        repo.tag(names, r, message, opts.get('local'), opts.get('user'), date)
+    finally:
+        release(lock, wlock)
 
 @command('tags', [], '')
 def tags(ui, repo):
diff --git a/tests/test-check-code-hg.t b/tests/test-check-code-hg.t
--- a/tests/test-check-code-hg.t
+++ b/tests/test-check-code-hg.t
@@ -320,9 +320,6 @@
    >             except:
    warning: naked except clause
   mercurial/commands.py:0:
-   >             raise util.Abort(_('tag names cannot consist entirely of whitespace'))
-   warning: line over 80 characters
-  mercurial/commands.py:0:
    >             ui.status(_("(run 'hg heads .' to see heads, 'hg merge' to merge)\n"))
    warning: line over 80 characters
   mercurial/commands.py:0:


More information about the Mercurial-devel mailing list