1#include "builtin.h"
2#include "cache.h"
3#include "config.h"
4#include "parse-options.h"
5#include "sequencer.h"
6#include "rebase-interactive.h"
7#include "argv-array.h"
8
9static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
10
11static int get_revision_ranges(const char *upstream, const char *onto,
12 const char **head_hash,
13 char **revisions, char **shortrevisions)
14{
15 const char *base_rev = upstream ? upstream : onto;
16 struct object_id orig_head;
17
18 if (get_oid("HEAD", &orig_head))
19 return error(_("no HEAD?"));
20
21 *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
22
23 if (revisions)
24 *revisions = xstrfmt("%s...%s", base_rev, *head_hash);
25 if (shortrevisions) {
26 const char *shorthead;
27
28 shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV);
29
30 if (upstream) {
31 const char *shortrev;
32 struct object_id rev_oid;
33
34 get_oid(base_rev, &rev_oid);
35 shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV);
36
37 *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
38 } else
39 *shortrevisions = xstrdup(shorthead);
40 }
41
42 return 0;
43}
44
45static const char * const builtin_rebase_helper_usage[] = {
46 N_("git rebase--helper [<options>]"),
47 NULL
48};
49
50int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
51{
52 struct replay_opts opts = REPLAY_OPTS_INIT;
53 unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
54 int abbreviate_commands = 0, rebase_cousins = -1, ret;
55 const char *head_hash = NULL, *onto = NULL, *restrict_revision = NULL,
56 *squash_onto = NULL, *upstream = NULL;
57 enum {
58 CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
59 CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC, EDIT_TODO, PREPARE_BRANCH,
60 COMPLETE_ACTION
61 } command = 0;
62 struct option options[] = {
63 OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
64 OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")),
65 OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message,
66 N_("allow commits with empty messages")),
67 OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
68 OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
69 N_("keep original branch points of cousins")),
70 OPT_BOOL(0, "autosquash", &autosquash,
71 N_("move commits that begin with squash!/fixup!")),
72 OPT__VERBOSE(&opts.verbose, N_("be verbose")),
73 OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
74 CONTINUE),
75 OPT_CMDMODE(0, "abort", &command, N_("abort rebase"),
76 ABORT),
77 OPT_CMDMODE(0, "make-script", &command,
78 N_("make rebase script"), MAKE_SCRIPT),
79 OPT_CMDMODE(0, "shorten-ids", &command,
80 N_("shorten commit ids in the todo list"), SHORTEN_OIDS),
81 OPT_CMDMODE(0, "expand-ids", &command,
82 N_("expand commit ids in the todo list"), EXPAND_OIDS),
83 OPT_CMDMODE(0, "check-todo-list", &command,
84 N_("check the todo list"), CHECK_TODO_LIST),
85 OPT_CMDMODE(0, "rearrange-squash", &command,
86 N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
87 OPT_CMDMODE(0, "add-exec-commands", &command,
88 N_("insert exec commands in todo list"), ADD_EXEC),
89 OPT_CMDMODE(0, "edit-todo", &command,
90 N_("edit the todo list during an interactive rebase"),
91 EDIT_TODO),
92 OPT_CMDMODE(0, "prepare-branch", &command,
93 N_("prepare the branch to be rebased"), PREPARE_BRANCH),
94 OPT_CMDMODE(0, "complete-action", &command,
95 N_("complete the action"), COMPLETE_ACTION),
96 OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
97 OPT_STRING(0, "restrict-revision", &restrict_revision,
98 N_("restrict-revision"), N_("restrict revision")),
99 OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"),
100 N_("squash onto")),
101 OPT_STRING(0, "upstream", &upstream, N_("upstream"),
102 N_("the upstream commit")),
103 OPT_END()
104 };
105
106 sequencer_init_config(&opts);
107 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
108
109 opts.action = REPLAY_INTERACTIVE_REBASE;
110 opts.allow_ff = 1;
111 opts.allow_empty = 1;
112
113 argc = parse_options(argc, argv, NULL, options,
114 builtin_rebase_helper_usage, PARSE_OPT_KEEP_ARGV0);
115
116 flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
117 flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
118 flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
119 flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
120 flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0;
121
122 if (rebase_cousins >= 0 && !rebase_merges)
123 warning(_("--[no-]rebase-cousins has no effect without "
124 "--rebase-merges"));
125
126 if (command == CONTINUE && argc == 1)
127 return !!sequencer_continue(&opts);
128 if (command == ABORT && argc == 1)
129 return !!sequencer_remove_state(&opts);
130 if (command == MAKE_SCRIPT && argc == 1) {
131 char *revisions = NULL;
132 struct argv_array make_script_args = ARGV_ARRAY_INIT;
133
134 if (!upstream && squash_onto)
135 write_file(path_squash_onto(), "%s\n", squash_onto);
136
137 ret = get_revision_ranges(upstream, onto, &head_hash, &revisions, NULL);
138 if (ret)
139 return ret;
140
141 argv_array_pushl(&make_script_args, "", revisions, NULL);
142 if (restrict_revision)
143 argv_array_push(&make_script_args, restrict_revision);
144
145 ret = sequencer_make_script(stdout,
146 make_script_args.argc, make_script_args.argv,
147 flags);
148
149 free(revisions);
150 argv_array_clear(&make_script_args);
151
152 return !!ret;
153 }
154 if ((command == SHORTEN_OIDS || command == EXPAND_OIDS) && argc == 1)
155 return !!transform_todos(flags);
156 if (command == CHECK_TODO_LIST && argc == 1)
157 return !!check_todo_list();
158 if (command == REARRANGE_SQUASH && argc == 1)
159 return !!rearrange_squash();
160 if (command == ADD_EXEC && argc == 2)
161 return !!sequencer_add_exec_commands(argv[1]);
162 if (command == EDIT_TODO && argc == 1)
163 return !!edit_todo_list(flags);
164 if (command == PREPARE_BRANCH && argc == 2)
165 return !!prepare_branch_to_be_rebased(&opts, argv[1]);
166 if (command == COMPLETE_ACTION && argc == 3) {
167 char *shortrevisions = NULL;
168
169 ret = get_revision_ranges(upstream, onto, &head_hash, NULL, &shortrevisions);
170 if (ret)
171 return ret;
172
173 ret = complete_action(&opts, flags, shortrevisions, argv[1], onto,
174 head_hash, argv[2], autosquash);
175
176 free(shortrevisions);
177 return !!ret;
178 }
179
180 usage_with_options(builtin_rebase_helper_usage, options);
181}