reflog_helpers.c 3.7 KB
Newer Older
1 2 3 4 5
#include "clar_libgit2.h"

#include "repository.h"
#include "reflog.h"

6 7 8 9 10 11 12 13 14 15 16 17
static int reflog_entry_tostr(git_buf *out, const git_reflog_entry *entry)
{
	char old_oid[GIT_OID_HEXSZ], new_oid[GIT_OID_HEXSZ];

	assert(out && entry);

	git_oid_tostr((char *)&old_oid, GIT_OID_HEXSZ, git_reflog_entry_id_old(entry));
	git_oid_tostr((char *)&new_oid, GIT_OID_HEXSZ, git_reflog_entry_id_new(entry));

	return git_buf_printf(out, "%s %s %s %s", old_oid, new_oid, "somesig", git_reflog_entry_message(entry));
}

18 19 20 21 22 23 24 25 26 27 28 29
size_t reflog_entrycount(git_repository *repo, const char *name)
{
	git_reflog *log;
	size_t ret;

	cl_git_pass(git_reflog_read(&log, repo, name));
	ret = git_reflog_entrycount(log);
	git_reflog_free(log);

	return ret;
}

30
void cl_reflog_check_entry_(git_repository *repo, const char *reflog, size_t idx,
31
	const char *old_spec, const char *new_spec,
32 33
	const char *email, const char *message,
	const char *file, const char *func, int line)
34 35 36
{
	git_reflog *log;
	const git_reflog_entry *entry;
37
	git_buf result = GIT_BUF_INIT;
38 39 40

	cl_git_pass(git_reflog_read(&log, repo, reflog));
	entry = git_reflog_entry_byindex(log, idx);
41
	if (entry == NULL)
42
		clar__fail(file, func, line, "Reflog has no such entry", NULL, 1);
43 44 45 46

	if (old_spec) {
		git_object *obj = NULL;
		if (git_revparse_single(&obj, repo, old_spec) == GIT_OK) {
47 48 49 50 51
			if (git_oid_cmp(git_object_id(obj), git_reflog_entry_id_old(entry)) != 0) {
				git_oid__writebuf(&result, "\tOld OID: \"", git_object_id(obj));
				git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_old(entry));
				git_buf_puts(&result, "\"\n");
			}
52 53 54 55
			git_object_free(obj);
		} else {
			git_oid *oid = git__calloc(1, sizeof(*oid));
			git_oid_fromstr(oid, old_spec);
56 57 58 59 60
			if (git_oid_cmp(oid, git_reflog_entry_id_old(entry)) != 0) {
				git_oid__writebuf(&result, "\tOld OID: \"", oid);
				git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_old(entry));
				git_buf_puts(&result, "\"\n");
			}
61 62 63 64 65 66
			git__free(oid);
		}
	}
	if (new_spec) {
		git_object *obj = NULL;
		if (git_revparse_single(&obj, repo, new_spec) == GIT_OK) {
67 68 69 70 71
			if (git_oid_cmp(git_object_id(obj), git_reflog_entry_id_new(entry)) != 0) {
				git_oid__writebuf(&result, "\tNew OID: \"", git_object_id(obj));
				git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_new(entry));
				git_buf_puts(&result, "\"\n");
			}
72 73 74 75
			git_object_free(obj);
		} else {
			git_oid *oid = git__calloc(1, sizeof(*oid));
			git_oid_fromstr(oid, new_spec);
76 77 78 79 80
			if (git_oid_cmp(oid, git_reflog_entry_id_new(entry)) != 0) {
				git_oid__writebuf(&result, "\tNew OID: \"", oid);
				git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_new(entry));
				git_buf_puts(&result, "\"\n");
			}
81 82 83 84
			git__free(oid);
		}
	}

85 86 87
	if (email && strcmp(email, git_reflog_entry_committer(entry)->email) != 0)
		git_buf_printf(&result, "\tEmail: \"%s\" != \"%s\"\n", email, git_reflog_entry_committer(entry)->email);

88
	if (message) {
89 90 91 92 93
		const char *entry_msg = git_reflog_entry_message(entry);
		if (entry_msg == NULL) entry_msg = "";

		if (entry_msg && strcmp(message, entry_msg) != 0)
			git_buf_printf(&result, "\tMessage: \"%s\" != \"%s\"\n", message, entry_msg);
94
	}
95
	if (git_buf_len(&result) != 0)
96
		clar__fail(file, func, line, "Reflog entry mismatch", git_buf_cstr(&result), 1);
97

98
	git_buf_dispose(&result);
99 100 101 102 103 104 105 106 107 108 109 110 111
	git_reflog_free(log);
}

void reflog_print(git_repository *repo, const char *reflog_name)
{
	git_reflog *reflog;
	size_t idx;
	git_buf out = GIT_BUF_INIT;

	git_reflog_read(&reflog, repo, reflog_name);

	for (idx = 0; idx < git_reflog_entrycount(reflog); idx++) {
		const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, idx);
112 113
		reflog_entry_tostr(&out, entry);
		git_buf_putc(&out, '\n');
114 115 116
	}

	fprintf(stderr, "%s", git_buf_cstr(&out));
117
	git_buf_dispose(&out);
118 119
	git_reflog_free(reflog);
}