Skip to content

Commit 8b42093

Browse files
prertikgitster
authored andcommitted
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 retun 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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e333175 commit 8b42093

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
/git-init-db
7979
/git-interpret-trailers
8080
/git-instaweb
81+
/git-legacy-rebase
8182
/git-log
8283
/git-ls-files
8384
/git-ls-remote

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ SCRIPT_SH += git-merge-one-file.sh
609609
SCRIPT_SH += git-merge-resolve.sh
610610
SCRIPT_SH += git-mergetool.sh
611611
SCRIPT_SH += git-quiltimport.sh
612-
SCRIPT_SH += git-rebase.sh
612+
SCRIPT_SH += git-legacy-rebase.sh
613613
SCRIPT_SH += git-remote-testgit.sh
614614
SCRIPT_SH += git-request-pull.sh
615615
SCRIPT_SH += git-stash.sh
@@ -1059,6 +1059,7 @@ BUILTIN_OBJS += builtin/prune.o
10591059
BUILTIN_OBJS += builtin/pull.o
10601060
BUILTIN_OBJS += builtin/push.o
10611061
BUILTIN_OBJS += builtin/read-tree.o
1062+
BUILTIN_OBJS += builtin/rebase.o
10621063
BUILTIN_OBJS += builtin/rebase--helper.o
10631064
BUILTIN_OBJS += builtin/receive-pack.o
10641065
BUILTIN_OBJS += builtin/reflog.o

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ extern int cmd_prune_packed(int argc, const char **argv, const char *prefix);
202202
extern int cmd_pull(int argc, const char **argv, const char *prefix);
203203
extern int cmd_push(int argc, const char **argv, const char *prefix);
204204
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);
205206
extern int cmd_rebase__helper(int argc, const char **argv, const char *prefix);
206207
extern int cmd_receive_pack(int argc, const char **argv, const char *prefix);
207208
extern int cmd_reflog(int argc, const char **argv, const char *prefix);

builtin/rebase.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
return 0;
24+
25+
strbuf_trim(&out);
26+
ret = !strcmp("true", out.buf);
27+
strbuf_release(&out);
28+
return ret;
29+
}
30+
31+
int cmd_rebase(int argc, const char **argv, const char *prefix)
32+
{
33+
/*
34+
* NEEDSWORK: Once the builtin rebase has been tested enough
35+
* and git-legacy-rebase.sh is retired to contrib/, this preamble
36+
* can be removed.
37+
*/
38+
39+
if (!use_builtin_rebase()) {
40+
const char *path = mkpath("%s/git-legacy-rebase",
41+
git_exec_path());
42+
43+
if (sane_execvp(path, (char **)argv) < 0)
44+
die_errno("could not exec %s", path);
45+
else
46+
die("sane_execvp() returned???");
47+
}
48+
49+
if (argc != 2)
50+
die("Usage: %s <base>", argv[0]);
51+
prefix = setup_git_directory();
52+
trace_repo_setup(prefix);
53+
setup_work_tree();
54+
55+
die("TODO");
56+
}
File renamed without changes.

git.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,12 @@ static struct cmd_struct commands[] = {
518518
{ "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE },
519519
{ "push", cmd_push, RUN_SETUP },
520520
{ "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX},
521+
/*
522+
* NEEDSWORK: Until the rebase is independent and needs no redirection
523+
* to rebase shell script this is kept as is, then should be changed to
524+
* RUN_SETUP | NEED_WORK_TREE
525+
*/
526+
{ "rebase", cmd_rebase },
521527
{ "rebase--helper", cmd_rebase__helper, RUN_SETUP | NEED_WORK_TREE },
522528
{ "receive-pack", cmd_receive_pack },
523529
{ "reflog", cmd_reflog, RUN_SETUP },

0 commit comments

Comments
 (0)