Skip to content

Commit

Permalink
Update gitignore when switching subdirectories between normal and gen…
Browse files Browse the repository at this point in the history
…erated

Fixes gittup#491.
  • Loading branch information
bojidar-bg committed Mar 16, 2024
1 parent 5bb6052 commit b2064a7
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 16 deletions.
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's .gitignore as
* needing update (t9010)
*/
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); // HACK -- ideally, tup_db_add_modify_list here would be sufficient, but as it is, we need to reparse the directory's Tupfile to update the .gitignore.
}
}
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)
{
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 (t9011)
*/
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
22 changes: 22 additions & 0 deletions test/t9010-gitignore-updated-generated-to-normal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#! /bin/sh -e

. ./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
25 changes: 25 additions & 0 deletions test/t9011-gitignore-updated-normal-to-generated.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! /bin/sh -e

. ./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

0 comments on commit b2064a7

Please sign in to comment.