From 790132a81aaea6327a7201ce1284bece98fdec4f Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Wed, 14 Aug 2013 11:25:32 +0200 Subject: [PATCH] Intelligible display for 'ostree show' Show something similar to git metadata display. Show raw variant data when --raw is specified https://bugzilla.gnome.org/show_bug.cgi?id=705973 --- Makefile-ostree.am | 2 + src/ostree/ot-builtin-show.c | 56 +++++++------- src/ostree/ot-dump.c | 139 +++++++++++++++++++++++++++++++++++ src/ostree/ot-dump.h | 39 ++++++++++ tests/test-basic.sh | 9 ++- 5 files changed, 217 insertions(+), 28 deletions(-) create mode 100644 src/ostree/ot-dump.c create mode 100644 src/ostree/ot-dump.h diff --git a/Makefile-ostree.am b/Makefile-ostree.am index d2e3be62..1fe74dee 100644 --- a/Makefile-ostree.am +++ b/Makefile-ostree.am @@ -40,6 +40,8 @@ ostree_SOURCES = src/ostree/main.c \ src/ostree/ot-builtin-write-refs.c \ src/ostree/ot-main.h \ src/ostree/ot-main.c \ + src/ostree/ot-dump.h \ + src/ostree/ot-dump.c \ $(NULL) # Admin subcommand diff --git a/src/ostree/ot-builtin-show.c b/src/ostree/ot-builtin-show.c index 206999ad..91d2b438 100644 --- a/src/ostree/ot-builtin-show.c +++ b/src/ostree/ot-builtin-show.c @@ -23,38 +23,23 @@ #include "config.h" #include "ot-builtins.h" +#include "ot-dump.h" #include "ostree.h" #include "otutil.h" static gboolean opt_print_related; static char* opt_print_variant_type; static char* opt_print_metadata_key; +static gboolean opt_raw; static GOptionEntry options[] = { { "print-related", 0, 0, G_OPTION_ARG_NONE, &opt_print_related, "If given, show the \"related\" commits", NULL }, { "print-variant-type", 0, 0, G_OPTION_ARG_STRING, &opt_print_variant_type, "If given, argument should be a filename and it will be interpreted as this type", NULL }, { "print-metadata-key", 0, 0, G_OPTION_ARG_STRING, &opt_print_metadata_key, "Print string value of metadata key KEY for given commit", "KEY" }, + { "raw", 0, 0, G_OPTION_ARG_NONE, &opt_raw, "Show raw variant data" }, { NULL } }; -static void -print_variant (GVariant *variant) -{ - gs_free char *formatted_variant = NULL; - gs_unref_variant GVariant *byteswapped = NULL; - - if (G_BYTE_ORDER != G_BIG_ENDIAN) - { - byteswapped = g_variant_byteswap (variant); - formatted_variant = g_variant_print (byteswapped, TRUE); - } - else - { - formatted_variant = g_variant_print (variant, TRUE); - } - g_print ("%s\n", formatted_variant); -} - static gboolean do_print_variant_generic (const GVariantType *type, const char *filename, @@ -69,7 +54,7 @@ do_print_variant_generic (const GVariantType *type, if (!ot_util_variant_map (f, type, TRUE, &variant, error)) goto out; - print_variant (variant); + ot_dump_variant (variant); ret = TRUE; out: @@ -140,6 +125,29 @@ do_print_metadata_key (OstreeRepo *repo, return ret; } + +static gboolean +print_object (OstreeRepo *repo, + OstreeObjectType objtype, + const char *checksum, + GError **error) +{ + gs_unref_variant GVariant *variant = NULL; + OstreeDumpFlags flags = OSTREE_DUMP_NONE; + gboolean ret = FALSE; + + if (!ostree_repo_load_variant (repo, objtype, checksum, + &variant, error)) + goto out; + if (opt_raw) + flags |= OSTREE_DUMP_RAW; + ot_dump_object (objtype, checksum, variant, flags); + + ret = TRUE; +out: + return ret; +} + static gboolean print_if_found (OstreeRepo *repo, OstreeObjectType objtype, @@ -159,13 +167,9 @@ print_if_found (OstreeRepo *repo, goto out; if (have_object) { - gs_unref_variant GVariant *variant = NULL; - if (!ostree_repo_load_variant (repo, objtype, checksum, - &variant, error)) + if (!print_object (repo, objtype, checksum, error)) goto out; *inout_was_found = TRUE; - g_print ("Object: %s\nType: %s\n", checksum, ostree_object_type_to_string (objtype)); - print_variant (variant); } ret = TRUE; @@ -228,10 +232,8 @@ ostree_builtin_show (int argc, char **argv, GFile *repo_path, GCancellable *canc gs_unref_variant GVariant *variant = NULL; if (!ostree_repo_resolve_rev (repo, rev, FALSE, &resolved_rev, error)) goto out; - if (!ostree_repo_load_variant (repo, OSTREE_OBJECT_TYPE_COMMIT, resolved_rev, - &variant, error)) + if (!print_object (repo, OSTREE_OBJECT_TYPE_COMMIT, resolved_rev, error)) goto out; - print_variant (variant); } else { diff --git a/src/ostree/ot-dump.c b/src/ostree/ot-dump.c new file mode 100644 index 00000000..08fea7e4 --- /dev/null +++ b/src/ostree/ot-dump.c @@ -0,0 +1,139 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters + * Copyright (C) 2013 Stef Walter + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Stef Walter + * Colin Walters + */ + +#include "config.h" + +#include "ot-dump.h" +#include "otutil.h" + +void +ot_dump_variant (GVariant *variant) +{ + gs_free char *formatted_variant = NULL; + gs_unref_variant GVariant *byteswapped = NULL; + + if (G_BYTE_ORDER != G_BIG_ENDIAN) + { + byteswapped = g_variant_byteswap (variant); + formatted_variant = g_variant_print (byteswapped, TRUE); + } + else + { + formatted_variant = g_variant_print (variant, TRUE); + } + g_print ("%s\n", formatted_variant); +} + +static gchar * +format_timestamp (guint64 timestamp) +{ + GDateTime *dt; + gchar *str; + + dt = g_date_time_new_from_unix_utc (timestamp); + if (dt == NULL) + return NULL; + + str = g_date_time_format (dt, "%Y-%m-%d %H:%M:%S +0000"); + g_date_time_unref (dt); + + return str; +} + +static void +dump_indented_lines (const gchar *data) +{ + const char const* indent = " "; + const gchar *pos; + + for (;;) + { + pos = strchr (data, '\n'); + if (pos) + { + g_print ("%s%*s", indent, (int)(pos + 1 - data), data); + data = pos + 1; + } + else + { + if (data[0] != '\0') + g_print ("%s%s\n", indent, data); + break; + } + } +} + +static void +dump_commit (GVariant *variant, + OstreeDumpFlags flags) +{ + const gchar *subject; + const gchar *body; + guint64 timestamp; + gs_free gchar *str; + + /* See OSTREE_COMMIT_GVARIANT_FORMAT */ + g_variant_get (variant, "(a{sv}aya(say)&s&stayay)", NULL, NULL, NULL, + &subject, &body, ×tamp, NULL, NULL); + + timestamp = GUINT64_FROM_BE (timestamp); + str = format_timestamp (timestamp); + if (str) + g_print ("Date: %s\n", str); + + g_print ("\n"); + dump_indented_lines (subject); + + if (body[0]) + { + g_print ("\n"); + dump_indented_lines (body); + } + g_print ("\n"); +} + +void +ot_dump_object (OstreeObjectType objtype, + const char *checksum, + GVariant *variant, + OstreeDumpFlags flags) +{ + g_print ("%s %s\n", ostree_object_type_to_string (objtype), checksum); + + if (flags & OSTREE_DUMP_RAW) + { + ot_dump_variant (variant); + return; + } + + switch (objtype) + { + case OSTREE_OBJECT_TYPE_COMMIT: + dump_commit (variant, flags); + break; + /* TODO: Others could be implemented here */ + default: + break; + } +} diff --git a/src/ostree/ot-dump.h b/src/ostree/ot-dump.h new file mode 100644 index 00000000..f6122655 --- /dev/null +++ b/src/ostree/ot-dump.h @@ -0,0 +1,39 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2013 Stef Walter + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: Stef Walter + */ + +#pragma once + +#include + +#include "ostree-core.h" + +typedef enum { + OSTREE_DUMP_NONE = 0, + OSTREE_DUMP_RAW = 1, +} OstreeDumpFlags; + +void ot_dump_variant (GVariant *variant); + +void ot_dump_object (OstreeObjectType objtype, + const char *checksum, + GVariant *variant, + OstreeDumpFlags flags); diff --git a/tests/test-basic.sh b/tests/test-basic.sh index baef00c9..0bf9fdc3 100755 --- a/tests/test-basic.sh +++ b/tests/test-basic.sh @@ -19,7 +19,7 @@ set -e -echo "1..36" +echo "1..37" . $(dirname $0)/libtest.sh @@ -248,3 +248,10 @@ echo "ok ls with no argument" cd ${test_tmpdir} $OSTREE show test2 echo "ok show with non-checksum" + +cd ${test_tmpdir} +checksum=$($OSTREE commit -b test4 -s "Third commit") +$OSTREE show test4 > show-output +assert_file_has_content show-output "Third commit" +assert_file_has_content show-output "commit $checksum" +echo "ok show full output"