]> git.proxmox.com Git - libgit2.git/blame - tests/online/push.c
Merge pull request #3422 from ethomson/workdir_diff
[libgit2.git] / tests / online / push.c
CommitLineData
613d5eb9
PK
1#include "clar_libgit2.h"
2#include "buffer.h"
3#include "posix.h"
4#include "vector.h"
5#include "../submodule/submodule_helpers.h"
6#include "push_util.h"
1d645aab
JM
7#include "refspec.h"
8#include "remote.h"
613d5eb9 9
613d5eb9
PK
10static git_repository *_repo;
11
e069c621 12static char *_remote_url = NULL;
84efffc3 13
e069c621
ET
14static char *_remote_user = NULL;
15static char *_remote_pass = NULL;
5be622fb 16
e069c621
ET
17static char *_remote_ssh_key = NULL;
18static char *_remote_ssh_pubkey = NULL;
19static char *_remote_ssh_passphrase = NULL;
613d5eb9 20
e069c621 21static char *_remote_default = NULL;
84efffc3 22
e3c131c5
CMN
23static int cred_acquire_cb(git_cred **, const char *, const char *, unsigned int, void *);
24
613d5eb9
PK
25static git_remote *_remote;
26static record_callbacks_data _record_cbs_data = {{ 0 }};
27static git_remote_callbacks _record_cbs = RECORD_CALLBACKS_INIT(&_record_cbs_data);
28
29static git_oid _oid_b6;
30static git_oid _oid_b5;
31static git_oid _oid_b4;
32static git_oid _oid_b3;
33static git_oid _oid_b2;
34static git_oid _oid_b1;
35
abeefbbe
MS
36static git_oid _tag_commit;
37static git_oid _tag_tree;
38static git_oid _tag_blob;
39static git_oid _tag_lightweight;
1c13b0bf 40static git_oid _tag_tag;
abeefbbe 41
7602cb7c 42static int cred_acquire_cb(
2648dc1a
ET
43 git_cred **cred,
44 const char *url,
45 const char *user_from_url,
46 unsigned int allowed_types,
47 void *payload)
613d5eb9
PK
48{
49 GIT_UNUSED(url);
7602cb7c 50 GIT_UNUSED(user_from_url);
2648dc1a 51 GIT_UNUSED(payload);
613d5eb9 52
e26b08d3
CMN
53 if (GIT_CREDTYPE_USERNAME & allowed_types) {
54 if (!_remote_user) {
55 printf("GITTEST_REMOTE_USER must be set\n");
56 return -1;
57 }
58
59 return git_cred_username_new(cred, _remote_user);
60 }
61
84efffc3
ET
62 if (GIT_CREDTYPE_DEFAULT & allowed_types) {
63 if (!_remote_default) {
64 printf("GITTEST_REMOTE_DEFAULT must be set to use NTLM/Negotiate credentials\n");
65 return -1;
66 }
67
68 return git_cred_default_new(cred);
69 }
70
70a8c78f 71 if (GIT_CREDTYPE_SSH_KEY & allowed_types) {
2648dc1a
ET
72 if (!_remote_user || !_remote_ssh_pubkey || !_remote_ssh_key || !_remote_ssh_passphrase) {
73 printf("GITTEST_REMOTE_USER, GITTEST_REMOTE_SSH_PUBKEY, GITTEST_REMOTE_SSH_KEY and GITTEST_REMOTE_SSH_PASSPHRASE must be set\n");
74 return -1;
75 }
84efffc3 76
70a8c78f 77 return git_cred_ssh_key_new(cred, _remote_user, _remote_ssh_pubkey, _remote_ssh_key, _remote_ssh_passphrase);
2648dc1a 78 }
5be622fb 79
2648dc1a
ET
80 if (GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) {
81 if (!_remote_user || !_remote_pass) {
82 printf("GITTEST_REMOTE_USER and GITTEST_REMOTE_PASS must be set\n");
83 return -1;
84 }
613d5eb9 85
2648dc1a
ET
86 return git_cred_userpass_plaintext_new(cred, _remote_user, _remote_pass);
87 }
88
89 return -1;
613d5eb9
PK
90}
91
613d5eb9
PK
92/**
93 * git_push_status_foreach callback that records status entries.
94 * @param data (git_vector *) of push_status instances
95 */
6eb9e39c 96static int record_push_status_cb(const char *ref, const char *msg, void *payload)
613d5eb9 97{
6eb9e39c 98 record_callbacks_data *data = (record_callbacks_data *) payload;
613d5eb9
PK
99 push_status *s;
100
6eb9e39c
CMN
101 cl_assert(s = git__calloc(1, sizeof(*s)));
102 if (ref)
103 cl_assert(s->ref = git__strdup(ref));
9d41984c 104 s->success = (msg == NULL);
6eb9e39c
CMN
105 if (msg)
106 cl_assert(s->msg = git__strdup(msg));
613d5eb9 107
6eb9e39c 108 git_vector_insert(&data->statuses, s);
613d5eb9
PK
109
110 return 0;
111}
112
6eb9e39c 113static void do_verify_push_status(record_callbacks_data *data, const push_status expected[], const size_t expected_len)
613d5eb9 114{
6eb9e39c 115 git_vector *actual = &data->statuses;
613d5eb9
PK
116 push_status *iter;
117 bool failed = false;
118 size_t i;
119
6eb9e39c 120 if (expected_len != actual->length)
613d5eb9
PK
121 failed = true;
122 else
6eb9e39c 123 git_vector_foreach(actual, i, iter)
613d5eb9 124 if (strcmp(expected[i].ref, iter->ref) ||
9d41984c
ET
125 (expected[i].success != iter->success) ||
126 (expected[i].msg && (!iter->msg || strcmp(expected[i].msg, iter->msg)))) {
613d5eb9
PK
127 failed = true;
128 break;
129 }
130
131 if (failed) {
132 git_buf msg = GIT_BUF_INIT;
133
134 git_buf_puts(&msg, "Expected and actual push statuses differ:\nEXPECTED:\n");
135
136 for(i = 0; i < expected_len; i++) {
137 git_buf_printf(&msg, "%s: %s\n",
138 expected[i].ref,
9d41984c 139 expected[i].success ? "success" : "failed");
613d5eb9
PK
140 }
141
142 git_buf_puts(&msg, "\nACTUAL:\n");
143
6eb9e39c 144 git_vector_foreach(actual, i, iter) {
9d41984c
ET
145 if (iter->success)
146 git_buf_printf(&msg, "%s: success\n", iter->ref);
147 else
148 git_buf_printf(&msg, "%s: failed with message: %s", iter->ref, iter->msg);
149 }
613d5eb9
PK
150
151 cl_fail(git_buf_cstr(&msg));
152
153 git_buf_free(&msg);
154 }
155
6eb9e39c 156 git_vector_foreach(actual, i, iter)
613d5eb9
PK
157 git__free(iter);
158
6eb9e39c 159 git_vector_free(actual);
613d5eb9
PK
160}
161
162/**
163 * Verifies that after git_push_finish(), refs on a remote have the expected
164 * names, oids, and order.
156cfec0 165 *
613d5eb9
PK
166 * @param remote remote to verify
167 * @param expected_refs expected remote refs after push
168 * @param expected_refs_len length of expected_refs
169 */
170static void verify_refs(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len)
171{
359dce72
CMN
172 const git_remote_head **actual_refs;
173 size_t actual_refs_len;
613d5eb9 174
359dce72
CMN
175 git_remote_ls(&actual_refs, &actual_refs_len, remote);
176 verify_remote_refs(actual_refs, actual_refs_len, expected_refs, expected_refs_len);
613d5eb9
PK
177}
178
1d645aab
JM
179/**
180 * Verifies that after git_push_update_tips(), remote tracking branches have the expected
181 * names and oids.
182 *
183 * @param remote remote to verify
184 * @param expected_refs expected remote refs after push
185 * @param expected_refs_len length of expected_refs
186 */
187static void verify_tracking_branches(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len)
188{
4330ab26 189 git_refspec *fetch_spec;
1d645aab
JM
190 size_t i, j;
191 git_buf msg = GIT_BUF_INIT;
192 git_buf ref_name = GIT_BUF_INIT;
1d645aab 193 git_vector actual_refs = GIT_VECTOR_INIT;
8ec889a4 194 git_branch_iterator *iter;
1d645aab
JM
195 char *actual_ref;
196 git_oid oid;
8ec889a4 197 int failed = 0, error;
a667ca82 198 git_branch_t branch_type;
8ec889a4 199 git_reference *ref;
1d645aab 200
fe794b2e 201 /* Get current remote-tracking branches */
8ec889a4
CMN
202 cl_git_pass(git_branch_iterator_new(&iter, remote->repo, GIT_BRANCH_REMOTE));
203
204 while ((error = git_branch_next(&ref, &branch_type, iter)) == 0) {
205 cl_assert_equal_i(branch_type, GIT_BRANCH_REMOTE);
206
207 cl_git_pass(git_vector_insert(&actual_refs, git__strdup(git_reference_name(ref))));
f96e7e6c
ET
208
209 git_reference_free(ref);
8ec889a4
CMN
210 }
211
212 cl_assert_equal_i(error, GIT_ITEROVER);
11bd7a03 213 git_branch_iterator_free(iter);
1d645aab
JM
214
215 /* Loop through expected refs, make sure they exist */
216 for (i = 0; i < expected_refs_len; i++) {
217
fe794b2e 218 /* Convert remote reference name into remote-tracking branch name.
1d645aab
JM
219 * If the spec is not under refs/heads/, then skip.
220 */
4330ab26
CMN
221 fetch_spec = git_remote__matching_refspec(remote, expected_refs[i].name);
222 if (!fetch_spec)
1d645aab
JM
223 continue;
224
bf522e08 225 cl_git_pass(git_refspec_transform(&ref_name, fetch_spec, expected_refs[i].name));
1d645aab
JM
226
227 /* Find matching remote branch */
228 git_vector_foreach(&actual_refs, j, actual_ref) {
8ec889a4 229 if (!strcmp(git_buf_cstr(&ref_name), actual_ref))
1d645aab
JM
230 break;
231 }
232
233 if (j == actual_refs.length) {
234 git_buf_printf(&msg, "Did not find expected tracking branch '%s'.", git_buf_cstr(&ref_name));
235 failed = 1;
236 goto failed;
237 }
238
239 /* Make sure tracking branch is at expected commit ID */
8ec889a4 240 cl_git_pass(git_reference_name_to_id(&oid, remote->repo, actual_ref));
1d645aab
JM
241
242 if (git_oid_cmp(expected_refs[i].oid, &oid) != 0) {
243 git_buf_puts(&msg, "Tracking branch commit does not match expected ID.");
244 failed = 1;
245 goto failed;
246 }
247
2ff4469a 248 git__free(actual_ref);
1d645aab
JM
249 cl_git_pass(git_vector_remove(&actual_refs, j));
250 }
251
252 /* Make sure there are no extra branches */
253 if (actual_refs.length > 0) {
254 git_buf_puts(&msg, "Unexpected remote tracking branches exist.");
255 failed = 1;
256 goto failed;
257 }
258
259failed:
f70cfd34 260 if (failed)
1d645aab
JM
261 cl_fail(git_buf_cstr(&msg));
262
263 git_vector_foreach(&actual_refs, i, actual_ref)
264 git__free(actual_ref);
265
266 git_vector_free(&actual_refs);
267 git_buf_free(&msg);
2ff4469a 268 git_buf_free(&ref_name);
f70cfd34
JG
269}
270
271static void verify_update_tips_callback(git_remote *remote, expected_ref expected_refs[], size_t expected_refs_len)
272{
273 git_refspec *fetch_spec;
274 git_buf msg = GIT_BUF_INIT;
275 git_buf ref_name = GIT_BUF_INIT;
276 updated_tip *tip = NULL;
277 size_t i, j;
278 int failed = 0;
279
280 for (i = 0; i < expected_refs_len; ++i) {
281 /* Convert remote reference name into tracking branch name.
282 * If the spec is not under refs/heads/, then skip.
283 */
284 fetch_spec = git_remote__matching_refspec(remote, expected_refs[i].name);
285 if (!fetch_spec)
286 continue;
287
288 cl_git_pass(git_refspec_transform(&ref_name, fetch_spec, expected_refs[i].name));
289
290 /* Find matching update_tip entry */
291 git_vector_foreach(&_record_cbs_data.updated_tips, j, tip) {
292 if (!strcmp(git_buf_cstr(&ref_name), tip->name))
293 break;
294 }
295
296 if (j == _record_cbs_data.updated_tips.length) {
297 git_buf_printf(&msg, "Did not find expected updated tip entry for branch '%s'.", git_buf_cstr(&ref_name));
298 failed = 1;
299 goto failed;
300 }
301
302 if (git_oid_cmp(expected_refs[i].oid, tip->new_oid) != 0) {
303 git_buf_printf(&msg, "Updated tip ID does not match expected ID");
304 failed = 1;
305 goto failed;
306 }
307 }
308
309failed:
310 if (failed)
311 cl_fail(git_buf_cstr(&msg));
312
313 git_buf_free(&ref_name);
314 git_buf_free(&msg);
1d645aab
JM
315}
316
6443eaf2 317void test_online_push__initialize(void)
613d5eb9
PK
318{
319 git_vector delete_specs = GIT_VECTOR_INIT;
ae297212 320 const git_remote_head **heads;
fe794b2e 321 size_t heads_len;
0f69b41d
CMN
322 git_push_options push_opts = GIT_PUSH_OPTIONS_INIT;
323 git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
613d5eb9
PK
324
325 _repo = cl_git_sandbox_init("push_src");
326
659cf202 327 cl_git_pass(git_repository_set_ident(_repo, "Random J. Hacker", "foo@example.com"));
613d5eb9
PK
328 cl_fixture_sandbox("testrepo.git");
329 cl_rename("push_src/submodule/.gitted", "push_src/submodule/.git");
330
331 rewrite_gitmodules(git_repository_workdir(_repo));
332
333 /* git log --format=oneline --decorate --graph
334 * *-. 951bbbb90e2259a4c8950db78946784fb53fcbce (HEAD, b6) merge b3, b4, and b5 to b6
335 * |\ \
336 * | | * fa38b91f199934685819bea316186d8b008c52a2 (b5) added submodule named 'submodule' pointing to '../testrepo.git'
337 * | * | 27b7ce66243eb1403862d05f958c002312df173d (b4) edited fold\b.txt
338 * | |/
339 * * | d9b63a88223d8367516f50bd131a5f7349b7f3e4 (b3) edited a.txt
340 * |/
341 * * a78705c3b2725f931d3ee05348d83cc26700f247 (b2, b1) added fold and fold/b.txt
342 * * 5c0bb3d1b9449d1cc69d7519fd05166f01840915 added a.txt
343 */
344 git_oid_fromstr(&_oid_b6, "951bbbb90e2259a4c8950db78946784fb53fcbce");
345 git_oid_fromstr(&_oid_b5, "fa38b91f199934685819bea316186d8b008c52a2");
346 git_oid_fromstr(&_oid_b4, "27b7ce66243eb1403862d05f958c002312df173d");
347 git_oid_fromstr(&_oid_b3, "d9b63a88223d8367516f50bd131a5f7349b7f3e4");
348 git_oid_fromstr(&_oid_b2, "a78705c3b2725f931d3ee05348d83cc26700f247");
349 git_oid_fromstr(&_oid_b1, "a78705c3b2725f931d3ee05348d83cc26700f247");
350
abeefbbe
MS
351 git_oid_fromstr(&_tag_commit, "805c54522e614f29f70d2413a0470247d8b424ac");
352 git_oid_fromstr(&_tag_tree, "ff83aa4c5e5d28e3bcba2f5c6e2adc61286a4e5e");
353 git_oid_fromstr(&_tag_blob, "b483ae7ba66decee9aee971f501221dea84b1498");
354 git_oid_fromstr(&_tag_lightweight, "951bbbb90e2259a4c8950db78946784fb53fcbce");
1c13b0bf 355 git_oid_fromstr(&_tag_tag, "eea4f2705eeec2db3813f2430829afce99cd00b5");
abeefbbe 356
613d5eb9 357 /* Remote URL environment variable must be set. User and password are optional. */
e069c621 358
613d5eb9
PK
359 _remote_url = cl_getenv("GITTEST_REMOTE_URL");
360 _remote_user = cl_getenv("GITTEST_REMOTE_USER");
361 _remote_pass = cl_getenv("GITTEST_REMOTE_PASS");
5be622fb
CMN
362 _remote_ssh_key = cl_getenv("GITTEST_REMOTE_SSH_KEY");
363 _remote_ssh_pubkey = cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
364 _remote_ssh_passphrase = cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");
84efffc3 365 _remote_default = cl_getenv("GITTEST_REMOTE_DEFAULT");
613d5eb9
PK
366 _remote = NULL;
367
0f65733b
VM
368 /* Skip the test if we're missing the remote URL */
369 if (!_remote_url)
370 cl_skip();
613d5eb9 371
0f65733b 372 cl_git_pass(git_remote_create(&_remote, _repo, "test", _remote_url));
613d5eb9 373
0f65733b 374 record_callbacks_data_clear(&_record_cbs_data);
613d5eb9 375
0f69b41d 376 cl_git_pass(git_remote_connect(_remote, GIT_DIRECTION_PUSH, &_record_cbs));
613d5eb9 377
0f65733b
VM
378 /* Clean up previously pushed branches. Fails if receive.denyDeletes is
379 * set on the remote. Also, on Git 1.7.0 and newer, you must run
380 * 'git config receive.denyDeleteCurrent ignore' in the remote repo in
381 * order to delete the remote branch pointed to by HEAD (usually master).
382 * See: https://raw.github.com/git/git/master/Documentation/RelNotes/1.7.0.txt
383 */
384 cl_git_pass(git_remote_ls(&heads, &heads_len, _remote));
385 cl_git_pass(create_deletion_refspecs(&delete_specs, heads, heads_len));
386 if (delete_specs.length) {
fe794b2e
CMN
387 git_strarray arr = {
388 (char **) delete_specs.contents,
389 delete_specs.length,
390 };
613d5eb9 391
0f69b41d
CMN
392 memcpy(&push_opts.callbacks, &_record_cbs, sizeof(git_remote_callbacks));
393 cl_git_pass(git_remote_upload(_remote, &arr, &push_opts));
0f65733b
VM
394 }
395
396 git_remote_disconnect(_remote);
397 git_vector_free(&delete_specs);
613d5eb9 398
0f65733b 399 /* Now that we've deleted everything, fetch from the remote */
0f69b41d
CMN
400 memcpy(&fetch_opts.callbacks, &_record_cbs, sizeof(git_remote_callbacks));
401 cl_git_pass(git_remote_fetch(_remote, NULL, &fetch_opts, NULL));
613d5eb9
PK
402}
403
6443eaf2 404void test_online_push__cleanup(void)
613d5eb9
PK
405{
406 if (_remote)
407 git_remote_free(_remote);
c4e3e797
BS
408 _remote = NULL;
409
e069c621
ET
410 git__free(_remote_url);
411 git__free(_remote_user);
412 git__free(_remote_pass);
413 git__free(_remote_ssh_key);
414 git__free(_remote_ssh_pubkey);
415 git__free(_remote_ssh_passphrase);
416 git__free(_remote_default);
417
c4e3e797
BS
418 /* Freed by cl_git_sandbox_cleanup */
419 _repo = NULL;
613d5eb9
PK
420
421 record_callbacks_data_clear(&_record_cbs_data);
422
423 cl_fixture_cleanup("testrepo.git");
424 cl_git_sandbox_cleanup();
425}
426
11bd7a03
RB
427static int push_pack_progress_cb(
428 int stage, unsigned int current, unsigned int total, void* payload)
b176eded 429{
6eb9e39c 430 record_callbacks_data *data = (record_callbacks_data *) payload;
7baa7631 431 GIT_UNUSED(stage); GIT_UNUSED(current); GIT_UNUSED(total);
6eb9e39c
CMN
432 if (data->pack_progress_calls < 0)
433 return data->pack_progress_calls;
434
435 data->pack_progress_calls++;
5b188225 436 return 0;
b176eded
JM
437}
438
11bd7a03
RB
439static int push_transfer_progress_cb(
440 unsigned int current, unsigned int total, size_t bytes, void* payload)
b176eded 441{
6eb9e39c 442 record_callbacks_data *data = (record_callbacks_data *) payload;
af302aca 443 GIT_UNUSED(current); GIT_UNUSED(total); GIT_UNUSED(bytes);
6eb9e39c
CMN
444 if (data->transfer_progress_calls < 0)
445 return data->transfer_progress_calls;
446
447 data->transfer_progress_calls++;
5b188225 448 return 0;
b176eded
JM
449}
450
613d5eb9
PK
451/**
452 * Calls push and relists refs on remote to verify success.
156cfec0 453 *
613d5eb9
PK
454 * @param refspecs refspecs to push
455 * @param refspecs_len length of refspecs
456 * @param expected_refs expected remote refs after push
457 * @param expected_refs_len length of expected_refs
458 * @param expected_ret expected return value from git_push_finish()
b176eded 459 * @param check_progress_cb Check that the push progress callbacks are called
613d5eb9 460 */
11bd7a03
RB
461static void do_push(
462 const char *refspecs[], size_t refspecs_len,
613d5eb9 463 push_status expected_statuses[], size_t expected_statuses_len,
11bd7a03 464 expected_ref expected_refs[], size_t expected_refs_len,
f70cfd34 465 int expected_ret, int check_progress_cb, int check_update_tips_cb)
613d5eb9 466{
b8b897bb 467 git_push_options opts = GIT_PUSH_OPTIONS_INIT;
613d5eb9 468 size_t i;
6eb9e39c 469 int error;
64e3e6d4 470 git_strarray specs = {0};
6eb9e39c 471 record_callbacks_data *data;
613d5eb9
PK
472
473 if (_remote) {
b8b897bb
PK
474 /* Auto-detect the number of threads to use */
475 opts.pb_parallelism = 0;
476
8f0104ec
CMN
477 memcpy(&opts.callbacks, &_record_cbs, sizeof(git_remote_callbacks));
478 data = opts.callbacks.payload;
613d5eb9 479
8f0104ec
CMN
480 opts.callbacks.pack_progress = push_pack_progress_cb;
481 opts.callbacks.push_transfer_progress = push_transfer_progress_cb;
482 opts.callbacks.push_update_reference = record_push_status_cb;
11bd7a03 483
64e3e6d4
CMN
484 if (refspecs_len) {
485 specs.count = refspecs_len;
486 specs.strings = git__calloc(refspecs_len, sizeof(char *));
487 cl_assert(specs.strings);
11bd7a03 488 }
b176eded 489
613d5eb9 490 for (i = 0; i < refspecs_len; i++)
6eb9e39c
CMN
491 specs.strings[i] = (char *) refspecs[i];
492
493 /* if EUSER, then abort in transfer */
494 if (check_progress_cb && expected_ret == GIT_EUSER)
495 data->transfer_progress_calls = GIT_EUSER;
496
412a3808 497 error = git_remote_push(_remote, &specs, &opts);
6eb9e39c 498 git__free(specs.strings);
613d5eb9
PK
499
500 if (expected_ret < 0) {
6eb9e39c 501 cl_git_fail_with(expected_ret, error);
11bd7a03 502 } else {
6eb9e39c 503 cl_git_pass(error);
613d5eb9
PK
504 }
505
6eb9e39c
CMN
506 if (check_progress_cb && expected_ret == 0) {
507 cl_assert(data->pack_progress_calls > 0);
508 cl_assert(data->transfer_progress_calls > 0);
b176eded
JM
509 }
510
6eb9e39c 511 do_verify_push_status(data, expected_statuses, expected_statuses_len);
613d5eb9 512
613d5eb9 513 verify_refs(_remote, expected_refs, expected_refs_len);
1d645aab
JM
514 verify_tracking_branches(_remote, expected_refs, expected_refs_len);
515
f70cfd34
JG
516 if (check_update_tips_cb)
517 verify_update_tips_callback(_remote, expected_refs, expected_refs_len);
518
613d5eb9 519 }
491cecfe 520
613d5eb9
PK
521}
522
523/* Call push_finish() without ever calling git_push_add_refspec() */
6443eaf2 524void test_online_push__noop(void)
613d5eb9 525{
f70cfd34 526 do_push(NULL, 0, NULL, 0, NULL, 0, 0, 0, 1);
613d5eb9
PK
527}
528
6443eaf2 529void test_online_push__b1(void)
613d5eb9
PK
530{
531 const char *specs[] = { "refs/heads/b1:refs/heads/b1" };
9d41984c 532 push_status exp_stats[] = { { "refs/heads/b1", 1 } };
613d5eb9
PK
533 expected_ref exp_refs[] = { { "refs/heads/b1", &_oid_b1 } };
534 do_push(specs, ARRAY_SIZE(specs),
535 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 536 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
613d5eb9
PK
537}
538
6443eaf2 539void test_online_push__b2(void)
613d5eb9
PK
540{
541 const char *specs[] = { "refs/heads/b2:refs/heads/b2" };
9d41984c 542 push_status exp_stats[] = { { "refs/heads/b2", 1 } };
613d5eb9
PK
543 expected_ref exp_refs[] = { { "refs/heads/b2", &_oid_b2 } };
544 do_push(specs, ARRAY_SIZE(specs),
545 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 546 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
613d5eb9
PK
547}
548
6443eaf2 549void test_online_push__b3(void)
613d5eb9
PK
550{
551 const char *specs[] = { "refs/heads/b3:refs/heads/b3" };
9d41984c 552 push_status exp_stats[] = { { "refs/heads/b3", 1 } };
613d5eb9
PK
553 expected_ref exp_refs[] = { { "refs/heads/b3", &_oid_b3 } };
554 do_push(specs, ARRAY_SIZE(specs),
555 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 556 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
613d5eb9
PK
557}
558
6443eaf2 559void test_online_push__b4(void)
613d5eb9
PK
560{
561 const char *specs[] = { "refs/heads/b4:refs/heads/b4" };
9d41984c 562 push_status exp_stats[] = { { "refs/heads/b4", 1 } };
613d5eb9
PK
563 expected_ref exp_refs[] = { { "refs/heads/b4", &_oid_b4 } };
564 do_push(specs, ARRAY_SIZE(specs),
565 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 566 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
613d5eb9
PK
567}
568
6443eaf2 569void test_online_push__b5(void)
613d5eb9
PK
570{
571 const char *specs[] = { "refs/heads/b5:refs/heads/b5" };
9d41984c 572 push_status exp_stats[] = { { "refs/heads/b5", 1 } };
613d5eb9
PK
573 expected_ref exp_refs[] = { { "refs/heads/b5", &_oid_b5 } };
574 do_push(specs, ARRAY_SIZE(specs),
575 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 576 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
613d5eb9
PK
577}
578
11bd7a03
RB
579void test_online_push__b5_cancel(void)
580{
581 const char *specs[] = { "refs/heads/b5:refs/heads/b5" };
f70cfd34 582 do_push(specs, ARRAY_SIZE(specs), NULL, 0, NULL, 0, GIT_EUSER, 1, 1);
11bd7a03
RB
583}
584
6443eaf2 585void test_online_push__multi(void)
613d5eb9 586{
491cecfe
BS
587 git_reflog *log;
588 const git_reflog_entry *entry;
589
613d5eb9
PK
590 const char *specs[] = {
591 "refs/heads/b1:refs/heads/b1",
592 "refs/heads/b2:refs/heads/b2",
593 "refs/heads/b3:refs/heads/b3",
594 "refs/heads/b4:refs/heads/b4",
595 "refs/heads/b5:refs/heads/b5"
596 };
597 push_status exp_stats[] = {
9d41984c
ET
598 { "refs/heads/b1", 1 },
599 { "refs/heads/b2", 1 },
600 { "refs/heads/b3", 1 },
601 { "refs/heads/b4", 1 },
602 { "refs/heads/b5", 1 }
613d5eb9
PK
603 };
604 expected_ref exp_refs[] = {
605 { "refs/heads/b1", &_oid_b1 },
606 { "refs/heads/b2", &_oid_b2 },
607 { "refs/heads/b3", &_oid_b3 },
608 { "refs/heads/b4", &_oid_b4 },
609 { "refs/heads/b5", &_oid_b5 }
610 };
611 do_push(specs, ARRAY_SIZE(specs),
612 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 613 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
491cecfe
BS
614
615 cl_git_pass(git_reflog_read(&log, _repo, "refs/remotes/test/b1"));
616 entry = git_reflog_entry_byindex(log, 0);
3094102f 617 if (entry) {
412a3808 618 cl_assert_equal_s("update by push", git_reflog_entry_message(entry));
3094102f
BS
619 cl_assert_equal_s("foo@example.com", git_reflog_entry_committer(entry)->email);
620 }
491cecfe
BS
621
622 git_reflog_free(log);
613d5eb9
PK
623}
624
6443eaf2 625void test_online_push__implicit_tgt(void)
613d5eb9 626{
aad638f3 627 const char *specs1[] = { "refs/heads/b1" };
9d41984c 628 push_status exp_stats1[] = { { "refs/heads/b1", 1 } };
613d5eb9
PK
629 expected_ref exp_refs1[] = { { "refs/heads/b1", &_oid_b1 } };
630
aad638f3 631 const char *specs2[] = { "refs/heads/b2" };
9d41984c 632 push_status exp_stats2[] = { { "refs/heads/b2", 1 } };
613d5eb9
PK
633 expected_ref exp_refs2[] = {
634 { "refs/heads/b1", &_oid_b1 },
635 { "refs/heads/b2", &_oid_b2 }
636 };
637
638 do_push(specs1, ARRAY_SIZE(specs1),
639 exp_stats1, ARRAY_SIZE(exp_stats1),
f70cfd34 640 exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
613d5eb9
PK
641 do_push(specs2, ARRAY_SIZE(specs2),
642 exp_stats2, ARRAY_SIZE(exp_stats2),
f70cfd34 643 exp_refs2, ARRAY_SIZE(exp_refs2), 0, 0, 0);
613d5eb9
PK
644}
645
6443eaf2 646void test_online_push__fast_fwd(void)
613d5eb9
PK
647{
648 /* Fast forward b1 in tgt from _oid_b1 to _oid_b6. */
649
650 const char *specs_init[] = { "refs/heads/b1:refs/heads/b1" };
9d41984c 651 push_status exp_stats_init[] = { { "refs/heads/b1", 1 } };
613d5eb9
PK
652 expected_ref exp_refs_init[] = { { "refs/heads/b1", &_oid_b1 } };
653
654 const char *specs_ff[] = { "refs/heads/b6:refs/heads/b1" };
9d41984c 655 push_status exp_stats_ff[] = { { "refs/heads/b1", 1 } };
613d5eb9
PK
656 expected_ref exp_refs_ff[] = { { "refs/heads/b1", &_oid_b6 } };
657
658 /* Do a force push to reset b1 in target back to _oid_b1 */
659 const char *specs_reset[] = { "+refs/heads/b1:refs/heads/b1" };
660 /* Force should have no effect on a fast forward push */
661 const char *specs_ff_force[] = { "+refs/heads/b6:refs/heads/b1" };
662
663 do_push(specs_init, ARRAY_SIZE(specs_init),
664 exp_stats_init, ARRAY_SIZE(exp_stats_init),
f70cfd34 665 exp_refs_init, ARRAY_SIZE(exp_refs_init), 0, 1, 1);
613d5eb9
PK
666
667 do_push(specs_ff, ARRAY_SIZE(specs_ff),
668 exp_stats_ff, ARRAY_SIZE(exp_stats_ff),
f70cfd34 669 exp_refs_ff, ARRAY_SIZE(exp_refs_ff), 0, 0, 0);
613d5eb9
PK
670
671 do_push(specs_reset, ARRAY_SIZE(specs_reset),
672 exp_stats_init, ARRAY_SIZE(exp_stats_init),
f70cfd34 673 exp_refs_init, ARRAY_SIZE(exp_refs_init), 0, 0, 0);
613d5eb9
PK
674
675 do_push(specs_ff_force, ARRAY_SIZE(specs_ff_force),
676 exp_stats_ff, ARRAY_SIZE(exp_stats_ff),
f70cfd34 677 exp_refs_ff, ARRAY_SIZE(exp_refs_ff), 0, 0, 0);
613d5eb9
PK
678}
679
abeefbbe
MS
680void test_online_push__tag_commit(void)
681{
682 const char *specs[] = { "refs/tags/tag-commit:refs/tags/tag-commit" };
9d41984c 683 push_status exp_stats[] = { { "refs/tags/tag-commit", 1 } };
abeefbbe
MS
684 expected_ref exp_refs[] = { { "refs/tags/tag-commit", &_tag_commit } };
685 do_push(specs, ARRAY_SIZE(specs),
686 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 687 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
abeefbbe
MS
688}
689
690void test_online_push__tag_tree(void)
691{
692 const char *specs[] = { "refs/tags/tag-tree:refs/tags/tag-tree" };
9d41984c 693 push_status exp_stats[] = { { "refs/tags/tag-tree", 1 } };
abeefbbe
MS
694 expected_ref exp_refs[] = { { "refs/tags/tag-tree", &_tag_tree } };
695 do_push(specs, ARRAY_SIZE(specs),
696 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 697 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
abeefbbe
MS
698}
699
700void test_online_push__tag_blob(void)
701{
702 const char *specs[] = { "refs/tags/tag-blob:refs/tags/tag-blob" };
9d41984c 703 push_status exp_stats[] = { { "refs/tags/tag-blob", 1 } };
abeefbbe
MS
704 expected_ref exp_refs[] = { { "refs/tags/tag-blob", &_tag_blob } };
705 do_push(specs, ARRAY_SIZE(specs),
706 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 707 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
abeefbbe
MS
708}
709
710void test_online_push__tag_lightweight(void)
711{
712 const char *specs[] = { "refs/tags/tag-lightweight:refs/tags/tag-lightweight" };
9d41984c 713 push_status exp_stats[] = { { "refs/tags/tag-lightweight", 1 } };
abeefbbe
MS
714 expected_ref exp_refs[] = { { "refs/tags/tag-lightweight", &_tag_lightweight } };
715 do_push(specs, ARRAY_SIZE(specs),
716 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 717 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
abeefbbe
MS
718}
719
1c13b0bf
ET
720void test_online_push__tag_to_tag(void)
721{
722 const char *specs[] = { "refs/tags/tag-tag:refs/tags/tag-tag" };
9d41984c 723 push_status exp_stats[] = { { "refs/tags/tag-tag", 1 } };
1c13b0bf
ET
724 expected_ref exp_refs[] = { { "refs/tags/tag-tag", &_tag_tag } };
725 do_push(specs, ARRAY_SIZE(specs),
726 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 727 exp_refs, ARRAY_SIZE(exp_refs), 0, 0, 0);
1c13b0bf
ET
728}
729
6443eaf2 730void test_online_push__force(void)
613d5eb9
PK
731{
732 const char *specs1[] = {"refs/heads/b3:refs/heads/tgt"};
9d41984c 733 push_status exp_stats1[] = { { "refs/heads/tgt", 1 } };
613d5eb9
PK
734 expected_ref exp_refs1[] = { { "refs/heads/tgt", &_oid_b3 } };
735
736 const char *specs2[] = {"refs/heads/b4:refs/heads/tgt"};
737
738 const char *specs2_force[] = {"+refs/heads/b4:refs/heads/tgt"};
9d41984c 739 push_status exp_stats2_force[] = { { "refs/heads/tgt", 1 } };
613d5eb9
PK
740 expected_ref exp_refs2_force[] = { { "refs/heads/tgt", &_oid_b4 } };
741
742 do_push(specs1, ARRAY_SIZE(specs1),
743 exp_stats1, ARRAY_SIZE(exp_stats1),
f70cfd34 744 exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
613d5eb9
PK
745
746 do_push(specs2, ARRAY_SIZE(specs2),
747 NULL, 0,
f70cfd34 748 exp_refs1, ARRAY_SIZE(exp_refs1), GIT_ENONFASTFORWARD, 0, 0);
613d5eb9
PK
749
750 /* Non-fast-forward update with force should pass. */
f70cfd34 751 record_callbacks_data_clear(&_record_cbs_data);
613d5eb9
PK
752 do_push(specs2_force, ARRAY_SIZE(specs2_force),
753 exp_stats2_force, ARRAY_SIZE(exp_stats2_force),
f70cfd34 754 exp_refs2_force, ARRAY_SIZE(exp_refs2_force), 0, 1, 1);
613d5eb9
PK
755}
756
6443eaf2 757void test_online_push__delete(void)
613d5eb9
PK
758{
759 const char *specs1[] = {
760 "refs/heads/b1:refs/heads/tgt1",
761 "refs/heads/b1:refs/heads/tgt2"
762 };
763 push_status exp_stats1[] = {
9d41984c
ET
764 { "refs/heads/tgt1", 1 },
765 { "refs/heads/tgt2", 1 }
613d5eb9
PK
766 };
767 expected_ref exp_refs1[] = {
768 { "refs/heads/tgt1", &_oid_b1 },
769 { "refs/heads/tgt2", &_oid_b1 }
770 };
771
772 const char *specs_del_fake[] = { ":refs/heads/fake" };
773 /* Force has no effect for delete. */
774 const char *specs_del_fake_force[] = { "+:refs/heads/fake" };
9d41984c 775 push_status exp_stats_fake[] = { { "refs/heads/fake", 1 } };
613d5eb9
PK
776
777 const char *specs_delete[] = { ":refs/heads/tgt1" };
9d41984c 778 push_status exp_stats_delete[] = { { "refs/heads/tgt1", 1 } };
613d5eb9
PK
779 expected_ref exp_refs_delete[] = { { "refs/heads/tgt2", &_oid_b1 } };
780 /* Force has no effect for delete. */
781 const char *specs_delete_force[] = { "+:refs/heads/tgt1" };
782
783 do_push(specs1, ARRAY_SIZE(specs1),
784 exp_stats1, ARRAY_SIZE(exp_stats1),
f70cfd34 785 exp_refs1, ARRAY_SIZE(exp_refs1), 0, 1, 1);
613d5eb9 786
d73d52df
CW
787 /* When deleting a non-existent branch, the git client sends zero for both
788 * the old and new commit id. This should succeed on the server with the
789 * same status report as if the branch were actually deleted. The server
790 * returns a warning on the side-band iff the side-band is supported.
791 * Since libgit2 doesn't support the side-band yet, there are no warnings.
613d5eb9
PK
792 */
793 do_push(specs_del_fake, ARRAY_SIZE(specs_del_fake),
d73d52df 794 exp_stats_fake, 1,
f70cfd34 795 exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
613d5eb9 796 do_push(specs_del_fake_force, ARRAY_SIZE(specs_del_fake_force),
d73d52df 797 exp_stats_fake, 1,
f70cfd34 798 exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
613d5eb9
PK
799
800 /* Delete one of the pushed branches. */
801 do_push(specs_delete, ARRAY_SIZE(specs_delete),
802 exp_stats_delete, ARRAY_SIZE(exp_stats_delete),
f70cfd34 803 exp_refs_delete, ARRAY_SIZE(exp_refs_delete), 0, 0, 0);
613d5eb9
PK
804
805 /* Re-push branches and retry delete with force. */
806 do_push(specs1, ARRAY_SIZE(specs1),
807 exp_stats1, ARRAY_SIZE(exp_stats1),
f70cfd34 808 exp_refs1, ARRAY_SIZE(exp_refs1), 0, 0, 0);
613d5eb9
PK
809 do_push(specs_delete_force, ARRAY_SIZE(specs_delete_force),
810 exp_stats_delete, ARRAY_SIZE(exp_stats_delete),
f70cfd34 811 exp_refs_delete, ARRAY_SIZE(exp_refs_delete), 0, 0, 0);
613d5eb9
PK
812}
813
6443eaf2 814void test_online_push__bad_refspecs(void)
613d5eb9
PK
815{
816 /* All classes of refspecs that should be rejected by
817 * git_push_add_refspec() should go in this test.
818 */
fe794b2e
CMN
819 char *specs = {
820 "b6:b6",
821 };
822 git_strarray arr = {
823 &specs,
824 1,
825 };
613d5eb9
PK
826
827 if (_remote) {
fe794b2e 828 cl_git_fail(git_remote_upload(_remote, &arr, NULL));
613d5eb9
PK
829 }
830}
831
6443eaf2 832void test_online_push__expressions(void)
613d5eb9
PK
833{
834 /* TODO: Expressions in refspecs doesn't actually work yet */
835 const char *specs_left_expr[] = { "refs/heads/b2~1:refs/heads/b2" };
836
613d5eb9
PK
837 /* TODO: Find a more precise way of checking errors than a exit code of -1. */
838 do_push(specs_left_expr, ARRAY_SIZE(specs_left_expr),
839 NULL, 0,
f70cfd34 840 NULL, 0, -1, 0, 0);
613d5eb9 841}
087f64d3 842
abeefbbe 843void test_online_push__notes(void)
087f64d3
JM
844{
845 git_oid note_oid, *target_oid, expected_oid;
846 git_signature *signature;
847 const char *specs[] = { "refs/notes/commits:refs/notes/commits" };
9d41984c 848 push_status exp_stats[] = { { "refs/notes/commits", 1 } };
087f64d3 849 expected_ref exp_refs[] = { { "refs/notes/commits", &expected_oid } };
0f29e967 850 const char *specs_del[] = { ":refs/notes/commits" };
0f29e967 851
087f64d3
JM
852 git_oid_fromstr(&expected_oid, "8461a99b27b7043e58ff6e1f5d2cf07d282534fb");
853
854 target_oid = &_oid_b6;
855
856 /* Create note to push */
857 cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
21083a71 858 cl_git_pass(git_note_create(&note_oid, _repo, NULL, signature, signature, target_oid, "hello world\n", 0));
087f64d3
JM
859
860 do_push(specs, ARRAY_SIZE(specs),
861 exp_stats, ARRAY_SIZE(exp_stats),
f70cfd34 862 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
087f64d3 863
0f29e967
ET
864 /* And make sure to delete the note */
865
866 do_push(specs_del, ARRAY_SIZE(specs_del),
867 exp_stats, 1,
aff70018 868 NULL, 0, 0, 0, 0);
0f29e967 869
087f64d3
JM
870 git_signature_free(signature);
871}
64e3e6d4
CMN
872
873void test_online_push__configured(void)
874{
875 git_oid note_oid, *target_oid, expected_oid;
876 git_signature *signature;
77254990 877 git_remote *old_remote;
64e3e6d4
CMN
878 const char *specs[] = { "refs/notes/commits:refs/notes/commits" };
879 push_status exp_stats[] = { { "refs/notes/commits", 1 } };
880 expected_ref exp_refs[] = { { "refs/notes/commits", &expected_oid } };
881 const char *specs_del[] = { ":refs/notes/commits" };
882
883 git_oid_fromstr(&expected_oid, "8461a99b27b7043e58ff6e1f5d2cf07d282534fb");
884
885 target_oid = &_oid_b6;
886
77254990
CMN
887 cl_git_pass(git_remote_add_push(_repo, git_remote_name(_remote), specs[0]));
888 old_remote = _remote;
889 cl_git_pass(git_remote_lookup(&_remote, _repo, git_remote_name(_remote)));
890 git_remote_free(old_remote);
64e3e6d4
CMN
891
892 /* Create note to push */
893 cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60)); /* Wed Dec 14 08:29:03 2011 +0100 */
21083a71 894 cl_git_pass(git_note_create(&note_oid, _repo, NULL, signature, signature, target_oid, "hello world\n", 0));
64e3e6d4
CMN
895
896 do_push(NULL, 0,
897 exp_stats, ARRAY_SIZE(exp_stats),
898 exp_refs, ARRAY_SIZE(exp_refs), 0, 1, 1);
899
900 /* And make sure to delete the note */
901
902 do_push(specs_del, ARRAY_SIZE(specs_del),
903 exp_stats, 1,
904 NULL, 0, 0, 0, 0);
905
906 git_signature_free(signature);
907}