Merge pull request #2903 from ethomson/rebase Fixups for rebase
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
diff --git a/include/git2/rebase.h b/include/git2/rebase.h
index 1da3fb5..19f5cb8 100644
--- a/include/git2/rebase.h
+++ b/include/git2/rebase.h
@@ -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
diff --git a/src/rebase.c b/src/rebase.c
index 2e80592..b0a93a6 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -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(¤t_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);
diff --git a/tests/rebase/iterator.c b/tests/rebase/iterator.c
index ddf4413..8117a09 100644
--- a/tests/rebase/iterator.c
+++ b/tests/rebase/iterator.c
@@ -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);
}
}
diff --git a/tests/rebase/setup.c b/tests/rebase/setup.c
index c81ca12..f1eb6a4 100644
--- a/tests/rebase/setup.c
+++ b/tests/rebase/setup.c
@@ -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;