fast-rebase: change assert() to BUG()
[git/debian.git] / t / helper / test-fast-rebase.c
blob39fb7f41e8c15c74e48ad03df40c55d3aa4bd0bc
1 /*
2 * "git fast-rebase" builtin command
4 * FAST: Forking Any Subprocesses (is) Taboo
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).
13 #define USE_THE_INDEX_COMPATIBILITY_MACROS
14 #include "test-tool.h"
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"
26 static const char *short_commit_name(struct commit *commit)
28 return find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV);
31 static struct commit *peel_committish(const char *name)
33 struct object *obj;
34 struct object_id oid;
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);
42 static char *get_author(const char *message)
44 size_t len;
45 const char *a;
47 a = find_commit_header(message, "author", &len);
48 if (a)
49 return xmemdupz(a, len);
51 return NULL;
54 static struct commit *create_commit(struct tree *tree,
55 struct commit *based_on,
56 struct commit *parent)
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 };
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;
81 free(author);
82 strbuf_release(&msg);
84 obj = parse_object(the_repository, &ret);
85 return (struct commit *)obj;
88 int cmd__fast_rebase(int argc, const char **argv)
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;
105 * test-tool stuff doesn't set up the git directory by default; need to
106 * do that manually.
108 setup_git_directory();
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);
115 if (argc != 5 || strcmp(argv[1], "--onto"))
116 die("usage: read the code, figure out how to use it, then do so");
118 onto = peel_committish(argv[2]);
119 strbuf_addf(&branch_name, "refs/heads/%s", argv[4]);
121 /* Sanity check */
122 if (get_oid("HEAD", &head))
123 die(_("Cannot read HEAD"));
124 assert(oideq(&onto->object.oid, &head));
126 hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
127 if (repo_read_index(the_repository) < 0)
128 BUG("Could not read index");
130 repo_init_revisions(the_repository, &revs, NULL);
131 revs.verbose_header = 1;
132 revs.max_parents = 1;
133 revs.cherry_mark = 1;
134 revs.limited = 1;
135 revs.reverse = 1;
136 revs.right_only = 1;
137 revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
138 revs.topo_order = 1;
139 strvec_pushl(&rev_walk_args, "", argv[4], "--not", argv[3], NULL);
141 if (setup_revisions(rev_walk_args.nr, rev_walk_args.v, &revs, NULL) > 1)
142 return error(_("unhandled options"));
144 strvec_clear(&rev_walk_args);
146 if (prepare_revision_walk(&revs) < 0)
147 return error(_("error preparing revisions"));
149 init_merge_options(&merge_opt, the_repository);
150 memset(&result, 0, sizeof(result));
151 merge_opt.show_rename_progress = 1;
152 merge_opt.branch1 = "HEAD";
153 head_tree = get_commit_tree(onto);
154 result.tree = head_tree;
155 last_commit = onto;
156 while ((commit = get_revision(&revs))) {
157 struct commit *base;
159 fprintf(stderr, "Rebasing %s...\r",
160 oid_to_hex(&commit->object.oid));
161 assert(commit->parents && !commit->parents->next);
162 base = commit->parents->item;
164 next_tree = get_commit_tree(commit);
165 base_tree = get_commit_tree(base);
167 merge_opt.branch2 = short_commit_name(commit);
168 merge_opt.ancestor = xstrfmt("parent of %s", merge_opt.branch2);
170 merge_incore_nonrecursive(&merge_opt,
171 base_tree,
172 result.tree,
173 next_tree,
174 &result);
176 free((char*)merge_opt.ancestor);
177 merge_opt.ancestor = NULL;
178 if (!result.clean)
179 die("Aborting: Hit a conflict and restarting is not implemented.");
180 last_picked_commit = commit;
181 last_commit = create_commit(result.tree, commit, last_commit);
183 fprintf(stderr, "\nDone.\n");
184 /* TODO: There should be some kind of rev_info_free(&revs) call... */
185 memset(&revs, 0, sizeof(revs));
187 merge_switch_to_result(&merge_opt, head_tree, &result, 1, !result.clean);
189 if (result.clean < 0)
190 exit(128);
192 strbuf_addf(&reflog_msg, "finish rebase %s onto %s",
193 oid_to_hex(&last_picked_commit->object.oid),
194 oid_to_hex(&last_commit->object.oid));
195 if (update_ref(reflog_msg.buf, branch_name.buf,
196 &last_commit->object.oid,
197 &last_picked_commit->object.oid,
198 REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
199 error(_("could not update %s"), argv[4]);
200 die("Failed to update %s", argv[4]);
202 if (create_symref("HEAD", branch_name.buf, reflog_msg.buf) < 0)
203 die(_("unable to update HEAD"));
204 strbuf_release(&reflog_msg);
205 strbuf_release(&branch_name);
207 prime_cache_tree(the_repository, the_repository->index, result.tree);
208 if (write_locked_index(&the_index, &lock,
209 COMMIT_LOCK | SKIP_IF_UNCHANGED))
210 die(_("unable to write %s"), get_index_file());
211 return (clean == 0);