ostree/dump: Improve formatting for well-known commit metadata keys

This follows on from commit a946c3d4, which added formatting for
well-known summary metadata keys. This commit adds it for commits.

Currently, the only well-known commit metadata key is
ostree.commit.timestamp. Formatting this correctly is especially
important, since it’s a big-endian uint64, which is completely unusable
for mere mortals when presented as a number rather than a date.

Non-formatted output can still be retrieved using the OSTREE_DUMP_RAW
flag, and the non-formatted key name is always printed for clarity.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

Closes: #911
Approved by: cgwalters
This commit is contained in:
Philip Withnall 2017-06-07 12:03:57 +01:00 committed by Atomic Bot
parent 87413ee63b
commit 7ce6777028
2 changed files with 31 additions and 14 deletions

View File

@ -71,6 +71,18 @@ format_timestamp (guint64 timestamp,
return str;
}
static gchar *
uint64_secs_to_iso8601 (guint64 secs)
{
g_autoptr(GDateTime) dt = g_date_time_new_from_unix_utc (secs);
g_autoptr(GDateTime) local = (dt != NULL) ? g_date_time_to_local (dt) : NULL;
if (local != NULL)
return g_date_time_format (local, "%FT%T%:::z");
else
return g_strdup ("invalid");
}
static void
dump_indented_lines (const gchar *data)
{
@ -196,23 +208,27 @@ dump_summary_ref (const char *ref_name,
while (g_variant_iter_loop (metadata, "{sv}", &key, &value))
{
g_autofree char *string = g_variant_print (value, FALSE);
g_print (" %s: %s\n", key, string);
g_autofree gchar *value_str = NULL;
const gchar *pretty_key = NULL;
if (g_strcmp0 (key, OSTREE_COMMIT_TIMESTAMP) == 0)
{
pretty_key = "Timestamp";
value_str = uint64_secs_to_iso8601 (GUINT64_FROM_BE (g_variant_get_uint64 (value)));
}
else
{
value_str = g_variant_print (value, FALSE);
}
/* Print out. */
if (pretty_key != NULL)
g_print (" %s (%s): %s\n", pretty_key, key, value_str);
else
g_print (" %s: %s\n", key, value_str);
}
}
static gchar *
uint64_secs_to_iso8601 (guint64 secs)
{
g_autoptr(GDateTime) dt = g_date_time_new_from_unix_utc (secs);
g_autoptr(GDateTime) local = (dt != NULL) ? g_date_time_to_local (dt) : NULL;
if (local != NULL)
return g_date_time_format (local, "%FT%T%:::z");
else
return g_strdup ("invalid");
}
void
ot_dump_summary_bytes (GBytes *summary_bytes,
OstreeDumpFlags flags)

View File

@ -52,6 +52,7 @@ assert_file_has_content_literal summary.txt "* main"
assert_file_has_content_literal summary.txt "* other"
assert_file_has_content_literal summary.txt "ostree.summary.last-modified"
assert_file_has_content_literal summary.txt "Static Deltas (ostree.static-deltas): {}"
assert_file_has_content_literal summary.txt "Timestamp (ostree.commit.timestamp): "
echo "ok view summary"
# Check the summary can be viewed raw too.