From 577a827e619601fcb4ba466fcc5d6f60b8f4f63e 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 | 52 +++++++++++++++++++++++++++++++++++++++++++ src/builder-context.h | 3 +++ src/builder-main.c | 6 +++++ 3 files changed, 61 insertions(+) diff --git a/src/builder-context.c b/src/builder-context.c index f813f3f4..38b06e62 100644 --- a/src/builder-context.c +++ b/src/builder-context.c @@ -1193,6 +1193,58 @@ 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); + + gboolean exists = g_file_query_exists (self->state_dir, NULL); + + if (!exists && !g_file_make_directory_with_parents (self->state_dir, NULL, error)) + { + 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 we the state-dir exists already, there's either already a file + // created before or it was deliberately modified or deleted + if (!exists) { + g_autoptr (GFile) gitignore_file = g_file_get_child (self->state_dir, ".gitignore"); + + if (!g_file_query_exists (gitignore_file, NULL)) { + const char *git_ignore_file = "# This file is autogenerated by flatpak-builder.\n" + "# If you change or delete it, it won't be recreated. \n" + "*\n"; + + g_autoptr(GFileOutputStream) out = g_file_create (gitignore_file, G_FILE_CREATE_NONE, NULL, error); + + if (!out) + { + flatpak_fail (error, "Could not create gitignore file inside the state dir"); + return FALSE; + } + + if (!g_output_stream_write_all (G_OUTPUT_STREAM (out), + git_ignore_file, strlen (git_ignore_file), + NULL, NULL, error)) + { + flatpak_fail (error, "Could write gitignore file contents"); + return FALSE; + } + + if (!g_output_stream_close (G_OUTPUT_STREAM (out), NULL, error)) + { + flatpak_fail (error, "Failed to close file stream"); + 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;