]>
git.proxmox.com Git - libgit2.git/blob - tests/clone/local.c
1 #include "clar_libgit2.h"
3 #include "git2/clone.h"
10 static int file_url(git_buf
*buf
, const char *host
, const char *path
)
16 return git_buf_printf(buf
, "file://%s/%s", host
, path
);
20 static int git_style_unc_path(git_buf
*buf
, const char *host
, const char *path
)
25 git_buf_printf(buf
, "//%s/", host
);
30 if (git__isalpha(path
[0]) && path
[1] == ':' && path
[2] == '/') {
31 git_buf_printf(buf
, "%c$/", path
[0]);
35 git_buf_puts(buf
, path
);
37 return git_buf_oom(buf
) ? -1 : 0;
40 static int unc_path(git_buf
*buf
, const char *host
, const char *path
)
44 if (git_style_unc_path(buf
, host
, path
) < 0)
47 for (c
= buf
->ptr
; *c
; c
++)
55 void test_clone_local__should_clone_local(void)
57 git_buf buf
= GIT_BUF_INIT
;
59 /* we use a fixture path because it needs to exist for us to want to clone */
60 const char *path
= cl_fixture("testrepo.git");
62 cl_git_pass(file_url(&buf
, "", path
));
63 cl_assert_equal_i(0, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL_AUTO
));
64 cl_assert_equal_i(1, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL
));
65 cl_assert_equal_i(1, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL_NO_LINKS
));
66 cl_assert_equal_i(0, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_NO_LOCAL
));
68 cl_git_pass(file_url(&buf
, "localhost", path
));
69 cl_assert_equal_i(0, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL_AUTO
));
70 cl_assert_equal_i(1, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL
));
71 cl_assert_equal_i(1, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL_NO_LINKS
));
72 cl_assert_equal_i(0, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_NO_LOCAL
));
74 cl_git_pass(file_url(&buf
, "other-host.mycompany.com", path
));
75 cl_assert_equal_i(0, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL_AUTO
));
76 cl_assert_equal_i(0, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL
));
77 cl_assert_equal_i(0, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL_NO_LINKS
));
78 cl_assert_equal_i(0, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_NO_LOCAL
));
80 /* Ensure that file:/// urls are percent decoded: .git == %2e%67%69%74 */
81 cl_git_pass(file_url(&buf
, "", path
));
82 git_buf_shorten(&buf
, 4);
83 cl_git_pass(git_buf_puts(&buf
, "%2e%67%69%74"));
84 cl_assert_equal_i(0, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL_AUTO
));
85 cl_assert_equal_i(1, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL
));
86 cl_assert_equal_i(1, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_LOCAL_NO_LINKS
));
87 cl_assert_equal_i(0, git_clone__should_clone_local(buf
.ptr
, GIT_CLONE_NO_LOCAL
));
89 cl_assert_equal_i(1, git_clone__should_clone_local(path
, GIT_CLONE_LOCAL_AUTO
));
90 cl_assert_equal_i(1, git_clone__should_clone_local(path
, GIT_CLONE_LOCAL
));
91 cl_assert_equal_i(1, git_clone__should_clone_local(path
, GIT_CLONE_LOCAL_NO_LINKS
));
92 cl_assert_equal_i(0, git_clone__should_clone_local(path
, GIT_CLONE_NO_LOCAL
));
94 git_buf_dispose(&buf
);
97 void test_clone_local__hardlinks(void)
100 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
101 git_buf buf
= GIT_BUF_INIT
;
105 * In this first clone, we just copy over, since the temp dir
106 * will often be in a different filesystem, so we cannot
107 * link. It also allows us to control the number of links
110 opts
.local
= GIT_CLONE_LOCAL_NO_LINKS
;
111 cl_git_pass(git_clone(&repo
, cl_fixture("testrepo.git"), "./clone.git", &opts
));
112 git_repository_free(repo
);
114 /* This second clone is in the same filesystem, so we can hardlink */
116 opts
.local
= GIT_CLONE_LOCAL
;
117 cl_git_pass(git_clone(&repo
, cl_git_path_url("clone.git"), "./clone2.git", &opts
));
121 cl_git_pass(git_buf_join_n(&buf
, '/', 4, git_repository_path(repo
), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125"));
123 cl_git_pass(p_stat(buf
.ptr
, &st
));
124 cl_assert_equal_i(2, st
.st_nlink
);
127 git_repository_free(repo
);
130 opts
.local
= GIT_CLONE_LOCAL_NO_LINKS
;
131 cl_git_pass(git_clone(&repo
, cl_git_path_url("clone.git"), "./clone3.git", &opts
));
134 cl_git_pass(git_buf_join_n(&buf
, '/', 4, git_repository_path(repo
), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125"));
136 cl_git_pass(p_stat(buf
.ptr
, &st
));
137 cl_assert_equal_i(1, st
.st_nlink
);
139 git_repository_free(repo
);
141 /* this one should automatically use links */
142 cl_git_pass(git_clone(&repo
, "./clone.git", "./clone4.git", NULL
));
146 cl_git_pass(git_buf_join_n(&buf
, '/', 4, git_repository_path(repo
), "objects", "08", "b041783f40edfe12bb406c9c9a8a040177c125"));
148 cl_git_pass(p_stat(buf
.ptr
, &st
));
149 cl_assert_equal_i(3, st
.st_nlink
);
152 git_buf_dispose(&buf
);
153 git_repository_free(repo
);
155 cl_git_pass(git_futils_rmdir_r("./clone.git", NULL
, GIT_RMDIR_REMOVE_FILES
));
156 cl_git_pass(git_futils_rmdir_r("./clone2.git", NULL
, GIT_RMDIR_REMOVE_FILES
));
157 cl_git_pass(git_futils_rmdir_r("./clone3.git", NULL
, GIT_RMDIR_REMOVE_FILES
));
158 cl_git_pass(git_futils_rmdir_r("./clone4.git", NULL
, GIT_RMDIR_REMOVE_FILES
));
161 void test_clone_local__standard_unc_paths_are_written_git_style(void)
164 git_repository
*repo
;
166 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
167 git_buf unc
= GIT_BUF_INIT
, git_unc
= GIT_BUF_INIT
;
169 /* we use a fixture path because it needs to exist for us to want to clone */
170 const char *path
= cl_fixture("testrepo.git");
172 cl_git_pass(unc_path(&unc
, "localhost", path
));
173 cl_git_pass(git_style_unc_path(&git_unc
, "localhost", path
));
175 cl_git_pass(git_clone(&repo
, unc
.ptr
, "./clone.git", &opts
));
176 cl_git_pass(git_remote_lookup(&remote
, repo
, "origin"));
178 cl_assert_equal_s(git_unc
.ptr
, git_remote_url(remote
));
180 git_remote_free(remote
);
181 git_repository_free(repo
);
182 git_buf_dispose(&unc
);
183 git_buf_dispose(&git_unc
);
185 cl_git_pass(git_futils_rmdir_r("./clone.git", NULL
, GIT_RMDIR_REMOVE_FILES
));
189 void test_clone_local__git_style_unc_paths(void)
192 git_repository
*repo
;
194 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
195 git_buf git_unc
= GIT_BUF_INIT
;
197 /* we use a fixture path because it needs to exist for us to want to clone */
198 const char *path
= cl_fixture("testrepo.git");
200 cl_git_pass(git_style_unc_path(&git_unc
, "localhost", path
));
202 cl_git_pass(git_clone(&repo
, git_unc
.ptr
, "./clone.git", &opts
));
203 cl_git_pass(git_remote_lookup(&remote
, repo
, "origin"));
205 cl_assert_equal_s(git_unc
.ptr
, git_remote_url(remote
));
207 git_remote_free(remote
);
208 git_repository_free(repo
);
209 git_buf_dispose(&git_unc
);
211 cl_git_pass(git_futils_rmdir_r("./clone.git", NULL
, GIT_RMDIR_REMOVE_FILES
));