Skip to content

Commit f213f06

Browse files
Denton-Lgitster
authored andcommitted
rebase: generify reset_head()
In the future, we plan on lib-ifying reset_head() so we need it to be more generic. Make it more generic by making it accept a `struct repository` argument instead of implicitly using the non-repo functions. Also, make it accept a `const char *default_reflog_action` argument so that the default action of "rebase" isn't hardcoded in. Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 86ed00a commit f213f06

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

builtin/rebase.c

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "sequencer.h"
2929
#include "rebase-interactive.h"
3030

31+
#define DEFAULT_REFLOG_ACTION "rebase"
32+
3133
static char const * const builtin_rebase_usage[] = {
3234
N_("git rebase [-i] [options] [--exec <cmd>] "
3335
"[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"),
@@ -772,9 +774,10 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
772774
#define RESET_HEAD_REFS_ONLY (1<<3)
773775
#define RESET_ORIG_HEAD (1<<4)
774776

775-
static int reset_head(struct object_id *oid, const char *action,
777+
static int reset_head(struct repository *r, struct object_id *oid, const char *action,
776778
const char *switch_to_branch, unsigned flags,
777-
const char *reflog_orig_head, const char *reflog_head)
779+
const char *reflog_orig_head, const char *reflog_head,
780+
const char *default_reflog_action)
778781
{
779782
unsigned detach_head = flags & RESET_HEAD_DETACH;
780783
unsigned reset_hard = flags & RESET_HEAD_HARD;
@@ -796,7 +799,7 @@ static int reset_head(struct object_id *oid, const char *action,
796799
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
797800
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
798801

799-
if (!refs_only && hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
802+
if (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
800803
ret = -1;
801804
goto leave_reset_head;
802805
}
@@ -815,26 +818,26 @@ static int reset_head(struct object_id *oid, const char *action,
815818
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
816819
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
817820
unpack_tree_opts.head_idx = 1;
818-
unpack_tree_opts.src_index = the_repository->index;
819-
unpack_tree_opts.dst_index = the_repository->index;
821+
unpack_tree_opts.src_index = r->index;
822+
unpack_tree_opts.dst_index = r->index;
820823
unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
821824
unpack_tree_opts.update = 1;
822825
unpack_tree_opts.merge = 1;
823826
if (!detach_head)
824827
unpack_tree_opts.reset = 1;
825828

826-
if (repo_read_index_unmerged(the_repository) < 0) {
829+
if (repo_read_index_unmerged(r) < 0) {
827830
ret = error(_("could not read index"));
828831
goto leave_reset_head;
829832
}
830833

831-
if (!reset_hard && !fill_tree_descriptor(the_repository, &desc[nr++], &head_oid)) {
834+
if (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {
832835
ret = error(_("failed to find tree of %s"),
833836
oid_to_hex(&head_oid));
834837
goto leave_reset_head;
835838
}
836839

837-
if (!fill_tree_descriptor(the_repository, &desc[nr++], oid)) {
840+
if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
838841
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
839842
goto leave_reset_head;
840843
}
@@ -845,16 +848,16 @@ static int reset_head(struct object_id *oid, const char *action,
845848
}
846849

847850
tree = parse_tree_indirect(oid);
848-
prime_cache_tree(the_repository, the_repository->index, tree);
851+
prime_cache_tree(r, r->index, tree);
849852

850-
if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
853+
if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
851854
ret = error(_("could not write index"));
852855
goto leave_reset_head;
853856
}
854857

855858
reset_head_refs:
856859
reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
857-
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
860+
strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : default_reflog_action);
858861
prefix_len = msg.len;
859862

860863
if (update_orig_head) {
@@ -916,8 +919,10 @@ static int move_to_original_branch(struct rebase_options *opts)
916919
opts->head_name, oid_to_hex(&opts->onto->object.oid));
917920
strbuf_addf(&head_reflog, "rebase finished: returning to %s",
918921
opts->head_name);
919-
ret = reset_head(NULL, "", opts->head_name, RESET_HEAD_REFS_ONLY,
920-
orig_head_reflog.buf, head_reflog.buf);
922+
ret = reset_head(the_repository, NULL, "", opts->head_name,
923+
RESET_HEAD_REFS_ONLY,
924+
orig_head_reflog.buf, head_reflog.buf,
925+
DEFAULT_REFLOG_ACTION);
921926

922927
strbuf_release(&orig_head_reflog);
923928
strbuf_release(&head_reflog);
@@ -1005,8 +1010,9 @@ static int run_am(struct rebase_options *opts)
10051010
free(rebased_patches);
10061011
argv_array_clear(&am.args);
10071012

1008-
reset_head(&opts->orig_head, "checkout", opts->head_name, 0,
1009-
"HEAD", NULL);
1013+
reset_head(the_repository, &opts->orig_head, "checkout",
1014+
opts->head_name, 0,
1015+
"HEAD", NULL, DEFAULT_REFLOG_ACTION);
10101016
error(_("\ngit encountered an error while preparing the "
10111017
"patches to replay\n"
10121018
"these revisions:\n"
@@ -1661,8 +1667,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
16611667
rerere_clear(the_repository, &merge_rr);
16621668
string_list_clear(&merge_rr, 1);
16631669

1664-
if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1665-
NULL, NULL) < 0)
1670+
if (reset_head(the_repository, NULL, "reset", NULL, RESET_HEAD_HARD,
1671+
NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
16661672
die(_("could not discard worktree changes"));
16671673
remove_branch_state(the_repository, 0);
16681674
if (read_basic_state(&options))
@@ -1679,9 +1685,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
16791685

16801686
if (read_basic_state(&options))
16811687
exit(1);
1682-
if (reset_head(&options.orig_head, "reset",
1688+
if (reset_head(the_repository, &options.orig_head, "reset",
16831689
options.head_name, RESET_HEAD_HARD,
1684-
NULL, NULL) < 0)
1690+
NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
16851691
die(_("could not move back to %s"),
16861692
oid_to_hex(&options.orig_head));
16871693
remove_branch_state(the_repository, 0);
@@ -2073,8 +2079,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
20732079
options.state_dir);
20742080
write_file(autostash, "%s", oid_to_hex(&oid));
20752081
printf(_("Created autostash: %s\n"), buf.buf);
2076-
if (reset_head(NULL, "reset --hard",
2077-
NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
2082+
if (reset_head(the_repository, NULL, "reset --hard",
2083+
NULL, RESET_HEAD_HARD, NULL, NULL,
2084+
DEFAULT_REFLOG_ACTION) < 0)
20782085
die(_("could not reset --hard"));
20792086

20802087
if (discard_index(the_repository->index) < 0 ||
@@ -2114,10 +2121,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
21142121
strbuf_addf(&buf, "%s: checkout %s",
21152122
getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
21162123
options.switch_to);
2117-
if (reset_head(&options.orig_head, "checkout",
2124+
if (reset_head(the_repository,
2125+
&options.orig_head, "checkout",
21182126
options.head_name,
21192127
RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
2120-
NULL, buf.buf) < 0) {
2128+
NULL, buf.buf,
2129+
DEFAULT_REFLOG_ACTION) < 0) {
21212130
ret = !!error(_("could not switch to "
21222131
"%s"),
21232132
options.switch_to);
@@ -2189,10 +2198,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
21892198

21902199
strbuf_addf(&msg, "%s: checkout %s",
21912200
getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
2192-
if (reset_head(&options.onto->object.oid, "checkout", NULL,
2201+
if (reset_head(the_repository, &options.onto->object.oid, "checkout", NULL,
21932202
RESET_HEAD_DETACH | RESET_ORIG_HEAD |
21942203
RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
2195-
NULL, msg.buf))
2204+
NULL, msg.buf, DEFAULT_REFLOG_ACTION))
21962205
die(_("Could not detach HEAD"));
21972206
strbuf_release(&msg);
21982207

@@ -2207,8 +2216,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
22072216
strbuf_addf(&msg, "rebase finished: %s onto %s",
22082217
options.head_name ? options.head_name : "detached HEAD",
22092218
oid_to_hex(&options.onto->object.oid));
2210-
reset_head(NULL, "Fast-forwarded", options.head_name,
2211-
RESET_HEAD_REFS_ONLY, "HEAD", msg.buf);
2219+
reset_head(the_repository, NULL, "Fast-forwarded", options.head_name,
2220+
RESET_HEAD_REFS_ONLY, "HEAD", msg.buf,
2221+
DEFAULT_REFLOG_ACTION);
22122222
strbuf_release(&msg);
22132223
ret = !!finish_rebase(&options);
22142224
goto cleanup;

0 commit comments

Comments
 (0)