Skip to content

Commit a9f5476

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

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

builtin/rebase--helper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
9797
if (command == ADD_EXEC && argc == 2)
9898
return !!sequencer_add_exec_commands(argv[1]);
9999
if (command == APPEND_TODO_HELP && argc == 1)
100-
return !!append_todo_help(0, keep_empty);
100+
return !!append_todo_help_to_file(0, keep_empty);
101101
if (command == EDIT_TODO && argc == 1)
102102
return !!edit_todo_list(flags);
103103
if (command == PREPARE_BRANCH && argc == 2)

rebase-interactive.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
#include "sequencer.h"
55
#include "strbuf.h"
66

7-
int append_todo_help(unsigned edit_todo, unsigned keep_empty)
7+
void append_todo_help(unsigned edit_todo, unsigned keep_empty,
8+
struct strbuf *buf)
89
{
9-
struct strbuf buf = STRBUF_INIT;
10-
FILE *todo;
11-
int ret;
1210
const char *msg = _("\nCommands:\n"
1311
"p, pick <commit> = use commit\n"
1412
"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)
2624
"\n"
2725
"These lines can be re-ordered; they are executed from top to bottom.\n");
2826

29-
todo = fopen_or_warn(rebase_path_todo(), "a");
30-
if (!todo)
31-
return 1;
32-
33-
strbuf_add_commented_lines(&buf, msg, strlen(msg));
27+
strbuf_add_commented_lines(buf, msg, strlen(msg));
3428

3529
if (get_missing_commit_check_level() == MISSING_COMMIT_CHECK_ERROR)
3630
msg = _("\nDo not remove any line. Use 'drop' "
@@ -39,7 +33,7 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty)
3933
msg = _("\nIf you remove a line here "
4034
"THAT COMMIT WILL BE LOST.\n");
4135

42-
strbuf_add_commented_lines(&buf, msg, strlen(msg));
36+
strbuf_add_commented_lines(buf, msg, strlen(msg));
4337

4438
if (edit_todo)
4539
msg = _("\nYou are editing the todo file "
@@ -50,12 +44,25 @@ int append_todo_help(unsigned edit_todo, unsigned keep_empty)
5044
msg = _("\nHowever, if you remove everything, "
5145
"the rebase will be aborted.\n\n");
5246

53-
strbuf_add_commented_lines(&buf, msg, strlen(msg));
47+
strbuf_add_commented_lines(buf, msg, strlen(msg));
5448

5549
if (!keep_empty) {
5650
msg = _("Note that empty commits are commented out");
57-
strbuf_add_commented_lines(&buf, msg, strlen(msg));
51+
strbuf_add_commented_lines(buf, msg, strlen(msg));
5852
}
53+
}
54+
55+
int append_todo_help_to_file(unsigned edit_todo, unsigned keep_empty)
56+
{
57+
struct strbuf buf = STRBUF_INIT;
58+
FILE *todo;
59+
int ret;
60+
61+
todo = fopen_or_warn(rebase_path_todo(), "a");
62+
if (!todo)
63+
return -1;
64+
65+
append_todo_help(edit_todo, keep_empty, &buf);
5966

6067
ret = fputs(buf.buf, todo);
6168
if (ret < 0)
@@ -84,7 +91,17 @@ int edit_todo_list(unsigned flags)
8491
strbuf_release(&buf);
8592

8693
transform_todos(flags | TODO_LIST_SHORTEN_IDS);
87-
append_todo_help(1, 0);
94+
95+
if (strbuf_read_file(&buf, todo_file, 0) < 0)
96+
return error_errno(_("could not read '%s'."), todo_file);
97+
98+
append_todo_help(1, 0, &buf);
99+
if (write_message(buf.buf, buf.len, todo_file, 0)) {
100+
strbuf_release(&buf);
101+
return -1;
102+
}
103+
104+
strbuf_release(&buf);
88105

89106
if (launch_sequence_editor(todo_file, NULL, NULL))
90107
return -1;

rebase-interactive.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef REBASE_INTERACTIVE_H
22
#define REBASE_INTERACTIVE_H
33

4-
int append_todo_help(unsigned edit_todo, unsigned keep_empty);
4+
void append_todo_help(unsigned edit_todo, unsigned keep_empty,
5+
struct strbuf *buf);
6+
int append_todo_help_to_file(unsigned edit_todo, unsigned keep_empty);
57
int edit_todo_list(unsigned flags);
68

79
#endif

0 commit comments

Comments
 (0)