[PATCH 3 of 5] merge: add conflict marker formatter

Mads Kiilerich mads at kiilerich.com
Fri May 9 05:37:05 CDT 2014


On 05/09/2014 02:33 AM, Durham Goode wrote:
> # HG changeset patch
> # User Durham Goode <durham at fb.com>
> # Date 1399593022 25200
> #      Thu May 08 16:50:22 2014 -0700
> # Node ID 69cf8247537f72bf864d4799270329a7ef00b04f
> # Parent  7ff0390bd790e788b91b85dee7fecd00d588ba4f
> merge: add conflict marker formatter
>
> Adds a conflict marker formatter that can produce custom conflict marker
> descriptions. It can be set via ui.conflictmarkertemplate.

Shouldn't that be merge.conflictmarkertemplate ?

Most other templates are real templates in mercurial/templates/map* . Is 
this template so special that it deserves special treatment?

It might be more convenient to customize in the configuration than to 
customize templates ... but that could perhaps be addressed by fully 
merging the config and template name spaces or making it possible to 
overrule templates in configuration.

> The default format is similar to:
>
>    {shortest(node)} {tag} {branch} {bookmarks} - "{desc|firstline}"

Perhaps out of scope, but I would like to have a generic one-liner style 
that could be used everywhere - also here. For instance, most tests 
define their own one-liner style for log ... and tests which don't 
probably should.

> And renders as:
>
>    contextblahblah
>    <<<<<<< local: 50c3  - "Merge 1"
>    line from my changes
>    =======
>    line from the other changes
>    >>>>>>> other: 40d1  - "two -> two-point-one"
>    morecontextblahblah
>
> diff --git a/mercurial/filemerge.py b/mercurial/filemerge.py
> --- a/mercurial/filemerge.py
> +++ b/mercurial/filemerge.py
> @@ -7,7 +7,7 @@
>   
>   from node import short
>   from i18n import _
> -import util, simplemerge, match, error
> +import util, simplemerge, match, error, templater, templatekw
>   import os, tempfile, re, filecmp
>   
>   def _toolstr(ui, tool, part, default=""):
> @@ -269,6 +269,46 @@
>           return True, r
>       return False, 0
>   
> +_defaultmarkertemplate = ('{shortest(node)}' +
> +    '{ifeq(tags, "tip", " ", " {tags}")}' +
> +    '{if(bookmarks, " {bookmarks}")}' +
> +    '{ifeq(branch, "default", "", " {branch}")}' +
> +    ' - "{desc|firstline}"')
> +
> +def _formatlabels(repo, fcd, fco, labels):
> +    """Formats the given labels using the conflict marker template.
> +
> +    Returns a list of formatted labels.
> +    """
> +    cd = fcd.changectx()
> +    co = fco.changectx()
> +    if cd.node() is None:
> +        cd = cd.p1()
> +
> +    ui = repo.ui
> +    tmpl = ui.config('ui', 'conflictmarkertemplate', _defaultmarkertemplate)
> +    tmpl = templater.parsestring(tmpl, quoted=False)
> +    t = templater.templater(None, cache={ 'marker': tmpl })
> +
> +    def getmarker(label, ctx):
> +        props = templatekw.keywords.copy()
> +        props['templ'] = t
> +        props['ctx'] = ctx
> +        props['repo'] = repo
> +        mark = '%s: %s' % (label, templater.stringify(t('marker', **props)))
> +
> +        # The <<< marks add 8 to the length, and '...' adds three, so max
> +        # length of the actual marker is 69.
> +        maxlength = 80 - 8 - 3
> +        if len(mark) > maxlength:
> +            mark = mark[:maxlength] + '...'

That looks like a missing ellipsis feature in the template language ;-)

> +        return mark
> +
> +    return [
> +        getmarker(labels[0], cd),
> +        getmarker(labels[1], co),
> +    ]
> +
>   def filemerge(repo, mynode, orig, fcd, fco, fca):
>       """perform a 3-way merge in the working directory
>   
> @@ -327,8 +367,9 @@
>       ui.debug("my %s other %s ancestor %s\n" % (fcd, fco, fca))
>   
>       labels = ['local', 'other']
> +    formattedlabels = _formatlabels(repo, fcd, fco, labels)
>       needcheck, r = func(repo, mynode, orig, fcd, fco, fca, toolconf,
> -                        (a, b, c, back), labels=labels)
> +                        (a, b, c, back), labels=formattedlabels)
>       if not needcheck:
>           if r:
>               if onfailure:
> diff --git a/tests/test-commit-amend.t b/tests/test-commit-amend.t
> --- a/tests/test-commit-amend.t
> +++ b/tests/test-commit-amend.t
> @@ -588,7 +588,7 @@
>     $ hg resolve -m cc
>     $ hg ci -m 'merge bar'
>     $ hg log --config diff.git=1 -pr .
> -  changeset:   23:d51446492733
> +  changeset:   23:7982f58410ed

Do you know why the hashes change? Do the test do weird stuff like 
committing the conflict markers?

/Mads


More information about the Mercurial-devel mailing list