Skip to content

Commit 0ef405b

Browse files
pks-tethomson
authored andcommitted
checkout: do not delete directories with untracked entries
If the `GIT_CHECKOUT_FORCE` flag is given to any of the `git_checkout` invocations, we remove files which were previously staged. But while doing so, we unfortunately also remove unstaged files in a directory which contains at least one staged file, resulting in potential data loss. This commit adds two tests to verify behavior.
1 parent 78b8f03 commit 0ef405b

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

tests/checkout/head.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,79 @@ void test_checkout_head__with_index_only_tree(void)
6060

6161
git_index_free(index);
6262
}
63+
64+
void test_checkout_head__do_not_remove_untracked_file(void)
65+
{
66+
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
67+
git_index *index;
68+
69+
cl_git_pass(p_mkdir("testrepo/tracked", 0755));
70+
cl_git_mkfile("testrepo/tracked/tracked", "tracked\n");
71+
cl_git_mkfile("testrepo/tracked/untracked", "untracked\n");
72+
73+
cl_git_pass(git_repository_index(&index, g_repo));
74+
cl_git_pass(git_index_add_bypath(index, "tracked/tracked"));
75+
cl_git_pass(git_index_write(index));
76+
77+
git_index_free(index);
78+
79+
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
80+
cl_git_pass(git_checkout_head(g_repo, &opts));
81+
82+
cl_assert(!git_path_isfile("testrepo/tracked/tracked"));
83+
cl_assert(git_path_isfile("testrepo/tracked/untracked"));
84+
}
85+
86+
void test_checkout_head__do_not_remove_untracked_file_in_subdir(void)
87+
{
88+
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
89+
git_index *index;
90+
91+
cl_git_pass(p_mkdir("testrepo/tracked", 0755));
92+
cl_git_pass(p_mkdir("testrepo/tracked/subdir", 0755));
93+
cl_git_mkfile("testrepo/tracked/tracked", "tracked\n");
94+
cl_git_mkfile("testrepo/tracked/subdir/tracked", "tracked\n");
95+
cl_git_mkfile("testrepo/tracked/subdir/untracked", "untracked\n");
96+
97+
cl_git_pass(git_repository_index(&index, g_repo));
98+
cl_git_pass(git_index_add_bypath(index, "tracked/tracked"));
99+
cl_git_pass(git_index_add_bypath(index, "tracked/subdir/tracked"));
100+
cl_git_pass(git_index_write(index));
101+
102+
git_index_free(index);
103+
104+
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
105+
cl_git_pass(git_checkout_head(g_repo, &opts));
106+
107+
cl_assert(!git_path_isfile("testrepo/tracked/tracked"));
108+
cl_assert(!git_path_isfile("testrepo/tracked/subdir/tracked"));
109+
cl_assert(git_path_isfile("testrepo/tracked/subdir/untracked"));
110+
}
111+
112+
void test_checkout_head__do_remove_tracked_subdir(void)
113+
{
114+
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
115+
git_index *index;
116+
117+
cl_git_pass(p_mkdir("testrepo/subdir", 0755));
118+
cl_git_pass(p_mkdir("testrepo/subdir/tracked", 0755));
119+
cl_git_mkfile("testrepo/subdir/tracked-file", "tracked\n");
120+
cl_git_mkfile("testrepo/subdir/untracked-file", "untracked\n");
121+
cl_git_mkfile("testrepo/subdir/tracked/tracked1", "tracked\n");
122+
cl_git_mkfile("testrepo/subdir/tracked/tracked2", "tracked\n");
123+
124+
cl_git_pass(git_repository_index(&index, g_repo));
125+
cl_git_pass(git_index_add_bypath(index, "subdir/tracked-file"));
126+
cl_git_pass(git_index_add_bypath(index, "subdir/tracked/tracked1"));
127+
cl_git_pass(git_index_add_bypath(index, "subdir/tracked/tracked2"));
128+
cl_git_pass(git_index_write(index));
129+
130+
git_index_free(index);
131+
132+
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
133+
cl_git_pass(git_checkout_head(g_repo, &opts));
134+
135+
cl_assert(!git_path_isdir("testrepo/subdir/tracked"));
136+
cl_assert(!git_path_isfile("testrepo/subdir/tracked-file"));
137+
cl_assert(git_path_isfile("testrepo/subdir/untracked-file"));
138+
}

0 commit comments

Comments
 (0)