]> git.ipfire.org Git - thirdparty/git.git/blame - t/helper/test-fast-rebase.c
object-name.h: move declarations for object-name.c functions from cache.h
[thirdparty/git.git] / t / helper / test-fast-rebase.c
CommitLineData
fe1a21d5
EN
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
bdafeae0 13#define USE_THE_INDEX_VARIABLE
fe1a21d5 14#include "test-tool.h"
a6dc3d36 15#include "cache.h"
fe1a21d5
EN
16#include "cache-tree.h"
17#include "commit.h"
32a8f510 18#include "environment.h"
f394e093 19#include "gettext.h"
41771fa4 20#include "hex.h"
fe1a21d5
EN
21#include "lockfile.h"
22#include "merge-ort.h"
dabab1d6 23#include "object-name.h"
fe1a21d5
EN
24#include "refs.h"
25#include "revision.h"
26#include "sequencer.h"
e38da487 27#include "setup.h"
fe1a21d5
EN
28#include "strvec.h"
29#include "tree.h"
30
31static const char *short_commit_name(struct commit *commit)
32{
d850b7a5
ÆAB
33 return repo_find_unique_abbrev(the_repository, &commit->object.oid,
34 DEFAULT_ABBREV);
fe1a21d5
EN
35}
36
37static struct commit *peel_committish(const char *name)
38{
39 struct object *obj;
40 struct object_id oid;
41
d850b7a5 42 if (repo_get_oid(the_repository, name, &oid))
fe1a21d5
EN
43 return NULL;
44 obj = parse_object(the_repository, &oid);
d850b7a5
ÆAB
45 return (struct commit *)repo_peel_to_type(the_repository, name, 0, obj,
46 OBJ_COMMIT);
fe1a21d5
EN
47}
48
49static char *get_author(const char *message)
50{
51 size_t len;
52 const char *a;
53
54 a = find_commit_header(message, "author", &len);
55 if (a)
56 return xmemdupz(a, len);
57
58 return NULL;
59}
60
61static struct commit *create_commit(struct tree *tree,
62 struct commit *based_on,
63 struct commit *parent)
64{
65 struct object_id ret;
66 struct object *obj;
67 struct commit_list *parents = NULL;
68 char *author;
69 char *sign_commit = NULL;
70 struct commit_extra_header *extra;
71 struct strbuf msg = STRBUF_INIT;
72 const char *out_enc = get_commit_output_encoding();
ecb5091f
ÆAB
73 const char *message = repo_logmsg_reencode(the_repository, based_on,
74 NULL, out_enc);
fe1a21d5
EN
75 const char *orig_message = NULL;
76 const char *exclude_gpgsig[] = { "gpgsig", NULL };
77
78 commit_list_insert(parent, &parents);
79 extra = read_commit_extra_headers(based_on, exclude_gpgsig);
80 find_commit_subject(message, &orig_message);
81 strbuf_addstr(&msg, orig_message);
82 author = get_author(message);
83 reset_ident_date();
84 if (commit_tree_extended(msg.buf, msg.len, &tree->object.oid, parents,
85 &ret, author, NULL, sign_commit, extra)) {
86 error(_("failed to write commit object"));
87 return NULL;
88 }
89 free(author);
90 strbuf_release(&msg);
91
92 obj = parse_object(the_repository, &ret);
93 return (struct commit *)obj;
94}
95
96int cmd__fast_rebase(int argc, const char **argv)
97{
98 struct commit *onto;
99 struct commit *last_commit = NULL, *last_picked_commit = NULL;
100 struct object_id head;
101 struct lock_file lock = LOCK_INIT;
fe1a21d5
EN
102 struct strvec rev_walk_args = STRVEC_INIT;
103 struct rev_info revs;
104 struct commit *commit;
105 struct merge_options merge_opt;
106 struct tree *next_tree, *base_tree, *head_tree;
107 struct merge_result result;
108 struct strbuf reflog_msg = STRBUF_INIT;
109 struct strbuf branch_name = STRBUF_INIT;
0139c58a 110 int ret = 0;
fe1a21d5
EN
111
112 /*
113 * test-tool stuff doesn't set up the git directory by default; need to
114 * do that manually.
115 */
116 setup_git_directory();
117
118 if (argc == 2 && !strcmp(argv[1], "-h")) {
119 printf("Sorry, I am not a psychiatrist; I can not give you the help you need. Oh, you meant usage...\n");
120 exit(129);
121 }
122
123 if (argc != 5 || strcmp(argv[1], "--onto"))
124 die("usage: read the code, figure out how to use it, then do so");
125
126 onto = peel_committish(argv[2]);
127 strbuf_addf(&branch_name, "refs/heads/%s", argv[4]);
128
129 /* Sanity check */
d850b7a5 130 if (repo_get_oid(the_repository, "HEAD", &head))
fe1a21d5
EN
131 die(_("Cannot read HEAD"));
132 assert(oideq(&onto->object.oid, &head));
133
0ea414a1 134 repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
caba91c3
EN
135 if (repo_read_index(the_repository) < 0)
136 BUG("Could not read index");
fe1a21d5
EN
137
138 repo_init_revisions(the_repository, &revs, NULL);
139 revs.verbose_header = 1;
140 revs.max_parents = 1;
141 revs.cherry_mark = 1;
142 revs.limited = 1;
143 revs.reverse = 1;
144 revs.right_only = 1;
145 revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
146 revs.topo_order = 1;
147 strvec_pushl(&rev_walk_args, "", argv[4], "--not", argv[3], NULL);
148
0139c58a
ÆAB
149 if (setup_revisions(rev_walk_args.nr, rev_walk_args.v, &revs, NULL) > 1) {
150 ret = error(_("unhandled options"));
151 goto cleanup;
152 }
fe1a21d5
EN
153
154 strvec_clear(&rev_walk_args);
155
0139c58a
ÆAB
156 if (prepare_revision_walk(&revs) < 0) {
157 ret = error(_("error preparing revisions"));
158 goto cleanup;
159 }
fe1a21d5
EN
160
161 init_merge_options(&merge_opt, the_repository);
162 memset(&result, 0, sizeof(result));
163 merge_opt.show_rename_progress = 1;
164 merge_opt.branch1 = "HEAD";
ecb5091f 165 head_tree = repo_get_commit_tree(the_repository, onto);
fe1a21d5
EN
166 result.tree = head_tree;
167 last_commit = onto;
168 while ((commit = get_revision(&revs))) {
169 struct commit *base;
170
171 fprintf(stderr, "Rebasing %s...\r",
172 oid_to_hex(&commit->object.oid));
173 assert(commit->parents && !commit->parents->next);
174 base = commit->parents->item;
175
ecb5091f
ÆAB
176 next_tree = repo_get_commit_tree(the_repository, commit);
177 base_tree = repo_get_commit_tree(the_repository, base);
fe1a21d5
EN
178
179 merge_opt.branch2 = short_commit_name(commit);
180 merge_opt.ancestor = xstrfmt("parent of %s", merge_opt.branch2);
181
182 merge_incore_nonrecursive(&merge_opt,
183 base_tree,
184 result.tree,
185 next_tree,
186 &result);
187
188 free((char*)merge_opt.ancestor);
189 merge_opt.ancestor = NULL;
190 if (!result.clean)
f9500261 191 break;
fe1a21d5
EN
192 last_picked_commit = commit;
193 last_commit = create_commit(result.tree, commit, last_commit);
194 }
fe1a21d5
EN
195
196 merge_switch_to_result(&merge_opt, head_tree, &result, 1, !result.clean);
197
198 if (result.clean < 0)
199 exit(128);
200
f9500261
EN
201 if (result.clean) {
202 fprintf(stderr, "\nDone.\n");
203 strbuf_addf(&reflog_msg, "finish rebase %s onto %s",
204 oid_to_hex(&last_picked_commit->object.oid),
205 oid_to_hex(&last_commit->object.oid));
206 if (update_ref(reflog_msg.buf, branch_name.buf,
207 &last_commit->object.oid,
208 &last_picked_commit->object.oid,
209 REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
210 error(_("could not update %s"), argv[4]);
211 die("Failed to update %s", argv[4]);
212 }
213 if (create_symref("HEAD", branch_name.buf, reflog_msg.buf) < 0)
214 die(_("unable to update HEAD"));
f9500261
EN
215
216 prime_cache_tree(the_repository, the_repository->index,
217 result.tree);
218 } else {
219 fprintf(stderr, "\nAborting: Hit a conflict.\n");
220 strbuf_addf(&reflog_msg, "rebase progress up to %s",
221 oid_to_hex(&last_picked_commit->object.oid));
222 if (update_ref(reflog_msg.buf, "HEAD",
223 &last_commit->object.oid,
224 &head,
225 REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
226 error(_("could not update %s"), argv[4]);
227 die("Failed to update %s", argv[4]);
228 }
fe1a21d5 229 }
fe1a21d5
EN
230 if (write_locked_index(&the_index, &lock,
231 COMMIT_LOCK | SKIP_IF_UNCHANGED))
232 die(_("unable to write %s"), get_index_file());
b925fcf1 233
0139c58a
ÆAB
234 ret = (result.clean == 0);
235cleanup:
b925fcf1
ÆAB
236 strbuf_release(&reflog_msg);
237 strbuf_release(&branch_name);
0139c58a
ÆAB
238 release_revisions(&revs);
239 return ret;
fe1a21d5 240}