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 <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Denton Liu committed
Apr 7, 2020 at 10:27 UTC
f213f069fb57996505f9d6c9dd0f4a3ef53e87e4
1 file changed
+37
-27
builtin/rebase.c
+37
-27
@@ -28,6 +28,8 @@
28
#include "sequencer.h"
29
#include "rebase-interactive.h"
30
31
+#define DEFAULT_REFLOG_ACTION "rebase"
32
+
33
static char const * const builtin_rebase_usage[] = {
34
N_("git rebase [-i] [options] [--exec <cmd>] "
35
"[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"),
@@ -772,9 +774,10 @@ static void add_var(struct strbuf *buf, const char *name, const char *value)
774
#define RESET_HEAD_REFS_ONLY (1<<3)
775
#define RESET_ORIG_HEAD (1<<4)
776
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,
778
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)
781
{
782
unsigned detach_head = flags & RESET_HEAD_DETACH;
783
unsigned reset_hard = flags & RESET_HEAD_HARD;
@@ -796,7 +799,7 @@ static int reset_head(struct object_id *oid, const char *action,
799
if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
800
BUG("Not a fully qualified branch: '%s'", switch_to_branch);
801
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) {
803
ret = -1;
804
goto leave_reset_head;
805
}
@@ -815,26 +818,26 @@ static int reset_head(struct object_id *oid, const char *action,
818
memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
819
setup_unpack_trees_porcelain(&unpack_tree_opts, action);
820
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;
823
unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
824
unpack_tree_opts.update = 1;
825
unpack_tree_opts.merge = 1;
826
if (!detach_head)
827
unpack_tree_opts.reset = 1;
828
826
- if (repo_read_index_unmerged(the_repository) < 0) {
829
+ if (repo_read_index_unmerged(r) < 0) {
830
ret = error(_("could not read index"));
831
goto leave_reset_head;
832
}
833
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)) {
835
ret = error(_("failed to find tree of %s"),
836
oid_to_hex(&head_oid));
837
goto leave_reset_head;
838
}
839
837
- if (!fill_tree_descriptor(the_repository, &desc[nr++], oid)) {
840
+ if (!fill_tree_descriptor(r, &desc[nr++], oid)) {
841
ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
842
goto leave_reset_head;
843
}
@@ -845,16 +848,16 @@ static int reset_head(struct object_id *oid, const char *action,
848
}
849
850
tree = parse_tree_indirect(oid);
848
- prime_cache_tree(the_repository, the_repository->index, tree);
851
+ prime_cache_tree(r, r->index, tree);
852
850
- if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
853
+ if (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {
854
ret = error(_("could not write index"));
855
goto leave_reset_head;
856
}
857
858
reset_head_refs:
859
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);
861
prefix_len = msg.len;
862
863
if (update_orig_head) {
@@ -916,8 +919,10 @@ static int move_to_original_branch(struct rebase_options *opts)
919
opts->head_name, oid_to_hex(&opts->onto->object.oid));
920
strbuf_addf(&head_reflog, "rebase finished: returning to %s",
921
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);
926
927
strbuf_release(&orig_head_reflog);
928
strbuf_release(&head_reflog);
@@ -1005,8 +1010,9 @@ static int run_am(struct rebase_options *opts)
1010
free(rebased_patches);
1011
argv_array_clear(&am.args);
1012
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);
1016
error(_("\ngit encountered an error while preparing the "
1017
"patches to replay\n"
1018
"these revisions:\n"
@@ -1661,8 +1667,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1667
rerere_clear(the_repository, &merge_rr);
1668
string_list_clear(&merge_rr, 1);
1669
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)
1672
die(_("could not discard worktree changes"));
1673
remove_branch_state(the_repository, 0);
1674
if (read_basic_state(&options))
@@ -1679,9 +1685,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1685
1686
if (read_basic_state(&options))
1687
exit(1);
1682
- if (reset_head(&options.orig_head, "reset",
1688
+ if (reset_head(the_repository, &options.orig_head, "reset",
1689
options.head_name, RESET_HEAD_HARD,
1684
- NULL, NULL) < 0)
1690
+ NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
1691
die(_("could not move back to %s"),
1692
oid_to_hex(&options.orig_head));
1693
remove_branch_state(the_repository, 0);
@@ -2073,8 +2079,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
2079
options.state_dir);
2080
write_file(autostash, "%s", oid_to_hex(&oid));
2081
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)
2085
die(_("could not reset --hard"));
2086
2087
if (discard_index(the_repository->index) < 0 ||
@@ -2114,10 +2121,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
2121
strbuf_addf(&buf, "%s: checkout %s",
2122
getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
2123
options.switch_to);
2117
- if (reset_head(&options.orig_head, "checkout",
2124
+ if (reset_head(the_repository,
2125
+ &options.orig_head, "checkout",
2126
options.head_name,
2127
RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
2120
- NULL, buf.buf) < 0) {
2128
+ NULL, buf.buf,
2129
+ DEFAULT_REFLOG_ACTION) < 0) {
2130
ret = !!error(_("could not switch to "
2131
"%s"),
2132
options.switch_to);
@@ -2189,10 +2198,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
2198
2199
strbuf_addf(&msg, "%s: checkout %s",
2200
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,
2202
RESET_HEAD_DETACH | RESET_ORIG_HEAD |
2203
RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
2195
- NULL, msg.buf))
2204
+ NULL, msg.buf, DEFAULT_REFLOG_ACTION))
2205
die(_("Could not detach HEAD"));
2206
strbuf_release(&msg);
2207
@@ -2207,8 +2216,9 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
2216
strbuf_addf(&msg, "rebase finished: %s onto %s",
2217
options.head_name ? options.head_name : "detached HEAD",
2218
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);
2222
strbuf_release(&msg);
2223
ret = !!finish_rebase(&options);
2224
goto cleanup;