refs: add "ostree refs --create" and unit tests
Added the ability to create a ref (much like a git tag) for an existing commit through "ostree refs EXISTING --create=NEWREF". Previously the only way to create a new ref was by creating a new commit, but refs --create allows multiple refs to point to the same commit. The command will fail if: - None/more than one existing ref is specified - The specified EXISTING tag does not exist, or was not specified - The specified NEWREF already exists, or is the name of a folder Add unit tests in tests-ref.sh to verify above functionality Closes: #340 Approved by: jlebon
This commit is contained in:
1 parent
7847bc7394
commit
f8fcdba0a5
2 files changed
+76
-1
No files matched your search
@@ -28,10 +28,12 @@
|
||||
|
||||
static gboolean opt_delete;
|
||||
static gboolean opt_list;
|
||||
static char *opt_create;
|
||||
|
||||
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 },
|
||||
{ "create", 0, 0, G_OPTION_ARG_STRING, &opt_create, "Create a new ref for an existing commit", "NEWREF" },
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
@@ -48,10 +50,16 @@ static gboolean do_ref (OstreeRepo *repo, const char *refspec_prefix, GCancellab
|
||||
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 (!opt_delete)
|
||||
if (!opt_delete && !opt_create)
|
||||
{
|
||||
g_hash_table_iter_init (&hashiter, refs);
|
||||
while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
|
||||
@@ -60,7 +68,30 @@ static gboolean do_ref (OstreeRepo *repo, const char *refspec_prefix, GCancellab
|
||||
g_print ("%s\n", ref);
|
||||
}
|
||||
}
|
||||
else if (opt_create)
|
||||
{
|
||||
g_autofree char *checksum = NULL;
|
||||
g_autofree char *checksum_existing = NULL;
|
||||
|
||||
if (!ostree_repo_resolve_rev (repo, opt_create, TRUE, &checksum_existing, error))
|
||||
goto out;
|
||||
|
||||
if (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;
|
||||
|
||||
if (!ostree_repo_set_ref_immediate (repo, NULL, opt_create, checksum,
|
||||
cancellable, error))
|
||||
goto out;
|
||||
}
|
||||
else
|
||||
/* delete */
|
||||
{
|
||||
g_hash_table_iter_init (&hashiter, refs);
|
||||
while (g_hash_table_iter_next (&hashiter, &hashkey, &hashvalue))
|
||||
@@ -97,6 +128,12 @@ ostree_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError **
|
||||
|
||||
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;
|
||||
@@ -110,6 +147,13 @@ ostree_builtin_refs (int argc, char **argv, GCancellable *cancellable, GError **
|
||||
"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 an existing ref when creating a new ref");
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = do_ref (repo, NULL, cancellable, error);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user