Commit b23c206e by Carlos Martín Nieto

Merge pull request #2903 from ethomson/rebase

Fixups for rebase
parents 57f45e7f ffbd51ed
......@@ -133,7 +133,8 @@ GIT_EXTERN(int) git_rebase_init_options(
*
* @param out Pointer to store the rebase object
* @param repo The repository to perform the rebase
* @param branch The terminal commit to rebase
* @param branch The terminal commit to rebase, or NULL to rebase the
* current branch
* @param upstream The commit to begin rebasing from, or NULL to rebase all
* reachable commits
* @param onto The branch to rebase onto, or NULL to rebase onto the given
......
......@@ -168,10 +168,31 @@ GIT_INLINE(int) rebase_readoid(
return 0;
}
static git_rebase_operation *rebase_operation_alloc(
git_rebase *rebase,
git_rebase_operation_t type,
git_oid *id,
const char *exec)
{
git_rebase_operation *operation;
assert((type == GIT_REBASE_OPERATION_EXEC) == !id);
assert((type == GIT_REBASE_OPERATION_EXEC) == !!exec);
if ((operation = git_array_alloc(rebase->operations)) == NULL)
return NULL;
operation->type = type;
git_oid_cpy((git_oid *)&operation->id, id);
operation->exec = exec;
return operation;
}
static int rebase_open_merge(git_rebase *rebase)
{
git_buf state_path = GIT_BUF_INIT, buf = GIT_BUF_INIT, cmt = GIT_BUF_INIT;
git_oid current_id = {{0}};
git_oid id;
git_rebase_operation *operation;
size_t i, msgnum = 0, end;
int error;
......@@ -194,7 +215,7 @@ static int rebase_open_merge(git_rebase *rebase)
goto done;
/* Read 'current' if it exists */
if ((error = rebase_readoid(&current_id, &buf, &state_path, CURRENT_FILE)) < 0 &&
if ((error = rebase_readoid(&id, &buf, &state_path, CURRENT_FILE)) < 0 &&
error != GIT_ENOTFOUND)
goto done;
......@@ -203,14 +224,14 @@ static int rebase_open_merge(git_rebase *rebase)
GITERR_CHECK_ARRAY(rebase->operations);
for (i = 0; i < end; i++) {
operation = git_array_alloc(rebase->operations);
GITERR_CHECK_ALLOC(operation);
git_buf_clear(&cmt);
if ((error = git_buf_printf(&cmt, "cmt.%" PRIuZ, (i+1))) < 0 ||
(error = rebase_readoid((git_oid *)&operation->id, &buf, &state_path, cmt.ptr)) < 0)
(error = rebase_readoid(&id, &buf, &state_path, cmt.ptr)) < 0)
goto done;
operation = rebase_operation_alloc(rebase, GIT_REBASE_OPERATION_PICK, &id, NULL);
GITERR_CHECK_ALLOC(operation);
}
/* Read 'onto_name' */
......@@ -553,9 +574,7 @@ static int rebase_init_operations(
if (merge)
continue;
operation = git_array_alloc(rebase->operations);
operation->type = GIT_REBASE_OPERATION_PICK;
git_oid_cpy((git_oid *)&operation->id, &id);
operation = rebase_operation_alloc(rebase, GIT_REBASE_OPERATION_PICK, &id, NULL);
}
error = 0;
......@@ -589,11 +608,21 @@ static int rebase_init(
const git_annotated_commit *onto,
const git_rebase_options *opts)
{
git_reference *head_ref = NULL;
git_annotated_commit *head_branch = NULL;
git_buf state_path = GIT_BUF_INIT;
int error;
if ((error = git_buf_joinpath(&state_path, repo->path_repository, REBASE_MERGE_DIR)) < 0)
return error;
goto done;
if (!branch) {
if ((error = git_repository_head(&head_ref, repo)) < 0 ||
(error = git_annotated_commit_from_ref(&head_branch, repo, head_ref)) < 0)
goto done;
branch = head_branch;
}
rebase->repo = repo;
rebase->type = GIT_REBASE_TYPE_MERGE;
......@@ -611,6 +640,10 @@ static int rebase_init(
git_buf_free(&state_path);
done:
git_reference_free(head_ref);
git_annotated_commit_free(head_branch);
return error;
}
......@@ -625,12 +658,12 @@ int git_rebase_init(
{
git_rebase *rebase = NULL;
git_rebase_options opts;
git_reference *head_ref = NULL;
git_buf reflog = GIT_BUF_INIT;
git_commit *onto_commit = NULL;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
int error;
assert(repo && branch && (upstream || onto));
assert(repo && (upstream || onto));
*out = NULL;
......@@ -639,24 +672,28 @@ int git_rebase_init(
if (!onto)
onto = upstream;
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
if ((error = rebase_normalize_opts(repo, &opts, given_opts)) < 0 ||
(error = git_repository__ensure_not_bare(repo, "rebase")) < 0 ||
(error = rebase_ensure_not_in_progress(repo)) < 0 ||
(error = rebase_ensure_not_dirty(repo)) < 0)
(error = rebase_ensure_not_dirty(repo)) < 0 ||
(error = git_commit_lookup(
&onto_commit, repo, git_annotated_commit_id(onto))) < 0)
return error;
rebase = git__calloc(1, sizeof(git_rebase));
GITERR_CHECK_ALLOC(rebase);
if ((error = rebase_init(rebase, repo, branch, upstream, onto, &opts)) < 0 ||
if ((error = rebase_init(
rebase, repo, branch, upstream, onto, &opts)) < 0 ||
(error = rebase_setupfiles(rebase)) < 0 ||
(error = git_buf_printf(&reflog,
"rebase: checkout %s", rebase_onto_name(onto))) < 0 ||
(error = git_reference_create(&head_ref, repo, GIT_HEAD_FILE,
git_annotated_commit_id(onto), 1, signature, reflog.ptr)) < 0 ||
(error = git_checkout_head(repo, &checkout_opts)) < 0)
(error = git_checkout_tree(
repo, (git_object *)onto_commit, &checkout_opts)) < 0 ||
(error = git_repository_set_head_detached(
repo, git_annotated_commit_id(onto), signature, reflog.ptr)) < 0)
goto done;
*out = rebase;
......@@ -667,7 +704,7 @@ done:
git_rebase_free(rebase);
}
git_reference_free(head_ref);
git_commit_free(onto_commit);
git_buf_free(&reflog);
rebase_opts_free(&opts);
......
......@@ -42,6 +42,7 @@ static void test_operations(git_rebase *rebase, size_t expected_current)
operation = git_rebase_operation_byindex(rebase, i);
cl_assert_equal_i(GIT_REBASE_OPERATION_PICK, operation->type);
cl_assert_equal_oid(&expected_oid[i], &operation->id);
cl_assert_equal_p(NULL, operation->exec);
}
}
......
......@@ -293,6 +293,55 @@ void test_rebase_setup__orphan_branch(void)
git_rebase_free(rebase);
}
/* git checkout beef && git rebase --merge master */
void test_rebase_setup__merge_null_branch_uses_HEAD(void)
{
git_rebase *rebase;
git_reference *upstream_ref;
git_annotated_commit *upstream_head;
git_reference *head;
git_commit *head_commit;
git_oid head_id;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));
cl_git_pass(git_repository_set_head(repo, "refs/heads/beef", NULL, NULL));
cl_git_pass(git_checkout_head(repo, &checkout_opts));
cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master"));
cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref));
cl_git_pass(git_rebase_init(&rebase, repo, NULL, upstream_head, NULL, signature, NULL));
cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
git_oid_fromstr(&head_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00");
cl_git_pass(git_repository_head(&head, repo));
cl_git_pass(git_reference_peel((git_object **)&head_commit, head, GIT_OBJ_COMMIT));
cl_assert_equal_oid(&head_id, git_commit_id(head_commit));
cl_assert_equal_file("b146bd7608eac53d9bf9e1a6963543588b555c64\n", 41, "rebase/.git/ORIG_HEAD");
cl_assert_equal_file("da9c51a23d02d931a486f45ad18cda05cf5d2b94\n", 41, "rebase/.git/rebase-merge/cmt.1");
cl_assert_equal_file("8d1f13f93c4995760ac07d129246ac1ff64c0be9\n", 41, "rebase/.git/rebase-merge/cmt.2");
cl_assert_equal_file("3069cc907e6294623e5917ef6de663928c1febfb\n", 41, "rebase/.git/rebase-merge/cmt.3");
cl_assert_equal_file("588e5d2f04d49707fe4aab865e1deacaf7ef6787\n", 41, "rebase/.git/rebase-merge/cmt.4");
cl_assert_equal_file("b146bd7608eac53d9bf9e1a6963543588b555c64\n", 41, "rebase/.git/rebase-merge/cmt.5");
cl_assert_equal_file("5\n", 2, "rebase/.git/rebase-merge/end");
cl_assert_equal_file("efad0b11c47cb2f0220cbd6f5b0f93bb99064b00\n", 41, "rebase/.git/rebase-merge/onto");
cl_assert_equal_file("master\n", 7, "rebase/.git/rebase-merge/onto_name");
cl_assert_equal_file("b146bd7608eac53d9bf9e1a6963543588b555c64\n", 41, "rebase/.git/rebase-merge/orig-head");
git_commit_free(head_commit);
git_reference_free(head);
git_annotated_commit_free(upstream_head);
git_reference_free(upstream_ref);
git_rebase_free(rebase);
}
static int rebase_is_blocked(void)
{
git_rebase *rebase = NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment