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

login
register
mail settings
Submitter phabricator
Date March 26, 2018, 11:47 a.m.
Message ID <1dcda8d406ca22e7f50ae8bf895cdfc7@localhost.localdomain>
Download mbox | patch
Permalink /patch/29857/
State Not Applicable
Headers show

Comments

phabricator - March 26, 2018, 11:47 a.m.
pulkit updated this revision to Diff 7286.
pulkit edited the summary of this revision.

REPOSITORY
  rHG Mercurial

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

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

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

CHANGE DETAILS




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

Patch

diff --git a/tests/test-logexchange.t b/tests/test-logexchange.t
--- a/tests/test-logexchange.t
+++ b/tests/test-logexchange.t
@@ -3,7 +3,7 @@ 
 
   $ cat >> $HGRCPATH << EOF
   > [alias]
-  > glog = log -G -T '{rev}:{node|short}  {desc}'
+  > glog = log -G -T '{rev}:{node|short}  {desc}\n({bookmarks})'
   > [experimental]
   > remotenames = True
   > [extensions]
@@ -22,21 +22,21 @@ 
   > done
   $ hg glog
   @  7:ec2426147f0e  Added h
-  |
+  |  ()
   o  6:87d6d6676308  Added g
-  |
+  |  ()
   o  5:825660c69f0c  Added f
-  |
+  |  ()
   o  4:aa98ab95a928  Added e
-  |
+  |  ()
   o  3:62615734edd5  Added d
-  |
+  |  ()
   o  2:28ad74487de9  Added c
-  |
+  |  ()
   o  1:29becc82797a  Added b
-  |
+  |  ()
   o  0:18d04c59bb5d  Added a
-  
+     ()
   $ hg bookmark -r 3 foo
   $ hg bookmark -r 6 bar
   $ hg up 4
@@ -333,3 +333,296 @@ 
      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
+  | |  hoisted name:  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
+  |  hoisted name:  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
+  | |  hoisted name:  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
+  |  hoisted name:  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
+  
+
+More testing of remotenames.pushtobookmark config option
+---------------------------------------------------------
+
+restarting with new server and client repo
+  $ cd ..
+  $ rm server/ -rf
+  $ rm server2/ -rf
+  $ rm client/ -rf
+
+  $ hg init server
+  $ cd server
+  $ for ch in a b; do echo foo > $ch; hg add $ch; hg ci -m "added "$ch; done;
+  $ hg up 0
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ for ch in x; do echo foo > $ch; hg add $ch; hg ci -m "added "$ch; done;
+  created new head
+  $ hg bookmark bm1
+
+  $ cd ..
+  $ hg clone server client
+  updating to branch default
+  2 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ cd client
+  $ echo '[remotenames]' >> .hg/hgrc
+  $ echo 'pushtobookmark = True' >> .hg/hgrc
+  $ hg glog
+  @  2:fa7447a9d391  added x
+  |  (bm1)
+  | o  1:80e6d2c47cfe  added b
+  |/   ()
+  o  0:f7ad41964313  added a
+     ()
+
+  $ echo foo > y
+  $ hg add y
+  $ hg ci -m "added y"
+  $ hg up 1
+  1 files updated, 0 files merged, 2 files removed, 0 files unresolved
+  $ echo foo > c
+  $ hg add c
+  $ hg ci -m "added c"
+
+Specifying -B adds the revision on which the bookmark is to revs to be pushed
+set, so here we are pushing multiple heads and hence push will fallback to
+default behvior
+
+  $ hg push -r . -B bm1 ../server --debug | grep 'multiple'
+  pushing multiple topological heads, falling back to default push behavior
+
+  $ hg glog -R ../server
+  o  3:8be98ac1a569  added c
+  |  ()
+  | @  2:fa7447a9d391  added x
+  | |  (bm1)
+  o |  1:80e6d2c47cfe  added b
+  |/   ()
+  o  0:f7ad41964313  added a
+     ()
+
+Passing multiple revs as -r but only one topological head
+
+  $ hg up 3
+  2 files updated, 0 files merged, 2 files removed, 0 files unresolved
+  $ echo z > z
+  $ hg ci -Aqm "added z"
+  $ hg glog
+  @  5:e8d0567f11cf  added z
+  |  ()
+  | o  4:8be98ac1a569  added c
+  | |  ()
+  o |  3:dc7d5acf2389  added y
+  | |  ()
+  o |  2:fa7447a9d391  added x
+  | |  (bm1)
+  | o  1:80e6d2c47cfe  added b
+  |/   ()
+  o  0:f7ad41964313  added a
+     ()
+  $ hg push -r 5 -r 3 -B bm1 ../server
+  pushing to ../server
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 2 changesets with 2 changes to 2 files
+  updating bookmark bm1
+
+bookmark is updated on the server on topological head
+  $ hg glog -R ../server
+  o  5:e8d0567f11cf  added z
+  |  (bm1)
+  o  4:dc7d5acf2389  added y
+  |  ()
+  | o  3:8be98ac1a569  added c
+  | |  ()
+  @ |  2:fa7447a9d391  added x
+  | |  ()
+  | o  1:80e6d2c47cfe  added b
+  |/   ()
+  o  0:f7ad41964313  added a
+     ()
diff --git a/hgext/remotenames.py b/hgext/remotenames.py
--- a/hgext/remotenames.py
+++ b/hgext/remotenames.py
@@ -22,6 +22,14 @@ 
 remotenames.hoistedpeer
   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 or multiple topological heads are
+  pushed, fallbacks to default behavior.
+  (default: False)
 """
 
 from __future__ import absolute_import
@@ -33,6 +41,8 @@ 
 )
 from mercurial import (
     bookmarks,
+    error,
+    exchange,
     extensions,
     logexchange,
     namespaces,
@@ -70,6 +80,40 @@ 
 configitem('remotenames', 'hoistedpeer',
     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:
+        pushop.repo.ui.debug("pushing multiple bookmarks, falling back to "
+                             "default push behavior\n")
+        return exchange._pushdiscoverybookmarks(pushop)
+
+    remotemarks = pushop.remote.listkeys('bookmarks')
+    bookmark = pushop.bookmarks[0]
+    revs = [pushop.repo[node].rev() for node in pushop.revs]
+    headrevs = pushop.repo.revs('heads(%ld)', revs)
+    if len(headrevs) != 1:
+        pushop.repo.ui.debug("pushing multiple topological heads, falling back "
+                             "to default push behavior\n")
+        return exchange._pushdiscoverybookmarks(pushop)
+    rev = pushop.repo[headrevs.first()]
+
+    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, rev.hex()))
 
 class lazyremotenamedict(mutablemapping):
     """
@@ -247,6 +291,7 @@ 
 
 def extsetup(ui):
     extensions.wrapfunction(bookmarks, '_printbookmarks', wrapprintbookmarks)
+    exchange.pushdiscoverymapping['bookmarks'] = expushdiscoverybookmarks
 
 def reposetup(ui, repo):
     if not repo.local():