Exemple #1
0
void test_merge_workdir_simple__unrelated_with_conflicts(void)
{
	git_oid their_oids[1];
	git_merge_head *their_heads[1];
	git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;

	struct merge_index_entry merge_index_entries[] = {
		{ 0100644, "233c0919c998ed110a4b6ff36f353aec8b713487", 0, "added-in-master.txt" },
		{ 0100644, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", 2, "automergeable.txt" },
		{ 0100644, "d07ec190c306ec690bac349e87d01c4358e49bb2", 3, "automergeable.txt" },
		{ 0100644, "ab6c44a2e84492ad4b41bb6bac87353e9d02ac8b", 0, "changed-in-branch.txt" },
		{ 0100644, "11deab00b2d3a6f5a3073988ac050c2d7b6655e2", 0, "changed-in-master.txt" },
		{ 0100644, "4e886e602529caa9ab11d71f86634bd1b6e0de10", 2, "conflicting.txt" },
		{ 0100644, "4b253da36a0ae8bfce63aeabd8c5b58429925594", 3, "conflicting.txt" },
		{ 0100644, "ef58fdd8086c243bdc81f99e379acacfd21d32d6", 0, "new-in-unrelated1.txt" },
		{ 0100644, "948ba6e701c1edab0c2d394fb7c5538334129793", 0, "new-in-unrelated2.txt" },
		{ 0100644, "dfe3f22baa1f6fce5447901c3086bae368de6bdd", 0, "removed-in-branch.txt" },
		{ 0100644, "c8f06f2e3bb2964174677e91f0abead0e43c9e5d", 0, "unchanged.txt" },
	};

	cl_git_pass(git_oid_fromstr(&their_oids[0], THEIRS_UNRELATED_OID));
	cl_git_pass(git_merge_head_from_id(&their_heads[0], repo, &their_oids[0]));

	merge_opts.file_favor = 0;
	cl_git_pass(git_merge(repo, (const git_merge_head **)their_heads, 1, &merge_opts, NULL));

	cl_assert(merge_test_index(repo_index, merge_index_entries, 11));

	git_merge_head_free(their_heads[0]);
}
Exemple #2
0
static int merge_trivial(const char *ours, const char *theirs, bool automerge)
{
	git_buf branch_buf = GIT_BUF_INIT;
	git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT;
	git_reference *our_ref, *their_ref;
	git_merge_head *their_heads[1];
	git_merge_opts opts = GIT_MERGE_OPTS_INIT;
	git_merge_result *result;

	checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	opts.merge_tree_opts.automerge_flags |= automerge ? 0 : GIT_MERGE_AUTOMERGE_NONE;

	git_buf_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, ours);
	cl_git_pass(git_reference_symbolic_create(&our_ref, repo, "HEAD", branch_buf.ptr, 1));

	cl_git_pass(git_checkout_head(repo, &checkout_opts));

	git_buf_clear(&branch_buf);
	git_buf_printf(&branch_buf, "%s%s", GIT_REFS_HEADS_DIR, theirs);
	cl_git_pass(git_reference_lookup(&their_ref, repo, branch_buf.ptr));
	cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, their_ref));

	cl_git_pass(git_merge(&result, repo, (const git_merge_head **)their_heads, 1, &opts));

	git_buf_free(&branch_buf);
	git_reference_free(our_ref);
	git_reference_free(their_ref);
	git_merge_head_free(their_heads[0]);
	git_merge_result_free(result);

	return 0;
}
Exemple #3
0
void test_merge_workdir_submodules__take_changed(void)
{
	git_reference *our_ref, *their_ref;
	git_commit *our_commit;
	git_annotated_commit *their_head;
	git_index *index;

	struct merge_index_entry merge_index_entries[] = {
		{ 0100644, "caff6b7d44973f53e3e0cf31d0d695188b19aec6", 0, ".gitmodules" },
		{ 0100644, "b438ff23300b2e0f80b84a6f30140dfa91e71423", 0, "file1.txt" },
		{ 0100644, "f27fbafdfa6693f8f7a5128506fe3e338dbfcad2", 0, "file2.txt" },
		{ 0160000, "297aa6cd028b3336c7802c7a6f49143da4e1602d", 0, "submodule" },
	};

	cl_git_pass(git_reference_lookup(&our_ref, repo, "refs/heads/" SUBMODULE_MAIN_BRANCH));
	cl_git_pass(git_commit_lookup(&our_commit, repo, git_reference_target(our_ref)));
	cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL));

	cl_git_pass(git_reference_lookup(&their_ref, repo, "refs/heads/" SUBMODULE_OTHER2_BRANCH));
	cl_git_pass(git_annotated_commit_from_ref(&their_head, repo, their_ref));

	cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head, 1, NULL, NULL));

	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(merge_test_index(index, merge_index_entries, 4));

	git_index_free(index);
	git_annotated_commit_free(their_head);
	git_commit_free(our_commit);
	git_reference_free(their_ref);
	git_reference_free(our_ref);
}
Exemple #4
0
void test_merge_workdir_submodules__update_delete_conflict(void)
{
	git_reference *our_ref, *their_ref;
	git_commit *our_commit;
	git_annotated_commit *their_head;
	git_index *index;

	struct merge_index_entry merge_index_entries[] = {
		{ 0100644, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", 0, ".gitmodules" },
		{ 0100644, "5887a5e516c53bd58efb0f02ec6aa031b6fe9ad7", 0, "file1.txt" },
		{ 0100644, "4218670ab81cc219a9f94befb5c5dad90ec52648", 0, "file2.txt" },
		{ 0160000, "d3d806a4bef96889117fd7ebac0e3cb5ec152932", 1, "submodule"},
		{ 0160000, "297aa6cd028b3336c7802c7a6f49143da4e1602d", 3, "submodule" },
	};

	cl_git_pass(git_reference_lookup(&our_ref, repo, "refs/heads/" SUBMODULE_DELETE_BRANCH));
	cl_git_pass(git_commit_lookup(&our_commit, repo, git_reference_target(our_ref)));
	cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL));

	cl_git_pass(git_reference_lookup(&their_ref, repo, "refs/heads/" SUBMODULE_MAIN_BRANCH));
	cl_git_pass(git_annotated_commit_from_ref(&their_head, repo, their_ref));

	cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head, 1, NULL, NULL));

	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(merge_test_index(index, merge_index_entries, 5));

	git_index_free(index);
	git_annotated_commit_free(their_head);
	git_commit_free(our_commit);
	git_reference_free(their_ref);
	git_reference_free(our_ref);
}
Exemple #5
0
void test_merge_workdir_simple__binary(void)
{
	git_oid our_oid, their_oid, our_file_oid;
	git_commit *our_commit;
	git_merge_head *their_head;
	const git_index_entry *binary_entry;

	struct merge_index_entry merge_index_entries[] = {
		{ 0100644, "1c51d885170f57a0c4e8c69ff6363d91a5b51f85", 1, "binary" },
		{ 0100644, "23ed141a6ae1e798b2f721afedbe947c119111ba", 2, "binary" },
		{ 0100644, "836b8b82b26cab22eaaed8820877c76d6c8bca19", 3, "binary" },
	};

	cl_git_pass(git_oid_fromstr(&our_oid, "cc338e4710c9b257106b8d16d82f86458d5beaf1"));
	cl_git_pass(git_oid_fromstr(&their_oid, "ad01aebfdf2ac13145efafe3f9fcf798882f1730"));

	cl_git_pass(git_commit_lookup(&our_commit, repo, &our_oid));
	cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL, NULL));

	cl_git_pass(git_merge_head_from_id(&their_head, repo, &their_oid));

	cl_git_pass(git_merge(repo, (const git_merge_head **)&their_head, 1, NULL, NULL));

	cl_assert(merge_test_index(repo_index, merge_index_entries, 3));

	cl_git_pass(git_index_add_bypath(repo_index, "binary"));
	cl_assert((binary_entry = git_index_get_bypath(repo_index, "binary", 0)) != NULL);

	cl_git_pass(git_oid_fromstr(&our_file_oid, "23ed141a6ae1e798b2f721afedbe947c119111ba"));
	cl_assert(git_oid_cmp(&binary_entry->id, &our_file_oid) == 0);

	git_merge_head_free(their_head);
	git_commit_free(our_commit);
}
Exemple #6
0
void test_merge_workdir_submodules__automerge(void)
{
	git_reference *our_ref, *their_ref;
	git_commit *our_commit;
	git_annotated_commit *their_head;
	git_index *index;

	struct merge_index_entry merge_index_entries[] = {
		{ 0100644, "caff6b7d44973f53e3e0cf31d0d695188b19aec6", 0, ".gitmodules" },
		{ 0100644, "950a663a6a7b2609eed1ed1ba9f41eb1a3192a9f", 0, "file1.txt" },
		{ 0100644, "343e660b9cb4bee5f407c2e33fcb9df24d9407a4", 0, "file2.txt" },
		{ 0160000, "d3d806a4bef96889117fd7ebac0e3cb5ec152932", 1, "submodule" },
		{ 0160000, "297aa6cd028b3336c7802c7a6f49143da4e1602d", 2, "submodule" },
		{ 0160000, "ae39c77c70cb6bad18bb471912460c4e1ba0f586", 3, "submodule" },
	};

	cl_git_pass(git_reference_lookup(&our_ref, repo, "refs/heads/" SUBMODULE_MAIN_BRANCH));
	cl_git_pass(git_commit_lookup(&our_commit, repo, git_reference_target(our_ref)));
	cl_git_pass(git_reset(repo, (git_object *)our_commit, GIT_RESET_HARD, NULL));

	cl_git_pass(git_reference_lookup(&their_ref, repo, "refs/heads/" SUBMODULE_OTHER_BRANCH));
	cl_git_pass(git_annotated_commit_from_ref(&their_head, repo, their_ref));

	cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head, 1, NULL, NULL));

	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(merge_test_index(index, merge_index_entries, 6));

	git_index_free(index);
	git_annotated_commit_free(their_head);
	git_commit_free(our_commit);
	git_reference_free(their_ref);
	git_reference_free(our_ref);
}
Exemple #7
0
PyObject *
Repository_merge(Repository *self, PyObject *py_oid)
{
    git_merge_result *merge_result;
    git_merge_head *oid_merge_head;
    git_oid oid;
    const git_merge_opts default_opts = GIT_MERGE_OPTS_INIT;
    int err;
    size_t len;
    PyObject *py_merge_result;

    len = py_oid_to_git_oid(py_oid, &oid);
    if (len == 0)
        return NULL;

    err = git_merge_head_from_oid(&oid_merge_head, self->repo, &oid);
    if (err < 0)
        return Error_set(err);

    err = git_merge(&merge_result, self->repo,
                    (const git_merge_head **)&oid_merge_head, 1,
                    &default_opts);
    git_merge_head_free(oid_merge_head);
    if (err < 0)
        return Error_set(err);

    py_merge_result = git_merge_result_to_python(merge_result);
    return py_merge_result;
}
Exemple #8
0
PyObject *
Repository_merge(Repository *self, PyObject *py_oid)
{
    git_annotated_commit *commit;
    git_oid oid;
    int err;
    size_t len;
    git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
    git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;

    len = py_oid_to_git_oid(py_oid, &oid);
    if (len == 0)
        return NULL;

    err = git_annotated_commit_lookup(&commit, self->repo, &oid);
    if (err < 0)
        return Error_set(err);

    checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
    err = git_merge(self->repo,
                    (const git_annotated_commit **)&commit, 1,
                    &merge_opts, &checkout_opts);

    git_annotated_commit_free(commit);
    if (err < 0)
        return Error_set(err);

    Py_RETURN_NONE;
}
Exemple #9
0
PyObject *
Repository_merge(Repository *self, PyObject *py_oid)
{
    git_merge_head *oid_merge_head;
    git_oid oid;
    int err;
    size_t len;
    git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
    git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;

    len = py_oid_to_git_oid(py_oid, &oid);
    if (len == 0)
        return NULL;

    err = git_merge_head_from_id(&oid_merge_head, self->repo, &oid);
    if (err < 0)
        return Error_set(err);

    err = git_merge(self->repo,
                    (const git_merge_head **)&oid_merge_head, 1,
                    &merge_opts, &checkout_opts);

    git_merge_head_free(oid_merge_head);
    if (err < 0)
        return Error_set(err);

    Py_RETURN_NONE;
}
Exemple #10
0
void test_index_addall__removes_deleted_conflicted_files(void)
{
	git_index *index;
	git_reference *ref;
	git_annotated_commit *annotated;

	g_repo = cl_git_sandbox_init("merge-resolve");
	cl_git_pass(git_repository_index(&index, g_repo));

	check_status(g_repo, 0, 0, 0, 0, 0, 0, 0, 0);

	cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/branch"));
	cl_git_pass(git_annotated_commit_from_ref(&annotated, g_repo, ref));

	cl_git_pass(git_merge(g_repo, &annotated, 1, NULL, NULL));
	check_status(g_repo, 0, 1, 2, 0, 0, 0, 0, 1);

	cl_git_rmfile("merge-resolve/conflicting.txt");

	cl_git_pass(git_index_add_all(index, NULL, 0, NULL, NULL));
	check_status(g_repo, 0, 2, 2, 0, 0, 0, 0, 0);

	git_annotated_commit_free(annotated);
	git_reference_free(ref);
	git_index_free(index);
}
Exemple #11
0
static void merge_branch(void)
{
	git_oid their_id;
	git_annotated_commit *their_head;

	cl_git_pass(git_oid_fromstr(&their_id, BRANCH_ID));
	cl_git_pass(git_annotated_commit_lookup(&their_head, repo, &their_id));

	cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head,
		1, NULL, NULL));

	git_annotated_commit_free(their_head);
}
Exemple #12
0
void test_merge_workdir_simple__directory_file(void)
{
	git_reference *head;
	git_oid their_oids[1], head_commit_id;
	git_merge_head *their_heads[1];
	git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
	git_commit *head_commit;

	struct merge_index_entry merge_index_entries[] = {
		{ 0100644, "49130a28ef567af9a6a6104c38773fedfa5f9742", 2, "dir-10" },
		{ 0100644, "6c06dcd163587c2cc18be44857e0b71116382aeb", 3, "dir-10" },
		{ 0100644, "43aafd43bea779ec74317dc361f45ae3f532a505", 0, "dir-6" },
		{ 0100644, "a031a28ae70e33a641ce4b8a8f6317f1ab79dee4", 3, "dir-7" },
		{ 0100644, "5012fd565b1393bdfda1805d4ec38ce6619e1fd1", 1, "dir-7/file.txt" },
		{ 0100644, "a5563304ddf6caba25cb50323a2ea6f7dbfcadca", 2, "dir-7/file.txt" },
		{ 0100644, "e9ad6ec3e38364a3d07feda7c4197d4d845c53b5", 0, "dir-8" },
		{ 0100644, "3ef4d30382ca33fdeba9fda895a99e0891ba37aa", 2, "dir-9" },
		{ 0100644, "fc4c636d6515e9e261f9260dbcf3cc6eca97ea08", 1, "dir-9/file.txt" },
		{ 0100644, "76ab0e2868197ec158ddd6c78d8a0d2fd73d38f9", 3, "dir-9/file.txt" },
		{ 0100644, "5c2411f8075f48a6b2fdb85ebc0d371747c4df15", 0, "file-1/new" },
		{ 0100644, "a39a620dae5bc8b4e771cd4d251b7d080401a21e", 1, "file-2" },
		{ 0100644, "d963979c237d08b6ba39062ee7bf64c7d34a27f8", 2, "file-2" },
		{ 0100644, "5c341ead2ba6f2af98ce5ec3fe84f6b6d2899c0d", 0, "file-2/new" },
		{ 0100644, "9efe7723802d4305142eee177e018fee1572c4f4", 0, "file-3/new" },
		{ 0100644, "bacac9b3493509aa15e1730e1545fc0919d1dae0", 1, "file-4" },
		{ 0100644, "7663fce0130db092936b137cabd693ec234eb060", 3, "file-4" },
		{ 0100644, "e49f917b448d1340b31d76e54ba388268fd4c922", 0, "file-4/new" },
		{ 0100644, "cab2cf23998b40f1af2d9d9a756dc9e285a8df4b", 2, "file-5/new" },
		{ 0100644, "f5504f36e6f4eb797a56fc5bac6c6c7f32969bf2", 3, "file-5/new" },
	};

	cl_git_pass(git_reference_symbolic_create(&head, repo, GIT_HEAD_FILE, GIT_REFS_HEADS_DIR OURS_DIRECTORY_FILE, 1, NULL, NULL));
	cl_git_pass(git_reference_name_to_id(&head_commit_id, repo, GIT_HEAD_FILE));
	cl_git_pass(git_commit_lookup(&head_commit, repo, &head_commit_id));
	cl_git_pass(git_reset(repo, (git_object *)head_commit, GIT_RESET_HARD, NULL, NULL));

	cl_git_pass(git_oid_fromstr(&their_oids[0], THEIRS_DIRECTORY_FILE));
	cl_git_pass(git_merge_head_from_id(&their_heads[0], repo, &their_oids[0]));

	merge_opts.file_favor = 0;
	cl_git_pass(git_merge(repo, (const git_merge_head **)their_heads, 1, &merge_opts, NULL));

	cl_assert(merge_test_index(repo_index, merge_index_entries, 20));

	git_reference_free(head);
	git_commit_free(head_commit);
	git_merge_head_free(their_heads[0]);
}
Exemple #13
0
void test_merge_workdir_fastforward__uptodate_merging_prev_commit(void)
{
    git_oid their_oid;
    git_merge_head *their_heads[1];
    git_merge_result *result;

    cl_git_pass(git_oid_fromstr(&their_oid, "c607fc30883e335def28cd686b51f6cfa02b06ec"));
    cl_git_pass(git_merge_head_from_oid(&their_heads[0], repo, &their_oid));

    cl_git_pass(git_merge(&result, repo, (const git_merge_head **)their_heads, 1, NULL));

    cl_assert(git_merge_result_is_uptodate(result));

    git_merge_head_free(their_heads[0]);
    git_merge_result_free(result);
}
Exemple #14
0
void test_merge_workdir_fastforward__uptodate(void)
{
    git_reference *their_ref;
    git_merge_head *their_heads[1];
    git_merge_result *result;

    cl_git_pass(git_reference_lookup(&their_ref, repo, GIT_HEAD_FILE));
    cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, their_ref));

    cl_git_pass(git_merge(&result, repo, (const git_merge_head **)their_heads, 1, NULL));

    cl_assert(git_merge_result_is_uptodate(result));

    git_merge_head_free(their_heads[0]);
    git_reference_free(their_ref);
    git_merge_result_free(result);
}
Exemple #15
0
static int merge_branch(void)
{
	git_oid their_oids[1];
	git_annotated_commit *their_head;
	git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
	git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
	int error;

	cl_git_pass(git_oid_fromstr(&their_oids[0], MERGE_BRANCH_OID));
	cl_git_pass(git_annotated_commit_lookup(&their_head, repo, &their_oids[0]));

	checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
	error = git_merge(repo, (const git_annotated_commit **)&their_head, 1, &merge_opts, &checkout_opts);

	git_annotated_commit_free(their_head);

	return error;
}
Exemple #16
0
static void merge_simple_branch(int merge_file_favor, int addl_checkout_strategy)
{
	git_oid their_oids[1];
	git_merge_head *their_heads[1];
	git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
	git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;

	cl_git_pass(git_oid_fromstr(&their_oids[0], THEIRS_SIMPLE_OID));
	cl_git_pass(git_merge_head_from_id(&their_heads[0], repo, &their_oids[0]));

	merge_opts.file_favor = merge_file_favor;
	checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS |
		addl_checkout_strategy;

	cl_git_pass(git_merge(repo, (const git_merge_head **)their_heads, 1, &merge_opts, &checkout_opts));

	git_merge_head_free(their_heads[0]);
}
Exemple #17
0
static git_merge_result *merge_fastforward_branch(int flags)
{
    git_reference *their_ref;
    git_merge_head *their_heads[1];
    git_merge_result *result;
    git_merge_opts opts = GIT_MERGE_OPTS_INIT;

    opts.merge_flags = flags;

    cl_git_pass(git_reference_lookup(&their_ref, repo, GIT_REFS_HEADS_DIR THEIRS_FASTFORWARD_BRANCH));
    cl_git_pass(git_merge_head_from_ref(&their_heads[0], repo, their_ref));

    cl_git_pass(git_merge(&result, repo, (const git_merge_head **)their_heads, 1, &opts));

    git_merge_head_free(their_heads[0]);
    git_reference_free(their_ref);

    return result;
}
Exemple #18
0
void test_merge_workdir_fastforward__fastforward_only(void)
{
    git_merge_result *result;
    git_merge_opts opts = GIT_MERGE_OPTS_INIT;
    git_reference *their_ref;
    git_merge_head *their_head;
    int error;

    opts.merge_flags = GIT_MERGE_FASTFORWARD_ONLY;

    cl_git_pass(git_reference_lookup(&their_ref, repo, GIT_REFS_HEADS_DIR THEIRS_NOFASTFORWARD_BRANCH));
    cl_git_pass(git_merge_head_from_ref(&their_head, repo, their_ref));

    cl_git_fail((error = git_merge(&result, repo, (const git_merge_head **)&their_head, 1, &opts)));
    cl_assert(error == GIT_ENONFASTFORWARD);

    git_merge_head_free(their_head);
    git_reference_free(their_ref);
}
Exemple #19
0
void test_merge_driver__default_can_be_specified(void)
{
	git_oid their_id;
	git_annotated_commit *their_head;
	git_merge_options merge_opts = GIT_MERGE_OPTIONS_INIT;
	const char *expected = "This is the `custom` driver.\n";

	merge_opts.default_driver = "custom";

	cl_git_pass(git_oid_fromstr(&their_id, BRANCH_ID));
	cl_git_pass(git_annotated_commit_lookup(&their_head, repo, &their_id));

	cl_git_pass(git_merge(repo, (const git_annotated_commit **)&their_head,
		1, &merge_opts, NULL));

	git_annotated_commit_free(their_head);

	cl_assert_equal_file(expected, strlen(expected),
		TEST_REPO_PATH "/applied.txt");
}
int merge_branches(git_merge_result **result, git_repository *repo, const char *ours_branch, const char *theirs_branch, git_merge_opts *opts)
{
	git_reference *head_ref, *theirs_ref;
	git_merge_head *theirs_head;
	git_checkout_opts head_checkout_opts = GIT_CHECKOUT_OPTS_INIT;

	head_checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_reference_symbolic_create(&head_ref, repo, "HEAD", ours_branch, 1));
	cl_git_pass(git_checkout_head(repo, &head_checkout_opts));

	cl_git_pass(git_reference_lookup(&theirs_ref, repo, theirs_branch));
	cl_git_pass(git_merge_head_from_ref(&theirs_head, repo, theirs_ref));

	cl_git_pass(git_merge(result, repo, (const git_merge_head **)&theirs_head, 1, opts));

	git_reference_free(head_ref);
	git_reference_free(theirs_ref);
	git_merge_head_free(theirs_head);

	return 0;
}
int merge_branches(git_repository *repo,
	const char *ours_branch, const char *theirs_branch,
	git_merge_options *merge_opts, git_checkout_options *checkout_opts)
{
	git_reference *head_ref, *theirs_ref;
	git_annotated_commit *theirs_head;
	git_checkout_options head_checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;

	head_checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_git_pass(git_reference_symbolic_create(&head_ref, repo, "HEAD", ours_branch, 1, NULL));
	cl_git_pass(git_checkout_head(repo, &head_checkout_opts));

	cl_git_pass(git_reference_lookup(&theirs_ref, repo, theirs_branch));
	cl_git_pass(git_annotated_commit_from_ref(&theirs_head, repo, theirs_ref));

	cl_git_pass(git_merge(repo, (const git_annotated_commit **)&theirs_head, 1, merge_opts, checkout_opts));

	git_reference_free(head_ref);
	git_reference_free(theirs_ref);
	git_annotated_commit_free(theirs_head);

	return 0;
}
Exemple #22
0
/**
 * Perform a normal merge
 *
 * @param merge_result S4 class git_merge_result
 * @param merge_heads The merge heads to merge
 * @param n The number of merge heads
 * @param repository The repository
 * @param message The commit message of the merge
 * @param merger Who is performing the merge
 * @param commit_on_success Commit merge commit, if one was created
 * @param merge_opts Merge options
 * @return 0 on success, or error code
 */
static int git2r_normal_merge(
    SEXP merge_result,
    const git_annotated_commit **merge_heads,
    size_t n,
    git_repository *repository,
    const char *message,
    git_signature *merger,
    int commit_on_success,
    const git_checkout_options *checkout_opts,
    const git_merge_options *merge_opts)
{
    int err;
    git_commit *commit = NULL;
    git_index *index = NULL;

    SET_SLOT(merge_result, Rf_install("fast_forward"), ScalarLogical(0));

    err = git_merge(
        repository,
        merge_heads,
        n,
        merge_opts,
        checkout_opts);
    if (err)
        goto cleanup;

    err = git_repository_index(&index, repository);
    if (err)
        goto cleanup;

    if (git_index_has_conflicts(index)) {
        SET_SLOT(merge_result, Rf_install("conflicts"), ScalarLogical(1));
    } else {
        SET_SLOT(merge_result, Rf_install("conflicts"), ScalarLogical(0));

        if (commit_on_success) {
            char sha[GIT_OID_HEXSZ + 1];
            git_oid oid;

            err = git2r_commit_create(
                &oid,
                repository,
                index,
                message,
                merger,
                merger);
            if (err)
                goto cleanup;

            git_oid_fmt(sha, &oid);
            sha[GIT_OID_HEXSZ] = '\0';
            SET_SLOT(merge_result, Rf_install("sha"), mkString(sha));
        }
    }

cleanup:
    if (commit)
        git_commit_free(commit);

    if (index)
        git_index_free(index);

    return err;
}
Exemple #23
0
int main()
{
    git_libgit2_init();

    git_repository* rep = nullptr;

    const git_annotated_commit* their_head[10];
    git_merge_options merge_opt = GIT_MERGE_OPTIONS_INIT;
    git_checkout_options checkout_opt = GIT_CHECKOUT_OPTIONS_INIT;

    git_reference* branch = nullptr;
    git_index* index = nullptr;
    git_index_conflict_iterator* conflict_iterator = nullptr;
    git_oid new_tree_id;

    git_tree* new_tree = nullptr;
    git_signature* me = nullptr;
    git_reference* head = nullptr;
    git_commit* parent_our = nullptr;
    git_commit* parent_their = nullptr;
    git_oid commit_id;

    int error = 0;

    // git open
    error = git_repository_open(&rep, path);
    if (error < 0)
    {
        const git_error *e = giterr_last();
        std::cout << "Error: " << error << " / " << e->klass << " : " << e->message << std::endl;

        goto SHUTDOWN;
    }

    // git merge <branch>
    git_reference_dwim(&branch, rep, "new");
    git_annotated_commit_from_ref((git_annotated_commit **)&their_head[0], rep, branch);
    error = git_merge(rep, their_head, 1, &merge_opt, &checkout_opt);
    if (error < 0)
    {
        const git_error *e = giterr_last();
        std::cout << "Error: " << error << " / " << e->klass << " : " << e->message << std::endl;

        goto SHUTDOWN;
    }

    git_repository_index(&index, rep);

    // reslove conflicts
    if (git_index_has_conflicts(index))
    {
        const git_index_entry* ancestor_out = nullptr;
        const git_index_entry* our_out = nullptr;
        const git_index_entry* their_out = nullptr;

        git_index_conflict_iterator_new(&conflict_iterator, index);

        while (git_index_conflict_next(&ancestor_out, &our_out, &their_out, conflict_iterator) != GIT_ITEROVER)
        {
            if (ancestor_out) std::cout<< "ancestor: " << ancestor_out->path <<std::endl;
            if (our_out) std::cout<< "our: " << our_out->path <<std::endl;
            if (their_out) std::cout<< "their: " << their_out->path <<std::endl;

            // do sth. or rewrite file by code. ...
            // and remove the conflict
//            git_index_conflict_remove(index, "...");
        }

        // git checkout --theirs <file>
        git_checkout_options opt = GIT_CHECKOUT_OPTIONS_INIT;
        opt.checkout_strategy |= GIT_CHECKOUT_USE_THEIRS;
//        const char* p = "file";
//        opt.paths.strings = (char**)&p;
//        opt.paths.count = 1;
        git_checkout_index(rep, index, &opt);

        git_index_conflict_iterator_free(conflict_iterator);
    }

    // add and commit
    git_index_update_all(index, nullptr, nullptr, nullptr);
    git_index_write(index);
    git_index_write_tree(&new_tree_id, index);
    git_tree_lookup(&new_tree, rep, &new_tree_id);

    git_signature_now(&me, "XiaochenFTX", "*****@*****.**");

    git_repository_head(&head, rep);
    git_commit_lookup(&parent_our, rep, git_reference_target(head));
    git_commit_lookup(&parent_their, rep, git_reference_target(branch));

    git_commit_create_v(&commit_id, rep, "HEAD", me, me, "UTF-8", "merge commit", new_tree, 2, parent_our, parent_their);

    git_repository_state_cleanup(rep);

SHUTDOWN:
    git_repository_free(rep);
    git_libgit2_shutdown();
    return 0;
}
Exemple #24
0
int NotesModel::pull() {

    git_repository *repo = NULL;
    git_remote *remote = NULL;
    git_merge_head *merge_head = NULL;

    if (!QSettings().value("gitRemoteUrl").isValid()) {
        qDebug() << "gitRemoteUrl setting invalid";
        return false; }

    if (QSettings().value("gitRemoteUrl") == "")
        return false;

    try {
        //Open Repo
        e(git_repository_open(&repo, notesFolder().absolutePath().toUtf8().constData()));

        //List repository and add one from Settings if none exists or doesn t have same URI than settings
        git_strarray   remotes = {0};
        git_remote_list(&remotes, repo);
        if (remotes.count >= 1) {
            for (size_t i = 0; i < remotes.count; i++) {
                e(git_remote_load(&remote, repo, remotes.strings[i]));
                qDebug() << git_remote_url(remote);
                if (QSettings().value("gitRemoteUrl").isValid() && git_remote_url(remote)) {
                    if (strcmp(git_remote_url(remote),QSettings().value("gitRemoteUrl").toString().toUtf8().constData()) != 0) {
                        e(git_remote_delete(remote));
                        e(git_remote_create(&remote, repo, "upstream",
                                            QSettings().value("gitRemoteUrl").toString().toUtf8().constData()));
                        e(git_remote_save(remote));
                    }

                }
            }
        } else if (remotes.count == 0) {
            e(git_remote_create(&remote, repo, "upstream",
                                QSettings().value("gitRemoteUrl").toString().toUtf8().constData()));
            e(git_remote_save(remote));
        }

        e(git_remote_load(&remote, repo, "upstream"));

        git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
        callbacks.credentials = cred_acquire_cb;
        e(git_remote_set_callbacks(remote, &callbacks));

        e(git_remote_connect(remote, GIT_DIRECTION_FETCH));
        int connected = git_remote_connected(remote);
        if (connected)
            e(git_remote_fetch(remote, NULL, NULL));


        git_checkout_options checkout_opt = GIT_CHECKOUT_OPTIONS_INIT;
        checkout_opt.checkout_strategy = GIT_CHECKOUT_FORCE;
        checkout_opt.file_mode = 0644;
        git_merge_options merge_opt = GIT_MERGE_OPTIONS_INIT;
        merge_opt.file_favor = GIT_MERGE_FILE_FAVOR_UNION;

        const git_remote_head **head = NULL;
        size_t size = 0;
        e(git_remote_ls(&head, &size, remote));
        if (size <= 0)
            e(-1);

        git_oid oid = head[0]->oid;

        e(git_merge_head_from_fetchhead(&merge_head, repo, "master", git_remote_url(remote), &oid));
        e(git_merge(repo, (const git_merge_head **)(&merge_head), 1, &merge_opt, &checkout_opt));

        //TRY TO COMMIT
        /* Create signature */
        git_signature *me = NULL;
        e(git_signature_now(&me, "sparkleNotes", "*****@*****.**"));

        //Tree Lookup
        git_object *tree_obj;
        e(git_revparse_single(&tree_obj, repo, "HEAD^{tree}"));

        // Get parent commit
        git_oid parentCommitId;
        git_commit *parent;
        git_oid remoteParentCommitId;
        git_commit *remoteParent;
        e(git_reference_name_to_id( &parentCommitId, repo, "ORIG_HEAD" ));
        e(git_commit_lookup( &parent, repo, &parentCommitId ));
        e(git_reference_name_to_id( &remoteParentCommitId, repo, "MERGE_HEAD" ));
        e(git_commit_lookup( &remoteParent, repo, &remoteParentCommitId ));

        if (remoteParent == parent) {
            //Same commit ... nothing to merge
            e(git_repository_state_cleanup(repo));
            git_merge_head_free(merge_head);
            git_remote_free(remote);
            git_repository_free(repo);
            return false;
        }
        const git_commit *parents [2] = { parent, remoteParent };

        git_oid new_commit_id;
        e(git_commit_create(
              &new_commit_id,
              repo,
              "HEAD",                           /* name of ref to update */
              me,                               /* author */
              me,                               /* committer */
              "UTF-8",                          /* message encoding */
              "Merge remote upstream/master",   /* message */
              (git_tree *) tree_obj,            /* root tree */
              2,                                /* parent count */
              parents));                        /* parents */


        git_merge_head_free(merge_head);
        git_remote_free(remote);

        e(git_repository_state_cleanup(repo));

        git_repository_free(repo);

    }

    catch (int error) {
        const git_error *err = giterr_last();
        if (err != NULL)
            qDebug() << QString::number(err->klass) + "\t" + QString(err->message);
        emit this->error(QString(err->message));
        giterr_clear();
        git_merge_head_free(merge_head);
        git_remote_free(remote);
        git_repository_free(repo);
    }
    return true;
}