Merge pull request #2235 from jacquesg/cherry-pick

Add cherry pick support
This commit is contained in:
Vicent Marti 2014-04-15 20:26:23 +02:00
commit 0f7aa47dbd
102 changed files with 923 additions and 2 deletions

View File

@ -25,6 +25,7 @@ Florian Forster
Holger Weiss
Ingmar Vanhassel
J. David Ibáñez
Jacques Germishuys
Jakob Pfender
Jason Penny
Jason R. McNeil

View File

@ -14,6 +14,7 @@
#include "git2/branch.h"
#include "git2/buffer.h"
#include "git2/checkout.h"
#include "git2/cherrypick.h"
#include "git2/clone.h"
#include "git2/commit.h"
#include "git2/common.h"

88
include/git2/cherrypick.h Normal file
View File

@ -0,0 +1,88 @@
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_git_cherrypick_h__
#define INCLUDE_git_cherrypick_h__
#include "common.h"
#include "types.h"
#include "merge.h"
/**
* @file git2/cherrypick.h
* @brief Git cherry-pick routines
* @defgroup git_cherrypick Git cherry-pick routines
* @ingroup Git
* @{
*/
GIT_BEGIN_DECL
typedef struct {
unsigned int version;
/** For merge commits, the "mainline" is treated as the parent. */
unsigned int mainline;
git_merge_options merge_opts;
git_checkout_options checkout_opts;
} git_cherry_pick_options;
#define GIT_CHERRY_PICK_OPTIONS_VERSION 1
#define GIT_CHERRY_PICK_OPTIONS_INIT {GIT_CHERRY_PICK_OPTIONS_VERSION, 0, GIT_MERGE_OPTIONS_INIT, GIT_CHECKOUT_OPTIONS_INIT}
/**
* Initializes a `git_cherry_pick_options` with default values. Equivalent to
* creating an instance with GIT_CHERRY_PICK_OPTIONS_INIT.
*
* @param opts the `git_cherry_pick_options` instance to initialize.
* @param version the version of the struct; you should pass
* `GIT_CHERRY_PICK_OPTIONS_VERSION` here.
* @return Zero on success; -1 on failure.
*/
GIT_EXTERN(int) git_cherry_pick_init_opts(
git_cherry_pick_options* opts,
int version);
/**
* Cherry-picks the given commit against the given "our" commit, producing an
* index that reflects the result of the cherry-pick.
*
* The returned index must be freed explicitly with `git_index_free`.
*
* @param out pointer to store the index result in
* @param repo the repository that contains the given commits
* @param cherry_pick_commit the commit to cherry-pick
* @param our_commit the commit to revert against (eg, HEAD)
* @param mainline the parent of the revert commit, if it is a merge
* @param merge_tree_opts the merge tree options (or null for defaults)
* @return zero on success, -1 on failure.
*/
GIT_EXTERN(int) git_cherry_pick_commit(
git_index **out,
git_repository *repo,
git_commit *cherry_pick_commit,
git_commit *our_commit,
unsigned int mainline,
const git_merge_options *merge_options);
/**
* Cherry-pick the given commit, producing changes in the index and working directory.
*
* @param repo the repository to cherry-pick
* @param commit the commit to cherry-pick
* @param cherry_pick_options the cherry-pick options (or null for defaults)
* @return zero on success, -1 on failure.
*/
GIT_EXTERN(int) git_cherry_pick(
git_repository *repo,
git_commit *commit,
const git_cherry_pick_options *cherry_pick_options);
/** @} */
GIT_END_DECL
#endif

View File

@ -86,6 +86,7 @@ typedef enum {
GITERR_FILTER,
GITERR_REVERT,
GITERR_CALLBACK,
GITERR_CHERRYPICK,
} git_error_t;
/**

230
src/cherrypick.c Normal file
View File

@ -0,0 +1,230 @@
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "repository.h"
#include "filebuf.h"
#include "merge.h"
#include "vector.h"
#include "git2/types.h"
#include "git2/merge.h"
#include "git2/cherrypick.h"
#include "git2/commit.h"
#include "git2/sys/commit.h"
#define GIT_CHERRY_PICK_FILE_MODE 0666
static int write_cherry_pick_head(
git_repository *repo,
const char *commit_oidstr)
{
git_filebuf file = GIT_FILEBUF_INIT;
git_buf file_path = GIT_BUF_INIT;
int error = 0;
if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_CHERRY_PICK_HEAD_FILE)) >= 0 &&
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_CHERRY_PICK_FILE_MODE)) >= 0 &&
(error = git_filebuf_printf(&file, "%s\n", commit_oidstr)) >= 0)
error = git_filebuf_commit(&file);
if (error < 0)
git_filebuf_cleanup(&file);
git_buf_free(&file_path);
return error;
}
static int write_merge_msg(
git_repository *repo,
const char *commit_msg)
{
git_filebuf file = GIT_FILEBUF_INIT;
git_buf file_path = GIT_BUF_INIT;
int error = 0;
if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_CHERRY_PICK_FILE_MODE)) < 0 ||
(error = git_filebuf_printf(&file, "%s", commit_msg)) < 0)
goto cleanup;
error = git_filebuf_commit(&file);
cleanup:
if (error < 0)
git_filebuf_cleanup(&file);
git_buf_free(&file_path);
return error;
}
static int cherry_pick_normalize_opts(
git_repository *repo,
git_cherry_pick_options *opts,
const git_cherry_pick_options *given,
const char *their_label)
{
int error = 0;
unsigned int default_checkout_strategy = GIT_CHECKOUT_SAFE_CREATE |
GIT_CHECKOUT_ALLOW_CONFLICTS;
GIT_UNUSED(repo);
if (given != NULL)
memcpy(opts, given, sizeof(git_cherry_pick_options));
else {
git_cherry_pick_options default_opts = GIT_CHERRY_PICK_OPTIONS_INIT;
memcpy(opts, &default_opts, sizeof(git_cherry_pick_options));
}
if (!opts->checkout_opts.checkout_strategy)
opts->checkout_opts.checkout_strategy = default_checkout_strategy;
if (!opts->checkout_opts.our_label)
opts->checkout_opts.our_label = "HEAD";
if (!opts->checkout_opts.their_label)
opts->checkout_opts.their_label = their_label;
return error;
}
static int cherry_pick_state_cleanup(git_repository *repo)
{
const char *state_files[] = { GIT_CHERRY_PICK_HEAD_FILE, GIT_MERGE_MSG_FILE };
return git_repository__cleanup_files(repo, state_files, ARRAY_SIZE(state_files));
}
static int cherry_pick_seterr(git_commit *commit, const char *fmt)
{
char commit_oidstr[GIT_OID_HEXSZ + 1];
giterr_set(GITERR_CHERRYPICK, fmt,
git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit)));
return -1;
}
int git_cherry_pick_commit(
git_index **out,
git_repository *repo,
git_commit *cherry_pick_commit,
git_commit *our_commit,
unsigned int mainline,
const git_merge_options *merge_opts)
{
git_commit *parent_commit = NULL;
git_tree *parent_tree = NULL, *our_tree = NULL, *cherry_pick_tree = NULL;
int parent = 0, error = 0;
assert(out && repo && cherry_pick_commit && our_commit);
if (git_commit_parentcount(cherry_pick_commit) > 1) {
if (!mainline)
return cherry_pick_seterr(cherry_pick_commit,
"Mainline branch is not specified but %s is a merge commit");
parent = mainline;
} else {
if (mainline)
return cherry_pick_seterr(cherry_pick_commit,
"Mainline branch specified but %s is not a merge commit");
parent = git_commit_parentcount(cherry_pick_commit);
}
if (parent &&
((error = git_commit_parent(&parent_commit, cherry_pick_commit, (parent - 1))) < 0 ||
(error = git_commit_tree(&parent_tree, parent_commit)) < 0))
goto done;
if ((error = git_commit_tree(&cherry_pick_tree, cherry_pick_commit)) < 0 ||
(error = git_commit_tree(&our_tree, our_commit)) < 0)
goto done;
error = git_merge_trees(out, repo, parent_tree, our_tree, cherry_pick_tree, merge_opts);
done:
git_tree_free(parent_tree);
git_tree_free(our_tree);
git_tree_free(cherry_pick_tree);
git_commit_free(parent_commit);
return error;
}
int git_cherry_pick(
git_repository *repo,
git_commit *commit,
const git_cherry_pick_options *given_opts)
{
git_cherry_pick_options opts;
git_reference *our_ref = NULL;
git_commit *our_commit = NULL;
char commit_oidstr[GIT_OID_HEXSZ + 1];
const char *commit_msg, *commit_summary;
git_buf their_label = GIT_BUF_INIT;
git_index *index_new = NULL, *index_repo = NULL;
int error = 0;
assert(repo && commit);
GITERR_CHECK_VERSION(given_opts, GIT_CHERRY_PICK_OPTIONS_VERSION, "git_cherry_pick_options");
if ((error = git_repository__ensure_not_bare(repo, "cherry-pick")) < 0)
return error;
if ((commit_msg = git_commit_message(commit)) == NULL ||
(commit_summary = git_commit_summary(commit)) == NULL) {
error = -1;
goto on_error;
}
git_oid_fmt(commit_oidstr, git_commit_id(commit));
if ((error = write_merge_msg(repo, commit_msg)) < 0 ||
(error = git_buf_printf(&their_label, "%.7s... %s", commit_oidstr, commit_summary)) < 0 ||
(error = cherry_pick_normalize_opts(repo, &opts, given_opts, git_buf_cstr(&their_label))) < 0 ||
(error = write_cherry_pick_head(repo, commit_oidstr)) < 0 ||
(error = git_repository_head(&our_ref, repo)) < 0 ||
(error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJ_COMMIT)) < 0 ||
(error = git_cherry_pick_commit(&index_new, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
(error = git_merge__indexes(repo, index_new)) < 0 ||
(error = git_repository_index(&index_repo, repo)) < 0 ||
(error = git_merge__append_conflicts_to_merge_msg(repo, index_repo)) < 0 ||
(error = git_checkout_index(repo, index_repo, &opts.checkout_opts)) < 0)
goto on_error;
goto done;
on_error:
cherry_pick_state_cleanup(repo);
done:
git_index_free(index_new);
git_index_free(index_repo);
git_commit_free(our_commit);
git_reference_free(our_ref);
git_buf_free(&their_label);
return error;
}
int git_cherry_pick_init_opts(git_cherry_pick_options* opts, int version)
{
if (version != GIT_CHERRY_PICK_OPTIONS_VERSION) {
giterr_set(GITERR_INVALID, "Invalid version %d for git_cherry_pick_options", version);
return -1;
} else {
git_cherry_pick_options o = GIT_CHERRY_PICK_OPTIONS_INIT;
memcpy(opts, &o, sizeof(o));
return 0;
}
}

View File

@ -2469,6 +2469,47 @@ done:
return error;
}
int git_merge__append_conflicts_to_merge_msg(
git_repository *repo,
git_index *index)
{
git_filebuf file = GIT_FILEBUF_INIT;
git_buf file_path = GIT_BUF_INIT;
const char *last = NULL;
size_t i;
int error;
if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_APPEND, GIT_MERGE_FILE_MODE)) < 0)
goto cleanup;
if (git_index_has_conflicts(index))
git_filebuf_printf(&file, "\nConflicts:\n");
for (i = 0; i < git_index_entrycount(index); i++) {
const git_index_entry *e = git_index_get_byindex(index, i);
if (git_index_entry_stage(e) == 0)
continue;
if (last == NULL || strcmp(e->path, last) != 0)
git_filebuf_printf(&file, "\t%s\n", e->path);
last = e->path;
}
error = git_filebuf_commit(&file);
cleanup:
if (error < 0)
git_filebuf_cleanup(&file);
git_buf_free(&file_path);
return error;
}
static int merge_state_cleanup(git_repository *repo)
{
const char *state_files[] = {
@ -2621,6 +2662,7 @@ int git_merge(
if ((error = git_merge_trees(&index_new, repo, ancestor_tree, our_tree, their_trees[0], merge_opts)) < 0 ||
(error = git_merge__indexes(repo, index_new)) < 0 ||
(error = git_repository_index(&index_repo, repo)) < 0 ||
(error = git_merge__append_conflicts_to_merge_msg(repo, index_repo)) < 0 ||
(error = git_checkout_index(repo, index_repo, &checkout_opts)) < 0)
goto on_error;

View File

@ -151,4 +151,6 @@ int git_merge__setup(
int git_merge__indexes(git_repository *repo, git_index *index_new);
int git_merge__append_conflicts_to_merge_msg(git_repository *repo, git_index *index);
#endif

View File

@ -201,6 +201,7 @@ int git_revert(
(error = git_revert_commit(&index_new, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 ||
(error = git_merge__indexes(repo, index_new)) < 0 ||
(error = git_repository_index(&index_repo, repo)) < 0 ||
(error = git_merge__append_conflicts_to_merge_msg(repo, index_repo)) < 0 ||
(error = git_checkout_index(repo, index_repo, &opts.checkout_opts)) < 0)
goto on_error;

106
tests/cherrypick/bare.c Normal file
View File

@ -0,0 +1,106 @@
#include "clar.h"
#include "clar_libgit2.h"
#include "buffer.h"
#include "fileops.h"
#include "git2/cherrypick.h"
#include "../merge/merge_helpers.h"
#define TEST_REPO_PATH "cherrypick"
static git_repository *repo;
void test_cherrypick_bare__initialize(void)
{
repo = cl_git_sandbox_init(TEST_REPO_PATH);
}
void test_cherrypick_bare__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_cherrypick_bare__automerge(void)
{
git_commit *head = NULL, *commit = NULL;
git_index *index = NULL;
git_oid head_oid, cherry_oid;
struct merge_index_entry merge_index_entries[] = {
{ 0100644, "38c05a857e831a7e759d83778bfc85d003e21c45", 0, "file1.txt" },
{ 0100644, "a661b5dec1004e2c62654ded3762370c27cf266b", 0, "file2.txt" },
{ 0100644, "df6b290e0bd6a89b01d69f66687e8abf385283ca", 0, "file3.txt" },
};
git_oid_fromstr(&head_oid, "d3d77487660ee3c0194ee01dc5eaf478782b1c7e");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
git_oid_fromstr(&cherry_oid, "cfc4f0999a8367568e049af4f72e452d40828a15");
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_git_pass(git_cherry_pick_commit(&index, repo, commit, head, 0, NULL));
cl_assert(merge_test_index(index, merge_index_entries, 3));
git_index_free(index);
git_commit_free(head);
git_commit_free(commit);
}
void test_cherrypick_bare__conflicts(void)
{
git_commit *head = NULL, *commit = NULL;
git_index *index = NULL;
git_oid head_oid, cherry_oid;
struct merge_index_entry merge_index_entries[] = {
{ 0100644, "242e7977ba73637822ffb265b46004b9b0e5153b", 0, "file1.txt" },
{ 0100644, "a58ca3fee5eb68b11adc2703e5843f968c9dad1e", 1, "file2.txt" },
{ 0100644, "bd6ffc8c6c41f0f85ff9e3d61c9479516bac0024", 2, "file2.txt" },
{ 0100644, "563f6473a3858f99b80e5f93c660512ed38e1e6f", 3, "file2.txt" },
{ 0100644, "28d9eb4208074ad1cc84e71ccc908b34573f05d2", 1, "file3.txt" },
{ 0100644, "1124c2c1ae07b26fded662d6c3f3631d9dc16f88", 2, "file3.txt" },
{ 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 3, "file3.txt" },
};
git_oid_fromstr(&head_oid, "bafbf6912c09505ac60575cd43d3f2aba3bd84d8");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
git_oid_fromstr(&cherry_oid, "e9b63f3655b2ad80c0ff587389b5a9589a3a7110");
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_git_pass(git_cherry_pick_commit(&index, repo, commit, head, 0, NULL));
cl_assert(merge_test_index(index, merge_index_entries, 7));
git_index_free(index);
git_commit_free(head);
git_commit_free(commit);
}
void test_cherrypick_bare__orphan(void)
{
git_commit *head = NULL, *commit = NULL;
git_index *index = NULL;
git_oid head_oid, cherry_oid;
struct merge_index_entry merge_index_entries[] = {
{ 0100644, "38c05a857e831a7e759d83778bfc85d003e21c45", 0, "file1.txt" },
{ 0100644, "a661b5dec1004e2c62654ded3762370c27cf266b", 0, "file2.txt" },
{ 0100644, "85a4a1d791973644f24c72f5e89420d3064cc452", 0, "file3.txt" },
{ 0100644, "9ccb9bf50c011fd58dcbaa65df917bf79539717f", 0, "orphan.txt" },
};
git_oid_fromstr(&head_oid, "d3d77487660ee3c0194ee01dc5eaf478782b1c7e");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
git_oid_fromstr(&cherry_oid, "74f06b5bfec6d33d7264f73606b57a7c0b963819");
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_git_pass(git_cherry_pick_commit(&index, repo, commit, head, 0, NULL));
cl_assert(merge_test_index(index, merge_index_entries, 4));
git_index_free(index);
git_commit_free(head);
git_commit_free(commit);
}

429
tests/cherrypick/workdir.c Normal file
View File

@ -0,0 +1,429 @@
#include "clar.h"
#include "clar_libgit2.h"
#include "buffer.h"
#include "fileops.h"
#include "git2/cherrypick.h"
#include "../merge/merge_helpers.h"
#define TEST_REPO_PATH "cherrypick"
static git_repository *repo;
static git_index *repo_index;
// Fixture setup and teardown
void test_cherrypick_workdir__initialize(void)
{
repo = cl_git_sandbox_init(TEST_REPO_PATH);
git_repository_index(&repo_index, repo);
}
void test_cherrypick_workdir__cleanup(void)
{
git_index_free(repo_index);
cl_git_sandbox_cleanup();
}
/* git reset --hard d3d77487660ee3c0194ee01dc5eaf478782b1c7e
* git cherry-pick cfc4f0999a8367568e049af4f72e452d40828a15
* git cherry-pick 964ea3da044d9083181a88ba6701de9e35778bf4
* git cherry-pick a43a050c588d4e92f11a6b139680923e9728477d
*/
void test_cherrypick_workdir__automerge(void)
{
git_oid head_oid;
git_signature *signature = NULL;
size_t i;
const char *cherry_pick_oids[] = {
"cfc4f0999a8367568e049af4f72e452d40828a15",
"964ea3da044d9083181a88ba6701de9e35778bf4",
"a43a050c588d4e92f11a6b139680923e9728477d",
};
struct merge_index_entry merge_index_entries[] = {
{ 0100644, "38c05a857e831a7e759d83778bfc85d003e21c45", 0, "file1.txt" },
{ 0100644, "a661b5dec1004e2c62654ded3762370c27cf266b", 0, "file2.txt" },
{ 0100644, "df6b290e0bd6a89b01d69f66687e8abf385283ca", 0, "file3.txt" },
{ 0100644, "38c05a857e831a7e759d83778bfc85d003e21c45", 0, "file1.txt" },
{ 0100644, "bd8fc3c59fb52d3c8b5907ace7defa5803f82419", 0, "file2.txt" },
{ 0100644, "df6b290e0bd6a89b01d69f66687e8abf385283ca", 0, "file3.txt" },
{ 0100644, "f06427bee380364bc7e0cb26a9245158e4726ce0", 0, "file1.txt" },
{ 0100644, "bd8fc3c59fb52d3c8b5907ace7defa5803f82419", 0, "file2.txt" },
{ 0100644, "df6b290e0bd6a89b01d69f66687e8abf385283ca", 0, "file3.txt" },
};
cl_git_pass(git_signature_new(&signature, "Picker", "picker@example.org", time(NULL), 0));
git_oid_fromstr(&head_oid, "d3d77487660ee3c0194ee01dc5eaf478782b1c7e");
for (i = 0; i < 3; ++i) {
git_commit *head = NULL, *commit = NULL;
git_oid cherry_oid, cherry_picked_oid, cherry_picked_tree_oid;
git_tree *cherry_picked_tree = NULL;
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&cherry_oid, cherry_pick_oids[i]);
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_git_pass(git_cherry_pick(repo, commit, NULL));
cl_assert(git_path_exists(TEST_REPO_PATH "/.git/CHERRY_PICK_HEAD"));
cl_assert(git_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG"));
cl_git_pass(git_index_write_tree(&cherry_picked_tree_oid, repo_index));
cl_git_pass(git_tree_lookup(&cherry_picked_tree, repo, &cherry_picked_tree_oid));
cl_git_pass(git_commit_create(&cherry_picked_oid, repo, "HEAD", signature, signature, NULL,
"Cherry picked!", cherry_picked_tree, 1, (const git_commit **)&head));
cl_assert(merge_test_index(repo_index, merge_index_entries + i * 3, 3));
git_oid_cpy(&head_oid, &cherry_picked_oid);
git_tree_free(cherry_picked_tree);
git_commit_free(head);
git_commit_free(commit);
}
git_signature_free(signature);
}
/* git reset --hard bafbf6912c09505ac60575cd43d3f2aba3bd84d8
* git cherry-pick e9b63f3655b2ad80c0ff587389b5a9589a3a7110
*/
void test_cherrypick_workdir__conflicts(void)
{
git_commit *head = NULL, *commit = NULL;
git_oid head_oid, cherry_oid;
git_buf conflicting_buf = GIT_BUF_INIT, mergemsg_buf = GIT_BUF_INIT;
struct merge_index_entry merge_index_entries[] = {
{ 0100644, "242e7977ba73637822ffb265b46004b9b0e5153b", 0, "file1.txt" },
{ 0100644, "a58ca3fee5eb68b11adc2703e5843f968c9dad1e", 1, "file2.txt" },
{ 0100644, "bd6ffc8c6c41f0f85ff9e3d61c9479516bac0024", 2, "file2.txt" },
{ 0100644, "563f6473a3858f99b80e5f93c660512ed38e1e6f", 3, "file2.txt" },
{ 0100644, "28d9eb4208074ad1cc84e71ccc908b34573f05d2", 1, "file3.txt" },
{ 0100644, "1124c2c1ae07b26fded662d6c3f3631d9dc16f88", 2, "file3.txt" },
{ 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 3, "file3.txt" },
};
git_oid_fromstr(&head_oid, "bafbf6912c09505ac60575cd43d3f2aba3bd84d8");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&cherry_oid, "e9b63f3655b2ad80c0ff587389b5a9589a3a7110");
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_git_pass(git_cherry_pick(repo, commit, NULL));
cl_assert(git_path_exists(TEST_REPO_PATH "/.git/CHERRY_PICK_HEAD"));
cl_assert(git_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG"));
cl_assert(merge_test_index(repo_index, merge_index_entries, 7));
cl_git_pass(git_futils_readbuffer(&mergemsg_buf,
TEST_REPO_PATH "/.git/MERGE_MSG"));
cl_assert(strcmp(git_buf_cstr(&mergemsg_buf),
"Change all files\n" \
"\n" \
"Conflicts:\n" \
"\tfile2.txt\n" \
"\tfile3.txt\n") == 0);
cl_git_pass(git_futils_readbuffer(&conflicting_buf,
TEST_REPO_PATH "/file2.txt"));
cl_assert(strcmp(git_buf_cstr(&conflicting_buf),
"!File 2\n" \
"File 2\n" \
"File 2\n" \
"File 2\n" \
"File 2\n" \
"File 2\n" \
"File 2\n" \
"File 2\n" \
"File 2\n" \
"File 2\n" \
"File 2!!\n" \
"File 2\n" \
"File 2\n" \
"File 2\n" \
"<<<<<<< HEAD\n" \
"File 2\n" \
"=======\n" \
"File 2!\n" \
"File 2\n" \
"File 2!\n" \
">>>>>>> e9b63f3... Change all files\n") == 0);
cl_git_pass(git_futils_readbuffer(&conflicting_buf,
TEST_REPO_PATH "/file3.txt"));
cl_assert(strcmp(git_buf_cstr(&conflicting_buf),
"!File 3\n" \
"File 3\n" \
"File 3\n" \
"File 3\n" \
"File 3\n" \
"File 3\n" \
"File 3\n" \
"File 3\n" \
"File 3\n" \
"File 3\n" \
"File 3\n" \
"File 3!!\n" \
"File 3\n" \
"File 3\n" \
"File 3\n" \
"<<<<<<< HEAD\n" \
"=======\n" \
"File 3!\n" \
"File 3!\n" \
">>>>>>> e9b63f3... Change all files\n") == 0);
git_commit_free(commit);
git_commit_free(head);
git_buf_free(&mergemsg_buf);
git_buf_free(&conflicting_buf);
}
/* git reset --hard bafbf6912c09505ac60575cd43d3f2aba3bd84d8
* git cherry-pick -X ours e9b63f3655b2ad80c0ff587389b5a9589a3a7110
*/
void test_cherrypick_workdir__conflict_use_ours(void)
{
git_commit *head = NULL, *commit = NULL;
git_oid head_oid, cherry_oid;
git_cherry_pick_options opts = GIT_CHERRY_PICK_OPTIONS_INIT;
struct merge_index_entry merge_index_entries[] = {
{ 0100644, "242e7977ba73637822ffb265b46004b9b0e5153b", 0, "file1.txt" },
{ 0100644, "a58ca3fee5eb68b11adc2703e5843f968c9dad1e", 1, "file2.txt" },
{ 0100644, "bd6ffc8c6c41f0f85ff9e3d61c9479516bac0024", 2, "file2.txt" },
{ 0100644, "563f6473a3858f99b80e5f93c660512ed38e1e6f", 3, "file2.txt" },
{ 0100644, "28d9eb4208074ad1cc84e71ccc908b34573f05d2", 1, "file3.txt" },
{ 0100644, "1124c2c1ae07b26fded662d6c3f3631d9dc16f88", 2, "file3.txt" },
{ 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 3, "file3.txt" },
};
struct merge_index_entry merge_filesystem_entries[] = {
{ 0100644, "242e7977ba73637822ffb265b46004b9b0e5153b", 0, "file1.txt" },
{ 0100644, "bd6ffc8c6c41f0f85ff9e3d61c9479516bac0024", 0, "file2.txt" },
{ 0100644, "1124c2c1ae07b26fded662d6c3f3631d9dc16f88", 0, "file3.txt" },
};
/* leave the index in a conflicted state, but checkout "ours" to the workdir */
opts.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_USE_OURS;
git_oid_fromstr(&head_oid, "bafbf6912c09505ac60575cd43d3f2aba3bd84d8");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&cherry_oid, "e9b63f3655b2ad80c0ff587389b5a9589a3a7110");
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_git_pass(git_cherry_pick(repo, commit, &opts));
cl_assert(merge_test_index(repo_index, merge_index_entries, 7));
cl_assert(merge_test_workdir(repo, merge_filesystem_entries, 3));
/* resolve conflicts in the index by taking "ours" */
opts.merge_opts.file_favor = GIT_MERGE_FILE_FAVOR_OURS;
cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
cl_git_pass(git_cherry_pick(repo, commit, &opts));
cl_assert(merge_test_index(repo_index, merge_filesystem_entries, 3));
cl_assert(merge_test_workdir(repo, merge_filesystem_entries, 3));
git_commit_free(commit);
git_commit_free(head);
}
/* git reset --hard cfc4f0999a8367568e049af4f72e452d40828a15
* git cherry-pick 2a26c7e88b285613b302ba76712bc998863f3cbc
*/
void test_cherrypick_workdir__rename(void)
{
git_commit *head, *commit;
git_oid head_oid, cherry_oid;
git_cherry_pick_options opts = GIT_CHERRY_PICK_OPTIONS_INIT;
struct merge_index_entry merge_index_entries[] = {
{ 0100644, "19c5c7207054604b69c84d08a7571ef9672bb5c2", 0, "file1.txt" },
{ 0100644, "a58ca3fee5eb68b11adc2703e5843f968c9dad1e", 0, "file2.txt" },
{ 0100644, "28d9eb4208074ad1cc84e71ccc908b34573f05d2", 0, "file3.txt.renamed" },
};
opts.merge_opts.flags |= GIT_MERGE_TREE_FIND_RENAMES;
opts.merge_opts.rename_threshold = 50;
git_oid_fromstr(&head_oid, "cfc4f0999a8367568e049af4f72e452d40828a15");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&cherry_oid, "2a26c7e88b285613b302ba76712bc998863f3cbc");
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_git_pass(git_cherry_pick(repo, commit, &opts));
cl_assert(merge_test_index(repo_index, merge_index_entries, 3));
git_commit_free(commit);
git_commit_free(head);
}
/* git reset --hard 44cd2ed2052c9c68f9a439d208e9614dc2a55c70
* git cherry-pick 2a26c7e88b285613b302ba76712bc998863f3cbc
*/
void test_cherrypick_workdir__both_renamed(void)
{
git_commit *head, *commit;
git_oid head_oid, cherry_oid;
git_buf mergemsg_buf = GIT_BUF_INIT;
git_cherry_pick_options opts = GIT_CHERRY_PICK_OPTIONS_INIT;
struct merge_index_entry merge_index_entries[] = {
{ 0100644, "19c5c7207054604b69c84d08a7571ef9672bb5c2", 0, "file1.txt" },
{ 0100644, "a58ca3fee5eb68b11adc2703e5843f968c9dad1e", 0, "file2.txt" },
{ 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 1, "file3.txt" },
{ 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 3, "file3.txt.renamed" },
{ 0100644, "28d9eb4208074ad1cc84e71ccc908b34573f05d2", 2, "file3.txt.renamed_on_branch" },
};
opts.merge_opts.flags |= GIT_MERGE_TREE_FIND_RENAMES;
opts.merge_opts.rename_threshold = 50;
git_oid_fromstr(&head_oid, "44cd2ed2052c9c68f9a439d208e9614dc2a55c70");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&cherry_oid, "2a26c7e88b285613b302ba76712bc998863f3cbc");
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_git_pass(git_cherry_pick(repo, commit, &opts));
cl_assert(merge_test_index(repo_index, merge_index_entries, 5));
cl_git_pass(git_futils_readbuffer(&mergemsg_buf,
TEST_REPO_PATH "/.git/MERGE_MSG"));
cl_assert(strcmp(git_buf_cstr(&mergemsg_buf),
"Renamed file3.txt -> file3.txt.renamed\n" \
"\n" \
"Conflicts:\n" \
"\tfile3.txt\n" \
"\tfile3.txt.renamed\n" \
"\tfile3.txt.renamed_on_branch\n") == 0);
git_buf_free(&mergemsg_buf);
git_commit_free(commit);
git_commit_free(head);
}
void test_cherrypick_workdir__nonmerge_fails_mainline_specified(void)
{
git_reference *head;
git_commit *commit;
git_cherry_pick_options opts = GIT_CHERRY_PICK_OPTIONS_INIT;
cl_git_pass(git_repository_head(&head, repo));
cl_git_pass(git_reference_peel((git_object **)&commit, head, GIT_OBJ_COMMIT));
opts.mainline = 1;
cl_must_fail(git_cherry_pick(repo, commit, &opts));
cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/CHERRY_PICK_HEAD"));
cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG"));
git_reference_free(head);
git_commit_free(commit);
}
/* git reset --hard cfc4f0999a8367568e049af4f72e452d40828a15
* git cherry-pick abe4603bc7cd5b8167a267e0e2418fd2348f8cff
*/
void test_cherrypick_workdir__merge_fails_without_mainline_specified(void)
{
git_commit *head, *commit;
git_oid head_oid, cherry_oid;
git_oid_fromstr(&head_oid, "cfc4f0999a8367568e049af4f72e452d40828a15");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&cherry_oid, "abe4603bc7cd5b8167a267e0e2418fd2348f8cff");
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_must_fail(git_cherry_pick(repo, commit, NULL));
cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/CHERRY_PICK_HEAD"));
cl_assert(!git_path_exists(TEST_REPO_PATH "/.git/MERGE_MSG"));
git_commit_free(commit);
git_commit_free(head);
}
/* git reset --hard cfc4f0999a8367568e049af4f72e452d40828a15
* git cherry-pick -m1 abe4603bc7cd5b8167a267e0e2418fd2348f8cff
*/
void test_cherrypick_workdir__merge_first_parent(void)
{
git_commit *head, *commit;
git_oid head_oid, cherry_oid;
git_cherry_pick_options opts = GIT_CHERRY_PICK_OPTIONS_INIT;
struct merge_index_entry merge_index_entries[] = {
{ 0100644, "f90f9dcbdac2cce5cc166346160e19cb693ef4e8", 0, "file1.txt" },
{ 0100644, "563f6473a3858f99b80e5f93c660512ed38e1e6f", 0, "file2.txt" },
{ 0100644, "e233b9ed408a95e9d4b65fec7fc34943a556deb2", 0, "file3.txt" },
};
opts.mainline = 1;
git_oid_fromstr(&head_oid, "cfc4f0999a8367568e049af4f72e452d40828a15");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&cherry_oid, "abe4603bc7cd5b8167a267e0e2418fd2348f8cff");
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_git_pass(git_cherry_pick(repo, commit, &opts));
cl_assert(merge_test_index(repo_index, merge_index_entries, 3));
git_commit_free(commit);
git_commit_free(head);
}
/* git reset --hard cfc4f0999a8367568e049af4f72e452d40828a15
* git cherry-pick -m2 abe4603bc7cd5b8167a267e0e2418fd2348f8cff
*/
void test_cherrypick_workdir__merge_second_parent(void)
{
git_commit *head, *commit;
git_oid head_oid, cherry_oid;
git_cherry_pick_options opts = GIT_CHERRY_PICK_OPTIONS_INIT;
struct merge_index_entry merge_index_entries[] = {
{ 0100644, "487434cace79238a7091e2220611d4f20a765690", 0, "file1.txt" },
{ 0100644, "e5183bfd18e3a0a691fadde2f0d5610b73282d31", 0, "file2.txt" },
{ 0100644, "409a1bec58bf35348e8b62b72bb9c1f45cf5a587", 0, "file3.txt" },
};
opts.mainline = 2;
git_oid_fromstr(&head_oid, "cfc4f0999a8367568e049af4f72e452d40828a15");
cl_git_pass(git_commit_lookup(&head, repo, &head_oid));
cl_git_pass(git_reset(repo, (git_object *)head, GIT_RESET_HARD, NULL, NULL));
git_oid_fromstr(&cherry_oid, "abe4603bc7cd5b8167a267e0e2418fd2348f8cff");
cl_git_pass(git_commit_lookup(&commit, repo, &cherry_oid));
cl_git_pass(git_cherry_pick(repo, commit, &opts));
cl_assert(merge_test_index(repo_index, merge_index_entries, 3));
git_commit_free(commit);
git_commit_free(head);
}

View File

@ -214,7 +214,7 @@ void test_merge_workdir_simple__automerge_crlf(void)
void test_merge_workdir_simple__mergefile(void)
{
git_buf conflicting_buf = GIT_BUF_INIT;
git_buf conflicting_buf = GIT_BUF_INIT, mergemsg_buf = GIT_BUF_INIT;
struct merge_index_entry merge_index_entries[] = {
ADDED_IN_MASTER_INDEX_ENTRY,
@ -240,7 +240,15 @@ void test_merge_workdir_simple__mergefile(void)
cl_git_pass(git_futils_readbuffer(&conflicting_buf,
TEST_REPO_PATH "/conflicting.txt"));
cl_assert(strcmp(conflicting_buf.ptr, CONFLICTING_MERGE_FILE) == 0);
cl_git_pass(git_futils_readbuffer(&mergemsg_buf,
TEST_REPO_PATH "/.git/MERGE_MSG"));
cl_assert(strcmp(git_buf_cstr(&mergemsg_buf),
"Merge commit '7cb63eed597130ba4abb87b3e544b85021905520'\n" \
"\n" \
"Conflicts:\n" \
"\tconflicting.txt\n") == 0);
git_buf_free(&conflicting_buf);
git_buf_free(&mergemsg_buf);
cl_assert(merge_test_index(repo_index, merge_index_entries, 8));
cl_assert(merge_test_reuc(repo_index, merge_reuc_entries, 3));

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More