def getrevstofix(ui, repo, opts): """Returns the set of revision numbers that should be fixed""" if opts[b'all']: revs = repo.revs(b'(not public() and not obsolete()) or wdir()') elif opts[b'source']: source_revs = scmutil.revrange(repo, opts[b'source']) revs = set(repo.revs(b'(%ld::) - obsolete()', source_revs)) if wdirrev in source_revs: # `wdir()::` is currently empty, so manually add wdir revs.add(wdirrev) if repo[b'.'].rev() in revs: revs.add(wdirrev) else: revs = set(scmutil.revrange(repo, opts[b'rev'])) if opts.get(b'working_dir'): revs.add(wdirrev) for rev in revs: checkfixablectx(ui, repo, repo[rev]) # Allow fixing only wdir() even if there's an unfinished operation if not (len(revs) == 1 and wdirrev in revs): cmdutil.checkunfinished(repo) rewriteutil.precheck(repo, revs, b'fix') if wdirrev in revs and list( mergestatemod.mergestate.read(repo).unresolved() ): raise error.Abort(b'unresolved conflicts', hint=b"use 'hg resolve'") if not revs: raise error.Abort( b'no changesets specified', hint=b'use --source or --working-dir' ) return revs
def unamend(ui, repo, **opts): """undo the most recent amend operation on a current changeset This command will roll back to the previous version of a changeset, leaving working directory in state in which it was before running `hg amend` (e.g. files modified as part of an amend will be marked as modified `hg status`) """ unfi = repo.unfiltered() with repo.wlock(), repo.lock(), repo.transaction(b'unamend'): # identify the commit from which to unamend curctx = repo[b'.'] rewriteutil.precheck(repo, [curctx.rev()], b'unamend') # identify the commit to which to unamend markers = list(predecessormarkers(curctx)) if len(markers) != 1: e = _( b"changeset must have one predecessor, found %i predecessors") raise error.Abort(e % len(markers)) prednode = markers[0].prednode() predctx = unfi[prednode] # add an extra so that we get a new hash # note: allowing unamend to undo an unamend is an intentional feature extras = predctx.extra() extras[b'unamend_source'] = curctx.hex() def filectxfn(repo, ctx_, path): try: return predctx.filectx(path) except KeyError: return None # Make a new commit same as predctx newctx = context.memctx( repo, parents=(predctx.p1(), predctx.p2()), text=predctx.description(), files=predctx.files(), filectxfn=filectxfn, user=predctx.user(), date=predctx.date(), extra=extras, ) newprednode = repo.commitctx(newctx) newpredctx = repo[newprednode] dirstate = repo.dirstate with dirstate.parentchange(): scmutil.movedirstate(repo, newpredctx) mapping = {curctx.node(): (newprednode, )} scmutil.cleanupnodes(repo, mapping, b'unamend', fixphase=True)
def uncommit(ui, repo, *pats, **opts): """uncommit part or all of a local changeset This command undoes the effect of a local commit, returning the affected files to their uncommitted state. This means that files modified or deleted in the changeset will be left unchanged, and so will remain modified in the working directory. If no files are specified, the commit will be pruned, unless --keep is given. """ opts = pycompat.byteskwargs(opts) with repo.wlock(), repo.lock(): if not pats and not repo.ui.configbool('experimental', 'uncommitondirtywdir'): cmdutil.bailifchanged(repo) old = repo['.'] rewriteutil.precheck(repo, [old.rev()], 'uncommit') if len(old.parents()) > 1: raise error.Abort(_("cannot uncommit merge changeset")) with repo.transaction('uncommit'): match = scmutil.match(old, pats, opts) keepcommit = opts.get('keep') or pats newid = _commitfiltered(repo, old, match, keepcommit) if newid is None: ui.status(_("nothing to uncommit\n")) return 1 mapping = {} if newid != old.p1().node(): # Move local changes on filtered changeset mapping[old.node()] = (newid, ) else: # Fully removed the old commit mapping[old.node()] = () scmutil.cleanupnodes(repo, mapping, 'uncommit') with repo.dirstate.parentchange(): repo.dirstate.setparents(newid, node.nullid) s = repo.status(old.p1(), old, match=match) _fixdirstate(repo, old, repo[newid], s)
def uncommit(ui, repo, *pats, **opts): """uncommit part or all of a local changeset This command undoes the effect of a local commit, returning the affected files to their uncommitted state. This means that files modified or deleted in the changeset will be left unchanged, and so will remain modified in the working directory. If no files are specified, the commit will be pruned, unless --keep is given. """ opts = pycompat.byteskwargs(opts) cmdutil.checknotesize(ui, opts) cmdutil.resolvecommitoptions(ui, opts) with repo.wlock(), repo.lock(): m, a, r, d = repo.status()[:4] isdirtypath = any(set(m + a + r + d) & set(pats)) allowdirtywcopy = opts[ b'allow_dirty_working_copy'] or repo.ui.configbool( b'experimental', b'uncommitondirtywdir') if not allowdirtywcopy and (not pats or isdirtypath): cmdutil.bailifchanged( repo, hint=_(b'requires --allow-dirty-working-copy to uncommit'), ) old = repo[b'.'] rewriteutil.precheck(repo, [old.rev()], b'uncommit') if len(old.parents()) > 1: raise error.Abort(_(b"cannot uncommit merge changeset")) match = scmutil.match(old, pats, opts) # Check all explicitly given files; abort if there's a problem. if match.files(): s = old.status(old.p1(), match, listclean=True) eligible = set(s.added) | set(s.modified) | set(s.removed) badfiles = set(match.files()) - eligible # Naming a parent directory of an eligible file is OK, even # if not everything tracked in that directory can be # uncommitted. if badfiles: badfiles -= {f for f in util.dirs(eligible)} for f in sorted(badfiles): if f in s.clean: hint = _( b"file was not changed in working directory parent") elif repo.wvfs.exists(f): hint = _(b"file was untracked in working directory parent") else: hint = _(b"file does not exist") raise error.Abort( _(b'cannot uncommit "%s"') % scmutil.getuipathfn(repo)(f), hint=hint, ) with repo.transaction(b'uncommit'): if not (opts[b'message'] or opts[b'logfile']): opts[b'message'] = old.description() message = cmdutil.logmessage(ui, opts) keepcommit = pats if not keepcommit: if opts.get(b'keep') is not None: keepcommit = opts.get(b'keep') else: keepcommit = ui.configbool(b'experimental', b'uncommit.keep') newid = _commitfiltered( repo, old, match, keepcommit, message=message, user=opts.get(b'user'), date=opts.get(b'date'), ) if newid is None: ui.status(_(b"nothing to uncommit\n")) return 1 mapping = {} if newid != old.p1().node(): # Move local changes on filtered changeset mapping[old.node()] = (newid, ) else: # Fully removed the old commit mapping[old.node()] = () with repo.dirstate.parentchange(): scmutil.movedirstate(repo, repo[newid], match) scmutil.cleanupnodes(repo, mapping, b'uncommit', fixphase=True)
def split(ui, repo, *revs, **opts): """split a changeset into smaller ones Repeatedly prompt changes and commit message for new changesets until there is nothing left in the original changeset. If --rev was not given, split the working directory parent. By default, rebase connected non-obsoleted descendants onto the new changeset. Use --no-rebase to avoid the rebase. """ opts = pycompat.byteskwargs(opts) revlist = [] if opts.get(b'rev'): revlist.append(opts.get(b'rev')) revlist.extend(revs) with repo.wlock(), repo.lock(), repo.transaction(b'split') as tr: revs = scmutil.revrange(repo, revlist or [b'.']) if len(revs) > 1: raise error.InputError(_(b'cannot split multiple revisions')) rev = revs.first() ctx = repo[rev] # Handle nullid specially here (instead of leaving for precheck() # below) so we get a nicer message and error code. if rev is None or ctx.node() == nullid: ui.status(_(b'nothing to split\n')) return 1 if ctx.node() is None: raise error.InputError(_(b'cannot split working directory')) if opts.get(b'rebase'): # Skip obsoleted descendants and their descendants so the rebase # won't cause conflicts for sure. descendants = list(repo.revs(b'(%d::) - (%d)', rev, rev)) torebase = list( repo.revs(b'%ld - (%ld & obsolete())::', descendants, descendants)) else: torebase = [] rewriteutil.precheck(repo, [rev] + torebase, b'split') if len(ctx.parents()) > 1: raise error.InputError(_(b'cannot split a merge changeset')) cmdutil.bailifchanged(repo) # Deactivate bookmark temporarily so it won't get moved unintentionally bname = repo._activebookmark if bname and repo._bookmarks[bname] != ctx.node(): bookmarks.deactivate(repo) wnode = repo[b'.'].node() top = None try: top = dosplit(ui, repo, tr, ctx, opts) finally: # top is None: split failed, need update --clean recovery. # wnode == ctx.node(): wnode split, no need to update. if top is None or wnode != ctx.node(): hg.clean(repo, wnode, show_stats=False) if bname: bookmarks.activate(repo, bname) if torebase and top: dorebase(ui, repo, torebase, top)