]>
Commit | Line | Data |
---|---|---|
613d5eb9 | 1 | #include "clar_libgit2.h" |
613d5eb9 PK |
2 | #include "vector.h" |
3 | #include "push_util.h" | |
4 | ||
5 | const git_oid OID_ZERO = {{ 0 }}; | |
6 | ||
7 | void updated_tip_free(updated_tip *t) | |
8 | { | |
9 | git__free(t->name); | |
613d5eb9 PK |
10 | git__free(t); |
11 | } | |
12 | ||
e579e0f7 | 13 | static void push_status_free(push_status *s) |
6eb9e39c CMN |
14 | { |
15 | git__free(s->ref); | |
16 | git__free(s->msg); | |
17 | git__free(s); | |
18 | } | |
19 | ||
613d5eb9 PK |
20 | void record_callbacks_data_clear(record_callbacks_data *data) |
21 | { | |
22 | size_t i; | |
23 | updated_tip *tip; | |
6eb9e39c | 24 | push_status *status; |
613d5eb9 PK |
25 | |
26 | git_vector_foreach(&data->updated_tips, i, tip) | |
27 | updated_tip_free(tip); | |
28 | ||
29 | git_vector_free(&data->updated_tips); | |
6eb9e39c CMN |
30 | |
31 | git_vector_foreach(&data->statuses, i, status) | |
32 | push_status_free(status); | |
33 | ||
34 | git_vector_free(&data->statuses); | |
35 | ||
36 | data->pack_progress_calls = 0; | |
37 | data->transfer_progress_calls = 0; | |
613d5eb9 PK |
38 | } |
39 | ||
40 | int record_update_tips_cb(const char *refname, const git_oid *a, const git_oid *b, void *data) | |
41 | { | |
42 | updated_tip *t; | |
43 | record_callbacks_data *record_data = (record_callbacks_data *)data; | |
44 | ||
1af5aecb | 45 | cl_assert(t = git__calloc(1, sizeof(*t))); |
613d5eb9 PK |
46 | |
47 | cl_assert(t->name = git__strdup(refname)); | |
1af5aecb CMN |
48 | git_oid_cpy(&t->old_oid, a); |
49 | git_oid_cpy(&t->new_oid, b); | |
613d5eb9 PK |
50 | |
51 | git_vector_insert(&record_data->updated_tips, t); | |
52 | ||
53 | return 0; | |
54 | } | |
55 | ||
ae297212 | 56 | int create_deletion_refspecs(git_vector *out, const git_remote_head **heads, size_t heads_len) |
613d5eb9 | 57 | { |
e579e0f7 | 58 | git_str del_spec = GIT_STR_INIT; |
c25aa7cd | 59 | int valid; |
ae297212 | 60 | size_t i; |
613d5eb9 | 61 | |
ae297212 CMN |
62 | for (i = 0; i < heads_len; i++) { |
63 | const git_remote_head *head = heads[i]; | |
64 | /* Ignore malformed ref names (which also saves us from tag^{} */ | |
c25aa7cd PP |
65 | cl_git_pass(git_reference_name_is_valid(&valid, head->name)); |
66 | if (!valid) | |
ae297212 CMN |
67 | return 0; |
68 | ||
69 | /* Create a refspec that deletes a branch in the remote */ | |
70 | if (strcmp(head->name, "refs/heads/master")) { | |
e579e0f7 MB |
71 | cl_git_pass(git_str_putc(&del_spec, ':')); |
72 | cl_git_pass(git_str_puts(&del_spec, head->name)); | |
73 | cl_git_pass(git_vector_insert(out, git_str_detach(&del_spec))); | |
ae297212 | 74 | } |
613d5eb9 PK |
75 | } |
76 | ||
77 | return 0; | |
78 | } | |
79 | ||
80 | int 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 | 86 | void 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; | |
e579e0f7 | 89 | git_str msg = GIT_STR_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 | ||
120 | failed: | |
e579e0f7 | 121 | git_str_puts(&msg, "Expected and actual refs differ:\nEXPECTED:\n"); |
613d5eb9 PK |
122 | |
123 | for(i = 0; i < expected_refs_len; i++) { | |
4ca0b566 | 124 | oid_str = git_oid_tostr_s(expected_refs[i].oid); |
e579e0f7 | 125 | cl_git_pass(git_str_printf(&msg, "%s = %s\n", expected_refs[i].name, oid_str)); |
613d5eb9 PK |
126 | } |
127 | ||
e579e0f7 | 128 | git_str_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); |
e579e0f7 | 135 | cl_git_pass(git_str_printf(&msg, "%s = %s\n", actual->name, oid_str)); |
613d5eb9 PK |
136 | } |
137 | ||
e579e0f7 | 138 | cl_fail(git_str_cstr(&msg)); |
613d5eb9 | 139 | |
e579e0f7 | 140 | git_str_dispose(&msg); |
613d5eb9 | 141 | } |