rebase: start implementing it as a builtin

This commit imitates the strategy that was used to convert the difftool to a builtin. We start by renaming the shell script `git-rebase.sh` to `git-legacy-rebase.sh` and introduce a `builtin/rebase.c` that simply executes the shell script version, unless the config setting `rebase.useBuiltin` is set to `true`. The motivation behind this is to rewrite all the functionality of the shell script version in the aforementioned `rebase.c`, one by one and be able to conveniently test new features by configuring `rebase.useBuiltin`. In the original difftool conversion, if sane_execvp() that attempts to run the legacy scripted version returned with non-negative status, the command silently exited without doing anything with success, but sane_execvp() should not return with non-negative status in the first place, so we use die() to notice such an abnormal case. We intentionally avoid reading the config directly to avoid messing up the GIT_* environment variables when we need to fall back to exec()ing the shell script. The test of builtin rebase can be done by `git -c rebase.useBuiltin=true rebase ...` Signed-off-by: Pratik Karki <predatoramigo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pratik Karki committed Aug 7, 2018 at 01:16 UTC 55071ea248ef8040e4b29575376273e4dd061683
6 files changed +68 -1
.gitignore
+1
@@ -78,6 +78,7 @@
78 /git-init-db
79 /git-interpret-trailers
80 /git-instaweb
81 +/git-legacy-rebase
82 /git-log
83 /git-ls-files
84 /git-ls-remote
Makefile
+2 -1
@@ -609,7 +609,7 @@ SCRIPT_SH += git-merge-one-file.sh
609 SCRIPT_SH += git-merge-resolve.sh
610 SCRIPT_SH += git-mergetool.sh
611 SCRIPT_SH += git-quiltimport.sh
612 -SCRIPT_SH += git-rebase.sh
612 +SCRIPT_SH += git-legacy-rebase.sh
613 SCRIPT_SH += git-remote-testgit.sh
614 SCRIPT_SH += git-request-pull.sh
615 SCRIPT_SH += git-stash.sh
@@ -1063,6 +1063,7 @@ BUILTIN_OBJS += builtin/prune.o
1063 BUILTIN_OBJS += builtin/pull.o
1064 BUILTIN_OBJS += builtin/push.o
1065 BUILTIN_OBJS += builtin/read-tree.o
1066 +BUILTIN_OBJS += builtin/rebase.o
1067 BUILTIN_OBJS += builtin/rebase--helper.o
1068 BUILTIN_OBJS += builtin/receive-pack.o
1069 BUILTIN_OBJS += builtin/reflog.o
builtin.h
+1
@@ -202,6 +202,7 @@ extern int cmd_prune_packed(int argc, const char **argv, const char *prefix);
202 extern int cmd_pull(int argc, const char **argv, const char *prefix);
203 extern int cmd_push(int argc, const char **argv, const char *prefix);
204 extern int cmd_read_tree(int argc, const char **argv, const char *prefix);
205 +extern int cmd_rebase(int argc, const char **argv, const char *prefix);
206 extern int cmd_rebase__helper(int argc, const char **argv, const char *prefix);
207 extern int cmd_receive_pack(int argc, const char **argv, const char *prefix);
208 extern int cmd_reflog(int argc, const char **argv, const char *prefix);
builtin/rebase.c new
+58
@@ -0,0 +1,58 @@
1 +/*
2 + * "git rebase" builtin command
3 + *
4 + * Copyright (c) 2018 Pratik Karki
5 + */
6 +
7 +#include "builtin.h"
8 +#include "run-command.h"
9 +#include "exec-cmd.h"
10 +#include "argv-array.h"
11 +#include "dir.h"
12 +
13 +static int use_builtin_rebase(void)
14 +{
15 + struct child_process cp = CHILD_PROCESS_INIT;
16 + struct strbuf out = STRBUF_INIT;
17 + int ret;
18 +
19 + argv_array_pushl(&cp.args,
20 + "config", "--bool", "rebase.usebuiltin", NULL);
21 + cp.git_cmd = 1;
22 + if (capture_command(&cp, &out, 6)) {
23 + strbuf_release(&out);
24 + return 0;
25 + }
26 +
27 + strbuf_trim(&out);
28 + ret = !strcmp("true", out.buf);
29 + strbuf_release(&out);
30 + return ret;
31 +}
32 +
33 +int cmd_rebase(int argc, const char **argv, const char *prefix)
34 +{
35 + /*
36 + * NEEDSWORK: Once the builtin rebase has been tested enough
37 + * and git-legacy-rebase.sh is retired to contrib/, this preamble
38 + * can be removed.
39 + */
40 +
41 + if (!use_builtin_rebase()) {
42 + const char *path = mkpath("%s/git-legacy-rebase",
43 + git_exec_path());
44 +
45 + if (sane_execvp(path, (char **)argv) < 0)
46 + die_errno(_("could not exec %s"), path);
47 + else
48 + BUG("sane_execvp() returned???");
49 + }
50 +
51 + if (argc != 2)
52 + die(_("Usage: %s <base>"), argv[0]);
53 + prefix = setup_git_directory();
54 + trace_repo_setup(prefix);
55 + setup_work_tree();
56 +
57 + die("TODO");
58 +}
git-legacy-rebase.sh renamed
git.c
+6
@@ -521,6 +521,12 @@ static struct cmd_struct commands[] = {
521 { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE },
522 { "push", cmd_push, RUN_SETUP },
523 { "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX},
524 + /*
525 + * NEEDSWORK: Until the rebase is independent and needs no redirection
526 + * to rebase shell script this is kept as is, then should be changed to
527 + * RUN_SETUP | NEED_WORK_TREE
528 + */
529 + { "rebase", cmd_rebase },
530 { "rebase--helper", cmd_rebase__helper, RUN_SETUP | NEED_WORK_TREE },
531 { "receive-pack", cmd_receive_pack },
532 { "reflog", cmd_reflog, RUN_SETUP },