From c737c0a149a05c12f085ff0b0b9a620cd21b5d6e Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Wed, 16 Oct 2024 01:27:53 +0300 Subject: [PATCH] builder-context: Write a gitignore file into the state-dir Unless specified, state-dir is placed on the current path, which is usually the source directory of the project. Write a .gitignore file to make sure that both the state-dir never gets checked out into git and that all the tooling will ignore it and avoid indexing it. Inpired by meson's gitignore file in the build directories. Retro-actively discovered the same change in flatpak-build init [1] [1]: https://github.com/flatpak/flatpak/commit/a62f8cfb62c4232ef2674cfa4431169a292120d6 --- src/builder-context.c | 38 ++++++++++++++++++++++++++++++++++++++ src/builder-context.h | 3 +++ src/builder-main.c | 6 ++++++ 3 files changed, 47 insertions(+) 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;