diff --git a/src/builder-context.c b/src/builder-context.c index f813f3f4..17240518 100644 --- a/src/builder-context.c +++ b/src/builder-context.c @@ -37,6 +37,9 @@ #include "builder-cache.h" #include "builder-utils.h" +#define GIT_IGNORE_FILE \ + "# This file is autogenerated by flatpak-builder.\n" \ + "*\n" struct BuilderContext { @@ -1193,6 +1196,41 @@ builder_context_get_sdk_config (BuilderContext *self) return self->sdk_config; } +gboolean +builder_context_create_state_dir (BuilderContext *self, + GError **error) +{ + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + if (!g_file_make_directory_with_parents (self->state_dir, NULL, error)) + { + if (g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_EXISTS)) + { + g_clear_error (error); + return TRUE; + } + + flatpak_fail (error, "Could not create state-dir"); + return FALSE; + } + + // Only create the gitignore file if we were the ones to create the state-dir + // Assume that if the state-dir exists already, there's either already a file + // created before or it was deliberately modified or deleted + g_autoptr (GFile) gitignore_file = g_file_get_child (self->state_dir, ".gitignore"); + if (!g_file_replace_contents (gitignore_file, + GIT_IGNORE_FILE, sizeof (GIT_IGNORE_FILE), + NULL, FALSE, + G_FILE_CREATE_REPLACE_DESTINATION, + NULL, NULL, error)) + { + flatpak_fail (error, "Could not create .gitignore file inside state-dir"); + return FALSE; + } + + return TRUE; +} + BuilderContext * builder_context_new (GFile *run_dir, GFile *app_dir, diff --git a/src/builder-context.h b/src/builder-context.h index e3954a92..69671398 100644 --- a/src/builder-context.h +++ b/src/builder-context.h @@ -182,6 +182,9 @@ const char * builder_context_get_opt_mirror_screenshots_url (BuilderContext * BuilderSdkConfig * builder_context_get_sdk_config (BuilderContext *self); +gboolean builder_context_create_state_dir (BuilderContext *self, + GError **error); + G_DEFINE_AUTOPTR_CLEANUP_FUNC (BuilderContext, g_object_unref) G_END_DECLS diff --git a/src/builder-main.c b/src/builder-main.c index 42f4784e..ecf5f925 100644 --- a/src/builder-main.c +++ b/src/builder-main.c @@ -533,6 +533,12 @@ main (int argc, git_init_email (); + if (!builder_context_create_state_dir (build_context, &error)) + { + g_printerr ("Can't create state directory: %s\n", error->message); + return 1; + } + if (opt_sources_dirs) { g_autoptr(GPtrArray) sources_dirs = NULL;