sequencer: refactor append_todo_help() to write its message to a buffer

This refactors append_todo_help() to write its message to a buffer
instead of the todo-list.  This is needed for the rewrite of
complete_action(), which will come after the next commit.

As rebase--helper still needs the file manipulation part of
append_todo_help(), it is extracted to a temporary function,
append_todo_help_to_file().  This function will go away after the
rewrite of complete_action().

Signed-off-by: Alban Gruin <alban.gruin@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Alban Gruin 2018-08-10 18:51:35 +02:00 committed by Junio C Hamano
parent 4df66c40b0
commit a9f5476fbc
3 changed files with 34 additions and 15 deletions

View File

@ -97,7 +97,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
if (command == ADD_EXEC && argc == 2) if (command == ADD_EXEC && argc == 2)
return !!sequencer_add_exec_commands(argv[1]); return !!sequencer_add_exec_commands(argv[1]);
if (command == APPEND_TODO_HELP && argc == 1) if (command == APPEND_TODO_HELP && argc == 1)
return !!append_todo_help(0, keep_empty); return !!append_todo_help_to_file(0, keep_empty);
if (command == EDIT_TODO && argc == 1) if (command == EDIT_TODO && argc == 1)
return !!edit_todo_list(flags); return !!edit_todo_list(flags);
if (command == PREPARE_BRANCH && argc == 2) if (command == PREPARE_BRANCH && argc == 2)

View File

@ -4,11 +4,9 @@
#include "sequencer.h" #include "sequencer.h"
#include "strbuf.h" #include "strbuf.h"
int append_todo_help(unsigned edit_todo, unsigned keep_empty) void append_todo_help(unsigned edit_todo, unsigned keep_empty,
struct strbuf *buf)
{ {
struct strbuf buf = STRBUF_INIT;
FILE *todo;
int ret;
const char *msg = _("\nCommands:\n" const char *msg = _("\nCommands:\n"
"p, pick <commit> = use commit\n" "p, pick <commit> = use commit\n"
"r, reword <commit> = use commit, but edit the commit message\n" "r, reword <commit> = use commit, but edit the commit message\n"
@ -26,11 +24,7 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty)
"\n" "\n"
"These lines can be re-ordered; they are executed from top to bottom.\n"); "These lines can be re-ordered; they are executed from top to bottom.\n");
todo = fopen_or_warn(rebase_path_todo(), "a"); strbuf_add_commented_lines(buf, msg, strlen(msg));
if (!todo)
return 1;
strbuf_add_commented_lines(&buf, msg, strlen(msg));
if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR) if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
msg = _("\nDo not remove any line. Use 'drop' " msg = _("\nDo not remove any line. Use 'drop' "
@ -39,7 +33,7 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty)
msg = _("\nIf you remove a line here " msg = _("\nIf you remove a line here "
"THAT COMMIT WILL BE LOST.\n"); "THAT COMMIT WILL BE LOST.\n");
strbuf_add_commented_lines(&buf, msg, strlen(msg)); strbuf_add_commented_lines(buf, msg, strlen(msg));
if (edit_todo) if (edit_todo)
msg = _("\nYou are editing the todo file " msg = _("\nYou are editing the todo file "
@ -50,12 +44,25 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty)
msg = _("\nHowever, if you remove everything, " msg = _("\nHowever, if you remove everything, "
"the rebase will be aborted.\n\n"); "the rebase will be aborted.\n\n");
strbuf_add_commented_lines(&buf, msg, strlen(msg)); strbuf_add_commented_lines(buf, msg, strlen(msg));
if (!keep_empty) { if (!keep_empty) {
msg = _("Note that empty commits are commented out"); msg = _("Note that empty commits are commented out");
strbuf_add_commented_lines(&buf, msg, strlen(msg)); strbuf_add_commented_lines(buf, msg, strlen(msg));
} }
}
int append_todo_help_to_file(unsigned edit_todo, unsigned keep_empty)
{
struct strbuf buf = STRBUF_INIT;
FILE *todo;
int ret;
todo = fopen_or_warn(rebase_path_todo(), "a");
if (!todo)
return -1;
append_todo_help(edit_todo, keep_empty, &buf);
ret = fputs(buf.buf, todo); ret = fputs(buf.buf, todo);
if (ret < 0) if (ret < 0)
@ -84,7 +91,17 @@ int edit_todo_list(unsigned flags)
strbuf_release(&buf); strbuf_release(&buf);
transform_todos(flags | TODO_LIST_SHORTEN_IDS); transform_todos(flags | TODO_LIST_SHORTEN_IDS);
append_todo_help(1, 0);
if (strbuf_read_file(&buf, todo_file, 0) < 0)
return error_errno(_("could not read '%s'."), todo_file);
append_todo_help(1, 0, &buf);
if (write_message(buf.buf, buf.len, todo_file, 0)) {
strbuf_release(&buf);
return -1;
}
strbuf_release(&buf);
if (launch_sequence_editor(todo_file, NULL, NULL)) if (launch_sequence_editor(todo_file, NULL, NULL))
return -1; return -1;

View File

@ -1,7 +1,9 @@
#ifndef REBASE_INTERACTIVE_H #ifndef REBASE_INTERACTIVE_H
#define REBASE_INTERACTIVE_H #define REBASE_INTERACTIVE_H
int append_todo_help(unsigned edit_todo, unsigned keep_empty); void append_todo_help(unsigned edit_todo, unsigned keep_empty,
struct strbuf *buf);
int append_todo_help_to_file(unsigned edit_todo, unsigned keep_empty);
int edit_todo_list(unsigned flags); int edit_todo_list(unsigned flags);
#endif #endif