fast-rebase: demonstrate merge-ort's API via new test-tool command

Add a new test-tool command named 'fast-rebase', which is a super-slimmed down and nowhere near as capable version of 'git rebase'. 'test-tool fast-rebase' is not currently planned for usage in the testsuite, but is here for two purposes: 1) Demonstrate the desired API of merge-ort. In particular, fast-rebase takes advantage of the separation of the merging operation from the updating of the index and working tree, to allow it to pick N commits, but only update the index and working tree once at the end. Look for the calls to merge_incore_nonrecursive() and merge_switch_to_result(). 2) Provide a convenient benchmark that isn't polluted by the heavy disk writing and forking of unnecessary processes that comes from sequencer.c and merge-recursive.c. fast-rebase is not meant to replace sequencer.c, just give ideas on how sequencer.c can be changed. Updating sequencer.c with these goals is probably a large amount of work; writing a simple targeted command with no documentation, less-than-useful help messages, numerous limitations in terms of flags it can accept and situations it can handle, and which is flagged off from users is a much easier interim step. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Oct 29, 2020 at 20:32 UTC fe1a21d5267cae88d4312e3595909720717eb31c
4 files changed +214
Makefile
+1
@@ -704,6 +704,7 @@ TEST_BUILTINS_OBJS += test-dump-fsmonitor.o
704 TEST_BUILTINS_OBJS += test-dump-split-index.o
705 TEST_BUILTINS_OBJS += test-dump-untracked-cache.o
706 TEST_BUILTINS_OBJS += test-example-decorate.o
707 +TEST_BUILTINS_OBJS += test-fast-rebase.o
708 TEST_BUILTINS_OBJS += test-genrandom.o
709 TEST_BUILTINS_OBJS += test-genzeros.o
710 TEST_BUILTINS_OBJS += test-hash-speed.o
t/helper/test-fast-rebase.c new
+211
@@ -0,0 +1,211 @@
1 +/*
2 + * "git fast-rebase" builtin command
3 + *
4 + * FAST: Forking Any Subprocesses (is) Taboo
5 + *
6 + * This is meant SOLELY as a demo of what is possible. sequencer.c and
7 + * rebase.c should be refactored to use the ideas here, rather than attempting
8 + * to extend this file to replace those (unless Phillip or Dscho say that
9 + * refactoring is too hard and we need a clean slate, but I'm guessing that
10 + * refactoring is the better route).
11 + */
12 +
13 +#define USE_THE_INDEX_COMPATIBILITY_MACROS
14 +#include "test-tool.h"
15 +
16 +#include "cache-tree.h"
17 +#include "commit.h"
18 +#include "lockfile.h"
19 +#include "merge-ort.h"
20 +#include "refs.h"
21 +#include "revision.h"
22 +#include "sequencer.h"
23 +#include "strvec.h"
24 +#include "tree.h"
25 +
26 +static const char *short_commit_name(struct commit *commit)
27 +{
28 + return find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV);
29 +}
30 +
31 +static struct commit *peel_committish(const char *name)
32 +{
33 + struct object *obj;
34 + struct object_id oid;
35 +
36 + if (get_oid(name, &oid))
37 + return NULL;
38 + obj = parse_object(the_repository, &oid);
39 + return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
40 +}
41 +
42 +static char *get_author(const char *message)
43 +{
44 + size_t len;
45 + const char *a;
46 +
47 + a = find_commit_header(message, "author", &len);
48 + if (a)
49 + return xmemdupz(a, len);
50 +
51 + return NULL;
52 +}
53 +
54 +static struct commit *create_commit(struct tree *tree,
55 + struct commit *based_on,
56 + struct commit *parent)
57 +{
58 + struct object_id ret;
59 + struct object *obj;
60 + struct commit_list *parents = NULL;
61 + char *author;
62 + char *sign_commit = NULL;
63 + struct commit_extra_header *extra;
64 + struct strbuf msg = STRBUF_INIT;
65 + const char *out_enc = get_commit_output_encoding();
66 + const char *message = logmsg_reencode(based_on, NULL, out_enc);
67 + const char *orig_message = NULL;
68 + const char *exclude_gpgsig[] = { "gpgsig", NULL };
69 +
70 + commit_list_insert(parent, &parents);
71 + extra = read_commit_extra_headers(based_on, exclude_gpgsig);
72 + find_commit_subject(message, &orig_message);
73 + strbuf_addstr(&msg, orig_message);
74 + author = get_author(message);
75 + reset_ident_date();
76 + if (commit_tree_extended(msg.buf, msg.len, &tree->object.oid, parents,
77 + &ret, author, NULL, sign_commit, extra)) {
78 + error(_("failed to write commit object"));
79 + return NULL;
80 + }
81 + free(author);
82 + strbuf_release(&msg);
83 +
84 + obj = parse_object(the_repository, &ret);
85 + return (struct commit *)obj;
86 +}
87 +
88 +int cmd__fast_rebase(int argc, const char **argv)
89 +{
90 + struct commit *onto;
91 + struct commit *last_commit = NULL, *last_picked_commit = NULL;
92 + struct object_id head;
93 + struct lock_file lock = LOCK_INIT;
94 + int clean = 1;
95 + struct strvec rev_walk_args = STRVEC_INIT;
96 + struct rev_info revs;
97 + struct commit *commit;
98 + struct merge_options merge_opt;
99 + struct tree *next_tree, *base_tree, *head_tree;
100 + struct merge_result result;
101 + struct strbuf reflog_msg = STRBUF_INIT;
102 + struct strbuf branch_name = STRBUF_INIT;
103 +
104 + /*
105 + * test-tool stuff doesn't set up the git directory by default; need to
106 + * do that manually.
107 + */
108 + setup_git_directory();
109 +
110 + if (argc == 2 && !strcmp(argv[1], "-h")) {
111 + printf("Sorry, I am not a psychiatrist; I can not give you the help you need. Oh, you meant usage...\n");
112 + exit(129);
113 + }
114 +
115 + if (argc != 5 || strcmp(argv[1], "--onto"))
116 + die("usage: read the code, figure out how to use it, then do so");
117 +
118 + onto = peel_committish(argv[2]);
119 + strbuf_addf(&branch_name, "refs/heads/%s", argv[4]);
120 +
121 + /* Sanity check */
122 + if (get_oid("HEAD", &head))
123 + die(_("Cannot read HEAD"));
124 + assert(oideq(&onto->object.oid, &head));
125 +
126 + hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
127 + assert(repo_read_index(the_repository) >= 0);
128 +
129 + repo_init_revisions(the_repository, &revs, NULL);
130 + revs.verbose_header = 1;
131 + revs.max_parents = 1;
132 + revs.cherry_mark = 1;
133 + revs.limited = 1;
134 + revs.reverse = 1;
135 + revs.right_only = 1;
136 + revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
137 + revs.topo_order = 1;
138 + strvec_pushl(&rev_walk_args, "", argv[4], "--not", argv[3], NULL);
139 +
140 + if (setup_revisions(rev_walk_args.nr, rev_walk_args.v, &revs, NULL) > 1)
141 + return error(_("unhandled options"));
142 +
143 + strvec_clear(&rev_walk_args);
144 +
145 + if (prepare_revision_walk(&revs) < 0)
146 + return error(_("error preparing revisions"));
147 +
148 + init_merge_options(&merge_opt, the_repository);
149 + memset(&result, 0, sizeof(result));
150 + merge_opt.show_rename_progress = 1;
151 + merge_opt.branch1 = "HEAD";
152 + head_tree = get_commit_tree(onto);
153 + result.tree = head_tree;
154 + last_commit = onto;
155 + while ((commit = get_revision(&revs))) {
156 + struct commit *base;
157 +
158 + fprintf(stderr, "Rebasing %s...\r",
159 + oid_to_hex(&commit->object.oid));
160 + assert(commit->parents && !commit->parents->next);
161 + base = commit->parents->item;
162 +
163 + next_tree = get_commit_tree(commit);
164 + base_tree = get_commit_tree(base);
165 +
166 + merge_opt.branch2 = short_commit_name(commit);
167 + merge_opt.ancestor = xstrfmt("parent of %s", merge_opt.branch2);
168 +
169 + merge_incore_nonrecursive(&merge_opt,
170 + base_tree,
171 + result.tree,
172 + next_tree,
173 + &result);
174 +
175 + free((char*)merge_opt.ancestor);
176 + merge_opt.ancestor = NULL;
177 + if (!result.clean)
178 + die("Aborting: Hit a conflict and restarting is not implemented.");
179 + last_picked_commit = commit;
180 + last_commit = create_commit(result.tree, commit, last_commit);
181 + }
182 + fprintf(stderr, "\nDone.\n");
183 + /* TODO: There should be some kind of rev_info_free(&revs) call... */
184 + memset(&revs, 0, sizeof(revs));
185 +
186 + merge_switch_to_result(&merge_opt, head_tree, &result, 1, !result.clean);
187 +
188 + if (result.clean < 0)
189 + exit(128);
190 +
191 + strbuf_addf(&reflog_msg, "finish rebase %s onto %s",
192 + oid_to_hex(&last_picked_commit->object.oid),
193 + oid_to_hex(&last_commit->object.oid));
194 + if (update_ref(reflog_msg.buf, branch_name.buf,
195 + &last_commit->object.oid,
196 + &last_picked_commit->object.oid,
197 + REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
198 + error(_("could not update %s"), argv[4]);
199 + die("Failed to update %s", argv[4]);
200 + }
201 + if (create_symref("HEAD", branch_name.buf, reflog_msg.buf) < 0)
202 + die(_("unable to update HEAD"));
203 + strbuf_release(&reflog_msg);
204 + strbuf_release(&branch_name);
205 +
206 + prime_cache_tree(the_repository, the_repository->index, result.tree);
207 + if (write_locked_index(&the_index, &lock,
208 + COMMIT_LOCK | SKIP_IF_UNCHANGED))
209 + die(_("unable to write %s"), get_index_file());
210 + return (clean == 0);
211 +}
t/helper/test-tool.c
+1
@@ -28,6 +28,7 @@ static struct test_cmd cmds[] = {
28 { "dump-split-index", cmd__dump_split_index },
29 { "dump-untracked-cache", cmd__dump_untracked_cache },
30 { "example-decorate", cmd__example_decorate },
31 + { "fast-rebase", cmd__fast_rebase },
32 { "genrandom", cmd__genrandom },
33 { "genzeros", cmd__genzeros },
34 { "hashmap", cmd__hashmap },
t/helper/test-tool.h
+1
@@ -18,6 +18,7 @@ int cmd__dump_fsmonitor(int argc, const char **argv);
18 int cmd__dump_split_index(int argc, const char **argv);
19 int cmd__dump_untracked_cache(int argc, const char **argv);
20 int cmd__example_decorate(int argc, const char **argv);
21 +int cmd__fast_rebase(int argc, const char **argv);
22 int cmd__genrandom(int argc, const char **argv);
23 int cmd__genzeros(int argc, const char **argv);
24 int cmd__hashmap(int argc, const char **argv);