Mercurial > public > mercurial-scm > hg-stable
diff mercurial/scmutil.py @ 41942:232d4b9d391a
uncommit: move _movedirstate() to scmutil for reuse
The function should be applicable generically when moving from one
commit to another. I'll try to add more callers when I find time.
I'm not convinced it's handling all the cases correctly, but we should
have a generic function for this kind of operation, so I think it
belongs somewhere in core (not in the uncommit extension).
Differential Revision: https://phab.mercurial-scm.org/D6119
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Mon, 11 Mar 2019 09:42:29 -0700 |
parents | 64de5f44eec3 |
children | ad4a3e2eedb3 |
line wrap: on
line diff
--- a/mercurial/scmutil.py Mon Mar 11 09:20:26 2019 -0700 +++ b/mercurial/scmutil.py Mon Mar 11 09:42:29 2019 -0700 @@ -28,6 +28,7 @@ ) from . import ( + copies as copiesmod, encoding, error, match as matchmod, @@ -1253,6 +1254,44 @@ elif not dryrun: wctx.copy(origsrc, dst) +def movedirstate(repo, newctx, match=None): + """Move the dirstate to newctx and adjust it as necessary.""" + oldctx = repo['.'] + ds = repo.dirstate + ds.setparents(newctx.node(), nullid) + copies = dict(ds.copies()) + s = newctx.status(oldctx, match=match) + for f in s.modified: + if ds[f] == 'r': + # modified + removed -> removed + continue + ds.normallookup(f) + + for f in s.added: + if ds[f] == 'r': + # added + removed -> unknown + ds.drop(f) + elif ds[f] != 'a': + ds.add(f) + + for f in s.removed: + if ds[f] == 'a': + # removed + added -> normal + ds.normallookup(f) + elif ds[f] != 'r': + ds.remove(f) + + # Merge old parent and old working dir copies + oldcopies = copiesmod.pathcopies(newctx, oldctx, match) + oldcopies.update(copies) + copies = dict((dst, oldcopies.get(src, src)) + for dst, src in oldcopies.iteritems()) + # Adjust the dirstate copies + for dst, src in copies.iteritems(): + if (src not in newctx or dst in newctx or ds[dst] != 'a'): + src = None + ds.copy(src, dst) + def writerequires(opener, requirements): with opener('requires', 'w', atomictemp=True) as fp: for r in sorted(requirements):