]> git.proxmox.com Git - libgit2.git/blame - tests/checkout/icase.c
checkout: break case-changes into delete/add
[libgit2.git] / tests / checkout / icase.c
CommitLineData
e74340b0
ET
1#include "clar_libgit2.h"
2
3#include "git2/checkout.h"
4#include "path.h"
5
53eb139d 6#ifdef GIT_WIN32
118e6fdc 7# include <windows.h>
53eb139d
ET
8#endif
9
e74340b0
ET
10static git_repository *repo;
11static git_object *obj;
12static git_checkout_options checkout_opts;
13
14void test_checkout_icase__initialize(void)
15{
16 git_oid id;
17
18 repo = cl_git_sandbox_init("testrepo");
19
20 cl_git_pass(git_reference_name_to_id(&id, repo, "refs/heads/dir"));
21 cl_git_pass(git_object_lookup(&obj, repo, &id, GIT_OBJ_ANY));
22
23 git_checkout_init_options(&checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION);
05f69012 24 checkout_opts.checkout_strategy = GIT_CHECKOUT_NONE;
e74340b0
ET
25}
26
27void test_checkout_icase__cleanup(void)
28{
29 git_object_free(obj);
30 cl_git_sandbox_cleanup();
31}
32
e0902fbc 33static char *test_realpath(const char *in)
53eb139d
ET
34{
35#ifdef GIT_WIN32
53eb139d 36 HANDLE fh;
e0902fbc 37 HMODULE kerneldll;
53eb139d
ET
38 char *filename;
39
40 typedef DWORD (__stdcall *getfinalpathname)(HANDLE, LPSTR, DWORD, DWORD);
41 getfinalpathname getfinalpathfn;
42
43 cl_assert(filename = malloc(MAX_PATH));
e0902fbc
ET
44 cl_assert(kerneldll = LoadLibrary("kernel32.dll"));
45 cl_assert(getfinalpathfn = (getfinalpathname)GetProcAddress(kerneldll, "GetFinalPathNameByHandleA"));
53eb139d 46
e0902fbc 47 cl_assert(fh = CreateFileA(in, FILE_READ_ATTRIBUTES | STANDARD_RIGHTS_READ, FILE_SHARE_READ,
53eb139d
ET
48 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL));
49
50 cl_win32_pass(getfinalpathfn(fh, filename, MAX_PATH, VOLUME_NAME_DOS));
51
52 CloseHandle(fh);
53
54 git_path_mkposix(filename);
55
56 return filename;
57#else
58 return realpath(in, NULL);
59#endif
60}
61
e74340b0
ET
62static void assert_name_is(const char *expected)
63{
64 char *actual;
65 size_t actual_len, expected_len, start;
66
e0902fbc 67 cl_assert(actual = test_realpath(expected));
e74340b0
ET
68
69 expected_len = strlen(expected);
70 actual_len = strlen(actual);
71 cl_assert(actual_len >= expected_len);
72
73 start = actual_len - expected_len;
74 cl_assert_equal_s(expected, actual + start);
75
76 if (start)
77 cl_assert_equal_strn("/", actual + (start - 1), 1);
78
79 free(actual);
80}
81
05f69012
ET
82void test_checkout_icase__refuses_to_overwrite_files_for_files(void)
83{
84 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
85
86 cl_git_write2file("testrepo/BRANCH_FILE.txt", "neue file\n", 10, \
87 O_WRONLY | O_CREAT | O_TRUNC, 0644);
88
89 cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
90 assert_name_is("testrepo/BRANCH_FILE.txt");
91}
92
93void test_checkout_icase__overwrites_files_for_files_when_forced(void)
e74340b0 94{
05f69012
ET
95 checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
96
e74340b0
ET
97 cl_git_write2file("testrepo/NEW.txt", "neue file\n", 10, \
98 O_WRONLY | O_CREAT | O_TRUNC, 0644);
99
100 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
101 assert_name_is("testrepo/new.txt");
102}
103
05f69012 104void test_checkout_icase__refuses_to_overwrite_links_for_files(void)
e74340b0 105{
05f69012
ET
106 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
107
108 cl_must_pass(p_symlink("../tmp", "testrepo/BRANCH_FILE.txt"));
109
110 cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
111
112 cl_assert(!git_path_exists("tmp"));
113 assert_name_is("testrepo/BRANCH_FILE.txt");
114}
115
116void test_checkout_icase__overwrites_links_for_files_when_forced(void)
117{
118 checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
119
e74340b0
ET
120 cl_must_pass(p_symlink("../tmp", "testrepo/NEW.txt"));
121
122 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
123
124 cl_assert(!git_path_exists("tmp"));
125 assert_name_is("testrepo/new.txt");
126}
127
05f69012
ET
128void test_checkout_icase__overwrites_empty_folders_for_files(void)
129{
130 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
131
132 cl_must_pass(p_mkdir("testrepo/NEW.txt", 0777));
133
134 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
135
136 assert_name_is("testrepo/new.txt");
137 cl_assert(!git_path_isdir("testrepo/new.txt"));
138}
139
140void test_checkout_icase__refuses_to_overwrite_populated_folders_for_files(void)
141{
142 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
143
144 cl_must_pass(p_mkdir("testrepo/BRANCH_FILE.txt", 0777));
145 cl_git_write2file("testrepo/BRANCH_FILE.txt/foobar", "neue file\n", 10, \
146 O_WRONLY | O_CREAT | O_TRUNC, 0644);
147
148 cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
149
150 assert_name_is("testrepo/BRANCH_FILE.txt");
151 cl_assert(git_path_isdir("testrepo/BRANCH_FILE.txt"));
152}
153
154void test_checkout_icase__overwrites_folders_for_files_when_forced(void)
e74340b0 155{
05f69012
ET
156 checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
157
e74340b0 158 cl_must_pass(p_mkdir("testrepo/NEW.txt", 0777));
05f69012
ET
159 cl_git_write2file("testrepo/NEW.txt/foobar", "neue file\n", 10, \
160 O_WRONLY | O_CREAT | O_TRUNC, 0644);
e74340b0
ET
161
162 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
163
164 assert_name_is("testrepo/new.txt");
165 cl_assert(!git_path_isdir("testrepo/new.txt"));
166}
167
05f69012 168void test_checkout_icase__refuses_to_overwrite_files_for_folders(void)
e74340b0 169{
05f69012
ET
170 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
171
172 cl_git_write2file("testrepo/A", "neue file\n", 10, \
173 O_WRONLY | O_CREAT | O_TRUNC, 0644);
174
175 cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
176 assert_name_is("testrepo/A");
177 cl_assert(!git_path_isdir("testrepo/A"));
178}
179
180void test_checkout_icase__overwrites_files_for_folders_when_forced(void)
181{
182 checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
183
e74340b0
ET
184 cl_git_write2file("testrepo/A", "neue file\n", 10, \
185 O_WRONLY | O_CREAT | O_TRUNC, 0644);
186
187 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
188 assert_name_is("testrepo/a");
189 cl_assert(git_path_isdir("testrepo/a"));
190}
191
05f69012
ET
192void test_checkout_icase__refuses_to_overwrite_links_for_folders(void)
193{
194 checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
195
196 cl_must_pass(p_symlink("..", "testrepo/A"));
197
198 cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
199
200 cl_assert(!git_path_exists("b.txt"));
201 assert_name_is("testrepo/A");
202}
203
204void test_checkout_icase__overwrites_links_for_folders_when_forced(void)
e74340b0 205{
05f69012
ET
206 checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
207
e74340b0
ET
208 cl_must_pass(p_symlink("..", "testrepo/A"));
209
210 cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
211
212 cl_assert(!git_path_exists("b.txt"));
213 assert_name_is("testrepo/a");
214}