summaryrefslogtreecommitdiff
path: root/src/ostree/ot-builtin-refs.c
blob: c687a5ffc78aa4b1a799faa36e511e4512ad21b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * Copyright (C) 2013 Colin Walters <walters@verbum.org>
 *
 * SPDX-License-Identifier: LGPL-2.0+
 *
 * 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, see <https://www.gnu.org/licenses/>.
 *
 * Author: Colin Walters <walters@verbum.org>
 */

#include "config.h"

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

static gboolean opt_delete;
static gboolean opt_list;
static gboolean opt_alias;
static char *opt_create;
static gboolean opt_collections;
static gboolean opt_force;

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

static GOptionEntry options[] = {
  { "delete", 0, 0, G_OPTION_ARG_NONE, &opt_delete, "Delete refs which match PREFIX, rather than listing them", NULL },
  { "list", 0, 0, G_OPTION_ARG_NONE, &opt_list, "Do not remove the prefix from the refs", NULL },
  { "alias", 'A', 0, G_OPTION_ARG_NONE, &opt_alias, "If used with --create, create an alias, otherwise just list aliases", NULL },
  { "create", 0, 0, G_OPTION_ARG_STRING, &opt_create, "Create a new ref for an existing commit", "NEWREF" },
  { "collections", 'c', 0, G_OPTION_ARG_NONE, &opt_collections, "Enable listing collection IDs for refs", NULL },
  { "force", 0, 0, G_OPTION_ARG_NONE, &opt_force, "Overwrite existing refs when creating", NULL },
  { NULL }
};

static gboolean
do_ref_with_collections (OstreeRepo    *repo,
                         const char    *refspec_prefix,
                         GCancellable  *cancellable,
                         GError       **error)
{
  g_autoptr(GHashTable) refs = NULL;  /* (element-type OstreeCollectionRef utf8) */
  GHashTableIter hashiter;
  gpointer hashkey, hashvalue;
  gboolean ret = FALSE;

  if (!ostree_repo_list_collection_refs (repo,
                                         (!opt_create) ? refspec_prefix : NULL,
                                         &refs, OSTREE_REPO_LIST_REFS_EXT_NONE,
                                         cancellable, error))
    goto out;

  if (!opt_delete && !opt_create)
    {
      g_hash_table_iter_init (&hashiter, refs);
      while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
        {
          const OstreeCollectionRef *ref = hashkey;
          g_print ("(%s, %s)\n", ref->collection_id, ref->ref_name);
        }
    }
  else if (opt_create)
    {
      g_autofree char *checksum = NULL;
      g_autofree char *checksum_existing = NULL;

      if (!ostree_repo_resolve_rev_ext (repo, opt_create, TRUE, OSTREE_REPO_RESOLVE_REV_EXT_NONE, &checksum_existing, error))
        {
          if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY))
            {
              /* A folder exists with the specified ref name,
               * which is handled by _ostree_repo_write_ref */
              g_clear_error (error);
            }
          else goto out;
        }

      if (!opt_force && checksum_existing != NULL)
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                       "--create specified but ref %s already exists", opt_create);
          goto out;
        }

      if (!ostree_repo_resolve_rev (repo, refspec_prefix, FALSE, &checksum, error))
        goto out;

      /* This is technically an abuse of the refspec syntax: collection IDs
       * should not be treated like remote names. */
      g_auto(GStrv) parts = g_strsplit (opt_create, ":", 2);
      const char *collection_id = parts[0];
      const char *ref_name = parts[1];
      if (!ostree_validate_collection_id (collection_id, error))
        goto out;
      if (!ostree_validate_rev (ref_name, error))
        goto out;

      const OstreeCollectionRef ref = { (gchar *) collection_id, (gchar *) ref_name };
      if (!ostree_repo_set_collection_ref_immediate (repo, &ref, checksum,
                                                     cancellable, error))
        goto out;
    }
  else
    /* delete */
    {
      g_hash_table_iter_init (&hashiter, refs);
      while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
        {
          const OstreeCollectionRef *ref = hashkey;

          if (!ostree_repo_set_collection_ref_immediate (repo, ref, NULL,
                                                         cancellable, error))
            goto out;
        }
    }
  ret = TRUE;
 out:
  return ret;
}

static gboolean do_ref (OstreeRepo *repo, const char *refspec_prefix, GCancellable *cancellable, GError **error)
{
  g_autoptr(GHashTable) refs = NULL;
  g_autoptr(GHashTable) ref_aliases = NULL;
  GHashTableIter hashiter;
  gpointer hashkey, hashvalue;
  gboolean ret = FALSE;
  gboolean is_list;

  if (opt_collections)
    return do_ref_with_collections (repo, refspec_prefix, cancellable, error);

  /* If we're doing aliasing, we need the full list of aliases mostly to allow
   * replacing existing aliases.
   * If we are deleting a ref, we want to make sure that it doesn't have
   * any corresponding aliases.
   */
  if (opt_alias || opt_delete)
    {
      if (!ostree_repo_list_refs_ext (repo, NULL, &ref_aliases,
                                      OSTREE_REPO_LIST_REFS_EXT_ALIASES,
                                      cancellable, error))
        goto out;
    }

  is_list = !(opt_delete || opt_create);

  if (opt_delete || opt_list || (!opt_create && opt_alias))
    {
      OstreeRepoListRefsExtFlags flags = OSTREE_REPO_LIST_REFS_EXT_NONE;
      if (opt_alias)
        flags |= OSTREE_REPO_LIST_REFS_EXT_ALIASES;
      if (!ostree_repo_list_refs_ext (repo, refspec_prefix, &refs, flags,
                                      cancellable, error))
        goto out;
    }
  else if (opt_create)
    {
      if (!ostree_repo_list_refs_ext (repo, NULL, &refs, OSTREE_REPO_LIST_REFS_EXT_NONE,
                                      cancellable, error))
        goto out;
    }
  else if (!ostree_repo_list_refs (repo, refspec_prefix, &refs, cancellable, error))
    goto out;

  if (is_list)
    {
      GLNX_HASH_TABLE_FOREACH_KV (refs, const char *, ref, const char *, value)
        {
          if (opt_alias)
            g_print ("%s -> %s\n", ref, value);
          else
            g_print ("%s\n", ref);
        }
    }
  else if (opt_create)
    {
      g_autofree char *checksum = NULL;
      g_autofree char *checksum_existing = NULL;
      g_autofree char *remote = NULL;
      g_autofree char *ref = NULL;

      if (!ostree_repo_resolve_rev_ext (repo, opt_create, TRUE, OSTREE_REPO_RESOLVE_REV_EXT_NONE, &checksum_existing, error))
        {
          if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY))
            {
              /* A folder exists with the specified ref name,
               * which is handled by _ostree_repo_write_ref */
              g_clear_error (error);
            }
          else goto out;
        }

      /* We want to allow replacing an existing alias or a normal ref when
       * forced
       */
      gboolean replacing_alias = opt_alias && g_hash_table_contains (ref_aliases, opt_create);
      if (!replacing_alias && !opt_force && checksum_existing != NULL)
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                       "--create specified but ref %s already exists", opt_create);
          goto out;
        }

      if (!ostree_parse_refspec (opt_create, &remote, &ref, error))
        goto out;

      if (opt_alias)
        {
          if (remote)
            return glnx_throw (error, "Cannot create alias to remote ref: %s", remote);
          if (!g_hash_table_contains (refs, refspec_prefix))
            return glnx_throw (error, "Cannot create alias to non-existent ref: %s",
                               refspec_prefix);
          if (!ostree_repo_set_alias_ref_immediate (repo, remote, ref, refspec_prefix,
                                                    cancellable, error))
            goto out;
        }
      else
        {
          if (!ostree_repo_resolve_rev (repo, refspec_prefix, FALSE, &checksum, error))
            goto out;

          if (!ostree_repo_set_ref_immediate (repo, remote, ref, checksum,
                                              cancellable, error))
            goto out;
        }
    }
  else
    /* delete */
    {
      g_hash_table_iter_init (&hashiter, refs);
      while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
        {
          const char *refspec = hashkey;
          g_autofree char *remote = NULL;
          g_autofree char *ref = NULL;

          if (!ostree_parse_refspec (refspec, &remote, &ref, error))
            goto out;

          /* Look for alias if it exists for a ref we want to delete */
          GLNX_HASH_TABLE_FOREACH_KV (ref_aliases, const char *,
                                      ref_alias, const char *, value)
            {
              if (!strcmp (ref, value))
                {
                  g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                               "Ref '%s' has an active alias: '%s'", ref, ref_alias);
                  goto out;
                }
            }
          if (!ostree_repo_set_ref_immediate (repo, remote, ref, NULL,
                                              cancellable, error))
            goto out;
        }
    }
  ret = TRUE;
 out:
  return ret;
}

gboolean
ostree_builtin_refs (int argc, char **argv, OstreeCommandInvocation *invocation, GCancellable *cancellable, GError **error)
{
  gboolean ret = FALSE;
  g_autoptr(GOptionContext) context = NULL;
  g_autoptr(OstreeRepo) repo = NULL;
  int i;

  context = g_option_context_new ("[PREFIX]");

  if (!ostree_option_context_parse (context, options, &argc, &argv, invocation, &repo, cancellable, error))
    goto out;

  if (argc >= 2)
    {
      if (opt_create && argc > 2)
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                       "You must specify only 1 existing ref when creating a new ref");
          goto out;
        }
      for (i = 1; i < argc; i++)
        if (!do_ref (repo, argv[i], cancellable, error))
          goto out;
    }
  else
    {
      /* Require a prefix when deleting to help avoid accidents. */
      if (opt_delete)
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                       "At least one PREFIX is required when deleting refs");
          goto out;
        }
      else if (opt_create)
        {
          g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                       "You must specify a revision when creating a new ref");
          goto out;
        }

      if (!do_ref (repo, NULL, cancellable, error))
        goto out;
    }

  ret = TRUE;
 out:
  if (repo)
    ostree_repo_abort_transaction (repo, cancellable, NULL);
  return ret;
}