diff mercurial/rewriteutil.py @ 47086:5b6dd0d9171b

rewriteutil: give examples of public changesets that can't be rewritten This patch copies the feature from the evolve extension. Differential Revision: https://phab.mercurial-scm.org/D10670
author Martin von Zweigbergk <martinvonz@google.com>
date Tue, 04 May 2021 10:16:34 -0700
parents ba6881c6a178
children d90f6237b3aa
line wrap: on
line diff
--- a/mercurial/rewriteutil.py	Mon May 03 15:14:09 2021 -0400
+++ b/mercurial/rewriteutil.py	Tue May 04 10:16:34 2021 -0700
@@ -17,6 +17,7 @@
 
 from . import (
     error,
+    node,
     obsolete,
     obsutil,
     revset,
@@ -28,6 +29,26 @@
 NODE_RE = re.compile(br'\b[0-9a-f]{6,64}\b')
 
 
+def _formatrevs(repo, revs, maxrevs=4):
+    """returns a string summarizing revisions in a decent size
+
+    If there are few enough revisions, we list them all. Otherwise we display a
+    summary of the form:
+
+        1ea73414a91b and 5 others
+    """
+    tonode = repo.changelog.node
+    numrevs = len(revs)
+    if numrevs < maxrevs:
+        shorts = [node.short(tonode(r)) for r in revs]
+        summary = b', '.join(shorts)
+    else:
+        first = revs.first()
+        summary = _(b'%s and %d others')
+        summary %= (node.short(tonode(first)), numrevs - 1)
+    return summary
+
+
 def precheck(repo, revs, action=b'rewrite'):
     """check if revs can be rewritten
     action is used to control the error message.
@@ -50,7 +71,8 @@
 
     publicrevs = repo.revs(b'%ld and public()', revs)
     if publicrevs:
-        msg = _(b"cannot %s public changesets") % action
+        summary = _formatrevs(repo, publicrevs)
+        msg = _(b"cannot %s public changesets: %s") % (action, summary)
         hint = _(b"see 'hg help phases' for details")
         raise error.InputError(msg, hint=hint)