Skip to content

Commit a1c7576

Browse files
dschogitster
authored andcommitted
sequencer: allow editing the commit message on a case-by-case basis
In the upcoming commits, we will implement more and more of rebase -i's functionality inside the sequencer. One particular feature of the commands to come is that some of them allow editing the commit message while others don't, i.e. we cannot define in the replay_opts whether the commit message should be edited or not. Let's add a new parameter to the run_git_commit() function. Previously, it was the duty of the caller to ensure that the opts->edit setting indicates whether to let the user edit the commit message or not, indicating that it is an "all or nothing" setting, i.e. that the sequencer wants to let the user edit *all* commit message, or none at all. In the upcoming rebase -i mode, it will depend on the particular command that is currently executed, though. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1dfc84e commit a1c7576

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

sequencer.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "merge-recursive.h"
1616
#include "refs.h"
1717
#include "argv-array.h"
18+
#include "quote.h"
1819

1920
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
2021

@@ -33,6 +34,11 @@ static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
3334
* being rebased.
3435
*/
3536
static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
37+
/*
38+
* The following files are written by git-rebase just after parsing the
39+
* command-line (and are only consumed, not modified, by the sequencer).
40+
*/
41+
static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
3642

3743
/* We will introduce the 'interactive rebase' mode later */
3844
static inline int is_rebase_i(const struct replay_opts *opts)
@@ -132,6 +138,16 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
132138
return 1;
133139
}
134140

141+
static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
142+
{
143+
static struct strbuf buf = STRBUF_INIT;
144+
145+
strbuf_reset(&buf);
146+
if (opts->gpg_sign)
147+
sq_quotef(&buf, "-S%s", opts->gpg_sign);
148+
return buf.buf;
149+
}
150+
135151
int sequencer_remove_state(struct replay_opts *opts)
136152
{
137153
struct strbuf dir = STRBUF_INIT;
@@ -468,7 +484,7 @@ static char **read_author_script(void)
468484
* author metadata.
469485
*/
470486
static int run_git_commit(const char *defmsg, struct replay_opts *opts,
471-
int allow_empty)
487+
int allow_empty, int edit)
472488
{
473489
char **env = NULL;
474490
struct argv_array array;
@@ -477,17 +493,20 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
477493

478494
if (is_rebase_i(opts)) {
479495
env = read_author_script();
480-
if (!env)
496+
if (!env) {
497+
const char *gpg_opt = gpg_sign_opt_quoted(opts);
498+
481499
return error("You have staged changes in your working "
482500
"tree. If these changes are meant to be\n"
483501
"squashed into the previous commit, run:\n\n"
484-
" git commit --amend $gpg_sign_opt_quoted\n\n"
502+
" git commit --amend %s\n\n"
485503
"If they are meant to go into a new commit, "
486504
"run:\n\n"
487-
" git commit $gpg_sign_opt_quoted\n\n"
505+
" git commit %s\n\n"
488506
"In both cases, once you're done, continue "
489507
"with:\n\n"
490-
" git rebase --continue\n");
508+
" git rebase --continue\n", gpg_opt, gpg_opt);
509+
}
491510
}
492511

493512
argv_array_init(&array);
@@ -500,7 +519,7 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
500519
argv_array_push(&array, "-s");
501520
if (defmsg)
502521
argv_array_pushl(&array, "-F", defmsg, NULL);
503-
if (opts->edit)
522+
if (edit)
504523
argv_array_push(&array, "-e");
505524
else if (!opts->signoff && !opts->record_origin &&
506525
git_config_get_value("commit.cleanup", &value))
@@ -767,7 +786,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
767786
}
768787
if (!opts->no_commit)
769788
res = run_git_commit(opts->edit ? NULL : git_path_merge_msg(),
770-
opts, allow);
789+
opts, allow, opts->edit);
771790

772791
leave:
773792
free_message(commit, &msg);
@@ -989,8 +1008,21 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
9891008

9901009
static int read_populate_opts(struct replay_opts *opts)
9911010
{
992-
if (is_rebase_i(opts))
1011+
if (is_rebase_i(opts)) {
1012+
struct strbuf buf = STRBUF_INIT;
1013+
1014+
if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) {
1015+
if (!starts_with(buf.buf, "-S"))
1016+
strbuf_reset(&buf);
1017+
else {
1018+
free(opts->gpg_sign);
1019+
opts->gpg_sign = xstrdup(buf.buf + 2);
1020+
}
1021+
}
1022+
strbuf_release(&buf);
1023+
9931024
return 0;
1025+
}
9941026

9951027
if (!file_exists(git_path_opts_file()))
9961028
return 0;

0 commit comments

Comments
 (0)