Skip to content

Commit

Permalink
builder-context: Write a gitignore file into the state-dir
Browse files Browse the repository at this point in the history
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]: flatpak/flatpak@a62f8cf
  • Loading branch information
alatiera committed Nov 7, 2024
1 parent f996a79 commit c737c0a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/builder-context.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/builder-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/builder-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c737c0a

Please sign in to comment.