rebase -i: rewrite write_basic_state() in C

This rewrites write_basic_state() from git-rebase.sh in C. This is the first step in the conversion of init_basic_state(), hence the mode in rebase--helper.c is called INIT_BASIC_STATE. init_basic_state() will be converted in the next commit. The part of read_strategy_opts() that parses the stategy options is moved to a new function to allow its use in rebase--helper.c. Finally, the call to write_basic_state() is removed from git-rebase--interactive.sh, replaced by a call to `--init-basic-state`. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alban Gruin committed Aug 28, 2018 at 14:10 UTC 65850686cf072d6de88880247adb7113db8a52f2
4 files changed +102 -14
builtin/rebase--helper.c
+26 -2
@@ -5,6 +5,8 @@
5 #include "sequencer.h"
6 #include "rebase-interactive.h"
7 #include "argv-array.h"
8 +#include "rerere.h"
9 +#include "alias.h"
10
11 static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
12
@@ -53,11 +55,12 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
55 unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
56 int abbreviate_commands = 0, rebase_cousins = -1, ret;
57 const char *head_hash = NULL, *onto = NULL, *restrict_revision = NULL,
56 - *squash_onto = NULL, *upstream = NULL;
58 + *squash_onto = NULL, *upstream = NULL, *head_name = NULL;
59 + char *raw_strategies = NULL;
60 enum {
61 CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
62 CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, PREPARE_BRANCH,
60 - COMPLETE_ACTION
63 + COMPLETE_ACTION, INIT_BASIC_STATE
64 } command = 0;
65 struct option options[] = {
66 OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -69,6 +72,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
72 N_("keep original branch points of cousins")),
73 OPT_BOOL(0, "autosquash", &autosquash,
74 N_("move commits that begin with squash!/fixup!")),
75 + OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")),
76 OPT__VERBOSE(&opts.verbose, N_("be verbose")),
77 OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
78 CONTINUE),
@@ -93,6 +97,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
97 N_("prepare the branch to be rebased"), PREPARE_BRANCH),
98 OPT_CMDMODE(0, "complete-action", &command,
99 N_("complete the action"), COMPLETE_ACTION),
100 + OPT_CMDMODE(0, "init-basic-state", &command,
101 + N_("initialise the rebase state"), INIT_BASIC_STATE),
102 OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
103 OPT_STRING(0, "restrict-revision", &restrict_revision,
104 N_("restrict-revision"), N_("restrict revision")),
@@ -100,6 +106,14 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
106 N_("squash onto")),
107 OPT_STRING(0, "upstream", &upstream, N_("upstream"),
108 N_("the upstream commit")),
109 + OPT_STRING(0, "head-name", &head_name, N_("head-name"), N_("head name")),
110 + OPT_STRING('S', "gpg-sign", &opts.gpg_sign, N_("gpg-sign"),
111 + N_("GPG-sign commits")),
112 + OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"),
113 + N_("rebase strategy")),
114 + OPT_STRING(0, "strategy-opts", &raw_strategies, N_("strategy-opts"),
115 + N_("strategy options")),
116 + OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto),
117 OPT_END()
118 };
119
@@ -176,6 +190,16 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
190 free(shortrevisions);
191 return !!ret;
192 }
193 + if (command == INIT_BASIC_STATE) {
194 + if (raw_strategies)
195 + parse_strategy_opts(&opts, raw_strategies);
196 +
197 + ret = get_revision_ranges(upstream, onto, &head_hash, NULL, NULL);
198 + if (ret)
199 + return ret;
200 +
201 + return !!write_basic_state(&opts, head_name, onto, head_hash);
202 + }
203
204 usage_with_options(builtin_rebase_helper_usage, options);
205 }
git-rebase--interactive.sh
+6 -1
@@ -57,7 +57,6 @@ init_basic_state () {
57 rm -f "$(git rev-parse --git-path REBASE_HEAD)"
58
59 : > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
60 - write_basic_state
60 }
61
62 git_rebase__interactive () {
@@ -70,6 +69,12 @@ git_rebase__interactive () {
69 git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose}
70 init_basic_state
71
72 + git rebase--helper --init-basic-state ${upstream:+--upstream "$upstream"} \
73 + ${onto:+--onto "$onto"} ${head_name:+--head-name "$head_name"} \
74 + ${verbose:+--verbose} ${strategy:+--strategy "$strategy"} \
75 + ${strategy_opts:+--strategy-opts="$strategy_opts"} \
76 + "$allow_rerere_autoupdate" "$gpg_sign_opt" "$signoff" || exit
77 +
78 git rebase--helper --make-script ${keep_empty:+--keep-empty} \
79 ${rebase_merges:+--rebase-merges} \
80 ${rebase_cousins:+--rebase-cousins} \
sequencer.c
+66 -11
@@ -144,7 +144,7 @@ static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete")
144
145 /*
146 * The following files are written by git-rebase just after parsing the
147 - * command-line (and are only consumed, not modified, by the sequencer).
147 + * command-line.
148 */
149 static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
150 static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head")
@@ -156,6 +156,7 @@ static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash")
156 static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy")
157 static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts")
158 static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate")
159 +static GIT_PATH_FUNC(rebase_path_quiet, "rebase-merge/quiet")
160
161 static int git_sequencer_config(const char *k, const char *v, void *cb)
162 {
@@ -2207,21 +2208,14 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
2208 return 0;
2209 }
2210
2210 -static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
2211 +void parse_strategy_opts(struct replay_opts *opts, char *raw_opts)
2212 {
2213 int i;
2213 - char *strategy_opts_string;
2214 + char *strategy_opts_string = raw_opts;
2215
2215 - strbuf_reset(buf);
2216 - if (!read_oneliner(buf, rebase_path_strategy(), 0))
2217 - return;
2218 - opts->strategy = strbuf_detach(buf, NULL);
2219 - if (!read_oneliner(buf, rebase_path_strategy_opts(), 0))
2220 - return;
2221 -
2222 - strategy_opts_string = buf->buf;
2216 if (*strategy_opts_string == ' ')
2217 strategy_opts_string++;
2218 +
2219 opts->xopts_nr = split_cmdline(strategy_opts_string,
2220 (const char ***)&opts->xopts);
2221 for (i = 0; i < opts->xopts_nr; i++) {
@@ -2232,6 +2226,18 @@ static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
2226 }
2227 }
2228
2229 +static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
2230 +{
2231 + strbuf_reset(buf);
2232 + if (!read_oneliner(buf, rebase_path_strategy(), 0))
2233 + return;
2234 + opts->strategy = strbuf_detach(buf, NULL);
2235 + if (!read_oneliner(buf, rebase_path_strategy_opts(), 0))
2236 + return;
2237 +
2238 + parse_strategy_opts(opts, buf->buf);
2239 +}
2240 +
2241 static int read_populate_opts(struct replay_opts *opts)
2242 {
2243 if (is_rebase_i(opts)) {
@@ -2299,6 +2305,55 @@ static int read_populate_opts(struct replay_opts *opts)
2305 return 0;
2306 }
2307
2308 +static void write_strategy_opts(struct replay_opts *opts)
2309 +{
2310 + int i;
2311 + struct strbuf buf = STRBUF_INIT;
2312 +
2313 + for (i = 0; i < opts->xopts_nr; ++i)
2314 + strbuf_addf(&buf, " --%s", opts->xopts[i]);
2315 +
2316 + write_file(rebase_path_strategy_opts(), "%s\n", buf.buf);
2317 + strbuf_release(&buf);
2318 +}
2319 +
2320 +int write_basic_state(struct replay_opts *opts, const char *head_name,
2321 + const char *onto, const char *orig_head)
2322 +{
2323 + const char *quiet = getenv("GIT_QUIET");
2324 +
2325 + if (head_name)
2326 + write_file(rebase_path_head_name(), "%s\n", head_name);
2327 + if (onto)
2328 + write_file(rebase_path_onto(), "%s\n", onto);
2329 + if (orig_head)
2330 + write_file(rebase_path_orig_head(), "%s\n", orig_head);
2331 +
2332 + if (quiet)
2333 + write_file(rebase_path_quiet(), "%s\n", quiet);
2334 + else
2335 + write_file(rebase_path_quiet(), "\n");
2336 +
2337 + if (opts->verbose)
2338 + write_file(rebase_path_verbose(), "");
2339 + if (opts->strategy)
2340 + write_file(rebase_path_strategy(), "%s\n", opts->strategy);
2341 + if (opts->xopts_nr > 0)
2342 + write_strategy_opts(opts);
2343 +
2344 + if (opts->allow_rerere_auto == RERERE_AUTOUPDATE)
2345 + write_file(rebase_path_allow_rerere_autoupdate(), "--rerere-autoupdate\n");
2346 + else if (opts->allow_rerere_auto == RERERE_NOAUTOUPDATE)
2347 + write_file(rebase_path_allow_rerere_autoupdate(), "--no-rerere-autoupdate\n");
2348 +
2349 + if (opts->gpg_sign)
2350 + write_file(rebase_path_gpg_sign_opt(), "-S%s\n", opts->gpg_sign);
2351 + if (opts->signoff)
2352 + write_file(rebase_path_signoff(), "--signoff\n");
2353 +
2354 + return 0;
2355 +}
2356 +
2357 static int walk_revs_populate_todo(struct todo_list *todo_list,
2358 struct replay_opts *opts)
2359 {
sequencer.h
+4
@@ -119,3 +119,7 @@ int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit);
119 void print_commit_summary(const char *prefix, const struct object_id *oid,
120 unsigned int flags);
121 #endif
122 +
123 +void parse_strategy_opts(struct replay_opts *opts, char *raw_opts);
124 +int write_basic_state(struct replay_opts *opts, const char *head_name,
125 + const char *onto, const char *orig_head);