D2873: remotenames: add functionality to override -B flag of push

pulkit (Pulkit Goyal) phabricator at mercurial-scm.org
Mon Mar 19 04:36:05 EDT 2018


pulkit updated this revision to Diff 7103.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D2873?vs=7061&id=7103

REVISION DETAIL
  https://phab.mercurial-scm.org/D2873

AFFECTED FILES
  hgext/remotenames.py
  tests/test-logexchange.t

CHANGE DETAILS

diff --git a/tests/test-logexchange.t b/tests/test-logexchange.t
--- a/tests/test-logexchange.t
+++ b/tests/test-logexchange.t
@@ -333,3 +333,194 @@
      default/bar               6:87d6d6676308
      default/foo               3:62615734edd5
    * foo                       8:3e1487808078
+
+Testing the remotenames.pushtobookmark config option to push to bookmark
+
+  $ hg up .
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  (leaving bookmark foo)
+  $ echo foo > foobar
+  $ hg add foobar
+  $ hg ci -m "added foobar"
+
+  $ hg log -G
+  @  changeset:   9:aa6a885086c0
+  |  branch:      wat
+  |  tag:         tip
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     added foobar
+  |
+  o  changeset:   8:3e1487808078
+  |  branch:      wat
+  |  bookmark:    foo
+  |  remote branch:  $TESTTMP/server2/wat
+  |  remote branch:  default/wat
+  |  parent:      4:aa98ab95a928
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     added bar
+  |
+  | o  changeset:   7:ec2426147f0e
+  | |  remote branch:  $TESTTMP/server2/default
+  | |  remote branch:  default/default
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     Added h
+  | |
+  | o  changeset:   6:87d6d6676308
+  | |  remote bookmark:  $TESTTMP/server2/bar
+  | |  remote bookmark:  default/bar
+  | |  hoistedname:  bar
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     Added g
+  | |
+  | o  changeset:   5:825660c69f0c
+  |/   user:        test
+  |    date:        Thu Jan 01 00:00:00 1970 +0000
+  |    summary:     Added f
+  |
+  o  changeset:   4:aa98ab95a928
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Added e
+  |
+  o  changeset:   3:62615734edd5
+  |  remote bookmark:  $TESTTMP/server2/foo
+  |  remote bookmark:  default/foo
+  |  hoistedname:  foo
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Added d
+  |
+  o  changeset:   2:28ad74487de9
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Added c
+  |
+  o  changeset:   1:29becc82797a
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Added b
+  |
+  o  changeset:   0:18d04c59bb5d
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     Added a
+  
+  $ echo '[remotenames]' >> .hg/hgrc
+  $ echo 'pushtobookmark = True' >> .hg/hgrc
+
+Trying to push a non-existing bookmark on the server
+
+  $ hg push ../server/ -B nonexistentbm
+  pushing to ../server/
+  searching for changes
+  abort: bookmark 'nonexistentbm' does not exists on remote
+  [255]
+
+  $ hg push ../server/ -B nonexistinentbm -f
+  pushing to ../server/
+  searching for changes
+  abort: bookmark 'nonexistinentbm' does not exists on remote
+  [255]
+
+Pushing changesets to a bookmark on the remote
+
+  $ hg bookmarks -R ../server/
+     bar                       6:87d6d6676308
+     foo                       3:62615734edd5
+
+  $ hg push ../server/ -r . -B foo
+  pushing to ../server/
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 1 files
+  updating bookmark foo
+
+After this push, `foo` bookmark on the server is updated whereas the local
+bookmark of that name stayed at its own place
+
+  $ hg log -G -R ../server/ -r tip
+  o  changeset:   9:aa6a885086c0
+  |  branch:      wat
+  ~  bookmark:    foo
+     tag:         tip
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     added foobar
+  
+  $ hg bookmarks -R ../server/
+     bar                       6:87d6d6676308
+     foo                       9:aa6a885086c0
+
+XXX: remotebookmarks should have been updated after this push
+  $ hg log -G
+  @  changeset:   9:aa6a885086c0
+  |  branch:      wat
+  |  tag:         tip
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     added foobar
+  |
+  o  changeset:   8:3e1487808078
+  |  branch:      wat
+  |  bookmark:    foo
+  |  remote branch:  $TESTTMP/server2/wat
+  |  remote branch:  default/wat
+  |  parent:      4:aa98ab95a928
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     added bar
+  |
+  | o  changeset:   7:ec2426147f0e
+  | |  remote branch:  $TESTTMP/server2/default
+  | |  remote branch:  default/default
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     Added h
+  | |
+  | o  changeset:   6:87d6d6676308
+  | |  remote bookmark:  $TESTTMP/server2/bar
+  | |  remote bookmark:  default/bar
+  | |  hoistedname:  bar
+  | |  user:        test
+  | |  date:        Thu Jan 01 00:00:00 1970 +0000
+  | |  summary:     Added g
+  | |
+  | o  changeset:   5:825660c69f0c
+  |/   user:        test
+  |    date:        Thu Jan 01 00:00:00 1970 +0000
+  |    summary:     Added f
+  |
+  o  changeset:   4:aa98ab95a928
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Added e
+  |
+  o  changeset:   3:62615734edd5
+  |  remote bookmark:  $TESTTMP/server2/foo
+  |  remote bookmark:  default/foo
+  |  hoistedname:  foo
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Added d
+  |
+  o  changeset:   2:28ad74487de9
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Added c
+  |
+  o  changeset:   1:29becc82797a
+  |  user:        test
+  |  date:        Thu Jan 01 00:00:00 1970 +0000
+  |  summary:     Added b
+  |
+  o  changeset:   0:18d04c59bb5d
+     user:        test
+     date:        Thu Jan 01 00:00:00 1970 +0000
+     summary:     Added a
+  
diff --git a/hgext/remotenames.py b/hgext/remotenames.py
--- a/hgext/remotenames.py
+++ b/hgext/remotenames.py
@@ -22,17 +22,27 @@
 remotenames.hoist
   Name of the peer whose remotebookmarks should be hoisted into the top-level
   namespace (default: 'default')
+
+remotenames.pushtobookmark
+  Boolean value to change the behavior of bookmark passed to push command using
+  `-B` flag. If set the true, the changesets will be pushed to that bookmark on
+  the server. Errors if bookmark does not exists on the server. If multiple
+  bookmarks are specified using `-B` flag, fallbacks to default behavior.
+  (default: False)
 """
 
 from __future__ import absolute_import
 
 from mercurial.i18n import _
 
 from mercurial.node import (
     bin,
+    hex,
 )
 from mercurial import (
     bookmarks,
+    error,
+    exchange,
     extensions,
     logexchange,
     namespaces,
@@ -70,6 +80,33 @@
 configitem('remotenames', 'hoist',
     default='default',
 )
+configitem('remotenames', 'pushtobookmark',
+    default=False,
+)
+
+def expushdiscoverybookmarks(pushop):
+    # config not set, fallback to normal push behavior
+    if not pushop.repo.ui.configbool('remotenames', 'pushtobookmark'):
+        return exchange._pushdiscoverybookmarks(pushop)
+
+    # either zero or more than one bookmarks specified, fallback to normal
+    # push behavior, maybe we should error out in case of multiple bookmarks
+    if len(pushop.bookmarks) != 1:
+        return exchange._pushdiscoverybookmarks(pushop)
+
+    remotemarks = pushop.remote.listkeys('bookmarks')
+    bookmark = pushop.bookmarks[0]
+    rev = pushop.revs[0]
+
+    # allow new bookmark only if --create is specified
+    old = ''
+    if bookmark in remotemarks:
+        old = remotemarks[bookmark]
+    else:
+        msg = _("bookmark '%s' does not exists on remote")
+        raise error.Abort(msg % bookmark)
+
+    pushop.outbookmarks.append((bookmark, old, hex(rev)))
 
 class lazyremotenamedict(mutablemapping):
     """
@@ -247,6 +284,7 @@
 
 def extsetup(ui):
     extensions.wrapfunction(bookmarks, '_printbookmarks', wrapprintbookmarks)
+    exchange.pushdiscoverymapping['bookmarks'] = expushdiscoverybookmarks
 
 def reposetup(ui, repo):
     if not repo.local():



To: pulkit, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list