From c492def9a71ecbed129d2c7277f0487f1a2f55e6 Mon Sep 17 00:00:00 2001 From: Mario Sanchez Prada Date: Tue, 11 Oct 2016 15:53:08 +0100 Subject: [PATCH] Fetch the default branch when adding a remote from its repo's summary file Just like we do with the title, fetch the default-branch from the repository's summary file and use that information when adding a remote reference from the command line. https://github.com/flatpak/flatpak/issues/221 --- app/flatpak-builtins-add-remote.c | 13 +++++++- common/flatpak-dir.c | 49 +++++++++++++++++++++++++++++++ common/flatpak-dir.h | 4 +++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/app/flatpak-builtins-add-remote.c b/app/flatpak-builtins-add-remote.c index 172a62c8b..38b28e9aa 100644 --- a/app/flatpak-builtins-add-remote.c +++ b/app/flatpak-builtins-add-remote.c @@ -267,6 +267,7 @@ update_remote_with_extra_metadata (FlatpakDir* dir, GError **error) { g_autofree char *title = NULL; + g_autofree char *default_branch = NULL; g_autoptr(GKeyFile) config = NULL; if (opt_title == NULL) @@ -279,7 +280,17 @@ update_remote_with_extra_metadata (FlatpakDir* dir, opt_title = title; } - if (title != NULL) + if (opt_default_branch == NULL) + { + default_branch = flatpak_dir_fetch_remote_default_branch (dir, + remote, + NULL, + NULL); + if (default_branch) + opt_default_branch = default_branch; + } + + if (title != NULL || default_branch != NULL) { config = get_config_from_opts (dir, remote); return flatpak_dir_modify_remote (dir, remote, config, gpg_data, cancellable, error); diff --git a/common/flatpak-dir.c b/common/flatpak-dir.c index a1e3cadc0..2e244ae48 100644 --- a/common/flatpak-dir.c +++ b/common/flatpak-dir.c @@ -5260,6 +5260,55 @@ flatpak_dir_fetch_remote_title (FlatpakDir *self, return g_steal_pointer (&title); } +char * +flatpak_dir_fetch_remote_default_branch (FlatpakDir *self, + const char *remote, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GError) my_error = NULL; + g_autoptr(GBytes) summary_bytes = NULL; + g_autoptr(GVariant) summary = NULL; + g_autoptr(GVariant) extensions = NULL; + GVariantDict dict; + g_autofree char *default_branch = NULL; + + if (error == NULL) + error = &my_error; + + if (!flatpak_dir_ensure_repo (self, cancellable, error)) + return NULL; + + if (!flatpak_dir_remote_fetch_summary (self, remote, + &summary_bytes, + cancellable, error)) + return FALSE; + + if (summary_bytes == NULL) + { + g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, + _("Remote default-branch not available; server has no summary file")); + return FALSE; + } + + summary = g_variant_ref_sink (g_variant_new_from_bytes (OSTREE_SUMMARY_GVARIANT_FORMAT, + summary_bytes, FALSE)); + extensions = g_variant_get_child_value (summary, 1); + + g_variant_dict_init (&dict, extensions); + g_variant_dict_lookup (&dict, "xa.default-branch", "s", &default_branch); + g_variant_dict_end (&dict); + + if (default_branch == NULL) + { + g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, + _("Remote default-branch not set")); + return FALSE; + } + + return g_steal_pointer (&default_branch); +} + static gboolean flatpak_dir_parse_summary_for_ref (FlatpakDir *self, GVariant *summary, diff --git a/common/flatpak-dir.h b/common/flatpak-dir.h index 9d74fcf49..6c372fbf8 100644 --- a/common/flatpak-dir.h +++ b/common/flatpak-dir.h @@ -424,6 +424,10 @@ char * flatpak_dir_fetch_remote_title (FlatpakDir *self, const char *remote, GCancellable *cancellable, GError **error); +char * flatpak_dir_fetch_remote_default_branch (FlatpakDir *self, + const char *remote, + GCancellable *cancellable, + GError **error); gboolean flatpak_dir_fetch_ref_cache (FlatpakDir *self, const char *remote_name, const char *ref,