Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.0 on 2022-01-18 at 14:00 UTC+1 (a few minutes of down time)

Commit 040162b3 authored by Georges Racinet's avatar Georges Racinet 🦑
Browse files

utils.hg: high level method to extract changesets info

This will be, I hope, more readable than the pervasive
"log with `:` separated template / split" pattern
we've been using.

Especially, I expect failures to be more readily understandable.

Using namedtuples for the extracts so that it's admissible in
dicts and sets. The drawback is that we have to return the class
as well, for litteral comparison. Each call gives a new class
but instances of two such namedtuple classes are comparable.
parent 1527ad0aedba
from collections import namedtuple
import json
import logging
import os
......@@ -124,6 +125,42 @@ class LocalRepo(object):
cmd.append('--hidden')
return self.hg(*cmd)
def changeset_extracts(self, template_exprs,
collection=tuple,
revs=None,
val_sep='|:|', eol="\n"):
"""Extract information from changesets, as a collection of entries.
This doens't represent the graph information, but the ordering given
by `hg log`, on which this method is based, is preserved if using
an ordered collection.
:param template_exprs: an iterable of template keywords.
Examples: `['rev', 'phase', 'desc']`. Later on, for more complicated
expressions, we could allow ``template_exprs`` elements to be
``('key', 'expr')`` pairs, where the key would be used in the
as key of the resulting class:`dict` and `expr` would be evaluated.
as template expressions, and keys
:param collection: the wished collection class. Must be instantiable
by a generator expression, like `set` and `tuple` are.
:return: a named tuple class whose attributes are the elements of
``template_exprs`` and a set of instances of that class,
one for each changeset.
WARNING: there is no escaping. The command will fail utterly
if the rendering of one template expression contains ``val_sep`` or
` ``eol``.
"""
template = val_sep.join("{%s}" % expr for expr in template_exprs)
cmd = ['log', '-T', template + eol]
if revs is not None:
cmd.extend(('-r', revs))
Extract = namedtuple('Extract', template_exprs)
return Extract, collection(Extract(*line.split(val_sep))
for line in self.hg(*cmd).split(eol)
if line)
def branches(self):
"""Return `hg branches` as a set of names."""
return set(self.hg('branches', '-T', '{branch}\n').splitlines())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment