]>
Commit | Line | Data |
---|---|---|
fb910281 | 1 | #include "clar_libgit2.h" |
2 | #include "refs.h" | |
3 | ||
4 | static git_repository *repo; | |
a258d8e3 | 5 | static git_reference *branch, *upstream; |
fb910281 | 6 | |
a258d8e3 | 7 | void test_refs_branches_upstream__initialize(void) |
fb910281 | 8 | { |
9 | cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo.git"))); | |
10 | ||
11 | branch = NULL; | |
a258d8e3 | 12 | upstream = NULL; |
fb910281 | 13 | } |
14 | ||
a258d8e3 | 15 | void test_refs_branches_upstream__cleanup(void) |
fb910281 | 16 | { |
a258d8e3 | 17 | git_reference_free(upstream); |
fb910281 | 18 | git_reference_free(branch); |
9094d30b | 19 | branch = NULL; |
fb910281 | 20 | |
21 | git_repository_free(repo); | |
9094d30b | 22 | repo = NULL; |
fb910281 | 23 | } |
24 | ||
a258d8e3 | 25 | void test_refs_branches_upstream__can_retrieve_the_remote_tracking_reference_of_a_local_branch(void) |
fb910281 | 26 | { |
fb910281 | 27 | cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master")); |
28 | ||
a258d8e3 | 29 | cl_git_pass(git_branch_upstream(&upstream, branch)); |
fb910281 | 30 | |
a258d8e3 | 31 | cl_assert_equal_s("refs/remotes/test/master", git_reference_name(upstream)); |
fb910281 | 32 | } |
33 | ||
a258d8e3 | 34 | void test_refs_branches_upstream__can_retrieve_the_local_upstream_reference_of_a_local_branch(void) |
fb910281 | 35 | { |
fb910281 | 36 | cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/track-local")); |
37 | ||
a258d8e3 | 38 | cl_git_pass(git_branch_upstream(&upstream, branch)); |
fb910281 | 39 | |
a258d8e3 | 40 | cl_assert_equal_s("refs/heads/master", git_reference_name(upstream)); |
fb910281 | 41 | } |
42 | ||
a258d8e3 | 43 | void test_refs_branches_upstream__cannot_retrieve_a_remote_upstream_reference_from_a_non_branch(void) |
fb910281 | 44 | { |
fb910281 | 45 | cl_git_pass(git_reference_lookup(&branch, repo, "refs/tags/e90810b")); |
46 | ||
a258d8e3 | 47 | cl_git_fail(git_branch_upstream(&upstream, branch)); |
fb910281 | 48 | } |
49 | ||
a258d8e3 | 50 | void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_plain_local_branch_returns_GIT_ENOTFOUND(void) |
fb910281 | 51 | { |
fb910281 | 52 | cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/subtrees")); |
53 | ||
a258d8e3 | 54 | cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch)); |
fb910281 | 55 | } |
e16fc07f | 56 | |
a258d8e3 | 57 | void test_refs_branches_upstream__trying_to_retrieve_a_remote_tracking_reference_from_a_branch_with_no_fetchspec_returns_GIT_ENOTFOUND(void) |
e16fc07f | 58 | { |
e16fc07f | 59 | cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/cannot-fetch")); |
60 | ||
a258d8e3 | 61 | cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch)); |
e16fc07f | 62 | } |
37849a8e | 63 | |
3da73c40 | 64 | static void assert_merge_and_or_remote_key_missing(git_repository *repository, const git_commit *target, const char *entry_name) |
37849a8e | 65 | { |
66 | git_reference *branch; | |
67 | ||
72629a10 | 68 | cl_assert_equal_i(GIT_OBJ_COMMIT, git_object_type((git_object*)target)); |
b31ebfbc | 69 | cl_git_pass(git_branch_create(&branch, repository, entry_name, (git_commit*)target, 0, NULL, NULL)); |
37849a8e | 70 | |
a258d8e3 | 71 | cl_assert_equal_i(GIT_ENOTFOUND, git_branch_upstream(&upstream, branch)); |
37849a8e | 72 | |
73 | git_reference_free(branch); | |
74 | } | |
75 | ||
a258d8e3 | 76 | void test_refs_branches_upstream__retrieve_a_remote_tracking_reference_from_a_branch_with_no_remote_returns_GIT_ENOTFOUND(void) |
37849a8e | 77 | { |
78 | git_reference *head; | |
79 | git_repository *repository; | |
3da73c40 | 80 | git_commit *target; |
37849a8e | 81 | |
82 | repository = cl_git_sandbox_init("testrepo.git"); | |
83 | ||
84 | cl_git_pass(git_repository_head(&head, repository)); | |
3da73c40 | 85 | cl_git_pass(git_reference_peel(((git_object **)&target), head, GIT_OBJ_COMMIT)); |
37849a8e | 86 | git_reference_free(head); |
87 | ||
88 | assert_merge_and_or_remote_key_missing(repository, target, "remoteless"); | |
89 | assert_merge_and_or_remote_key_missing(repository, target, "mergeless"); | |
90 | assert_merge_and_or_remote_key_missing(repository, target, "mergeandremoteless"); | |
91 | ||
3da73c40 | 92 | git_commit_free(target); |
37849a8e | 93 | |
94 | cl_git_sandbox_cleanup(); | |
95 | } | |
d59942c2 CMN |
96 | |
97 | void test_refs_branches_upstream__set_unset_upstream(void) | |
98 | { | |
99 | git_reference *branch; | |
100 | git_repository *repository; | |
101 | const char *value; | |
102 | git_config *config; | |
103 | ||
104 | repository = cl_git_sandbox_init("testrepo.git"); | |
105 | ||
3d42e9a3 | 106 | /* remote */ |
d59942c2 CMN |
107 | cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test")); |
108 | cl_git_pass(git_branch_set_upstream(branch, "test/master")); | |
109 | ||
110 | cl_git_pass(git_repository_config(&config, repository)); | |
111 | cl_git_pass(git_config_get_string(&value, config, "branch.test.remote")); | |
112 | cl_assert_equal_s(value, "test"); | |
113 | cl_git_pass(git_config_get_string(&value, config, "branch.test.merge")); | |
114 | cl_assert_equal_s(value, "refs/heads/master"); | |
115 | ||
24988894 | 116 | git_reference_free(branch); |
117 | ||
3d42e9a3 NV |
118 | /* local */ |
119 | cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/test")); | |
120 | cl_git_pass(git_branch_set_upstream(branch, "master")); | |
121 | ||
3d42e9a3 NV |
122 | cl_git_pass(git_config_get_string(&value, config, "branch.test.remote")); |
123 | cl_assert_equal_s(value, "."); | |
124 | cl_git_pass(git_config_get_string(&value, config, "branch.test.merge")); | |
125 | cl_assert_equal_s(value, "refs/heads/master"); | |
126 | ||
127 | /* unset */ | |
d59942c2 CMN |
128 | cl_git_pass(git_branch_set_upstream(branch, NULL)); |
129 | cl_git_fail_with(git_config_get_string(&value, config, "branch.test.merge"), GIT_ENOTFOUND); | |
130 | cl_git_fail_with(git_config_get_string(&value, config, "branch.test.remote"), GIT_ENOTFOUND); | |
131 | ||
132 | git_reference_free(branch); | |
133 | ||
134 | cl_git_pass(git_reference_lookup(&branch, repository, "refs/heads/master")); | |
135 | cl_git_pass(git_branch_set_upstream(branch, NULL)); | |
136 | cl_git_fail_with(git_config_get_string(&value, config, "branch.master.merge"), GIT_ENOTFOUND); | |
137 | cl_git_fail_with(git_config_get_string(&value, config, "branch.master.remote"), GIT_ENOTFOUND); | |
138 | ||
139 | git_reference_free(branch); | |
140 | ||
141 | git_config_free(config); | |
142 | cl_git_sandbox_cleanup(); | |
143 | } |