# HG changeset patch # User Martin von Zweigbergk # Date 1602800196 25200 # Node ID 4a38b12141b5a94e4492ddeed507f008196f995c # Parent edb026c44f61b614f13adf2102a2e15261de8726 evolvecmd: extract function for rewriting hashes in commit message The logic for rewriting hashes is somewhat long and well isolated, so a function makes good sense. diff -r edb026c44f61 -r 4a38b12141b5 hgext3rd/evolve/evolvecmd.py --- a/hgext3rd/evolve/evolvecmd.py Tue Feb 09 19:11:39 2021 +0800 +++ b/hgext3rd/evolve/evolvecmd.py Thu Oct 15 15:16:36 2020 -0700 @@ -896,6 +896,42 @@ ordering.extend(sorted(dependencies)) return ordering +def _rewrite_commit_message_hashes(repo, commitmsg): + """filter a commit description to update has to their successors + + The goal of this function is to avoid description referencing obsolete + hashes when a stack is rewritten or evolved. + + Each hash in the description will be detected and, if matching an obsolete + changeset, it will be replaced by its successors. + + + Note: They might be case were such behavior might be is wrong, for example + if the commit message is explicitely referencing an older, obsolete changesets. + """ + cache = {} + sha1s = re.findall(sha1re, commitmsg) + unfi = repo.unfiltered() + for sha1 in sha1s: + fullnode = scmutil.resolvehexnodeidprefix(unfi, sha1) + if fullnode is None: + continue + ctx = unfi[fullnode] + if not ctx.obsolete(): + continue + + successors = obsutil.successorssets(repo, ctx.node(), cache=cache) + + # We can't make any assumptions about how to update the hash if the + # cset in question was split or diverged. + if len(successors) == 1 and len(successors[0]) == 1: + newsha1 = nodemod.hex(successors[0][0]) + commitmsg = commitmsg.replace(sha1, newsha1[:len(sha1)]) + else: + repo.ui.note(_(b'The stale commit message reference to %s could ' + b'not be updated\n') % sha1) + return commitmsg + def _relocate(repo, orig, dest, evolvestate, pctx=None, keepbranch=False, category=None): """rewrites the orig rev on dest rev @@ -917,29 +953,7 @@ raise error.ProgrammingError(msg, hint) pctx = orig.p1() - commitmsg = orig.description() - - cache = {} - sha1s = re.findall(sha1re, commitmsg) - unfi = repo.unfiltered() - for sha1 in sha1s: - fullnode = scmutil.resolvehexnodeidprefix(unfi, sha1) - if fullnode is None: - continue - ctx = unfi[fullnode] - if not ctx.obsolete(): - continue - - successors = obsutil.successorssets(repo, ctx.node(), cache=cache) - - # We can't make any assumptions about how to update the hash if the - # cset in question was split or diverged. - if len(successors) == 1 and len(successors[0]) == 1: - newsha1 = nodemod.hex(successors[0][0]) - commitmsg = commitmsg.replace(sha1, newsha1[:len(sha1)]) - else: - repo.ui.note(_(b'The stale commit message reference to %s could ' - b'not be updated\n') % sha1) + commitmsg = _rewrite_commit_message_hashes(repo, orig.description()) tr = repo.currenttransaction() assert tr is not None