Skip to content

Commit f28d40d

Browse files
prertikgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac7f467 commit f28d40d

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

builtin/rebase.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
#include "cache-tree.h"
1717
#include "unpack-trees.h"
1818
#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+
};
1929

2030
static GIT_PATH_FUNC(apply_dir, "rebase-apply")
2131
static GIT_PATH_FUNC(merge_dir, "rebase-merge")
@@ -301,6 +311,12 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
301311
int ret, flags;
302312
struct strbuf msg = STRBUF_INIT;
303313
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+
};
304320

305321
/*
306322
* NEEDSWORK: Once the builtin rebase has been tested enough
@@ -318,13 +334,22 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
318334
BUG("sane_execvp() returned???");
319335
}
320336

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+
323341
prefix = setup_git_directory();
324342
trace_repo_setup(prefix);
325343
setup_work_tree();
326344

327345
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);
328353

329354
switch (options.type) {
330355
case REBASE_MERGE:
@@ -343,10 +368,10 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
343368
}
344369

345370
if (!options.root) {
346-
if (argc < 2)
371+
if (argc < 1)
347372
die("TODO: handle @{upstream}");
348373
else {
349-
options.upstream_name = argv[1];
374+
options.upstream_name = argv[0];
350375
argc--;
351376
argv++;
352377
if (!strcmp(options.upstream_name, "-"))
@@ -377,7 +402,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
377402
* orig_head -- commit object name of tip of the branch before rebasing
378403
* head_name -- refs/heads/<that-branch> or "detached HEAD"
379404
*/
380-
if (argc > 1)
405+
if (argc > 0)
381406
die("TODO: handle switch_to");
382407
else {
383408
/* Do not need to switch branches, we are already on it. */

0 commit comments

Comments
 (0)