core: checkout: Add --from-stdin option

This allows checking out many branches in one go, useful in
combination with the --union flag for ostbuild to combine a lot of
components.
This commit is contained in:
Colin Walters 2012-04-06 15:11:11 -04:00
parent 92ab820c0b
commit a69e4452b4
1 changed files with 188 additions and 82 deletions

View File

@ -25,20 +25,24 @@
#include "ot-builtins.h" #include "ot-builtins.h"
#include "ostree.h" #include "ostree.h"
#include <gio/gunixinputstream.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
static gboolean user_mode; static gboolean opt_user_mode;
static gboolean opt_atomic_retarget; static gboolean opt_atomic_retarget;
static gboolean opt_no_triggers; static gboolean opt_no_triggers;
static char *subpath; static char *opt_subpath;
static gboolean opt_union; static gboolean opt_union;
static gboolean opt_from_stdin;
static GOptionEntry options[] = { static GOptionEntry options[] = {
{ "user-mode", 'U', 0, G_OPTION_ARG_NONE, &user_mode, "Do not change file ownership or initialze extended attributes", NULL }, { "user-mode", 'U', 0, G_OPTION_ARG_NONE, &opt_user_mode, "Do not change file ownership or initialze extended attributes", NULL },
{ "subpath", 0, 0, G_OPTION_ARG_STRING, &subpath, "Checkout sub-directory PATH", "PATH" }, { "subpath", 0, 0, G_OPTION_ARG_STRING, &opt_subpath, "Checkout sub-directory PATH", "PATH" },
{ "union", 0, 0, G_OPTION_ARG_NONE, &opt_union, "Keep existing directories, overwrite existing files", NULL }, { "union", 0, 0, G_OPTION_ARG_NONE, &opt_union, "Keep existing directories, overwrite existing files", NULL },
{ "atomic-retarget", 0, 0, G_OPTION_ARG_NONE, &opt_atomic_retarget, "Make a symbolic link for destination, suffix with checksum", NULL }, { "atomic-retarget", 0, 0, G_OPTION_ARG_NONE, &opt_atomic_retarget, "Make a symbolic link for destination, suffix with checksum", NULL },
{ "no-triggers", 0, 0, G_OPTION_ARG_NONE, &opt_no_triggers, "Don't run triggers", NULL }, { "no-triggers", 0, 0, G_OPTION_ARG_NONE, &opt_no_triggers, "Don't run triggers", NULL },
{ "from-stdin", 0, 0, G_OPTION_ARG_NONE, &opt_from_stdin, "Process many checkouts from standard input", NULL },
{ NULL } { NULL }
}; };
@ -116,6 +120,110 @@ parse_commit_from_symlink (GFile *symlink,
return ret; return ret;
} }
static gboolean
process_one_checkout (OstreeRepo *repo,
const char *resolved_commit,
const char *subpath,
GFile *target,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
OstreeRepoFile *root = NULL;
OstreeRepoFile *subtree = NULL;
GFileInfo *file_info = NULL;
root = (OstreeRepoFile*)ostree_repo_file_new_root (repo, resolved_commit);
if (!ostree_repo_file_ensure_resolved (root, error))
goto out;
if (subpath)
subtree = (OstreeRepoFile*)g_file_resolve_relative_path ((GFile*)root, subpath);
else
subtree = g_object_ref (root);
file_info = g_file_query_info ((GFile*)subtree, OSTREE_GIO_FAST_QUERYINFO,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable, error);
if (!file_info)
goto out;
if (!ostree_repo_checkout_tree (repo, opt_user_mode ? OSTREE_REPO_CHECKOUT_MODE_USER : 0,
opt_union ? OSTREE_REPO_CHECKOUT_OVERWRITE_UNION_FILES : 0,
target, subtree, file_info, cancellable, error))
goto out;
ret = TRUE;
out:
g_clear_object (&subtree);
g_clear_object (&root);
g_clear_object (&file_info);
return ret;
}
static gboolean
process_many_checkouts (OstreeRepo *repo,
GFile *target,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
GInputStream *stdin_stream = NULL;
GDataInputStream *stdin_data = NULL;
char *revision = NULL;
char *subpath = NULL;
char *resolved_commit = NULL;
gsize len;
GError *temp_error = NULL;
stdin_stream = (GInputStream*)g_unix_input_stream_new (0, FALSE);
stdin_data = g_data_input_stream_new (stdin_stream);
while ((revision = g_data_input_stream_read_upto (stdin_data, "", 1, &len,
cancellable, &temp_error)) != NULL)
{
if (revision[0] == '\0')
break;
/* Read the null byte */
(void) g_data_input_stream_read_byte (stdin_data, cancellable, NULL);
g_free (subpath);
subpath = g_data_input_stream_read_upto (stdin_data, "", 1, &len,
cancellable, &temp_error);
if (temp_error)
{
g_propagate_error (error, temp_error);
goto out;
}
/* Read the null byte */
(void) g_data_input_stream_read_byte (stdin_data, cancellable, NULL);
if (!ostree_repo_resolve_rev (repo, revision, FALSE, &resolved_commit, error))
goto out;
if (!process_one_checkout (repo, resolved_commit, subpath, target,
cancellable, error))
goto out;
g_free (revision);
}
if (temp_error)
{
g_propagate_error (error, temp_error);
goto out;
}
ret = TRUE;
out:
g_free (subpath);
g_free (revision);
g_free (resolved_commit);
g_clear_object (&stdin_stream);
g_clear_object (&stdin_data);
return ret;
}
gboolean gboolean
ostree_builtin_checkout (int argc, char **argv, GFile *repo_path, GError **error) ostree_builtin_checkout (int argc, char **argv, GFile *repo_path, GError **error)
{ {
@ -129,9 +237,6 @@ ostree_builtin_checkout (int argc, char **argv, GFile *repo_path, GError **error
const char *destination; const char *destination;
char *suffixed_destination = NULL; char *suffixed_destination = NULL;
char *tmp_destination = NULL; char *tmp_destination = NULL;
OstreeRepoFile *root = NULL;
OstreeRepoFile *subtree = NULL;
GFileInfo *file_info = NULL;
GFileInfo *symlink_file_info = NULL; GFileInfo *symlink_file_info = NULL;
GFile *checkout_target = NULL; GFile *checkout_target = NULL;
GFile *checkout_target_tmp = NULL; GFile *checkout_target_tmp = NULL;
@ -158,6 +263,29 @@ ostree_builtin_checkout (int argc, char **argv, GFile *repo_path, GError **error
goto out; goto out;
} }
if (opt_from_stdin)
{
if (opt_atomic_retarget)
{
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"--atomic-retarget may not be used with --from-stdin");
goto out;
}
destination = argv[1];
checkout_target = ot_gfile_new_for_path (destination);
if (!process_many_checkouts (repo, checkout_target, cancellable, error))
goto out;
if (!opt_no_triggers)
{
if (!ostree_run_triggers_in_root (checkout_target, cancellable, error))
goto out;
}
}
else
{
commit = argv[1]; commit = argv[1];
if (argc < 3) if (argc < 3)
destination = commit; destination = commit;
@ -204,29 +332,9 @@ ostree_builtin_checkout (int argc, char **argv, GFile *repo_path, GError **error
if (!skip_checkout) if (!skip_checkout)
{ {
root = (OstreeRepoFile*)ostree_repo_file_new_root (repo, resolved_commit); if (!process_one_checkout (repo, resolved_commit, opt_subpath,
if (!ostree_repo_file_ensure_resolved (root, error))
goto out;
if (subpath)
{
subtree = (OstreeRepoFile*)g_file_resolve_relative_path ((GFile*)root, subpath);
}
else
{
subtree = g_object_ref (root);
}
file_info = g_file_query_info ((GFile*)subtree, OSTREE_GIO_FAST_QUERYINFO,
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
cancellable, error);
if (!file_info)
goto out;
if (!ostree_repo_checkout_tree (repo, user_mode ? OSTREE_REPO_CHECKOUT_MODE_USER : 0,
opt_union ? OSTREE_REPO_CHECKOUT_OVERWRITE_UNION_FILES : 0,
checkout_target_tmp ? checkout_target_tmp : checkout_target, checkout_target_tmp ? checkout_target_tmp : checkout_target,
subtree, file_info, cancellable, error)) cancellable, error))
goto out; goto out;
if (!opt_no_triggers) if (!opt_no_triggers)
@ -246,6 +354,7 @@ ostree_builtin_checkout (int argc, char **argv, GFile *repo_path, GError **error
goto out; goto out;
} }
} }
}
ret = TRUE; ret = TRUE;
out: out:
@ -259,9 +368,6 @@ ostree_builtin_checkout (int argc, char **argv, GFile *repo_path, GError **error
if (context) if (context)
g_option_context_free (context); g_option_context_free (context);
g_clear_object (&repo); g_clear_object (&repo);
g_clear_object (&root);
g_clear_object (&subtree);
g_clear_object (&file_info);
g_clear_object (&symlink_file_info); g_clear_object (&symlink_file_info);
return ret; return ret;
} }