If you list a certificate which is a direct descendant of the root certificate the certificate view in kleopatra shows:
-> root
-> root
-> certthus the root is inserted in the view twice. I added a test option to gpgme's run-keylist which uses the correct algorithm starting at cert:
next_cert:
/* List the certificate and save the chain_id. */
[...]
if (chain_id && *chain_id
&& (!last_chain_id || strcmp (last_chain_id, chain_id)))
{
if (++with_chain > 30)
{
fprintf (stderr, PGM ": certificate chain too long - circle?\n");
exit (1);
}
free (last_chain_id);
last_chain_id = xstrdup (chain_id);
err = gpgme_op_keylist_start (ctx, chain_id, 0);
fail_if_err (err);
goto next_cert;
}