Skip to content

Commit

Permalink
remote: Add new modify builtin
Browse files Browse the repository at this point in the history
Provide a new modify subcommand to the remote builtin to allow making
changes on existing remotes using the new
OSTREE_REPO_REMOTE_CHANGE_MERGE operation. Modifying non-existent
remotes is a failure. The interface is basically the same as add except
that URL is neither required nor a positional parameter.
  • Loading branch information
dbnicholson committed Sep 13, 2017
1 parent e06ad2e commit 7582250
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile-ostree.am
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ ostree_SOURCES += \
src/ostree/ot-remote-builtins.h \
src/ostree/ot-remote-builtin-add.c \
src/ostree/ot-remote-builtin-delete.c \
src/ostree/ot-remote-builtin-modify.c \
src/ostree/ot-remote-builtin-gpg-import.c \
src/ostree/ot-remote-builtin-list.c \
src/ostree/ot-remote-builtin-show-url.c \
Expand Down
1 change: 1 addition & 0 deletions Makefile-tests.am
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ _installed_or_uninstalled_test_scripts = \
tests/test-pull-subpath.sh \
tests/test-archivez.sh \
tests/test-remote-add.sh \
tests/test-remote-modify.sh \
tests/test-remote-headers.sh \
tests/test-remote-gpg-import.sh \
tests/test-commit-sign.sh \
Expand Down
38 changes: 38 additions & 0 deletions bash/ostree
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,43 @@ _ostree_remote_list_cookies() {
return 0
}

_ostree_remote_modify() {
local boolean_options="
$main_boolean_options
--no-gpg-verify
"

local options_with_args="
--url
--contenturl
--set
--collection-id
--repo
--sysroot
"

local options_with_args_glob=$( __ostree_to_extglob "$options_with_args" )

case "$prev" in
--repo|--sysroot)
__ostree_compreply_dirs_only
return 0
;;
$options_with_args_glob )
return 0
;;
esac

case "$cur" in
-*)
local all_options="$boolean_options $options_with_args"
__ostree_compreply_all_options
;;
esac

return 0
}

_ostree_remote_refs() {
local boolean_options="
$main_boolean_options
Expand Down Expand Up @@ -1691,6 +1728,7 @@ _ostree_remote() {
gpg-import
list
list-cookies
modify
refs
show-url
summary
Expand Down
56 changes: 54 additions & 2 deletions man/ostree-remote.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Boston, MA 02111-1307, USA.
<cmdsynopsis>
<command>ostree remote delete</command> <arg choice="req">NAME</arg>
</cmdsynopsis>
<cmdsynopsis>
<command>ostree remote modify</command> <arg choice="opt" rep="repeat">OPTIONS</arg> <arg choice="req">NAME</arg> <arg choice="opt" rep="repeat">BRANCH</arg>
</cmdsynopsis>
<cmdsynopsis>
<command>ostree remote show-url</command> <arg choice="req">NAME</arg>
</cmdsynopsis>
Expand Down Expand Up @@ -98,8 +101,9 @@ Boston, MA 02111-1307, USA.
</para>
<para>
The <literal>BRANCH</literal> arguments to the
<command>add</command> subcommand specifies the configured branches
for the remote. See the <literal>branches</literal> section in
<command>add</command> and <command>modify</command>
subcommands specify the configured branches for the remote.
See the <literal>branches</literal> section in
<citerefentry><refentrytitle>ostree.repo-config</refentrytitle><manvolnum>5</manvolnum></citerefentry>
for more information.
</para>
Expand Down Expand Up @@ -173,6 +177,54 @@ Boston, MA 02111-1307, USA.
</variablelist>
</refsect1>

<refsect1>
<title>'Modify' Options</title>

<variablelist>
<varlistentry>
<term><option>--url</option>=URL</term>

<listitem><para>
Set the remote URL.
</para></listitem>
</varlistentry>

<varlistentry>
<term><option>--set</option>="KEY=VALUE"</term>

<listitem><para>
Set config option KEY=VALUE for remote.
</para></listitem>
</varlistentry>

<varlistentry>
<term><option>--no-gpg-verify</option></term>

<listitem><para>
Disable GPG verification.
</para></listitem>
</varlistentry>

<varlistentry>
<term><option>--contenturl</option>=URL</term>

<listitem><para>
Set the URL for fetching content.
</para></listitem>
</varlistentry>

<varlistentry>
<term><option>--collection-id</option>=COLLECTION-ID</term>

<listitem><para>
Set the collection ID for the remote to a value provided by
the repository owner, which allows refs from this remote to be
shared peer to peer.
</para></listitem>
</varlistentry>
</variablelist>
</refsect1>

<refsect1>
<title>'List' Options</title>

Expand Down
1 change: 1 addition & 0 deletions src/ostree/ot-builtin-remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef struct {
static OstreeRemoteCommand remote_subcommands[] = {
{ "add", ot_remote_builtin_add },
{ "delete", ot_remote_builtin_delete },
{ "modify", ot_remote_builtin_modify },
{ "show-url", ot_remote_builtin_show_url },
{ "list", ot_remote_builtin_list },
{ "gpg-import", ot_remote_builtin_gpg_import },
Expand Down
149 changes: 149 additions & 0 deletions src/ostree/ot-remote-builtin-modify.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright © 2017 Endless Mobile, Inc.
*
* 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.
*
* Authors:
* - Dan Nicholson <nicholson@endlessm.com>
*/

#include "config.h"

#include "otutil.h"
#include "ot-tool-util.h"

#include "ot-main.h"
#include "ot-remote-builtins.h"

static char *opt_url;
static char **opt_set;
static gboolean opt_no_gpg_verify;
static char *opt_contenturl;
#ifdef OSTREE_ENABLE_EXPERIMENTAL_API
static char *opt_collection_id;
#endif /* OSTREE_ENABLE_EXPERIMENTAL_API */
static char *opt_sysroot;
static char *opt_repo;

/* ATTENTION:
* Please remember to update the bash-completion script (bash/ostree) and
* man page (man/ostree-remote.xml) when changing the option list.
*/

static GOptionEntry option_entries[] = {
{ "url", 0, 0, G_OPTION_ARG_STRING, &opt_url, "Set remote URL", "URL" },
{ "set", 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_set, "Set config option KEY=VALUE for remote", "KEY=VALUE" },
{ "no-gpg-verify", 0, 0, G_OPTION_ARG_NONE, &opt_no_gpg_verify, "Disable GPG verification", NULL },
{ "contenturl", 0, 0, G_OPTION_ARG_STRING, &opt_contenturl, "Use URL when fetching content", "URL" },
#ifdef OSTREE_ENABLE_EXPERIMENTAL_API
{ "collection-id", 0, 0, G_OPTION_ARG_STRING, &opt_collection_id,
"Globally unique ID for this repository as an collection of refs for redistribution to other repositories", "COLLECTION-ID" },
#endif /* OSTREE_ENABLE_EXPERIMENTAL_API */
{ "repo", 0, 0, G_OPTION_ARG_FILENAME, &opt_repo, "Path to OSTree repository (defaults to /sysroot/ostree/repo)", "PATH" },
{ "sysroot", 0, 0, G_OPTION_ARG_FILENAME, &opt_sysroot, "Use sysroot at PATH (overrides --repo)", "PATH" },
{ NULL }
};

gboolean
ot_remote_builtin_modify (int argc, char **argv, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(OstreeSysroot) sysroot = NULL;
g_autoptr(OstreeRepo) repo = NULL;
const char *remote_name;
char **iter;
g_autoptr(GVariantBuilder) optbuilder = NULL;
g_autoptr(GVariant) options = NULL;

context = g_option_context_new ("NAME [BRANCH...] - Modify a remote repository");

if (!ostree_option_context_parse (context, option_entries, &argc, &argv,
OSTREE_BUILTIN_FLAG_NO_REPO, NULL, cancellable, error))
return FALSE;

if (!ostree_parse_sysroot_or_repo_option (context, opt_sysroot, opt_repo,
&sysroot, &repo,
cancellable, error))
return FALSE;

if (argc < 2)
{
ot_util_usage_error (context, "NAME must be specified", error);
return FALSE;
}

remote_name = argv[1];

optbuilder = g_variant_builder_new (G_VARIANT_TYPE ("a{sv}"));

if (argc > 2)
{
g_autoptr(GPtrArray) branchesp = g_ptr_array_new ();
int i;

for (i = 2; i < argc; i++)
g_ptr_array_add (branchesp, argv[i]);
g_ptr_array_add (branchesp, NULL);

g_variant_builder_add (optbuilder, "{s@v}",
"branches",
g_variant_new_variant (g_variant_new_strv ((const char*const*)branchesp->pdata, -1)));
}

/* We could just make users use --set instead for this since it's a string,
* but e.g. when mirrorlist support is added, it'll be kinda awkward to type:
* --set=contenturl=mirrorlist=... */

if (opt_contenturl != NULL)
g_variant_builder_add (optbuilder, "{s@v}",
"contenturl", g_variant_new_variant (g_variant_new_string (opt_contenturl)));

for (iter = opt_set; iter && *iter; iter++)
{
const char *keyvalue = *iter;
g_autofree char *subkey = NULL;
g_autofree char *subvalue = NULL;

if (!ot_parse_keyvalue (keyvalue, &subkey, &subvalue, error))
return FALSE;

g_variant_builder_add (optbuilder, "{s@v}",
subkey, g_variant_new_variant (g_variant_new_string (subvalue)));
}

if (opt_no_gpg_verify)
g_variant_builder_add (optbuilder, "{s@v}",
"gpg-verify",
g_variant_new_variant (g_variant_new_boolean (FALSE)));

#ifdef OSTREE_ENABLE_EXPERIMENTAL_API
if (opt_collection_id != NULL)
g_variant_builder_add (optbuilder, "{s@v}", "collection-id",
g_variant_new_variant (g_variant_new_take_string (g_steal_pointer (&opt_collection_id))));
#endif /* OSTREE_ENABLE_EXPERIMENTAL_API */

options = g_variant_ref_sink (g_variant_builder_end (optbuilder));

if (!ostree_repo_remote_change (repo, NULL,
OSTREE_REPO_REMOTE_CHANGE_MERGE,
remote_name, opt_url,
options,
cancellable, error))
return FALSE;

return TRUE;
}
1 change: 1 addition & 0 deletions src/ostree/ot-remote-builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ G_BEGIN_DECLS

gboolean ot_remote_builtin_add (int argc, char **argv, GCancellable *cancellable, GError **error);
gboolean ot_remote_builtin_delete (int argc, char **argv, GCancellable *cancellable, GError **error);
gboolean ot_remote_builtin_modify (int argc, char **argv, GCancellable *cancellable, GError **error);
gboolean ot_remote_builtin_gpg_import (int argc, char **argv, GCancellable *cancellable, GError **error);
gboolean ot_remote_builtin_list (int argc, char **argv, GCancellable *cancellable, GError **error);
#ifdef HAVE_LIBSOUP
Expand Down
84 changes: 84 additions & 0 deletions tests/test-remote-modify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/bin/bash
#
# Copyright © 2017 Endless Mobile, Inc.
#
# 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.

set -euo pipefail

. $(dirname $0)/libtest.sh

echo '1..8'

setup_test_repository "bare"
$OSTREE remote add origin http://example.com/repo1

# Modify with no args is a no-op
cp repo/config orig-config
$OSTREE remote modify origin
diff=$(diff -u orig-config repo/config)
assert_streq "$diff" ""

echo "ok modify no-op"

$OSTREE remote modify --url http://example.com/repo2 origin
url=$($OSTREE remote show-url origin)
assert_streq "$url" http://example.com/repo2

echo "ok modify url"

$OSTREE remote modify --no-gpg-verify origin
url=$($OSTREE remote show-url origin)
assert_streq "$url" http://example.com/repo2
assert_file_has_content repo/config "^gpg-verify=false$"

echo "ok modify no gpg-verify"

$OSTREE remote modify origin branch1 branch2
assert_file_has_content repo/config "^branches=branch1;branch2;$"

echo "ok modify branches"

$OSTREE remote modify --set=gpg-verify=true --set=someopt=blah origin
assert_file_has_content repo/config "^gpg-verify=true$"
assert_file_has_content repo/config "^someopt=blah$"

echo "ok modify set option"

$OSTREE remote modify --contenturl=http://example.com/content origin
url=$($OSTREE remote show-url origin)
assert_streq "$url" http://example.com/repo2
assert_file_has_content repo/config "^contenturl=http://example.com/content$"

echo "ok modify contenturl"

if $OSTREE remote modify --url=http://foo.com nosuchremote 2>/dev/null; then
assert_not_reached "Modifying non-existent remote should fail"
fi

echo "ok modify non-existent fails"

# Add a 2nd remote and test modifying one doesn't affect the other
$OSTREE remote add other http://other.com/repo
$OSTREE remote modify other branch3 branch4
assert_file_has_content repo/config "^branches=branch1;branch2;$"
assert_file_has_content repo/config "^branches=branch3;branch4;$"
origin_url=$($OSTREE remote show-url origin)
assert_streq "$origin_url" http://example.com/repo2
other_url=$($OSTREE remote show-url other)
assert_streq "$other_url" http://other.com/repo

echo "ok modify multiple remotes"

0 comments on commit 7582250

Please sign in to comment.