builtin rebase: support --onto

The `--onto` option is important, as it allows to rebase a range of commits onto a different base commit (which gave the command its odd name: "rebase"). This commit introduces options parsing so that different options can be added in future commits. Note: As this commit introduces to the parse_options() call (which "eats" argv[0]), the argc is now expected to be lower by one after this patch, compared to before this patch: argv[0] no longer refers to the command name, but to the first (non-option) command-line parameter. Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pratik Karki committed Sep 4, 2018 at 14:27 UTC f28d40d3a999f99662a7df1363fc7bec9f18a52d
1 file changed +30 -5
builtin/rebase.c
+30 -5
@@ -16,6 +16,16 @@
16 #include "cache-tree.h"
17 #include "unpack-trees.h"
18 #include "lockfile.h"
19 +#include "parse-options.h"
20 +
21 +static char const * const builtin_rebase_usage[] = {
22 + N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
23 + "[<upstream>] [<branch>]"),
24 + N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
25 + "--root [<branch>]"),
26 + N_("git rebase --continue | --abort | --skip | --edit-todo"),
27 + NULL
28 +};
29
30 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
31 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
@@ -301,6 +311,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
311 int ret, flags;
312 struct strbuf msg = STRBUF_INIT;
313 struct strbuf revisions = STRBUF_INIT;
314 + struct option builtin_rebase_options[] = {
315 + OPT_STRING(0, "onto", &options.onto_name,
316 + N_("revision"),
317 + N_("rebase onto given branch instead of upstream")),
318 + OPT_END(),
319 + };
320
321 /*
322 * NEEDSWORK: Once the builtin rebase has been tested enough
@@ -318,13 +334,22 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
334 BUG("sane_execvp() returned???");
335 }
336
321 - if (argc != 2)
322 - die(_("Usage: %s <base>"), argv[0]);
337 + if (argc == 2 && !strcmp(argv[1], "-h"))
338 + usage_with_options(builtin_rebase_usage,
339 + builtin_rebase_options);
340 +
341 prefix = setup_git_directory();
342 trace_repo_setup(prefix);
343 setup_work_tree();
344
345 git_config(git_default_config, NULL);
346 + argc = parse_options(argc, argv, prefix,
347 + builtin_rebase_options,
348 + builtin_rebase_usage, 0);
349 +
350 + if (argc > 2)
351 + usage_with_options(builtin_rebase_usage,
352 + builtin_rebase_options);
353
354 switch (options.type) {
355 case REBASE_MERGE:
@@ -343,10 +368,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
368 }
369
370 if (!options.root) {
346 - if (argc < 2)
371 + if (argc < 1)
372 die("TODO: handle @{upstream}");
373 else {
349 - options.upstream_name = argv[1];
374 + options.upstream_name = argv[0];
375 argc--;
376 argv++;
377 if (!strcmp(options.upstream_name, "-"))
@@ -377,7 +402,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
402 * orig_head -- commit object name of tip of the branch before rebasing
403 * head_name -- refs/heads/<that-branch> or "detached HEAD"
404 */
380 - if (argc > 1)
405 + if (argc > 0)
406 die("TODO: handle switch_to");
407 else {
408 /* Do not need to switch branches, we are already on it. */