# HG changeset patch # User Pierre-Yves David # Date 1500779233 -7200 # Node ID 3137185b1bfe17602b22d659258c648605e5f102 # Parent 684feae20be555cf23047a9edfc2804a11bb1671 rewriteutil: move the 'rewrite' function diff -r 684feae20be5 -r 3137185b1bfe hgext3rd/evolve/__init__.py --- a/hgext3rd/evolve/__init__.py Sun Jul 23 04:54:42 2017 +0200 +++ b/hgext3rd/evolve/__init__.py Sun Jul 23 05:07:13 2017 +0200 @@ -318,6 +318,7 @@ commitopts3 = evocommands.commitopts3 interactiveopt = evocommands.interactiveopt _bookmarksupdater = rewriteutil.bookmarksupdater +rewrite = rewriteutil.rewrite # This extension contains the following code # @@ -807,92 +808,6 @@ ### changeset rewriting logic ############################# -def rewrite(repo, old, updates, head, newbases, commitopts): - """Return (nodeid, created) where nodeid is the identifier of the - changeset generated by the rewrite process, and created is True if - nodeid was actually created. If created is False, nodeid - references a changeset existing before the rewrite call. - """ - wlock = lock = tr = None - try: - wlock = repo.wlock() - lock = repo.lock() - tr = repo.transaction('rewrite') - if len(old.parents()) > 1: # XXX remove this unnecessary limitation. - raise error.Abort(_('cannot amend merge changesets')) - base = old.p1() - updatebookmarks = _bookmarksupdater(repo, old.node(), tr) - - # commit a new version of the old changeset, including the update - # collect all files which might be affected - files = set(old.files()) - for u in updates: - files.update(u.files()) - - # Recompute copies (avoid recording a -> b -> a) - copied = copies.pathcopies(base, head) - - # prune files which were reverted by the updates - def samefile(f): - if f in head.manifest(): - a = head.filectx(f) - if f in base.manifest(): - b = base.filectx(f) - return (a.data() == b.data() - and a.flags() == b.flags()) - else: - return False - else: - return f not in base.manifest() - files = [f for f in files if not samefile(f)] - # commit version of these files as defined by head - headmf = head.manifest() - - def filectxfn(repo, ctx, path): - if path in headmf: - fctx = head[path] - flags = fctx.flags() - mctx = context.memfilectx(repo, fctx.path(), fctx.data(), - islink='l' in flags, - isexec='x' in flags, - copied=copied.get(path)) - return mctx - return None - - message = cmdutil.logmessage(repo.ui, commitopts) - if not message: - message = old.description() - - user = commitopts.get('user') or old.user() - # TODO: In case not date is given, we should take the old commit date - # if we are working one one changeset or mimic the fold behavior about - # date - date = commitopts.get('date') or None - extra = dict(commitopts.get('extra', old.extra())) - extra['branch'] = head.branch() - - new = context.memctx(repo, - parents=newbases, - text=message, - files=files, - filectxfn=filectxfn, - user=user, - date=date, - extra=extra) - - if commitopts.get('edit'): - new._text = cmdutil.commitforceeditor(repo, new, []) - revcount = len(repo) - newid = repo.commitctx(new) - new = repo[newid] - created = len(repo) != revcount - updatebookmarks(newid) - - tr.close() - return newid, created - finally: - lockmod.release(tr, lock, wlock) - class MergeFailure(error.Abort): pass diff -r 684feae20be5 -r 3137185b1bfe hgext3rd/evolve/rewriteutil.py --- a/hgext3rd/evolve/rewriteutil.py Sun Jul 23 04:54:42 2017 +0200 +++ b/hgext3rd/evolve/rewriteutil.py Sun Jul 23 05:07:13 2017 +0200 @@ -12,7 +12,11 @@ # commands). from mercurial import ( + cmdutil, + context, + copies, error, + lock as lockmod, obsolete, phases, revset, @@ -64,3 +68,89 @@ hint = _("new unstable changesets are not allowed") raise error.Abort(msg, hint=hint) return root, head + +def rewrite(repo, old, updates, head, newbases, commitopts): + """Return (nodeid, created) where nodeid is the identifier of the + changeset generated by the rewrite process, and created is True if + nodeid was actually created. If created is False, nodeid + references a changeset existing before the rewrite call. + """ + wlock = lock = tr = None + try: + wlock = repo.wlock() + lock = repo.lock() + tr = repo.transaction('rewrite') + if len(old.parents()) > 1: # XXX remove this unnecessary limitation. + raise error.Abort(_('cannot amend merge changesets')) + base = old.p1() + updatebookmarks = bookmarksupdater(repo, old.node(), tr) + + # commit a new version of the old changeset, including the update + # collect all files which might be affected + files = set(old.files()) + for u in updates: + files.update(u.files()) + + # Recompute copies (avoid recording a -> b -> a) + copied = copies.pathcopies(base, head) + + # prune files which were reverted by the updates + def samefile(f): + if f in head.manifest(): + a = head.filectx(f) + if f in base.manifest(): + b = base.filectx(f) + return (a.data() == b.data() + and a.flags() == b.flags()) + else: + return False + else: + return f not in base.manifest() + files = [f for f in files if not samefile(f)] + # commit version of these files as defined by head + headmf = head.manifest() + + def filectxfn(repo, ctx, path): + if path in headmf: + fctx = head[path] + flags = fctx.flags() + mctx = context.memfilectx(repo, fctx.path(), fctx.data(), + islink='l' in flags, + isexec='x' in flags, + copied=copied.get(path)) + return mctx + return None + + message = cmdutil.logmessage(repo.ui, commitopts) + if not message: + message = old.description() + + user = commitopts.get('user') or old.user() + # TODO: In case not date is given, we should take the old commit date + # if we are working one one changeset or mimic the fold behavior about + # date + date = commitopts.get('date') or None + extra = dict(commitopts.get('extra', old.extra())) + extra['branch'] = head.branch() + + new = context.memctx(repo, + parents=newbases, + text=message, + files=files, + filectxfn=filectxfn, + user=user, + date=date, + extra=extra) + + if commitopts.get('edit'): + new._text = cmdutil.commitforceeditor(repo, new, []) + revcount = len(repo) + newid = repo.commitctx(new) + new = repo[newid] + created = len(repo) != revcount + updatebookmarks(newid) + + tr.close() + return newid, created + finally: + lockmod.release(tr, lock, wlock)