Patchwork [09,of,15,V4] bookmarks: prevent divergent bookmark from being updated unexpectedly

login
register
mail settings
Submitter Katsunori FUJIWARA
Date Oct. 15, 2013, 3:25 p.m.
Message ID <4caa72f350d910aa9454.1381850731@juju>
Download mbox | patch
Permalink /patch/2780/
State Superseded
Headers show

Comments

Katsunori FUJIWARA - Oct. 15, 2013, 3:25 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1381849966 -32400
#      Wed Oct 16 00:12:46 2013 +0900
# Node ID 4caa72f350d910aa9454903a25293ec0ed5bce8b
# Parent  fbe4006cebaaaca5eabf4769e652c88b2f7b694b
bookmarks: prevent divergent bookmark from being updated unexpectedly

Before this patch, "@99" suffixed bookmark may be updated unexpectedly
by the bookmark value on the remote side at "hg pull", if all of "@1"
to "@99" suffixed bookmarks exist in the local repository, because
variable "n" still refers "@99" suffixed bookmark after the loop to
examine "@num" suffixes, even though it already exists in the local
repository.

This patch prevents divergent bookmark from being updated
unexpectedly, and shows warning message in such situation.

This patch uses original python script "seq.py" instead of "seq"
command to create sequence numbers in the test, because "seq" command
may not be available: it isn't defined in recent POSIX specification
(POSIX.1-2001 2013 Edition or XPG7)

Patch

diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py
--- a/mercurial/bookmarks.py
+++ b/mercurial/bookmarks.py
@@ -333,6 +333,8 @@ 
         n = '%s@%d' % (b, x)
         if n not in localmarks:
             break
+    else:
+        n = None
     # try to use an @pathalias suffix
     # if an @pathalias already exists, we overwrite (update) it
     for p, u in ui.configitems("paths"):
@@ -356,8 +358,13 @@ 
                         _("updating bookmark %s\n") % (b)))
     for b, scid, dcid in diverge:
         db = _diverge(ui, b, path, localmarks)
-        changed.append((db, bin(scid), ui.warn,
-                        _("divergent bookmark %s stored as %s\n") % (b, db)))
+        if db:
+            changed.append((db, bin(scid), ui.warn,
+                            _("divergent bookmark %s stored as %s\n") %
+                            (b, db)))
+        else:
+            ui.warn(_("warning: fail to assign new divergent bookmark to %s\n")
+                    % (b))
     if changed:
         for b, node, writer, msg in sorted(changed):
             localmarks[b] = node
diff --git a/tests/test-bookmarks-pushpull.t b/tests/test-bookmarks-pushpull.t
--- a/tests/test-bookmarks-pushpull.t
+++ b/tests/test-bookmarks-pushpull.t
@@ -158,6 +158,23 @@ 
      Z                         2:0d2164f0ce0d
      foo                       -1:000000000000
    * foobar                    1:9b140be10808
+  $ cat > $TESTTMP/seq.py <<EOF
+  > import sys
+  > for i in xrange(*[int(a) for a in sys.argv[1:]]):
+  >     print i
+  > EOF
+  $ python $TESTTMP/seq.py 1 100 | while read i; do hg bookmarks -r 000000000000 "X@${i}"; done
+  $ hg pull ../a
+  pulling from ../a
+  searching for changes
+  no changes found
+  warning: fail to assign new divergent bookmark to X
+  divergent bookmark @ stored as @1
+  $ hg bookmarks | grep '^   X' | grep -v ':000000000000'
+     X                         1:9b140be10808
+     X@foo                     2:0d2164f0ce0d
+  $ python $TESTTMP/seq.py 1 100 | while read i; do hg bookmarks -d "X@${i}"; done
+  $ hg bookmarks -d "@1"
   $ hg push -f ../a
   pushing to ../a
   searching for changes