Skip to content

Commit e702468

Browse files
agrngitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e372252 commit e702468

File tree

4 files changed

+102
-14
lines changed

4 files changed

+102
-14
lines changed

builtin/rebase--helper.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "sequencer.h"
66
#include "rebase-interactive.h"
77
#include "argv-array.h"
8+
#include "rerere.h"
9+
#include "alias.h"
810

911
static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
1012

@@ -53,11 +55,12 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
5355
unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
5456
int abbreviate_commands = 0, rebase_cousins = -1, ret;
5557
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;
5760
enum {
5861
CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
5962
CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, PREPARE_BRANCH,
60-
COMPLETE_ACTION
63+
COMPLETE_ACTION, INIT_BASIC_STATE
6164
} command = 0;
6265
struct option options[] = {
6366
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)
6972
N_("keep original branch points of cousins")),
7073
OPT_BOOL(0, "autosquash", &autosquash,
7174
N_("move commits thas begin with squash!/fixup!")),
75+
OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")),
7276
OPT__VERBOSE(&opts.verbose, N_("be verbose")),
7377
OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
7478
CONTINUE),
@@ -93,13 +97,23 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
9397
N_("prepare the branch to be rebased"), PREPARE_BRANCH),
9498
OPT_CMDMODE(0, "complete-action", &command,
9599
N_("complete the action"), COMPLETE_ACTION),
100+
OPT_CMDMODE(0, "init-basic-state", &command,
101+
N_("initialise the rebase state"), INIT_BASIC_STATE),
96102
OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
97103
OPT_STRING(0, "restrict-revision", &restrict_revision,
98104
N_("restrict-revision"), N_("restrict revision")),
99105
OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"),
100106
N_("squash onto")),
101107
OPT_STRING(0, "upstream", &upstream, N_("upstream"),
102108
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),
103117
OPT_END()
104118
};
105119

@@ -176,6 +190,16 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
176190
free(shortrevisions);
177191
return !!ret;
178192
}
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+
}
179203

180204
usage_with_options(builtin_rebase_helper_usage, options);
181205
}

git-rebase--interactive.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ init_basic_state () {
5757
rm -f "$(git rev-parse --git-path REBASE_HEAD)"
5858

5959
: > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
60-
write_basic_state
6160
}
6261

6362
git_rebase__interactive () {
@@ -70,6 +69,12 @@ git_rebase__interactive () {
7069
git rebase--helper --prepare-branch "$switch_to" ${verbose:+--verbose}
7170
init_basic_state
7271

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+
7378
git rebase--helper --make-script ${keep_empty:+--keep-empty} \
7479
${rebase_merges:+--rebase-merges} \
7580
${rebase_cousins:+--rebase-cousins} \

sequencer.c

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete")
144144

145145
/*
146146
* 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.
148148
*/
149149
static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
150150
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")
156156
static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy")
157157
static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts")
158158
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")
159160

160161
static int git_sequencer_config(const char *k, const char *v, void *cb)
161162
{
@@ -2207,21 +2208,14 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
22072208
return 0;
22082209
}
22092210

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)
22112212
{
22122213
int i;
2213-
char *strategy_opts_string;
2214+
char *strategy_opts_string = raw_opts;
22142215

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;
22232216
if (*strategy_opts_string == ' ')
22242217
strategy_opts_string++;
2218+
22252219
opts->xopts_nr = split_cmdline(strategy_opts_string,
22262220
(const char ***)&opts->xopts);
22272221
for (i = 0; i < opts->xopts_nr; i++) {
@@ -2232,6 +2226,18 @@ static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf)
22322226
}
22332227
}
22342228

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+
22352241
static int read_populate_opts(struct replay_opts *opts)
22362242
{
22372243
if (is_rebase_i(opts)) {
@@ -2299,6 +2305,55 @@ static int read_populate_opts(struct replay_opts *opts)
22992305
return 0;
23002306
}
23012307

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(), "");
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+
23022357
static int walk_revs_populate_todo(struct todo_list *todo_list,
23032358
struct replay_opts *opts)
23042359
{

sequencer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,7 @@ int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit);
116116
void print_commit_summary(const char *prefix, const struct object_id *oid,
117117
unsigned int flags);
118118
#endif
119+
120+
void parse_strategy_opts(struct replay_opts *opts, char *raw_opts);
121+
int write_basic_state(struct replay_opts *opts, const char *head_name,
122+
const char *onto, const char *orig_head);

0 commit comments

Comments
 (0)