[PATCH 1 of 2] resolve: move resolve in cmdutil
Siddharth Agarwal
sid at less-broken.com
Wed Dec 2 23:45:09 CST 2015
On 12/2/15 16:41, Laurent Charignon wrote:
> # HG changeset patch
> # User Laurent Charignon <lcharignon at fb.com>
> # Date 1449103124 28800
> # Wed Dec 02 16:38:44 2015 -0800
> # Node ID 8d5a092b64d9ddb7f6481bfe16aaeb6a7dab5951
> # Parent 91905c0c47377520eebdedcfa6bcb73b77e24e0c
> resolve: move resolve in cmdutil
Outside of the discussion about patch 2, +1 on patch 1 -- it unblocks
some work I'm doing.
>
> Having resolve in cmdutil makes it easier to call it outside the commands
> module. This patch moves all the code from resolve from commands to cmdutil.
>
> diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
> --- a/mercurial/cmdutil.py
> +++ b/mercurial/cmdutil.py
> @@ -2310,6 +2310,165 @@ def forget(ui, repo, match, prefix, expl
> forgot.extend(f for f in forget if f not in rejected)
> return bad, forgot
>
> +def resolve(ui, repo, *pats, **opts):
> + import merge as mergemod
> + all, mark, unmark, show, nostatus = \
> + [opts.get(o) for o in 'all mark unmark list no_status'.split()]
> +
> + if (show and (mark or unmark)) or (mark and unmark):
> + raise error.Abort(_("too many options specified"))
> + if pats and all:
> + raise error.Abort(_("can't specify --all and patterns"))
> + if not (all or pats or show or mark or unmark):
> + raise error.Abort(_('no files or directories specified'),
> + hint=('use --all to re-merge all unresolved files'))
> +
> + if show:
> + fm = ui.formatter('resolve', opts)
> + ms = mergemod.mergestate.read(repo)
> + m = scmutil.match(repo[None], pats, opts)
> + for f in ms:
> + if not m(f):
> + continue
> + l = 'resolve.' + {'u': 'unresolved', 'r': 'resolved',
> + 'd': 'driverresolved'}[ms[f]]
> + fm.startitem()
> + fm.condwrite(not nostatus, 'status', '%s ', ms[f].upper(), label=l)
> + fm.write('path', '%s\n', f, label=l)
> + fm.end()
> + return 0
> +
> + wlock = repo.wlock()
> + try:
> + ms = mergemod.mergestate.read(repo)
> +
> + if not (ms.active() or repo.dirstate.p2() != nullid):
> + raise error.Abort(
> + _('resolve command not applicable when not merging'))
> +
> + wctx = repo[None]
> +
> + if ms.mergedriver and ms.mdstate() == 'u':
> + proceed = mergemod.driverpreprocess(repo, ms, wctx)
> + ms.commit()
> + # allow mark and unmark to go through
> + if not mark and not unmark and not proceed:
> + return 1
> +
> + m = scmutil.match(wctx, pats, opts)
> + ret = 0
> + didwork = False
> + runconclude = False
> +
> + tocomplete = []
> + for f in ms:
> + if not m(f):
> + continue
> +
> + didwork = True
> +
> + # don't let driver-resolved files be marked, and run the conclude
> + # step if asked to resolve
> + if ms[f] == "d":
> + exact = m.exact(f)
> + if mark:
> + if exact:
> + ui.warn(_('not marking %s as it is driver-resolved\n')
> + % f)
> + elif unmark:
> + if exact:
> + ui.warn(_('not unmarking %s as it is driver-resolved\n')
> + % f)
> + else:
> + runconclude = True
> + continue
> +
> + if mark:
> + ms.mark(f, "r")
> + elif unmark:
> + ms.mark(f, "u")
> + else:
> + # backup pre-resolve (merge uses .orig for its own purposes)
> + a = repo.wjoin(f)
> + try:
> + util.copyfile(a, a + ".resolve")
> + except (IOError, OSError) as inst:
> + if inst.errno != errno.ENOENT:
> + raise
> +
> + try:
> + # preresolve file
> + ui.setconfig('ui', 'forcemerge', opts.get('tool', ''),
> + 'resolve')
> + complete, r = ms.preresolve(f, wctx)
> + if not complete:
> + tocomplete.append(f)
> + elif r:
> + ret = 1
> + finally:
> + ui.setconfig('ui', 'forcemerge', '', 'resolve')
> + ms.commit()
> +
> + # replace filemerge's .orig file with our resolve file, but only
> + # for merges that are complete
> + if complete:
> + try:
> + util.rename(a + ".resolve", origpath(ui, repo, a))
> + except OSError as inst:
> + if inst.errno != errno.ENOENT:
> + raise
> +
> + for f in tocomplete:
> + try:
> + # resolve file
> + ui.setconfig('ui', 'forcemerge', opts.get('tool', ''),
> + 'resolve')
> + r = ms.resolve(f, wctx)
> + if r:
> + ret = 1
> + finally:
> + ui.setconfig('ui', 'forcemerge', '', 'resolve')
> + ms.commit()
> +
> + # replace filemerge's .orig file with our resolve file
> + a = repo.wjoin(f)
> + try:
> + util.rename(a + ".resolve", origpath(ui, repo, a))
> + except OSError as inst:
> + if inst.errno != errno.ENOENT:
> + raise
> +
> + ms.commit()
> + ms.recordactions()
> +
> + if not didwork and pats:
> + ui.warn(_("arguments do not match paths that need resolving\n"))
> + elif ms.mergedriver and ms.mdstate() != 's':
> + # run conclude step when either a driver-resolved file is requested
> + # or there are no driver-resolved files
> + # we can't use 'ret' to determine whether any files are unresolved
> + # because we might not have tried to resolve some
> + if ((runconclude or not list(ms.driverresolved()))
> + and not list(ms.unresolved())):
> + proceed = mergemod.driverconclude(repo, ms, wctx)
> + ms.commit()
> + if not proceed:
> + return 1
> +
> + finally:
> + wlock.release()
> +
> + # Nudge users into finishing an unfinished operation
> + unresolvedf = list(ms.unresolved())
> + driverresolvedf = list(ms.driverresolved())
> + if not unresolvedf and not driverresolvedf:
> + ui.status(_('(no more unresolved files)\n'))
> + elif not unresolvedf:
> + ui.status(_('(no more unresolved files -- '
> + 'run "hg resolve --all" to conclude)\n'))
> +
> + return ret
> +
> def files(ui, ctx, m, fm, fmt, subrepos):
> rev = ctx.rev()
> ret = 1
> diff --git a/mercurial/commands.py b/mercurial/commands.py
> --- a/mercurial/commands.py
> +++ b/mercurial/commands.py
> @@ -5674,164 +5674,7 @@ def resolve(ui, repo, *pats, **opts):
>
> Returns 0 on success, 1 if any files fail a resolve attempt.
> """
> -
> - all, mark, unmark, show, nostatus = \
> - [opts.get(o) for o in 'all mark unmark list no_status'.split()]
> -
> - if (show and (mark or unmark)) or (mark and unmark):
> - raise error.Abort(_("too many options specified"))
> - if pats and all:
> - raise error.Abort(_("can't specify --all and patterns"))
> - if not (all or pats or show or mark or unmark):
> - raise error.Abort(_('no files or directories specified'),
> - hint=('use --all to re-merge all unresolved files'))
> -
> - if show:
> - fm = ui.formatter('resolve', opts)
> - ms = mergemod.mergestate.read(repo)
> - m = scmutil.match(repo[None], pats, opts)
> - for f in ms:
> - if not m(f):
> - continue
> - l = 'resolve.' + {'u': 'unresolved', 'r': 'resolved',
> - 'd': 'driverresolved'}[ms[f]]
> - fm.startitem()
> - fm.condwrite(not nostatus, 'status', '%s ', ms[f].upper(), label=l)
> - fm.write('path', '%s\n', f, label=l)
> - fm.end()
> - return 0
> -
> - wlock = repo.wlock()
> - try:
> - ms = mergemod.mergestate.read(repo)
> -
> - if not (ms.active() or repo.dirstate.p2() != nullid):
> - raise error.Abort(
> - _('resolve command not applicable when not merging'))
> -
> - wctx = repo[None]
> -
> - if ms.mergedriver and ms.mdstate() == 'u':
> - proceed = mergemod.driverpreprocess(repo, ms, wctx)
> - ms.commit()
> - # allow mark and unmark to go through
> - if not mark and not unmark and not proceed:
> - return 1
> -
> - m = scmutil.match(wctx, pats, opts)
> - ret = 0
> - didwork = False
> - runconclude = False
> -
> - tocomplete = []
> - for f in ms:
> - if not m(f):
> - continue
> -
> - didwork = True
> -
> - # don't let driver-resolved files be marked, and run the conclude
> - # step if asked to resolve
> - if ms[f] == "d":
> - exact = m.exact(f)
> - if mark:
> - if exact:
> - ui.warn(_('not marking %s as it is driver-resolved\n')
> - % f)
> - elif unmark:
> - if exact:
> - ui.warn(_('not unmarking %s as it is driver-resolved\n')
> - % f)
> - else:
> - runconclude = True
> - continue
> -
> - if mark:
> - ms.mark(f, "r")
> - elif unmark:
> - ms.mark(f, "u")
> - else:
> - # backup pre-resolve (merge uses .orig for its own purposes)
> - a = repo.wjoin(f)
> - try:
> - util.copyfile(a, a + ".resolve")
> - except (IOError, OSError) as inst:
> - if inst.errno != errno.ENOENT:
> - raise
> -
> - try:
> - # preresolve file
> - ui.setconfig('ui', 'forcemerge', opts.get('tool', ''),
> - 'resolve')
> - complete, r = ms.preresolve(f, wctx)
> - if not complete:
> - tocomplete.append(f)
> - elif r:
> - ret = 1
> - finally:
> - ui.setconfig('ui', 'forcemerge', '', 'resolve')
> - ms.commit()
> -
> - # replace filemerge's .orig file with our resolve file, but only
> - # for merges that are complete
> - if complete:
> - try:
> - util.rename(a + ".resolve",
> - cmdutil.origpath(ui, repo, a))
> - except OSError as inst:
> - if inst.errno != errno.ENOENT:
> - raise
> -
> - for f in tocomplete:
> - try:
> - # resolve file
> - ui.setconfig('ui', 'forcemerge', opts.get('tool', ''),
> - 'resolve')
> - r = ms.resolve(f, wctx)
> - if r:
> - ret = 1
> - finally:
> - ui.setconfig('ui', 'forcemerge', '', 'resolve')
> - ms.commit()
> -
> - # replace filemerge's .orig file with our resolve file
> - a = repo.wjoin(f)
> - try:
> - util.rename(a + ".resolve", cmdutil.origpath(ui, repo, a))
> - except OSError as inst:
> - if inst.errno != errno.ENOENT:
> - raise
> -
> - ms.commit()
> - ms.recordactions()
> -
> - if not didwork and pats:
> - ui.warn(_("arguments do not match paths that need resolving\n"))
> - elif ms.mergedriver and ms.mdstate() != 's':
> - # run conclude step when either a driver-resolved file is requested
> - # or there are no driver-resolved files
> - # we can't use 'ret' to determine whether any files are unresolved
> - # because we might not have tried to resolve some
> - if ((runconclude or not list(ms.driverresolved()))
> - and not list(ms.unresolved())):
> - proceed = mergemod.driverconclude(repo, ms, wctx)
> - ms.commit()
> - if not proceed:
> - return 1
> -
> - finally:
> - wlock.release()
> -
> - # Nudge users into finishing an unfinished operation
> - unresolvedf = list(ms.unresolved())
> - driverresolvedf = list(ms.driverresolved())
> - if not unresolvedf and not driverresolvedf:
> - ui.status(_('(no more unresolved files)\n'))
> - elif not unresolvedf:
> - ui.status(_('(no more unresolved files -- '
> - 'run "hg resolve --all" to conclude)\n'))
> -
> - return ret
> + return cmdutil.resolve(ui, repo, *pats, **opts)
>
> @command('revert',
> [('a', 'all', None, _('revert all changes when no arguments given')),
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> https://selenic.com/mailman/listinfo/mercurial-devel
More information about the Mercurial-devel
mailing list