Skip to content

Commit 402ba25

Browse files
committed
Merge branch 'pk/rebase-in-c' into pu
Piecemeal rewrite of the "rebase" machinery in C. * pk/rebase-in-c: builtin/rebase: support running "git rebase <upstream>" sequencer: refactor the code to detach HEAD to checkout.c rebase: refactor common shell functions into their own file rebase: start implementing it as a builtin
2 parents b1240c4 + 5a721bc commit 402ba25

File tree

10 files changed

+446
-121
lines changed

10 files changed

+446
-121
lines changed

.gitignore

Lines changed: 2 additions & 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
@@ -117,6 +118,7 @@
117118
/git-read-tree
118119
/git-rebase
119120
/git-rebase--am
121+
/git-rebase--common
120122
/git-rebase--helper
121123
/git-rebase--interactive
122124
/git-rebase--merge

Makefile

Lines changed: 3 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
@@ -619,6 +619,7 @@ SCRIPT_SH += git-web--browse.sh
619619
SCRIPT_LIB += git-mergetool--lib
620620
SCRIPT_LIB += git-parse-remote
621621
SCRIPT_LIB += git-rebase--am
622+
SCRIPT_LIB += git-rebase--common
622623
SCRIPT_LIB += git-rebase--interactive
623624
SCRIPT_LIB += git-rebase--preserve-merges
624625
SCRIPT_LIB += git-rebase--merge
@@ -1071,6 +1072,7 @@ BUILTIN_OBJS += builtin/pull.o
10711072
BUILTIN_OBJS += builtin/push.o
10721073
BUILTIN_OBJS += builtin/range-diff.o
10731074
BUILTIN_OBJS += builtin/read-tree.o
1075+
BUILTIN_OBJS += builtin/rebase.o
10741076
BUILTIN_OBJS += builtin/rebase--helper.o
10751077
BUILTIN_OBJS += builtin/receive-pack.o
10761078
BUILTIN_OBJS += builtin/reflog.o

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ 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_range_diff(int argc, const char **argv, const char *prefix);
205205
extern int cmd_read_tree(int argc, const char **argv, const char *prefix);
206+
extern int cmd_rebase(int argc, const char **argv, const char *prefix);
206207
extern int cmd_rebase__helper(int argc, const char **argv, const char *prefix);
207208
extern int cmd_receive_pack(int argc, const char **argv, const char *prefix);
208209
extern int cmd_reflog(int argc, const char **argv, const char *prefix);

builtin/rebase.c

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
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+
#include "packfile.h"
13+
#include "checkout.h"
14+
#include "refs.h"
15+
#include "quote.h"
16+
17+
static GIT_PATH_FUNC(apply_dir, "rebase-apply");
18+
static GIT_PATH_FUNC(merge_dir, "rebase-merge");
19+
20+
enum rebase_type {
21+
REBASE_AM,
22+
REBASE_MERGE,
23+
REBASE_INTERACTIVE,
24+
REBASE_PRESERVE_MERGES
25+
};
26+
27+
static int use_builtin_rebase(void)
28+
{
29+
struct child_process cp = CHILD_PROCESS_INIT;
30+
struct strbuf out = STRBUF_INIT;
31+
int ret;
32+
33+
argv_array_pushl(&cp.args,
34+
"config", "--bool", "rebase.usebuiltin", NULL);
35+
cp.git_cmd = 1;
36+
if (capture_command(&cp, &out, 6))
37+
return 0;
38+
39+
strbuf_trim(&out);
40+
ret = !strcmp("true", out.buf);
41+
strbuf_release(&out);
42+
return ret;
43+
}
44+
45+
static int apply_autostash(void)
46+
{
47+
warning("TODO");
48+
return 0;
49+
}
50+
51+
struct rebase_options {
52+
enum rebase_type type;
53+
const char *state_dir;
54+
struct commit *upstream;
55+
const char *upstream_name;
56+
char *head_name;
57+
struct object_id orig_head;
58+
struct commit *onto;
59+
const char *onto_name;
60+
const char *revisions;
61+
const char *root;
62+
};
63+
64+
static int finish_rebase(struct rebase_options *opts)
65+
{
66+
struct strbuf dir = STRBUF_INIT;
67+
const char *argv_gc_auto[] = { "gc", "--auto", NULL };
68+
69+
delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
70+
apply_autostash();
71+
close_all_packs(the_repository->objects);
72+
/*
73+
* We ignore errors in 'gc --auto', since the
74+
* user should see them.
75+
*/
76+
run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
77+
strbuf_addstr(&dir, opts->state_dir);
78+
remove_dir_recursively(&dir, 0);
79+
strbuf_release(&dir);
80+
81+
return 0;
82+
}
83+
84+
static struct commit *peel_committish(const char *name)
85+
{
86+
struct object *obj;
87+
struct object_id oid;
88+
89+
if (get_oid(name, &oid))
90+
return NULL;
91+
obj = parse_object(the_repository, &oid);
92+
return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
93+
}
94+
95+
static void add_var(struct strbuf *buf, const char *name, const char *value)
96+
{
97+
strbuf_addstr(buf, name);
98+
strbuf_addstr(buf, "=");
99+
sq_quote_buf(buf, value);
100+
strbuf_addstr(buf, "; ");
101+
}
102+
103+
static int run_specific_rebase(struct rebase_options *opts)
104+
{
105+
const char *argv[] = { NULL, NULL };
106+
struct strbuf script_snippet = STRBUF_INIT;
107+
int status;
108+
const char *backend, *backend_func;
109+
110+
add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
111+
112+
add_var(&script_snippet, "upstream_name", opts->upstream_name);
113+
add_var(&script_snippet, "upstream",
114+
oid_to_hex(&opts->upstream->object.oid));
115+
add_var(&script_snippet, "head_name", opts->head_name);
116+
add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
117+
add_var(&script_snippet, "onto", oid_to_hex(&opts->onto->object.oid));
118+
add_var(&script_snippet, "onto_name", opts->onto_name);
119+
add_var(&script_snippet, "revisions", opts->revisions);
120+
121+
switch (opts->type) {
122+
case REBASE_AM:
123+
backend = "git-rebase--am";
124+
backend_func = "git_rebase__am";
125+
break;
126+
case REBASE_INTERACTIVE:
127+
backend = "git-rebase--interactive";
128+
backend_func = "git_rebase__interactive";
129+
break;
130+
case REBASE_MERGE:
131+
backend = "git-rebase--merge";
132+
backend_func = "git_rebase__merge";
133+
break;
134+
case REBASE_PRESERVE_MERGES:
135+
backend = "git-rebase--preserve-merges";
136+
backend_func = "git_rebase__preserve_merges";
137+
break;
138+
default:
139+
BUG("Unhandled rebase type %d", opts->type);
140+
break;
141+
}
142+
143+
strbuf_addf(&script_snippet,
144+
". git-rebase--common && . %s && %s",
145+
backend, backend_func);
146+
argv[0] = script_snippet.buf;
147+
148+
status = run_command_v_opt(argv, RUN_USING_SHELL);
149+
if (status == 0)
150+
finish_rebase(opts);
151+
else if (status == 2) {
152+
struct strbuf dir = STRBUF_INIT;
153+
154+
apply_autostash();
155+
strbuf_addstr(&dir, opts->state_dir);
156+
remove_dir_recursively(&dir, 0);
157+
strbuf_release(&dir);
158+
die("Nothing to do");
159+
}
160+
161+
strbuf_release(&script_snippet);
162+
163+
return status ? -1 : 0;
164+
}
165+
166+
int cmd_rebase(int argc, const char **argv, const char *prefix)
167+
{
168+
struct rebase_options options = { -1 };
169+
const char *branch_name;
170+
int ret, flags, quiet = 0;
171+
struct strbuf msg = STRBUF_INIT;
172+
struct strbuf revisions = STRBUF_INIT;
173+
const char *restrict_revision = NULL;
174+
175+
/*
176+
* NEEDSWORK: Once the builtin rebase has been tested enough
177+
* and git-legacy-rebase.sh is retired to contrib/, this preamble
178+
* can be removed.
179+
*/
180+
181+
if (!use_builtin_rebase()) {
182+
const char *path = mkpath("%s/git-legacy-rebase",
183+
git_exec_path());
184+
185+
if (sane_execvp(path, (char **)argv) < 0)
186+
die_errno("could not exec %s", path);
187+
else
188+
die("sane_execvp() returned???");
189+
}
190+
191+
if (argc != 2)
192+
die("Usage: %s <base>", argv[0]);
193+
prefix = setup_git_directory();
194+
trace_repo_setup(prefix);
195+
setup_work_tree();
196+
197+
options.type = REBASE_AM;
198+
199+
switch (options.type) {
200+
case REBASE_AM:
201+
options.state_dir = apply_dir();
202+
break;
203+
case REBASE_MERGE:
204+
case REBASE_INTERACTIVE:
205+
case REBASE_PRESERVE_MERGES:
206+
options.state_dir = merge_dir();
207+
break;
208+
}
209+
if (!options.root) {
210+
if (argc != 2)
211+
die("TODO: handle @{upstream}");
212+
else {
213+
options.upstream_name = argv[1];
214+
argc--;
215+
argv++;
216+
if (!strcmp(options.upstream_name, "-"))
217+
options.upstream_name = "@{-1}";
218+
}
219+
options.upstream = peel_committish(options.upstream_name);
220+
if (!options.upstream)
221+
die(_("invalid upstream '%s'"), options.upstream_name);
222+
} else
223+
die("TODO: upstream for --root");
224+
225+
/* Make sure the branch to rebase onto is valid. */
226+
if (!options.onto_name)
227+
options.onto_name = options.upstream_name;
228+
if (strstr(options.onto_name, "...")) {
229+
die("TODO");
230+
} else {
231+
options.onto = peel_committish(options.onto_name);
232+
if (!options.onto)
233+
die(_("Does not point to a valid commit '%s'"),
234+
options.onto_name);
235+
}
236+
237+
/*
238+
* If the branch to rebase is given, that is the branch we will rebase
239+
* branch_name -- branch/commit being rebased, or
240+
* HEAD (already detached)
241+
* orig_head -- commit object name of tip of the branch before rebasing
242+
* head_name -- refs/heads/<that-branch> or "detached HEAD"
243+
*/
244+
if (argc > 1)
245+
die("TODO: handle switch_to");
246+
else {
247+
/* Do not need to switch branches, we are already on it. */
248+
options.head_name =
249+
xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
250+
&flags));
251+
if (!options.head_name)
252+
die(_("No such ref: %s"), "HEAD");
253+
if (flags & REF_ISSYMREF) {
254+
if (!skip_prefix(options.head_name,
255+
"refs/heads/", &branch_name))
256+
branch_name = options.head_name;
257+
258+
} else {
259+
options.head_name = xstrdup("detached HEAD");
260+
branch_name = "HEAD";
261+
}
262+
if (get_oid("HEAD", &options.orig_head))
263+
die(_("Could not resolve HEAD to a revision"));
264+
}
265+
266+
/* Detach HEAD and reset the tree */
267+
if (!quiet)
268+
printf("First, rewinding head to replay your work on top of it...");
269+
270+
strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
271+
if (detach_head_to(&options.onto->object.oid, "checkout", msg.buf))
272+
die(_("Could not detach HEAD"));
273+
strbuf_release(&msg);
274+
if (update_ref("rebase", "ORIG_HEAD", &options.orig_head, NULL, 0,
275+
UPDATE_REFS_MSG_ON_ERR) < 0)
276+
die(_("Could not update ORIG_HEAD to '%s'"),
277+
oid_to_hex(&options.orig_head));
278+
279+
strbuf_addf(&revisions, "%s..%s",
280+
!options.root ? oid_to_hex(&options.onto->object.oid) :
281+
(restrict_revision ? restrict_revision :
282+
oid_to_hex(&options.upstream->object.oid)),
283+
oid_to_hex(&options.orig_head));
284+
285+
options.revisions = revisions.buf;
286+
287+
ret = !!run_specific_rebase(&options);
288+
289+
strbuf_release(&revisions);
290+
free(options.head_name);
291+
return ret;
292+
}

0 commit comments

Comments
 (0)