summaryrefslogtreecommitdiff
path: root/tests/refs/settargetwithlog.c
blob: e1f73db567d065db7c0a988b13dba9140cc2077a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "clar_libgit2.h"

#include "repository.h"
#include "git2/reflog.h"
#include "reflog.h"
#include "ref_helpers.h"

static const char *br2_tip = "a4a7dce85cf63874e984719f4fdd239f5145052f";
static const char *master_tip = "a65fedf39aefe402d3bb6e24df4d4f5fe4547750";
static const char *br2_name = "refs/heads/br2";

static git_repository *g_repo;

void test_refs_settargetwithlog__initialize(void)
{
   g_repo = cl_git_sandbox_init("testrepo.git");
}

void test_refs_settargetwithlog__cleanup(void)
{
   cl_git_sandbox_cleanup();
}

void test_refs_settargetwithlog__updating_a_direct_reference_adds_a_reflog_entry(void)
{
	git_reference *reference, *reference_out;
	git_oid current_id, target_id;
	git_signature *signature;
	git_reflog *reflog;
	const git_reflog_entry *entry;

	const char *message = "You've been logged, mate!";

	git_oid_fromstr(&current_id, br2_tip);
	git_oid_fromstr(&target_id, master_tip);

	cl_git_pass(git_reference_lookup(&reference, g_repo, br2_name));

	cl_git_pass(git_signature_now(&signature, "foo", "foo@bar"));

	cl_git_pass(git_reference_set_target_with_log(
		&reference_out, reference, &target_id, signature, message));

	cl_git_pass(git_reflog_read(&reflog, reference_out));

	entry = git_reflog_entry_byindex(reflog, 0);
	cl_assert(git_oid_cmp(&current_id, &entry->oid_old) == 0);
	cl_assert(git_oid_cmp(&target_id, &entry->oid_cur) == 0);
	cl_assert_equal_s(message, entry->msg);

	git_reflog_free(reflog);
	git_reference_free(reference_out);
	git_reference_free(reference);
	git_signature_free(signature);
}

void test_refs_settargetwithlog__updating_a_symbolic_reference_adds_a_reflog_entry(void)
{
	git_reference *reference, *reference_out;
	git_oid id;
	git_signature *signature;
	git_reflog *reflog;
	const git_reflog_entry *entry;

	const char *name = "HEAD";
	const char *message = "You've been logged, mate!";

	git_oid_fromstr(&id, br2_tip);

	cl_git_pass(git_signature_now(&signature, "foo", "foo@bar"));
	
	cl_git_pass(git_reference_lookup(&reference, g_repo, name));

	cl_assert_equal_s(
		"refs/heads/master", git_reference_symbolic_target(reference));

	cl_git_pass(git_reference_symbolic_set_target_with_log(&reference_out,
		reference, br2_name, signature, message));

	cl_assert_equal_s(
		br2_name, git_reference_symbolic_target(reference_out));

	cl_git_pass(git_reflog_read(&reflog, reference));

	entry = git_reflog_entry_byindex(reflog, 0);
	cl_assert(git_oid_streq(&entry->oid_old, master_tip) == 0);
	cl_assert(git_oid_streq(&entry->oid_cur, br2_tip) == 0);
	cl_assert_equal_s(message, entry->msg);

	git_reflog_free(reflog);
	git_reference_free(reference_out);
	git_reference_free(reference);
	git_signature_free(signature);
}