Skip to content

Commit c30955e

Browse files
committed
Merge pull request libgit2#3748 from libgit2/ethomson/rebase_detached
Rebase improvements with IDs
2 parents b058297 + 1f84caf commit c30955e

File tree

3 files changed

+161
-32
lines changed

3 files changed

+161
-32
lines changed

src/rebase.c

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ static int rebase_setupfiles_merge(git_rebase *rebase)
472472
static int rebase_setupfiles(git_rebase *rebase)
473473
{
474474
char onto[GIT_OID_HEXSZ], orig_head[GIT_OID_HEXSZ];
475+
const char *orig_head_name;
475476

476477
git_oid_fmt(onto, &rebase->onto_id);
477478
git_oid_fmt(orig_head, &rebase->orig_head_id);
@@ -481,8 +482,11 @@ static int rebase_setupfiles(git_rebase *rebase)
481482
return -1;
482483
}
483484

485+
orig_head_name = rebase->head_detached ? ORIG_DETACHED_HEAD :
486+
rebase->orig_head_name;
487+
484488
if (git_repository__set_orig_head(rebase->repo, &rebase->orig_head_id) < 0 ||
485-
rebase_setupfile(rebase, HEAD_NAME_FILE, -1, "%s\n", rebase->orig_head_name) < 0 ||
489+
rebase_setupfile(rebase, HEAD_NAME_FILE, -1, "%s\n", orig_head_name) < 0 ||
486490
rebase_setupfile(rebase, ONTO_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, onto) < 0 ||
487491
rebase_setupfile(rebase, ORIG_HEAD_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, orig_head) < 0 ||
488492
rebase_setupfile(rebase, QUIET_FILE, -1, rebase->quiet ? "t\n" : "\n") < 0)
@@ -626,8 +630,12 @@ static int rebase_init_merge(
626630
rebase->state_path = git_buf_detach(&state_path);
627631
GITERR_CHECK_ALLOC(rebase->state_path);
628632

629-
rebase->orig_head_name = git__strdup(branch->ref_name ? branch->ref_name : ORIG_DETACHED_HEAD);
630-
GITERR_CHECK_ALLOC(rebase->orig_head_name);
633+
if (branch->ref_name) {
634+
rebase->orig_head_name = git__strdup(branch->ref_name);
635+
GITERR_CHECK_ALLOC(rebase->orig_head_name);
636+
} else {
637+
rebase->head_detached = 1;
638+
}
631639

632640
rebase->onto_name = git__strdup(rebase_onto_name(onto));
633641
GITERR_CHECK_ALLOC(rebase->onto_name);
@@ -1254,42 +1262,33 @@ static int rebase_copy_notes(
12541262
return error;
12551263
}
12561264

1257-
int git_rebase_finish(
1258-
git_rebase *rebase,
1259-
const git_signature *signature)
1265+
static int return_to_orig_head(git_rebase *rebase)
12601266
{
12611267
git_reference *terminal_ref = NULL, *branch_ref = NULL, *head_ref = NULL;
12621268
git_commit *terminal_commit = NULL;
12631269
git_buf branch_msg = GIT_BUF_INIT, head_msg = GIT_BUF_INIT;
12641270
char onto[GIT_OID_HEXSZ];
1265-
int error;
1266-
1267-
assert(rebase);
1268-
1269-
if (rebase->inmemory)
1270-
return 0;
1271+
int error = 0;
12711272

12721273
git_oid_fmt(onto, &rebase->onto_id);
12731274

1274-
if ((error = git_buf_printf(&branch_msg, "rebase finished: %s onto %.*s",
1275-
rebase->orig_head_name, GIT_OID_HEXSZ, onto)) < 0 ||
1276-
(error = git_buf_printf(&head_msg, "rebase finished: returning to %s",
1277-
rebase->orig_head_name)) < 0 ||
1278-
(error = git_repository_head(&terminal_ref, rebase->repo)) < 0 ||
1275+
if ((error = git_buf_printf(&branch_msg,
1276+
"rebase finished: %s onto %.*s",
1277+
rebase->orig_head_name, GIT_OID_HEXSZ, onto)) == 0 &&
1278+
(error = git_buf_printf(&head_msg,
1279+
"rebase finished: returning to %s",
1280+
rebase->orig_head_name)) == 0 &&
1281+
(error = git_repository_head(&terminal_ref, rebase->repo)) == 0 &&
12791282
(error = git_reference_peel((git_object **)&terminal_commit,
1280-
terminal_ref, GIT_OBJ_COMMIT)) < 0 ||
1283+
terminal_ref, GIT_OBJ_COMMIT)) == 0 &&
12811284
(error = git_reference_create_matching(&branch_ref,
1282-
rebase->repo, rebase->orig_head_name, git_commit_id(terminal_commit), 1,
1283-
&rebase->orig_head_id, branch_msg.ptr)) < 0 ||
1284-
(error = git_reference_symbolic_create(&head_ref,
1285+
rebase->repo, rebase->orig_head_name,
1286+
git_commit_id(terminal_commit), 1,
1287+
&rebase->orig_head_id, branch_msg.ptr)) == 0)
1288+
error = git_reference_symbolic_create(&head_ref,
12851289
rebase->repo, GIT_HEAD_FILE, rebase->orig_head_name, 1,
1286-
head_msg.ptr)) < 0 ||
1287-
(error = rebase_copy_notes(rebase, signature)) < 0)
1288-
goto done;
1289-
1290-
error = rebase_cleanup(rebase);
1290+
head_msg.ptr);
12911291

1292-
done:
12931292
git_buf_free(&head_msg);
12941293
git_buf_free(&branch_msg);
12951294
git_commit_free(terminal_commit);
@@ -1300,6 +1299,26 @@ int git_rebase_finish(
13001299
return error;
13011300
}
13021301

1302+
int git_rebase_finish(
1303+
git_rebase *rebase,
1304+
const git_signature *signature)
1305+
{
1306+
int error = 0;
1307+
1308+
assert(rebase);
1309+
1310+
if (rebase->inmemory)
1311+
return 0;
1312+
1313+
if (!rebase->head_detached)
1314+
error = return_to_orig_head(rebase);
1315+
1316+
if (error == 0 && (error = rebase_copy_notes(rebase, signature)) == 0)
1317+
error = rebase_cleanup(rebase);
1318+
1319+
return error;
1320+
}
1321+
13031322
size_t git_rebase_operation_entrycount(git_rebase *rebase)
13041323
{
13051324
assert(rebase);

tests/rebase/abort.c

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@ void test_rebase_abort__cleanup(void)
1919
cl_git_sandbox_cleanup();
2020
}
2121

22-
static void test_abort(git_annotated_commit *branch, git_annotated_commit *onto)
22+
static void ensure_aborted(
23+
git_annotated_commit *branch,
24+
git_annotated_commit *onto)
2325
{
24-
git_rebase *rebase;
2526
git_reference *head_ref, *branch_ref = NULL;
2627
git_status_list *statuslist;
2728
git_reflog *reflog;
2829
const git_reflog_entry *reflog_entry;
2930

30-
cl_git_pass(git_rebase_open(&rebase, repo, NULL));
31-
cl_git_pass(git_rebase_abort(rebase));
32-
3331
cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));
3432

3533
/* Make sure the refs are updated appropriately */
@@ -58,6 +56,18 @@ static void test_abort(git_annotated_commit *branch, git_annotated_commit *onto)
5856
git_reflog_free(reflog);
5957
git_reference_free(head_ref);
6058
git_reference_free(branch_ref);
59+
}
60+
61+
static void test_abort(
62+
git_annotated_commit *branch, git_annotated_commit *onto)
63+
{
64+
git_rebase *rebase;
65+
66+
cl_git_pass(git_rebase_open(&rebase, repo, NULL));
67+
cl_git_pass(git_rebase_abort(rebase));
68+
69+
ensure_aborted(branch, onto);
70+
6171
git_rebase_free(rebase);
6272
}
6373

@@ -86,6 +96,32 @@ void test_rebase_abort__merge(void)
8696
git_rebase_free(rebase);
8797
}
8898

99+
void test_rebase_abort__merge_immediately_after_init(void)
100+
{
101+
git_rebase *rebase;
102+
git_reference *branch_ref, *onto_ref;
103+
git_annotated_commit *branch_head, *onto_head;
104+
105+
cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef"));
106+
cl_git_pass(git_reference_lookup(&onto_ref, repo, "refs/heads/master"));
107+
108+
cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref));
109+
cl_git_pass(git_annotated_commit_from_ref(&onto_head, repo, onto_ref));
110+
111+
cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
112+
cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
113+
114+
cl_git_pass(git_rebase_abort(rebase));
115+
ensure_aborted(branch_head, onto_head);
116+
117+
git_annotated_commit_free(branch_head);
118+
git_annotated_commit_free(onto_head);
119+
120+
git_reference_free(branch_ref);
121+
git_reference_free(onto_ref);
122+
git_rebase_free(rebase);
123+
}
124+
89125
void test_rebase_abort__merge_by_id(void)
90126
{
91127
git_rebase *rebase;
@@ -109,6 +145,30 @@ void test_rebase_abort__merge_by_id(void)
109145
git_rebase_free(rebase);
110146
}
111147

148+
void test_rebase_abort__merge_by_id_immediately_after_init(void)
149+
{
150+
git_rebase *rebase;
151+
git_oid branch_id, onto_id;
152+
git_annotated_commit *branch_head, *onto_head;
153+
154+
cl_git_pass(git_oid_fromstr(&branch_id, "b146bd7608eac53d9bf9e1a6963543588b555c64"));
155+
cl_git_pass(git_oid_fromstr(&onto_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00"));
156+
157+
cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id));
158+
cl_git_pass(git_annotated_commit_lookup(&onto_head, repo, &onto_id));
159+
160+
cl_git_pass(git_rebase_init(&rebase, repo, branch_head, NULL, onto_head, NULL));
161+
cl_assert_equal_i(GIT_REPOSITORY_STATE_REBASE_MERGE, git_repository_state(repo));
162+
163+
cl_git_pass(git_rebase_abort(rebase));
164+
ensure_aborted(branch_head, onto_head);
165+
166+
git_annotated_commit_free(branch_head);
167+
git_annotated_commit_free(onto_head);
168+
169+
git_rebase_free(rebase);
170+
}
171+
112172
void test_rebase_abort__detached_head(void)
113173
{
114174
git_rebase *rebase;

tests/rebase/merge.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,56 @@ void test_rebase_merge__finish(void)
475475
git_rebase_free(rebase);
476476
}
477477

478+
void test_rebase_merge__finish_with_ids(void)
479+
{
480+
git_rebase *rebase;
481+
git_reference *head_ref;
482+
git_oid branch_id, upstream_id;
483+
git_annotated_commit *branch_head, *upstream_head;
484+
git_rebase_operation *rebase_operation;
485+
git_oid commit_id;
486+
git_reflog *reflog;
487+
const git_reflog_entry *reflog_entry;
488+
int error;
489+
490+
cl_git_pass(git_oid_fromstr(&branch_id, "d616d97082eb7bb2dc6f180a7cca940993b7a56f"));
491+
cl_git_pass(git_oid_fromstr(&upstream_id, "f87d14a4a236582a0278a916340a793714256864"));
492+
493+
cl_git_pass(git_annotated_commit_lookup(&branch_head, repo, &branch_id));
494+
cl_git_pass(git_annotated_commit_lookup(&upstream_head, repo, &upstream_id));
495+
496+
cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, NULL));
497+
498+
cl_git_pass(git_rebase_next(&rebase_operation, rebase));
499+
cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature,
500+
NULL, NULL));
501+
502+
cl_git_fail(error = git_rebase_next(&rebase_operation, rebase));
503+
cl_assert_equal_i(GIT_ITEROVER, error);
504+
505+
cl_git_pass(git_rebase_finish(rebase, signature));
506+
507+
cl_assert_equal_i(GIT_REPOSITORY_STATE_NONE, git_repository_state(repo));
508+
509+
cl_git_pass(git_reference_lookup(&head_ref, repo, "HEAD"));
510+
cl_assert_equal_i(GIT_REF_OID, git_reference_type(head_ref));
511+
cl_assert_equal_oid(&commit_id, git_reference_target(head_ref));
512+
513+
/* reflogs are not updated as if we were operating on proper
514+
* branches. check that the last reflog entry is the rebase.
515+
*/
516+
cl_git_pass(git_reflog_read(&reflog, repo, "HEAD"));
517+
cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0));
518+
cl_assert_equal_oid(&commit_id, git_reflog_entry_id_new(reflog_entry));
519+
cl_assert_equal_s("rebase: Modification 3 to gravy", git_reflog_entry_message(reflog_entry));
520+
git_reflog_free(reflog);
521+
522+
git_annotated_commit_free(branch_head);
523+
git_annotated_commit_free(upstream_head);
524+
git_reference_free(head_ref);
525+
git_rebase_free(rebase);
526+
}
527+
478528
static void test_copy_note(
479529
const git_rebase_options *opts,
480530
bool should_exist)

0 commit comments

Comments
 (0)