int
rpmostree_db_builtin_version (int argc, char **argv, GCancellable *cancellable, GError **error)
{
  int exit_status = EXIT_FAILURE;
  GOptionContext *context;
  gs_unref_object OstreeRepo *repo = NULL;
  gs_unref_ptrarray GPtrArray *revs = NULL;
  gint ii;

  context = g_option_context_new ("COMMIT... - Show rpmdb version of packages within the commits");

  if (!rpmostree_db_option_context_parse (context, db_version_entries, &argc, &argv, &repo,
                                          cancellable, error))
    goto out;

  revs = g_ptr_array_new ();

  for (ii = 1; ii < argc; ii++)
    g_ptr_array_add (revs, argv[ii]);

  if (!_builtin_db_version (repo, revs, cancellable, error))
    goto out;

  exit_status = EXIT_SUCCESS;

out:
  g_option_context_free (context);

  return exit_status;
}
static gboolean
_builtin_db_version (OstreeRepo *repo, GPtrArray *revs,
                     GCancellable   *cancellable,
                     GError        **error)
{
  for (guint num = 0; num < revs->len; num++)
    {
      char *rev = revs->pdata[num];
      g_autoptr(RpmRevisionData) rpmrev = NULL;
      g_autofree char *rpmdbv = NULL;
      char *mrev = strstr (rev, "..");

      if (mrev)
        {
          g_autoptr(GPtrArray) range_revs = NULL;
          g_autofree char *revdup = g_strdup (rev);

          mrev = revdup + (mrev - rev);
          *mrev = 0;
          mrev += 2;

          if (!*mrev)
            mrev = NULL;

          range_revs = _rpmostree_util_get_commit_hashes (repo, revdup, mrev, cancellable, error);
          if (!range_revs)
            return FALSE;

          if (!_builtin_db_version (repo, range_revs,
                                    cancellable, error))
            return FALSE;

          continue;
        }

        rpmrev = rpmrev_new (repo, rev, NULL, cancellable, error);
        if (!rpmrev)
          return FALSE;

        rpmdbv = rpmhdrs_rpmdbv (rpmrev_get_headers (rpmrev),
                                 cancellable, error);
        if (rpmdbv == NULL)
          return FALSE;

        /* FIXME: g_console? */
        if (!g_str_equal (rev, rpmrev_get_commit (rpmrev)))
          printf ("ostree commit: %s (%s)\n", rev, rpmrev_get_commit (rpmrev));
        else
          printf ("ostree commit: %s\n", rev);

        printf ("  rpmdbv is: %66s\n", rpmdbv);
      }

  return TRUE;
}
gboolean
rpmostree_db_builtin_version (int argc, char **argv,
                              RpmOstreeCommandInvocation *invocation,
                              GCancellable *cancellable, GError **error)
{
  g_autoptr(GOptionContext) context =
    g_option_context_new ("COMMIT...");

  g_autoptr(OstreeRepo) repo = NULL;
  if (!rpmostree_db_option_context_parse (context, db_version_entries, &argc,
                                          &argv, invocation, &repo, cancellable,
                                          error))
    return FALSE;

  g_autoptr(GPtrArray) revs = g_ptr_array_new ();

  for (int ii = 1; ii < argc; ii++)
    g_ptr_array_add (revs, argv[ii]);

  if (!_builtin_db_version (repo, revs, cancellable, error))
    return FALSE;

  return TRUE;
}
static gboolean
_builtin_db_version (OstreeRepo *repo, GPtrArray *revs,
                     GCancellable   *cancellable,
                     GError        **error)
{
  int num = 0;
  gboolean ret = FALSE;

  for (num = 0; num < revs->len; num++)
    {
      char *rev = revs->pdata[num];
      _cleanup_rpmrev_ struct RpmRevisionData *rpmrev = NULL;
      gs_free char *rpmdbv = NULL;
      char *mrev = strstr (rev, "..");

      if (mrev)
        {
          gs_unref_ptrarray GPtrArray *range_revs = NULL;
          gs_free char *revdup = g_strdup (rev);

          mrev = revdup + (mrev - rev);
          *mrev = 0;
          mrev += 2;

          if (!*mrev)
            mrev = NULL;

          range_revs = _rpmostree_util_get_commit_hashes (repo, revdup, mrev, cancellable, error);
          if (!range_revs)
            goto out;

          if (!_builtin_db_version (repo, range_revs,
                                    cancellable, error))
            goto out;

          continue;
        }

        rpmrev = rpmrev_new (repo, rev, NULL, cancellable, error);
        if (!rpmrev)
          goto out;

        rpmdbv = rpmhdrs_rpmdbv (rpmrev_get_headers (rpmrev),
                                 cancellable, error);
        if (rpmdbv == NULL)
          goto out;

        /* FIXME: g_console? */
        if (!g_str_equal (rev, rpmrev_get_commit (rpmrev)))
          printf ("ostree commit: %s (%s)\n", rev, rpmrev_get_commit (rpmrev));
        else
          printf ("ostree commit: %s\n", rev);

        printf ("  rpmdbv is: %66s\n", rpmdbv);
      }

  ret = TRUE;

 out:
  return ret;
}