]> git.proxmox.com Git - libgit2.git/blob - tests/network/fetchlocal.c
Merge pull request #2575 from cirosantilli/factor-struct-typedef
[libgit2.git] / tests / network / fetchlocal.c
1 #include "clar_libgit2.h"
2
3 #include "buffer.h"
4 #include "path.h"
5 #include "remote.h"
6
7 static int transfer_cb(const git_transfer_progress *stats, void *payload)
8 {
9 int *callcount = (int*)payload;
10 GIT_UNUSED(stats);
11 (*callcount)++;
12 return 0;
13 }
14
15 static void cleanup_local_repo(void *path)
16 {
17 cl_fixture_cleanup((char *)path);
18 }
19
20 void test_network_fetchlocal__complete(void)
21 {
22 git_repository *repo;
23 git_remote *origin;
24 int callcount = 0;
25 git_strarray refnames = {0};
26
27 const char *url = cl_git_fixture_url("testrepo.git");
28 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
29
30 callbacks.transfer_progress = transfer_cb;
31 callbacks.payload = &callcount;
32
33 cl_set_cleanup(&cleanup_local_repo, "foo");
34 cl_git_pass(git_repository_init(&repo, "foo", true));
35
36 cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url));
37 git_remote_set_callbacks(origin, &callbacks);
38 cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH));
39 cl_git_pass(git_remote_download(origin, NULL));
40 cl_git_pass(git_remote_update_tips(origin, NULL, NULL));
41
42 cl_git_pass(git_reference_list(&refnames, repo));
43 cl_assert_equal_i(19, (int)refnames.count);
44 cl_assert(callcount > 0);
45
46 git_strarray_free(&refnames);
47 git_remote_free(origin);
48 git_repository_free(repo);
49 }
50
51 static void cleanup_sandbox(void *unused)
52 {
53 GIT_UNUSED(unused);
54 cl_git_sandbox_cleanup();
55 }
56
57 void test_network_fetchlocal__partial(void)
58 {
59 git_repository *repo = cl_git_sandbox_init("partial-testrepo");
60 git_remote *origin;
61 int callcount = 0;
62 git_strarray refnames = {0};
63 const char *url;
64 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
65
66 callbacks.transfer_progress = transfer_cb;
67 callbacks.payload = &callcount;
68
69 cl_set_cleanup(&cleanup_sandbox, NULL);
70 cl_git_pass(git_reference_list(&refnames, repo));
71 cl_assert_equal_i(1, (int)refnames.count);
72
73 url = cl_git_fixture_url("testrepo.git");
74 cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url));
75 git_remote_set_callbacks(origin, &callbacks);
76 cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH));
77 cl_git_pass(git_remote_download(origin, NULL));
78 cl_git_pass(git_remote_update_tips(origin, NULL, NULL));
79
80 git_strarray_free(&refnames);
81
82 cl_git_pass(git_reference_list(&refnames, repo));
83 cl_assert_equal_i(20, (int)refnames.count); /* 18 remote + 1 local */
84 cl_assert(callcount > 0);
85
86 git_strarray_free(&refnames);
87 git_remote_free(origin);
88 }
89
90 static int remote_mirror_cb(git_remote **out, git_repository *repo,
91 const char *name, const char *url, void *payload)
92 {
93 int error;
94 git_remote *remote;
95
96 GIT_UNUSED(payload);
97
98 if ((error = git_remote_create(&remote, repo, name, url)) < 0)
99 return error;
100
101 git_remote_clear_refspecs(remote);
102
103 if ((error = git_remote_add_fetch(remote, "+refs/*:refs/*")) < 0) {
104 git_remote_free(remote);
105 return error;
106 }
107
108 *out = remote;
109 return 0;
110 }
111
112 void test_network_fetchlocal__clone_into_mirror(void)
113 {
114 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
115 git_repository *repo;
116 git_reference *head;
117
118 opts.bare = true;
119 opts.remote_cb = remote_mirror_cb;
120 cl_git_pass(git_clone(&repo, cl_git_fixture_url("testrepo.git"), "./foo.git", &opts));
121
122 cl_git_pass(git_reference_lookup(&head, repo, "HEAD"));
123 cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
124 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
125
126 git_reference_free(head);
127 git_repository_free(repo);
128 cl_fixture_cleanup("./foo.git");
129 }
130
131 void test_network_fetchlocal__multi_remotes(void)
132 {
133 git_repository *repo = cl_git_sandbox_init("testrepo.git");
134 git_remote *test, *test2;
135 git_strarray refnames = {0};
136 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
137
138 callbacks.transfer_progress = transfer_cb;
139 cl_git_pass(git_remote_load(&test, repo, "test"));
140 cl_git_pass(git_remote_set_url(test, cl_git_fixture_url("testrepo.git")));
141 git_remote_set_callbacks(test, &callbacks);
142 cl_git_pass(git_remote_connect(test, GIT_DIRECTION_FETCH));
143 cl_git_pass(git_remote_download(test, NULL));
144 cl_git_pass(git_remote_update_tips(test, NULL, NULL));
145
146 cl_git_pass(git_reference_list(&refnames, repo));
147 cl_assert_equal_i(32, (int)refnames.count);
148
149 cl_git_pass(git_remote_load(&test2, repo, "test_with_pushurl"));
150 cl_git_pass(git_remote_set_url(test2, cl_git_fixture_url("testrepo.git")));
151 git_remote_set_callbacks(test2, &callbacks);
152 cl_git_pass(git_remote_connect(test2, GIT_DIRECTION_FETCH));
153 cl_git_pass(git_remote_download(test2, NULL));
154 cl_git_pass(git_remote_update_tips(test2, NULL, NULL));
155
156 cl_git_pass(git_reference_list(&refnames, repo));
157 cl_assert_equal_i(44, (int)refnames.count);
158
159 git_strarray_free(&refnames);
160 git_remote_free(test);
161 git_remote_free(test2);
162 git_repository_free(repo);
163 }