]> git.proxmox.com Git - libgit2.git/blame - tests/online/push_util.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / online / push_util.c
CommitLineData
613d5eb9
PK
1
2#include "clar_libgit2.h"
3#include "buffer.h"
4#include "vector.h"
5#include "push_util.h"
6
7const git_oid OID_ZERO = {{ 0 }};
8
9void updated_tip_free(updated_tip *t)
10{
11 git__free(t->name);
613d5eb9
PK
12 git__free(t);
13}
14
6eb9e39c
CMN
15void push_status_free(push_status *s)
16{
17 git__free(s->ref);
18 git__free(s->msg);
19 git__free(s);
20}
21
613d5eb9
PK
22void record_callbacks_data_clear(record_callbacks_data *data)
23{
24 size_t i;
25 updated_tip *tip;
6eb9e39c 26 push_status *status;
613d5eb9
PK
27
28 git_vector_foreach(&data->updated_tips, i, tip)
29 updated_tip_free(tip);
30
31 git_vector_free(&data->updated_tips);
6eb9e39c
CMN
32
33 git_vector_foreach(&data->statuses, i, status)
34 push_status_free(status);
35
36 git_vector_free(&data->statuses);
37
38 data->pack_progress_calls = 0;
39 data->transfer_progress_calls = 0;
613d5eb9
PK
40}
41
42int record_update_tips_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
43{
44 updated_tip *t;
45 record_callbacks_data *record_data = (record_callbacks_data *)data;
46
1af5aecb 47 cl_assert(t = git__calloc(1, sizeof(*t)));
613d5eb9
PK
48
49 cl_assert(t->name = git__strdup(refname));
1af5aecb
CMN
50 git_oid_cpy(&t->old_oid, a);
51 git_oid_cpy(&t->new_oid, b);
613d5eb9
PK
52
53 git_vector_insert(&record_data->updated_tips, t);
54
55 return 0;
56}
57
ae297212 58int create_deletion_refspecs(git_vector *out, const git_remote_head **heads, size_t heads_len)
613d5eb9 59{
613d5eb9 60 git_buf del_spec = GIT_BUF_INIT;
ae297212 61 size_t i;
613d5eb9 62
ae297212
CMN
63 for (i = 0; i < heads_len; i++) {
64 const git_remote_head *head = heads[i];
65 /* Ignore malformed ref names (which also saves us from tag^{} */
66 if (!git_reference_is_valid_name(head->name))
67 return 0;
68
69 /* Create a refspec that deletes a branch in the remote */
70 if (strcmp(head->name, "refs/heads/master")) {
71 cl_git_pass(git_buf_putc(&del_spec, ':'));
72 cl_git_pass(git_buf_puts(&del_spec, head->name));
73 cl_git_pass(git_vector_insert(out, git_buf_detach(&del_spec)));
74 }
613d5eb9
PK
75 }
76
77 return 0;
78}
79
80int record_ref_cb(git_remote_head *head, void *payload)
81{
82 git_vector *refs = (git_vector *) payload;
83 return git_vector_insert(refs, head);
84}
85
359dce72 86void verify_remote_refs(const git_remote_head *actual_refs[], size_t actual_refs_len, const expected_ref expected_refs[], size_t expected_refs_len)
613d5eb9
PK
87{
88 size_t i, j = 0;
89 git_buf msg = GIT_BUF_INIT;
359dce72 90 const git_remote_head *actual;
613d5eb9
PK
91 char *oid_str;
92 bool master_present = false;
93
94 /* We don't care whether "master" is present on the other end or not */
359dce72
CMN
95 for (i = 0; i < actual_refs_len; i++) {
96 actual = actual_refs[i];
613d5eb9
PK
97 if (!strcmp(actual->name, "refs/heads/master")) {
98 master_present = true;
99 break;
100 }
101 }
102
359dce72 103 if (expected_refs_len + (master_present ? 1 : 0) != actual_refs_len)
613d5eb9
PK
104 goto failed;
105
359dce72
CMN
106 for (i = 0; i < actual_refs_len; i++) {
107 actual = actual_refs[i];
613d5eb9
PK
108 if (master_present && !strcmp(actual->name, "refs/heads/master"))
109 continue;
110
111 if (strcmp(expected_refs[j].name, actual->name) ||
112 git_oid_cmp(expected_refs[j].oid, &actual->oid))
113 goto failed;
114
115 j++;
116 }
117
118 return;
119
120failed:
121 git_buf_puts(&msg, "Expected and actual refs differ:\nEXPECTED:\n");
122
123 for(i = 0; i < expected_refs_len; i++) {
4ca0b566 124 oid_str = git_oid_tostr_s(expected_refs[i].oid);
613d5eb9 125 cl_git_pass(git_buf_printf(&msg, "%s = %s\n", expected_refs[i].name, oid_str));
613d5eb9
PK
126 }
127
128 git_buf_puts(&msg, "\nACTUAL:\n");
359dce72
CMN
129 for (i = 0; i < actual_refs_len; i++) {
130 actual = actual_refs[i];
613d5eb9
PK
131 if (master_present && !strcmp(actual->name, "refs/heads/master"))
132 continue;
133
4ca0b566 134 oid_str = git_oid_tostr_s(&actual->oid);
613d5eb9 135 cl_git_pass(git_buf_printf(&msg, "%s = %s\n", actual->name, oid_str));
613d5eb9
PK
136 }
137
138 cl_fail(git_buf_cstr(&msg));
139
ac3d33df 140 git_buf_dispose(&msg);
613d5eb9 141}