def getconfigstr(configitems, ui=None):
    """Given a set of configuration entries of the form (section, item, value),
       a string in the hgrc format is generated.
       Giving the 'ui' argument makes sure values are taken from that argument,
       ignoring the 'value' entries in configitems."""
    data = []
    configitems.sort()
    previoussection = None
    for section, item, value in configitems:
        if ui and not ui.hasconfig(section, item):
            continue
        if not previoussection or section != previoussection:
            data.append(b'[%s]' % section)
        previoussection = section
        if ui:
            value = ui.config(section, item)
        if value is None:
            value = ''
        data.append(b'%s=%s' % (item, value))
    if not data:
        return ''
    data.append(b'')
    return b'\n'.join(data)
