rebase--interactive: move rearrange_squash_in_todo_file()

As rearrange_squash_in_todo_file() is only needed inside of rebase--interactive.c for `rebase -p', it is moved there from sequencer.c. The parameter r (repository) is dropped along the way, and the error handling is slightly improved. Signed-off-by: Alban Gruin <alban.gruin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alban Gruin committed Mar 5, 2019 at 20:17 UTC 79d7e883bb69803daf4a6f44692cb8fc0eee5b24
3 files changed +29 -29
builtin/rebase--interactive.c
+27 -1
@@ -38,6 +38,32 @@ static int add_exec_commands(struct string_list *commands)
38 return 0;
39 }
40
41 +static int rearrange_squash_in_todo_file(void)
42 +{
43 + const char *todo_file = rebase_path_todo();
44 + struct todo_list todo_list = TODO_LIST_INIT;
45 + int res = 0;
46 +
47 + if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
48 + return error_errno(_("could not read '%s'."), todo_file);
49 + if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
50 + &todo_list)) {
51 + todo_list_release(&todo_list);
52 + return error(_("unusable todo list: '%s'"), todo_file);
53 + }
54 +
55 + res = todo_list_rearrange_squash(&todo_list);
56 + if (!res)
57 + res = todo_list_write_to_file(the_repository, &todo_list,
58 + todo_file, NULL, NULL, -1, 0);
59 +
60 + todo_list_release(&todo_list);
61 +
62 + if (res)
63 + return error_errno(_("could not write '%s'."), todo_file);
64 + return 0;
65 +}
66 +
67 static int get_revision_ranges(const char *upstream, const char *onto,
68 const char **head_hash,
69 char **revisions, char **shortrevisions)
@@ -288,7 +314,7 @@ int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
314 ret = check_todo_list_from_file(the_repository);
315 break;
316 case REARRANGE_SQUASH:
291 - ret = rearrange_squash_in_todo_file(the_repository);
317 + ret = rearrange_squash_in_todo_file();
318 break;
319 case ADD_EXEC:
320 ret = add_exec_commands(&commands);
sequencer.c
+1 -27
@@ -4793,8 +4793,6 @@ static int skip_unnecessary_picks(struct repository *r, struct object_id *output
4793 return 0;
4794 }
4795
4796 -static int todo_list_rearrange_squash(struct todo_list *todo_list);
4797 -
4796 int complete_action(struct repository *r, struct replay_opts *opts, unsigned flags,
4797 const char *shortrevisions, const char *onto_name,
4798 const char *onto, const char *orig_head, struct string_list *commands,
@@ -4906,7 +4904,7 @@ define_commit_slab(commit_todo_item, struct todo_item *);
4904 * message will have to be retrieved from the commit (as the oneline in the
4905 * script cannot be trusted) in order to normalize the autosquash arrangement.
4906 */
4909 -static int todo_list_rearrange_squash(struct todo_list *todo_list)
4907 +int todo_list_rearrange_squash(struct todo_list *todo_list)
4908 {
4909 struct hashmap subject2item;
4910 int rearranged = 0, *next, *tail, i, nr = 0, alloc = 0;
@@ -5044,27 +5042,3 @@ static int todo_list_rearrange_squash(struct todo_list *todo_list)
5042
5043 return 0;
5044 }
5047 -
5048 -int rearrange_squash_in_todo_file(struct repository *r)
5049 -{
5050 - const char *todo_file = rebase_path_todo();
5051 - struct todo_list todo_list = TODO_LIST_INIT;
5052 - int res = 0;
5053 -
5054 - if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
5055 - return -1;
5056 - if (todo_list_parse_insn_buffer(r, todo_list.buf.buf, &todo_list) < 0) {
5057 - todo_list_release(&todo_list);
5058 - return -1;
5059 - }
5060 -
5061 - res = todo_list_rearrange_squash(&todo_list);
5062 - if (!res)
5063 - res = todo_list_write_to_file(r, &todo_list, todo_file, NULL, NULL, -1, 0);
5064 -
5065 - todo_list_release(&todo_list);
5066 -
5067 - if (res)
5068 - return error_errno(_("could not write '%s'."), todo_file);
5069 - return 0;
5070 -}
sequencer.h
+1 -1
@@ -153,7 +153,7 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
153 const char *shortrevisions, const char *onto_name,
154 const char *onto, const char *orig_head, struct string_list *commands,
155 unsigned autosquash, struct todo_list *todo_list);
156 -int rearrange_squash_in_todo_file(struct repository *r);
156 +int todo_list_rearrange_squash(struct todo_list *todo_list);
157
158 extern const char sign_off_header[];
159