]> git.proxmox.com Git - libgit2.git/blob - tests/repo/reservedname.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / repo / reservedname.c
1 #include "clar_libgit2.h"
2 #include "../submodule/submodule_helpers.h"
3 #include "repository.h"
4
5 void test_repo_reservedname__cleanup(void)
6 {
7 cl_git_sandbox_cleanup();
8 }
9
10 void test_repo_reservedname__includes_shortname_on_win32(void)
11 {
12 git_repository *repo;
13 git_str *reserved;
14 size_t reserved_len;
15
16 repo = cl_git_sandbox_init("nasty");
17 cl_assert(git_repository__reserved_names(&reserved, &reserved_len, repo, false));
18
19 #ifdef GIT_WIN32
20 cl_assert_equal_i(2, reserved_len);
21 cl_assert_equal_s(".git", reserved[0].ptr);
22 cl_assert_equal_s("GIT~1", reserved[1].ptr);
23 #else
24 cl_assert_equal_i(1, reserved_len);
25 cl_assert_equal_s(".git", reserved[0].ptr);
26 #endif
27 }
28
29 void test_repo_reservedname__includes_shortname_when_requested(void)
30 {
31 git_repository *repo;
32 git_str *reserved;
33 size_t reserved_len;
34
35 repo = cl_git_sandbox_init("nasty");
36 cl_assert(git_repository__reserved_names(&reserved, &reserved_len, repo, true));
37
38 cl_assert_equal_i(2, reserved_len);
39 cl_assert_equal_s(".git", reserved[0].ptr);
40 cl_assert_equal_s("GIT~1", reserved[1].ptr);
41 }
42
43 /* Ensures that custom shortnames are included: creates a GIT~1 so that the
44 * .git folder itself will have to be named GIT~2
45 */
46 void test_repo_reservedname__custom_shortname_recognized(void)
47 {
48 #ifdef GIT_WIN32
49 git_repository *repo;
50 git_str *reserved;
51 size_t reserved_len;
52
53 if (!cl_sandbox_supports_8dot3())
54 clar__skip();
55
56 repo = cl_git_sandbox_init("nasty");
57
58 cl_must_pass(p_rename("nasty/.git", "nasty/_temp"));
59 cl_git_write2file("nasty/git~1", "", 0, O_RDWR|O_CREAT, 0666);
60 cl_must_pass(p_rename("nasty/_temp", "nasty/.git"));
61
62 cl_assert(git_repository__reserved_names(&reserved, &reserved_len, repo, true));
63
64 cl_assert_equal_i(3, reserved_len);
65 cl_assert_equal_s(".git", reserved[0].ptr);
66 cl_assert_equal_s("GIT~1", reserved[1].ptr);
67 cl_assert_equal_s("GIT~2", reserved[2].ptr);
68 #endif
69 }
70
71 /* When looking at the short name for a submodule, we need to prevent
72 * people from overwriting the `.git` file in the submodule working
73 * directory itself. We don't want to look at the actual repository
74 * path, since it will be in the super's repository above us, and
75 * typically named with the name of our subrepository. Consequently,
76 * preventing access to the short name of the actual repository path
77 * would prevent us from creating files with the same name as the
78 * subrepo. (Eg, a submodule named "libgit2" could not contain a file
79 * named "libgit2", which would be unfortunate.)
80 */
81 void test_repo_reservedname__submodule_pointer(void)
82 {
83 #ifdef GIT_WIN32
84 git_repository *super_repo, *sub_repo;
85 git_submodule *sub;
86 git_str *sub_reserved;
87 size_t sub_reserved_len;
88
89 if (!cl_sandbox_supports_8dot3())
90 clar__skip();
91
92 super_repo = setup_fixture_submod2();
93
94 assert_submodule_exists(super_repo, "sm_unchanged");
95
96 cl_git_pass(git_submodule_lookup(&sub, super_repo, "sm_unchanged"));
97 cl_git_pass(git_submodule_open(&sub_repo, sub));
98
99 cl_assert(git_repository__reserved_names(&sub_reserved, &sub_reserved_len, sub_repo, true));
100
101 cl_assert_equal_i(2, sub_reserved_len);
102 cl_assert_equal_s(".git", sub_reserved[0].ptr);
103 cl_assert_equal_s("GIT~1", sub_reserved[1].ptr);
104
105 git_submodule_free(sub);
106 git_repository_free(sub_repo);
107 #endif
108 }
109
110 /* Like the `submodule_pointer` test (above), this ensures that we do not
111 * follow the gitlink to the submodule's repository location and treat that
112 * as a reserved name. This tests at an initial submodule update, where the
113 * submodule repo is being created.
114 */
115 void test_repo_reservedname__submodule_pointer_during_create(void)
116 {
117 git_repository *repo;
118 git_submodule *sm;
119 git_submodule_update_options update_options = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
120 git_str url = GIT_STR_INIT;
121
122 repo = setup_fixture_super();
123
124 cl_git_pass(git_str_joinpath(&url, clar_sandbox_path(), "sub.git"));
125 cl_repo_set_string(repo, "submodule.sub.url", url.ptr);
126
127 cl_git_pass(git_submodule_lookup(&sm, repo, "sub"));
128 cl_git_pass(git_submodule_update(sm, 1, &update_options));
129
130 git_submodule_free(sm);
131 git_str_dispose(&url);
132 }