Skip to content

Commit 2891774

Browse files
committed
rebase: move reset_head() into a better spot
Over the next commits, we want to make use of it in `run_am()` (i.e. running the `--am` backend directly, without detouring to Unix shell script code) which in turn will be called from `run_specific_rebase()`. So let's move it before that latter function. This commit is best viewed using --color-moved. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 9a093f4 commit 2891774

File tree

1 file changed

+125
-125
lines changed

1 file changed

+125
-125
lines changed

builtin/rebase.c

Lines changed: 125 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,131 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
333333
}
334334
}
335335

336+
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
337+
338+
#define RESET_HEAD_DETACH (1<<0)
339+
#define RESET_HEAD_HARD (1<<1)
340+
#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
341+
342+
static int reset_head(struct object_id *oid, const char *action,
343+
const char *switch_to_branch, unsigned flags,
344+
const char *reflog_orig_head, const char *reflog_head)
345+
{
346+
unsigned detach_head = flags & RESET_HEAD_DETACH;
347+
unsigned reset_hard = flags & RESET_HEAD_HARD;
348+
unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
349+
struct object_id head_oid;
350+
struct tree_desc desc[2] = { { NULL }, { NULL } };
351+
struct lock_file lock = LOCK_INIT;
352+
struct unpack_trees_options unpack_tree_opts;
353+
struct tree *tree;
354+
const char *reflog_action;
355+
struct strbuf msg = STRBUF_INIT;
356+
size_t prefix_len;
357+
struct object_id *orig = NULL, oid_orig,
358+
*old_orig = NULL, oid_old_orig;
359+
int ret = 0, nr = 0;
360+
361+
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
362+
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
363+
364+
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
365+
ret = -1;
366+
goto leave_reset_head;
367+
}
368+
369+
if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
370+
ret = error(_("could not determine HEAD revision"));
371+
goto leave_reset_head;
372+
}
373+
374+
if (!oid)
375+
oid = &head_oid;
376+
377+
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
378+
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
379+
unpack_tree_opts.head_idx = 1;
380+
unpack_tree_opts.src_index = the_repository->index;
381+
unpack_tree_opts.dst_index = the_repository->index;
382+
unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
383+
unpack_tree_opts.update = 1;
384+
unpack_tree_opts.merge = 1;
385+
if (!detach_head)
386+
unpack_tree_opts.reset = 1;
387+
388+
if (read_index_unmerged(the_repository->index) < 0) {
389+
ret = error(_("could not read index"));
390+
goto leave_reset_head;
391+
}
392+
393+
if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
394+
ret = error(_("failed to find tree of %s"),
395+
oid_to_hex(&head_oid));
396+
goto leave_reset_head;
397+
}
398+
399+
if (!fill_tree_descriptor(&desc[nr++], oid)) {
400+
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
401+
goto leave_reset_head;
402+
}
403+
404+
if (unpack_trees(nr, desc, &unpack_tree_opts)) {
405+
ret = -1;
406+
goto leave_reset_head;
407+
}
408+
409+
tree = parse_tree_indirect(oid);
410+
prime_cache_tree(the_repository->index, tree);
411+
412+
if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
413+
ret = error(_("could not write index"));
414+
goto leave_reset_head;
415+
}
416+
417+
reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
418+
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
419+
prefix_len = msg.len;
420+
421+
if (!get_oid("ORIG_HEAD", &oid_old_orig))
422+
old_orig = &oid_old_orig;
423+
if (!get_oid("HEAD", &oid_orig)) {
424+
orig = &oid_orig;
425+
if (!reflog_orig_head) {
426+
strbuf_addstr(&msg, "updating ORIG_HEAD");
427+
reflog_orig_head = msg.buf;
428+
}
429+
update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
430+
UPDATE_REFS_MSG_ON_ERR);
431+
} else if (old_orig)
432+
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
433+
if (!reflog_head) {
434+
strbuf_setlen(&msg, prefix_len);
435+
strbuf_addstr(&msg, "updating HEAD");
436+
reflog_head = msg.buf;
437+
}
438+
if (!switch_to_branch)
439+
ret = update_ref(reflog_head, "HEAD", oid, orig,
440+
detach_head ? REF_NO_DEREF : 0,
441+
UPDATE_REFS_MSG_ON_ERR);
442+
else {
443+
ret = create_symref("HEAD", switch_to_branch, msg.buf);
444+
if (!ret)
445+
ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
446+
UPDATE_REFS_MSG_ON_ERR);
447+
}
448+
if (run_hook)
449+
run_hook_le(NULL, "post-checkout",
450+
oid_to_hex(orig ? orig : &null_oid),
451+
oid_to_hex(oid), "1", NULL);
452+
453+
leave_reset_head:
454+
strbuf_release(&msg);
455+
rollback_lock_file(&lock);
456+
while (nr)
457+
free((void *)desc[--nr].buffer);
458+
return ret;
459+
}
460+
336461
static const char *resolvemsg =
337462
N_("Resolve all conflicts manually, mark them as resolved with\n"
338463
"\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
@@ -526,131 +651,6 @@ static int run_specific_rebase(struct rebase_options *opts)
526651
return status ? -1 : 0;
527652
}
528653

529-
#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
530-
531-
#define RESET_HEAD_DETACH (1<<0)
532-
#define RESET_HEAD_HARD (1<<1)
533-
#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
534-
535-
static int reset_head(struct object_id *oid, const char *action,
536-
const char *switch_to_branch, unsigned flags,
537-
const char *reflog_orig_head, const char *reflog_head)
538-
{
539-
unsigned detach_head = flags & RESET_HEAD_DETACH;
540-
unsigned reset_hard = flags & RESET_HEAD_HARD;
541-
unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
542-
struct object_id head_oid;
543-
struct tree_desc desc[2] = { { NULL }, { NULL } };
544-
struct lock_file lock = LOCK_INIT;
545-
struct unpack_trees_options unpack_tree_opts;
546-
struct tree *tree;
547-
const char *reflog_action;
548-
struct strbuf msg = STRBUF_INIT;
549-
size_t prefix_len;
550-
struct object_id *orig = NULL, oid_orig,
551-
*old_orig = NULL, oid_old_orig;
552-
int ret = 0, nr = 0;
553-
554-
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
555-
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
556-
557-
if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
558-
ret = -1;
559-
goto leave_reset_head;
560-
}
561-
562-
if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
563-
ret = error(_("could not determine HEAD revision"));
564-
goto leave_reset_head;
565-
}
566-
567-
if (!oid)
568-
oid = &head_oid;
569-
570-
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
571-
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
572-
unpack_tree_opts.head_idx = 1;
573-
unpack_tree_opts.src_index = the_repository->index;
574-
unpack_tree_opts.dst_index = the_repository->index;
575-
unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
576-
unpack_tree_opts.update = 1;
577-
unpack_tree_opts.merge = 1;
578-
if (!detach_head)
579-
unpack_tree_opts.reset = 1;
580-
581-
if (read_index_unmerged(the_repository->index) < 0) {
582-
ret = error(_("could not read index"));
583-
goto leave_reset_head;
584-
}
585-
586-
if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
587-
ret = error(_("failed to find tree of %s"),
588-
oid_to_hex(&head_oid));
589-
goto leave_reset_head;
590-
}
591-
592-
if (!fill_tree_descriptor(&desc[nr++], oid)) {
593-
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
594-
goto leave_reset_head;
595-
}
596-
597-
if (unpack_trees(nr, desc, &unpack_tree_opts)) {
598-
ret = -1;
599-
goto leave_reset_head;
600-
}
601-
602-
tree = parse_tree_indirect(oid);
603-
prime_cache_tree(the_repository->index, tree);
604-
605-
if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
606-
ret = error(_("could not write index"));
607-
goto leave_reset_head;
608-
}
609-
610-
reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
611-
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
612-
prefix_len = msg.len;
613-
614-
if (!get_oid("ORIG_HEAD", &oid_old_orig))
615-
old_orig = &oid_old_orig;
616-
if (!get_oid("HEAD", &oid_orig)) {
617-
orig = &oid_orig;
618-
if (!reflog_orig_head) {
619-
strbuf_addstr(&msg, "updating ORIG_HEAD");
620-
reflog_orig_head = msg.buf;
621-
}
622-
update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
623-
UPDATE_REFS_MSG_ON_ERR);
624-
} else if (old_orig)
625-
delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
626-
if (!reflog_head) {
627-
strbuf_setlen(&msg, prefix_len);
628-
strbuf_addstr(&msg, "updating HEAD");
629-
reflog_head = msg.buf;
630-
}
631-
if (!switch_to_branch)
632-
ret = update_ref(reflog_head, "HEAD", oid, orig,
633-
detach_head ? REF_NO_DEREF : 0,
634-
UPDATE_REFS_MSG_ON_ERR);
635-
else {
636-
ret = create_symref("HEAD", switch_to_branch, msg.buf);
637-
if (!ret)
638-
ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
639-
UPDATE_REFS_MSG_ON_ERR);
640-
}
641-
if (run_hook)
642-
run_hook_le(NULL, "post-checkout",
643-
oid_to_hex(orig ? orig : &null_oid),
644-
oid_to_hex(oid), "1", NULL);
645-
646-
leave_reset_head:
647-
strbuf_release(&msg);
648-
rollback_lock_file(&lock);
649-
while (nr)
650-
free((void *)desc[--nr].buffer);
651-
return ret;
652-
}
653-
654654
static int rebase_config(const char *var, const char *value, void *data)
655655
{
656656
struct rebase_options *opts = data;

0 commit comments

Comments
 (0)