push_util.c 3.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "clar_libgit2.h"
#include "vector.h"
#include "push_util.h"

const git_oid OID_ZERO = {{ 0 }};

void updated_tip_free(updated_tip *t)
{
	git__free(t->name);
	git__free(t);
}

13
static void push_status_free(push_status *s)
14 15 16 17 18 19
{
	git__free(s->ref);
	git__free(s->msg);
	git__free(s);
}

20 21 22 23
void record_callbacks_data_clear(record_callbacks_data *data)
{
	size_t i;
	updated_tip *tip;
24
	push_status *status;
25 26 27 28 29

	git_vector_foreach(&data->updated_tips, i, tip)
		updated_tip_free(tip);

	git_vector_free(&data->updated_tips);
30 31 32 33 34 35 36 37

	git_vector_foreach(&data->statuses, i, status)
		push_status_free(status);

	git_vector_free(&data->statuses);

	data->pack_progress_calls = 0;
	data->transfer_progress_calls = 0;
38 39 40 41 42 43 44
}

int record_update_tips_cb(const char *refname, const git_oid *a, const git_oid *b, void *data)
{
	updated_tip *t;
	record_callbacks_data *record_data = (record_callbacks_data *)data;

45
	cl_assert(t = git__calloc(1, sizeof(*t)));
46 47

	cl_assert(t->name = git__strdup(refname));
48 49
	git_oid_cpy(&t->old_oid, a);
	git_oid_cpy(&t->new_oid, b);
50 51 52 53 54 55

	git_vector_insert(&record_data->updated_tips, t);

	return 0;
}

56
int create_deletion_refspecs(git_vector *out, const git_remote_head **heads, size_t heads_len)
57
{
58
	git_str del_spec = GIT_STR_INIT;
59
	int valid;
60
	size_t i;
61

62 63 64
	for (i = 0; i < heads_len; i++) {
		const git_remote_head *head = heads[i];
		/* Ignore malformed ref names (which also saves us from tag^{} */
65 66
		cl_git_pass(git_reference_name_is_valid(&valid, head->name));
		if (!valid)
67 68 69 70
			return 0;

		/* Create a refspec that deletes a branch in the remote */
		if (strcmp(head->name, "refs/heads/master")) {
71 72 73
			cl_git_pass(git_str_putc(&del_spec, ':'));
			cl_git_pass(git_str_puts(&del_spec, head->name));
			cl_git_pass(git_vector_insert(out, git_str_detach(&del_spec)));
74
		}
75 76 77 78 79 80 81 82 83 84 85
	}

	return 0;
}

int record_ref_cb(git_remote_head *head, void *payload)
{
	git_vector *refs = (git_vector *) payload;
	return git_vector_insert(refs, head);
}

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)
87 88
{
	size_t i, j = 0;
89
	git_str msg = GIT_STR_INIT;
90
	const git_remote_head *actual;
91 92 93 94
	char *oid_str;
	bool master_present = false;

	/* We don't care whether "master" is present on the other end or not */
95 96
	for (i = 0; i < actual_refs_len; i++) {
		actual = actual_refs[i];
97 98 99 100 101 102
		if (!strcmp(actual->name, "refs/heads/master")) {
			master_present = true;
			break;
		}
	}

103
	if (expected_refs_len + (master_present ? 1 : 0) != actual_refs_len)
104 105
		goto failed;

106 107
	for (i = 0; i < actual_refs_len; i++) {
		actual = actual_refs[i];
108 109 110 111 112 113 114 115 116 117 118 119 120
		if (master_present && !strcmp(actual->name, "refs/heads/master"))
			continue;

		if (strcmp(expected_refs[j].name, actual->name) ||
			git_oid_cmp(expected_refs[j].oid, &actual->oid))
			goto failed;

		j++;
	}

	return;

failed:
121
	git_str_puts(&msg, "Expected and actual refs differ:\nEXPECTED:\n");
122 123

	for(i = 0; i < expected_refs_len; i++) {
124
		oid_str = git_oid_tostr_s(expected_refs[i].oid);
125
		cl_git_pass(git_str_printf(&msg, "%s = %s\n", expected_refs[i].name, oid_str));
126 127
	}

128
	git_str_puts(&msg, "\nACTUAL:\n");
129 130
	for (i = 0; i < actual_refs_len; i++) {
		actual = actual_refs[i];
131 132 133
		if (master_present && !strcmp(actual->name, "refs/heads/master"))
			continue;

134
		oid_str = git_oid_tostr_s(&actual->oid);
135
		cl_git_pass(git_str_printf(&msg, "%s = %s\n", actual->name, oid_str));
136 137
	}

138
	cl_fail(git_str_cstr(&msg));
139

140
	git_str_dispose(&msg);
141
}