Skip to content

Commit d2c4f76

Browse files
authored
Merge pull request libgit2#4260 from libgit2/ethomson/forced_checkout_2
Update to forced checkout and untracked files
2 parents e476d52 + 4a0df57 commit d2c4f76

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

src/checkout.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,8 @@ static int checkout_action_wd_only(
370370
*/
371371
const git_index_entry *e = git_index_get_byindex(data->index, pos);
372372

373-
if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0) {
374-
notify = GIT_CHECKOUT_NOTIFY_DIRTY;
375-
remove = ((data->strategy & GIT_CHECKOUT_FORCE) != 0);
376-
}
373+
if (e != NULL && data->diff->pfxcomp(e->path, wd->path) == 0)
374+
return git_iterator_advance_into(wditem, workdir);
377375
}
378376
}
379377

src/fileops.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,9 @@ static int futils__rmdir_empty_parent(void *opaque, const char *path)
770770

771771
if (en == ENOENT || en == ENOTDIR) {
772772
/* do nothing */
773+
} else if ((data->flags & GIT_RMDIR_SKIP_NONEMPTY) == 0 &&
774+
en == EBUSY) {
775+
error = git_path_set_error(errno, path, "rmdir");
773776
} else if (en == ENOTEMPTY || en == EEXIST || en == EBUSY) {
774777
error = GIT_ITEROVER;
775778
} else {

tests/checkout/head.c

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void test_checkout_head__with_index_only_tree(void)
3838
cl_git_pass(git_repository_index(&index, g_repo));
3939

4040
p_mkdir("testrepo/newdir", 0777);
41-
cl_git_mkfile("testrepo/newdir/newfile.txt", "new file\n");
41+
cl_git_mkfile("testrepo/newdir/newfile.txt", "new file\n");
4242

4343
cl_git_pass(git_index_add_bypath(index, "newdir/newfile.txt"));
4444
cl_git_pass(git_index_write(index));
@@ -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)