Hash :
a5696702
Author :
Date :
2021-07-08T11:58:28
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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
#include "../clar_libgit2.h"
#include "remote.h"
#include "repository.h"
static git_repository *repo1;
static git_repository *repo2;
static char* repo1_path;
static char* repo2_path;
static const char *REPO1_REFNAME = "refs/heads/main";
static const char *REPO2_REFNAME = "refs/remotes/repo1/main";
static char *FORCE_FETCHSPEC = "+refs/heads/main:refs/remotes/repo1/main";
static char *NON_FORCE_FETCHSPEC = "refs/heads/main:refs/remotes/repo1/main";
void test_remote_fetch__initialize(void) {
git_config *c;
git_buf repo1_path_buf = GIT_BUF_INIT;
git_buf repo2_path_buf = GIT_BUF_INIT;
const char *sandbox = clar_sandbox_path();
cl_git_pass(git_buf_joinpath(&repo1_path_buf, sandbox, "fetchtest_repo1"));
repo1_path = git_buf_detach(&repo1_path_buf);
cl_git_pass(git_repository_init(&repo1, repo1_path, true));
cl_git_pass(git_buf_joinpath(&repo2_path_buf, sandbox, "fetchtest_repo2"));
repo2_path = git_buf_detach(&repo2_path_buf);
cl_git_pass(git_repository_init(&repo2, repo2_path, true));
cl_git_pass(git_repository_config(&c, repo1));
cl_git_pass(git_config_set_string(c, "user.email", "some@email"));
cl_git_pass(git_config_set_string(c, "user.name", "some@name"));
git_config_free(c);
git_buf_dispose(&repo1_path_buf);
git_buf_dispose(&repo2_path_buf);
}
void test_remote_fetch__cleanup(void) {
git_repository_free(repo1);
git_repository_free(repo2);
cl_git_pass(git_futils_rmdir_r(repo1_path, NULL, GIT_RMDIR_REMOVE_FILES));
free(repo1_path);
cl_git_pass(git_futils_rmdir_r(repo2_path, NULL, GIT_RMDIR_REMOVE_FILES));
free(repo2_path);
}
/**
* This checks that the '+' flag on fetchspecs is respected. We create a
* repository that has a reference to two commits, one a child of the other.
* We fetch this repository into a second repository. Then we reset the
* reference in the first repository and run the fetch again. If the '+' flag
* is used then the reference in the second repository will change, but if it
* is not then it should stay the same.
*
* @param commit1id A pointer to an OID which will be populated with the first
* commit.
* @param commit2id A pointer to an OID which will be populated with the second
* commit, which is a descendant of the first.
* @param force Whether to use a spec with '+' prefixed to force the refs
* to update
*/
void do_time_travelling_fetch(git_oid *commit1id, git_oid *commit2id,
bool force) {
char *refspec_strs = {
force ? FORCE_FETCHSPEC : NON_FORCE_FETCHSPEC,
};
git_strarray refspecs = {
.count = 1,
.strings = &refspec_strs,
};
// create two commits in repo 1 and a reference to them
{
git_oid empty_tree_id;
git_tree *empty_tree;
git_signature *sig;
git_treebuilder *tb;
cl_git_pass(git_treebuilder_new(&tb, repo1, NULL));
cl_git_pass(git_treebuilder_write(&empty_tree_id, tb));
cl_git_pass(git_tree_lookup(&empty_tree, repo1, &empty_tree_id));
cl_git_pass(git_signature_default(&sig, repo1));
cl_git_pass(git_commit_create(commit1id, repo1, REPO1_REFNAME, sig,
sig, NULL, "one", empty_tree, 0, NULL));
cl_git_pass(git_commit_create_v(commit2id, repo1, REPO1_REFNAME, sig,
sig, NULL, "two", empty_tree, 1, commit1id));
git_tree_free(empty_tree);
git_signature_free(sig);
git_treebuilder_free(tb);
}
// fetch the reference via the remote
{
git_remote *remote;
cl_git_pass(git_remote_create_anonymous(&remote, repo2,
git_repository_path(repo1)));
cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, "some message"));
git_remote_free(remote);
}
// assert that repo2 references the second commit
{
const git_oid *target;
git_reference *ref;
cl_git_pass(git_reference_lookup(&ref, repo2, REPO2_REFNAME));
target = git_reference_target(ref);
cl_assert_equal_b(git_oid_cmp(target, commit2id), 0);
git_reference_free(ref);
}
// set the reference in repo1 to point to the older commit
{
git_reference *ref;
git_reference *ref2;
cl_git_pass(git_reference_lookup(&ref, repo1, REPO1_REFNAME));
cl_git_pass(git_reference_set_target(&ref2, ref, commit1id,
"rollback"));
git_reference_free(ref);
git_reference_free(ref2);
}
// fetch the reference again
{
git_remote *remote;
cl_git_pass(git_remote_create_anonymous(&remote, repo2,
git_repository_path(repo1)));
cl_git_pass(git_remote_fetch(remote, &refspecs, NULL, "some message"));
git_remote_free(remote);
}
}
void test_remote_fetch__dont_update_refs_if_not_descendant_and_not_force(void) {
const git_oid *target;
git_oid commit1id;
git_oid commit2id;
git_reference *ref;
do_time_travelling_fetch(&commit1id, &commit2id, false);
// assert that the reference in repo2 has not changed
cl_git_pass(git_reference_lookup(&ref, repo2, REPO2_REFNAME));
target = git_reference_target(ref);
cl_assert_equal_b(git_oid_cmp(target, &commit2id), 0);
git_reference_free(ref);
}
void test_remote_fetch__do_update_refs_if_not_descendant_and_force(void) {
const git_oid *target;
git_oid commit1id;
git_oid commit2id;
git_reference *ref;
do_time_travelling_fetch(&commit1id, &commit2id, true);
// assert that the reference in repo2 has changed
cl_git_pass(git_reference_lookup(&ref, repo2, REPO2_REFNAME));
target = git_reference_target(ref);
cl_assert_equal_b(git_oid_cmp(target, &commit1id), 0);
git_reference_free(ref);
}