Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gitignore when switching subdirectories between normal and generated #492

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/tup/create_name_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ int make_dirs_normal(struct tup_entry *dtent)
tup_db_del_ghost_tree(dtent);
dtent = dtent->parent;
}
if(dtent) {
/* Mark normal parent as needing update
* to regenerate gitignore (t2244)
*/
struct tup_entry *gitignore_tent;
if(tup_db_select_tent(dtent, ".gitignore", &gitignore_tent) < 0)
return -1;
if(gitignore_tent && gitignore_tent->type == TUP_NODE_GENERATED) {
tup_db_add_create_list(gitignore_tent->dt);
}
}
return 0;
}

Expand Down
9 changes: 4 additions & 5 deletions src/tup/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ static int eval_eq(struct tupfile *tf, char *expr, char *eol);
static int error_directive(struct tupfile *tf, char *cmdline);
static int preload(struct tupfile *tf, char *cmdline);
static int run_script(struct tupfile *tf, char *cmdline, int lno);
static int remove_tup_gitignore(struct tupfile *tf, struct tup_entry *tent);
static int gitignore(struct tupfile *tf, struct tup_entry *dtent);
static int check_toplevel_gitignore(struct tupfile *tf);
static int parse_rule(struct tupfile *tf, char *p, int lno);
Expand Down Expand Up @@ -313,7 +312,7 @@ int parse(struct node *n, struct graph *g, struct timespan *retts, int refactori
fprintf(tf.f, "tup refactoring error: Attempting to remove the .gitignore file.\n");
goto out_free_bs;
}
if(remove_tup_gitignore(&tf, tent) < 0)
if(remove_tup_gitignore(tf.g, tent) < 0)
goto out_free_bs;
}
}
Expand Down Expand Up @@ -1130,7 +1129,7 @@ int import(struct tupfile *tf, const char *cmdline, const char **retvar, const c
/* If a .gitignore directive is removed, we need to either revert back to the
* user's explicit .gitignore file, or remove it entirely.
*/
static int remove_tup_gitignore(struct tupfile *tf, struct tup_entry *tent)
int remove_tup_gitignore(struct graph *g, struct tup_entry *tent)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably still take a struct tupfile, since this function should be printing errors to tf->f instead of stderr. The former will print the error messages under the banner of the directory being parsed, rather than immediately, so it is more obvious which directory actually failed. I can fix this in a separate commit if you like.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err in retrospect, keeping the struct tupfile would make moving it into updater.c more difficult. I am making a change now to make this a FILE *err input instead, so the updater.c usage of it can pass in stderr and the parser.c usage can pass in tf->f.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, I was wondering if we still had a valid tupfile in updater.c for that 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I left this one for you, since you said you already had a patch waiting; but I fixed the other issues. I think it might be good to go, but you'll be the judge of that XD

{
int dfd;
int fdold;
Expand Down Expand Up @@ -1207,8 +1206,8 @@ static int remove_tup_gitignore(struct tupfile *tf, struct tup_entry *tent)
return -1;
if(tup_db_set_srcid(tent, -1) < 0)
return -1;
tent_tree_remove(&tf->g->gen_delete_root, tent);
tent_tree_remove(&tf->g->save_root, tent);
tent_tree_remove(&g->gen_delete_root, tent);
tent_tree_remove(&g->save_root, tent);
} else {
if(unlinkat(dfd, ".gitignore.new", 0) < 0) {
perror("unlinkat");
Expand Down
1 change: 1 addition & 0 deletions src/tup/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ void init_rule(struct rule *r);
int execute_rule(struct tupfile *tf, struct rule *r, struct name_list *output_nl);
int parser_include_file(struct tupfile *tf, const char *file);
int parser_include_rules(struct tupfile *tf, const char *tuprules);
int remove_tup_gitignore(struct graph *g, struct tup_entry *tent); // HACK

struct node;
struct graph;
Expand Down
33 changes: 22 additions & 11 deletions src/tup/updater.c
Original file line number Diff line number Diff line change
Expand Up @@ -1529,17 +1529,6 @@ static int process_create_nodes(void)
tup_lua_parser_cleanup();

if(rc == 0) {
if(g.gen_delete_root.count) {
tup_main_progress("Deleting files...\n");
} else {
tup_main_progress("No files to delete.\n");
}
rc = delete_files(&g);
if(rc == 0 && group_need_circ_check()) {
tup_show_message("Checking circular dependencies among groups...\n");
if(group_circ_check() < 0)
rc = -1;
}
if(rc == 0 && !RB_EMPTY(&g.normal_dir_root)) {
int msg_shown = 0;
while(!RB_EMPTY(&g.normal_dir_root)) {
Expand Down Expand Up @@ -1569,9 +1558,31 @@ static int process_create_nodes(void)
*/
if(tent_tree_add_dup(&g.parse_gitignore_root, tent->parent) < 0)
return -1;
/* And remove extra gitignore file in
* the generated folder (t2245)
*/
struct tup_entry *gitignore_tent;
if(tup_db_select_tent(tent, ".gitignore", &gitignore_tent) < 0)
return -1;
if(gitignore_tent && gitignore_tent->type == TUP_NODE_GENERATED) {
tent_tree_add(&g.gen_delete_root, gitignore_tent);
if(remove_tup_gitignore(&g, gitignore_tent) < 0)
return -1;
}
}
}
}
if(g.gen_delete_root.count) {
tup_main_progress("Deleting files...\n");
} else {
tup_main_progress("No files to delete.\n");
}
rc = delete_files(&g);
if(rc == 0 && group_need_circ_check()) {
tup_show_message("Checking circular dependencies among groups...\n");
if(group_circ_check() < 0)
rc = -1;
}
if(rc == 0 && !RB_EMPTY(&g.parse_gitignore_root) && !refactoring) {
tup_show_message("Generating .gitignore files...\n");
RB_FOREACH(tt, tent_entries, &g.parse_gitignore_root) {
Expand Down
41 changes: 41 additions & 0 deletions test/t2244-gitignore-updated-generated-to-normal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#! /bin/sh -e
# tup - A file-based build system
#
# Copyright (C) 2024 Bojidar Marinov <bojidar.marinov.bg@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Make sure importing with a default value works when the environment variable
# is set the first time tup is called.

. ./tup.sh

cat > Tupfile <<HERE
.gitignore
: |> touch %o |> out/generated.txt
HERE
cat > Tupdefault <<HERE
.gitignore
HERE

update
gitignore_good out .gitignore

touch out/non-generated.txt

update
gitignore_bad out .gitignore
gitignore_good generated.txt out/.gitignore

eotup
44 changes: 44 additions & 0 deletions test/t2245-gitignore-updated-normal-to-generated.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#! /bin/sh -e
# tup - A file-based build system
#
# Copyright (C) 2024 Bojidar Marinov <bojidar.marinov.bg@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# Make sure importing with a default value works when the environment variable
# is set the first time tup is called.

. ./tup.sh

cat > Tupfile <<HERE
.gitignore
: |> touch %o |> out/generated.txt
HERE
cat > Tupdefault <<HERE
.gitignore
HERE
mkdir out
touch out/non-generated.txt

update
gitignore_bad out .gitignore
gitignore_good generated.txt out/.gitignore

rm out/non-generated.txt

update
gitignore_good out .gitignore
check_not_exist out/.gitignore

eotup