upstream.c 4.69 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"
#include "refs.h"

static git_repository *repo;
5
static git_reference *branch, *upstream;
6

7
void test_refs_branches_upstream__initialize(void)
8 9 10 11
{
	cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git")));

	branch = NULL;
12
	upstream = NULL;
13 14
}

15
void test_refs_branches_upstream__cleanup(void)
16
{
17
	git_reference_free(upstream);
18
	git_reference_free(branch);
19
	branch = NULL;
20 21

	git_repository_free(repo);
22
	repo = NULL;
23 24
}

25
void test_refs_branches_upstream__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void)
26 27 28
{
	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));

29
	cl_git_pass(git_branch_upstream(&upstream, branch));
30

31
	cl_assert_equal_s("refs/remotes/test/master", git_reference_name(upstream));
32 33
}

34
void test_refs_branches_upstream__can_retrieve_the_local_upstream_reference_of_a_local_branch(void)
35 36 37
{
	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/track-local"));

38
	cl_git_pass(git_branch_upstream(&upstream, branch));
39

40
	cl_assert_equal_s("refs/heads/master", git_reference_name(upstream));
41 42
}

43
void test_refs_branches_upstream__cannot_retrieve_a_remote_upstream_reference_from_a_non_branch(void)
44 45 46
{
	cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b"));

47
	cl_git_fail(git_branch_upstream(&upstream, branch));
48 49
}

50
void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void)
51 52 53
{
	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees"));

54
	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
55
}
56

57
void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void)
58 59 60
{
	cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/cannot-fetch"));

61
	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
62
}
63

nulltoken committed
64
static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name)
65 66 67
{
	git_reference *branch;

Ben Straub committed
68
	cl_assert_equal_i(GIT_OBJ_COMMIT, git_object_type((git_object*)target));
69
	cl_git_pass(git_branch_create(&branch, repository, entry_name, (git_commit*)target, 0, NULL, NULL));
70

71
	cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch));
72 73 74 75

	git_reference_free(branch);
}

76
void test_refs_branches_upstream__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void)
77 78 79
{
	git_reference *head;
	git_repository *repository;
nulltoken committed
80
	git_commit *target;
81 82 83 84

	repository = cl_git_sandbox_init("testrepo.git");

	cl_git_pass(git_repository_head(&head, repository));
nulltoken committed
85
	cl_git_pass(git_reference_peel(((git_object **)&target), head, GIT_OBJ_COMMIT));
86 87 88 89 90 91
	git_reference_free(head);

	assert_merge_and_or_remote_key_missing(repository, target, "remoteless");
	assert_merge_and_or_remote_key_missing(repository, target, "mergeless");
	assert_merge_and_or_remote_key_missing(repository, target, "mergeandremoteless");

nulltoken committed
92
	git_commit_free(target);
93 94 95

	cl_git_sandbox_cleanup();
}
96 97 98 99 100 101 102 103 104 105

void test_refs_branches_upstream__set_unset_upstream(void)
{
	git_reference *branch;
	git_repository *repository;
	const char *value;
	git_config *config;

	repository = cl_git_sandbox_init("testrepo.git");

106
	/* remote */
107 108 109 110 111 112 113 114 115
	cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
	cl_git_pass(git_branch_set_upstream(branch, "test/master"));

	cl_git_pass(git_repository_config(&config, repository));
	cl_git_pass(git_config_get_string(&value, config, "branch.test.remote"));
	cl_assert_equal_s(value, "test");
	cl_git_pass(git_config_get_string(&value, config, "branch.test.merge"));
	cl_assert_equal_s(value, "refs/heads/master");

nulltoken committed
116 117
	git_reference_free(branch);

118 119 120 121 122 123 124 125 126 127
	/* local */
	cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test"));
	cl_git_pass(git_branch_set_upstream(branch, "master"));

	cl_git_pass(git_config_get_string(&value, config, "branch.test.remote"));
	cl_assert_equal_s(value, ".");
	cl_git_pass(git_config_get_string(&value, config, "branch.test.merge"));
	cl_assert_equal_s(value, "refs/heads/master");

	/* unset */
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	cl_git_pass(git_branch_set_upstream(branch, NULL));
	cl_git_fail_with(git_config_get_string(&value, config, "branch.test.merge"), GIT_ENOTFOUND);
	cl_git_fail_with(git_config_get_string(&value, config, "branch.test.remote"), GIT_ENOTFOUND);

	git_reference_free(branch);

	cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/master"));
	cl_git_pass(git_branch_set_upstream(branch, NULL));
	cl_git_fail_with(git_config_get_string(&value, config, "branch.master.merge"), GIT_ENOTFOUND);
	cl_git_fail_with(git_config_get_string(&value, config, "branch.master.remote"), GIT_ENOTFOUND);

	git_reference_free(branch);

	git_config_free(config);
	cl_git_sandbox_cleanup();
}