log: add bookmark option to "hg log"
authorSebastien Boisvert <sebhtml@protonmail.com>
Tue, 17 Nov 2020 21:30:50 -0500
changeset 45915 0aa118f18d4b
parent 45914 f96059fa519c
child 45916 bb9085ba12e6
log: add bookmark option to "hg log" Before pushing a bookmark with "hg push origin -B 'my-topic'", it is useful to inspect the list of commits that are ancestors of the bookmark. By relying on scmutil.bookmarkrevs(), "hg log -B topic" has the same bookmark semantics found in other commands like hg export, hg email, hg strip. Differential Revision: https://phab.mercurial-scm.org/D9341
mercurial/commands.py
mercurial/logcmdutil.py
tests/test-completion.t
tests/test-log-bookmark.t
--- a/mercurial/commands.py	Sat Nov 21 16:55:07 2020 -0500
+++ b/mercurial/commands.py	Tue Nov 17 21:30:50 2020 -0500
@@ -4456,6 +4456,13 @@
             _(b'BRANCH'),
         ),
         (
+            b'B',
+            b'bookmark',
+            [],
+            _(b"show changesets within the given bookmark"),
+            _(b'BOOKMARK'),
+        ),
+        (
             b'P',
             b'prune',
             [],
@@ -4613,14 +4620,19 @@
         )
 
     repo = scmutil.unhidehashlikerevs(repo, opts.get(b'rev'), b'nowarn')
-    revs, differ = logcmdutil.getrevs(
-        repo, logcmdutil.parseopts(ui, pats, opts)
-    )
+    walk_opts = logcmdutil.parseopts(ui, pats, opts)
+    revs, differ = logcmdutil.getrevs(repo, walk_opts)
     if linerange:
         # TODO: should follow file history from logcmdutil._initialrevs(),
         # then filter the result by logcmdutil._makerevset() and --limit
         revs, differ = logcmdutil.getlinerangerevs(repo, revs, opts)
 
+    if opts.get(b'bookmark'):
+        cmdutil.check_at_most_one_arg(opts, b'rev', b'bookmark')
+        bookmarks = opts.get(b'bookmark')
+        bookmark = bookmarks[0]
+        revs, differ = logcmdutil.get_bookmark_revs(repo, bookmark, walk_opts)
+
     getcopies = None
     if opts.get(b'copies'):
         endrev = None
--- a/mercurial/logcmdutil.py	Sat Nov 21 16:55:07 2020 -0500
+++ b/mercurial/logcmdutil.py	Tue Nov 17 21:30:50 2020 -0500
@@ -1024,6 +1024,26 @@
     return revs, differ
 
 
+def get_bookmark_revs(repo, bookmark, walk_opts):
+    # type: (Any, bookmark, walk_opts) -> Tuple[smartset.abstractsmartset, Optional[changesetdiffer]]
+    """Return (revs, differ) where revs is a smartset
+
+    differ is a changesetdiffer with pre-configured file matcher.
+    """
+    revs, filematcher = makewalker(repo, walk_opts)
+    if not revs:
+        return revs, None
+    differ = changesetdiffer()
+    differ._makefilematcher = filematcher
+
+    if bookmark:
+        if bookmark not in repo._bookmarks:
+            raise error.Abort(_(b"bookmark '%s' not found") % bookmark)
+        revs = scmutil.bookmarkrevs(repo, bookmark)
+
+    return revs, differ
+
+
 def _parselinerangeopt(repo, opts):
     """Parse --line-range log option and return a list of tuples (filename,
     (fromline, toline)).
--- a/tests/test-completion.t	Sat Nov 21 16:55:07 2020 -0500
+++ b/tests/test-completion.t	Tue Nov 17 21:30:50 2020 -0500
@@ -346,7 +346,7 @@
   incoming: force, newest-first, bundle, rev, bookmarks, branch, patch, git, limit, no-merges, stat, graph, style, template, ssh, remotecmd, insecure, subrepos
   init: ssh, remotecmd, insecure
   locate: rev, print0, fullpath, include, exclude
-  log: follow, follow-first, date, copies, keyword, rev, line-range, removed, only-merges, user, only-branch, branch, prune, patch, git, limit, no-merges, stat, graph, style, template, include, exclude
+  log: follow, follow-first, date, copies, keyword, rev, line-range, removed, only-merges, user, only-branch, branch, bookmark, prune, patch, git, limit, no-merges, stat, graph, style, template, include, exclude
   manifest: rev, all, template
   merge: force, rev, preview, abort, tool
   outgoing: force, rev, newest-first, bookmarks, branch, patch, git, limit, no-merges, stat, graph, style, template, ssh, remotecmd, insecure, subrepos
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-log-bookmark.t	Tue Nov 17 21:30:50 2020 -0500
@@ -0,0 +1,127 @@
+Test 'hg log' with a bookmark
+
+
+Create the repository
+
+  $ hg init Test-D8973
+  $ cd Test-D8973
+  $ echo "bar" > foo.txt
+  $ hg add foo.txt
+  $ hg commit -m "Add foo in 'default'"
+
+
+Add a bookmark for topic X
+
+  $ hg branch -f sebhtml
+  marked working directory as branch sebhtml
+  (branches are permanent and global, did you want a bookmark?)
+
+  $ hg bookmark sebhtml/99991-topic-X
+  $ hg up sebhtml/99991-topic-X
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+
+  $ echo "X" > x.txt
+  $ hg add x.txt
+  $ hg commit -m "Add x.txt in 'sebhtml/99991-topic-X'"
+
+  $ hg log -B sebhtml/99991-topic-X
+  changeset:   1:29f39dea9bf9
+  branch:      sebhtml
+  bookmark:    sebhtml/99991-topic-X
+  tag:         tip
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     Add x.txt in 'sebhtml/99991-topic-X'
+  
+
+Add a bookmark for topic Y
+
+  $ hg update default
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  (leaving bookmark sebhtml/99991-topic-X)
+
+  $ echo "Y" > y.txt
+  $ hg add y.txt
+  $ hg branch -f sebhtml
+  marked working directory as branch sebhtml
+  $ hg bookmark sebhtml/99992-topic-Y
+  $ hg up sebhtml/99992-topic-Y
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg commit -m "Add y.txt in 'sebhtml/99992-topic-Y'"
+  created new head
+
+  $ hg log -B sebhtml/99992-topic-Y
+  changeset:   2:11df7969cf8d
+  branch:      sebhtml
+  bookmark:    sebhtml/99992-topic-Y
+  tag:         tip
+  parent:      0:eaea25376a59
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     Add y.txt in 'sebhtml/99992-topic-Y'
+  
+
+The log of topic Y does not interfere with the log of topic X
+
+  $ hg log -B sebhtml/99991-topic-X
+  changeset:   1:29f39dea9bf9
+  branch:      sebhtml
+  bookmark:    sebhtml/99991-topic-X
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     Add x.txt in 'sebhtml/99991-topic-X'
+  
+
+Merge topics Y and X in the default branch
+
+  $ hg update default
+  0 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  (leaving bookmark sebhtml/99992-topic-Y)
+
+  $ hg bookmark
+     sebhtml/99991-topic-X     1:29f39dea9bf9
+     sebhtml/99992-topic-Y     2:11df7969cf8d
+
+  $ hg merge sebhtml/99992-topic-Y
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  (branch merge, don't forget to commit)
+
+  $ hg commit -m "Merge branch 'sebhtml/99992-topic-Y' into 'default'"
+
+  $ hg update default
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+
+  $ hg merge sebhtml/99991-topic-X
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  (branch merge, don't forget to commit)
+
+  $ hg commit -m "Merge branch 'sebhtml/99991-topic-X' into 'default'"
+
+
+Check the log of topic X, topic Y, and default branch
+
+  $ hg log -B sebhtml/99992-topic-Y
+
+  $ hg log -B sebhtml/99991-topic-X
+
+  $ hg log -b default
+  changeset:   4:c26ba8c1e1cb
+  tag:         tip
+  parent:      3:2189f3fb90d6
+  parent:      1:29f39dea9bf9
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     Merge branch 'sebhtml/99991-topic-X' into 'default'
+  
+  changeset:   3:2189f3fb90d6
+  parent:      0:eaea25376a59
+  parent:      2:11df7969cf8d
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     Merge branch 'sebhtml/99992-topic-Y' into 'default'
+  
+  changeset:   0:eaea25376a59
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     Add foo in 'default'
+