blob: b5092658724e2172abdf54a1af6d91bda0e176da [file] [log] [blame]
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08001#include "cache.h"
Linus Torvalds961784e2005-05-18 16:14:22 -07002#include "tag.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07003#include "commit.h"
Johannes Schindelined09aef2006-10-30 20:09:06 +01004#include "pkt-line.h"
Junio C Hamano52883fb2006-12-25 11:48:35 -08005#include "utf8.h"
Junio C Hamano199c45b2007-04-09 02:34:05 -07006#include "diff.h"
7#include "revision.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07008
Linus Torvalds60ab26d2005-09-15 14:43:17 -07009int save_commit_buffer = 1;
10
Jon Seymourab580ac2005-07-07 02:39:34 +100011struct sort_node
12{
13 /*
Tilman Sauerbeck55c3eb42006-08-17 20:44:16 +020014 * the number of children of the associated commit
15 * that also occur in the list being sorted.
16 */
Jon Seymourab580ac2005-07-07 02:39:34 +100017 unsigned int indegree;
18
19 /*
Tilman Sauerbeck55c3eb42006-08-17 20:44:16 +020020 * reference to original list item that we will re-use
21 * on output.
22 */
Jon Seymourab580ac2005-07-07 02:39:34 +100023 struct commit_list * list_item;
24
25};
26
Daniel Barkalow175785e2005-04-18 11:39:48 -070027const char *commit_type = "commit";
28
Junio C Hamanof76412e2005-08-21 02:51:10 -070029static struct commit *check_commit(struct object *obj,
30 const unsigned char *sha1,
31 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070032{
Linus Torvalds19746322006-07-11 20:45:31 -070033 if (obj->type != OBJ_COMMIT) {
Junio C Hamanof76412e2005-08-21 02:51:10 -070034 if (!quiet)
35 error("Object %s is a %s, not a commit",
Linus Torvalds885a86a2006-06-14 16:45:13 -070036 sha1_to_hex(sha1), typename(obj->type));
Linus Torvalds961784e2005-05-18 16:14:22 -070037 return NULL;
38 }
39 return (struct commit *) obj;
40}
41
Junio C Hamanof76412e2005-08-21 02:51:10 -070042struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
43 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070044{
Junio C Hamano9534f402005-11-02 15:19:13 -080045 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070046
47 if (!obj)
48 return NULL;
Junio C Hamanof76412e2005-08-21 02:51:10 -070049 return check_commit(obj, sha1, quiet);
50}
51
52struct commit *lookup_commit_reference(const unsigned char *sha1)
53{
54 return lookup_commit_reference_gently(sha1, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070055}
56
Jason McMullan5d6ccf52005-06-03 11:05:39 -040057struct commit *lookup_commit(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070058{
59 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -070060 if (!obj)
61 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -040062 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070063 obj->type = OBJ_COMMIT;
Junio C Hamanof76412e2005-08-21 02:51:10 -070064 return check_commit(obj, sha1, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070065}
66
67static unsigned long parse_commit_date(const char *buf)
68{
69 unsigned long date;
70
71 if (memcmp(buf, "author", 6))
72 return 0;
73 while (*buf++ != '\n')
74 /* nada */;
75 if (memcmp(buf, "committer", 9))
76 return 0;
77 while (*buf++ != '>')
78 /* nada */;
79 date = strtoul(buf, NULL, 10);
80 if (date == ULONG_MAX)
81 date = 0;
82 return date;
83}
84
Junio C Hamano5040f172006-04-06 23:58:51 -070085static struct commit_graft **commit_graft;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -070086static int commit_graft_alloc, commit_graft_nr;
87
88static int commit_graft_pos(const unsigned char *sha1)
89{
90 int lo, hi;
91 lo = 0;
92 hi = commit_graft_nr;
93 while (lo < hi) {
94 int mi = (lo + hi) / 2;
95 struct commit_graft *graft = commit_graft[mi];
David Rientjesa89fccd2006-08-17 11:54:57 -070096 int cmp = hashcmp(sha1, graft->sha1);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -070097 if (!cmp)
98 return mi;
99 if (cmp < 0)
100 hi = mi;
101 else
102 lo = mi + 1;
103 }
104 return -lo - 1;
105}
106
Junio C Hamano5040f172006-04-06 23:58:51 -0700107int register_commit_graft(struct commit_graft *graft, int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700108{
Junio C Hamano5040f172006-04-06 23:58:51 -0700109 int pos = commit_graft_pos(graft->sha1);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700110
Junio C Hamano5040f172006-04-06 23:58:51 -0700111 if (0 <= pos) {
112 if (ignore_dups)
113 free(graft);
114 else {
115 free(commit_graft[pos]);
116 commit_graft[pos] = graft;
117 }
118 return 1;
119 }
120 pos = -pos - 1;
121 if (commit_graft_alloc <= ++commit_graft_nr) {
122 commit_graft_alloc = alloc_nr(commit_graft_alloc);
123 commit_graft = xrealloc(commit_graft,
124 sizeof(*commit_graft) *
125 commit_graft_alloc);
126 }
127 if (pos < commit_graft_nr)
128 memmove(commit_graft + pos + 1,
129 commit_graft + pos,
130 (commit_graft_nr - pos - 1) *
131 sizeof(*commit_graft));
132 commit_graft[pos] = graft;
133 return 0;
134}
135
136struct commit_graft *read_graft_line(char *buf, int len)
137{
138 /* The format is just "Commit Parent1 Parent2 ...\n" */
139 int i;
140 struct commit_graft *graft = NULL;
141
142 if (buf[len-1] == '\n')
143 buf[--len] = 0;
Yann Dirson360204c2006-04-17 13:41:49 +0200144 if (buf[0] == '#' || buf[0] == '\0')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700145 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700146 if ((len + 1) % 41) {
147 bad_graft_data:
148 error("bad graft data: %s", buf);
149 free(graft);
150 return NULL;
151 }
152 i = (len + 1) / 41 - 1;
153 graft = xmalloc(sizeof(*graft) + 20 * i);
154 graft->nr_parent = i;
155 if (get_sha1_hex(buf, graft->sha1))
156 goto bad_graft_data;
157 for (i = 40; i < len; i += 41) {
158 if (buf[i] != ' ')
159 goto bad_graft_data;
160 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
161 goto bad_graft_data;
162 }
163 return graft;
164}
165
166int read_graft_file(const char *graft_file)
167{
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700168 FILE *fp = fopen(graft_file, "r");
169 char buf[1024];
Junio C Hamano5040f172006-04-06 23:58:51 -0700170 if (!fp)
171 return -1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700172 while (fgets(buf, sizeof(buf), fp)) {
173 /* The format is just "Commit Parent1 Parent2 ...\n" */
174 int len = strlen(buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700175 struct commit_graft *graft = read_graft_line(buf, len);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700176 if (!graft)
177 continue;
Junio C Hamano5040f172006-04-06 23:58:51 -0700178 if (register_commit_graft(graft, 1))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700179 error("duplicate graft data: %s", buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700180 }
181 fclose(fp);
Junio C Hamano5040f172006-04-06 23:58:51 -0700182 return 0;
183}
184
185static void prepare_commit_graft(void)
186{
187 static int commit_graft_prepared;
188 char *graft_file;
189
190 if (commit_graft_prepared)
191 return;
192 graft_file = get_graft_file();
193 read_graft_file(graft_file);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100194 /* make sure shallows are read */
195 is_repository_shallow();
Junio C Hamano5040f172006-04-06 23:58:51 -0700196 commit_graft_prepared = 1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700197}
198
199static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
200{
201 int pos;
Junio C Hamano5040f172006-04-06 23:58:51 -0700202 prepare_commit_graft();
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700203 pos = commit_graft_pos(sha1);
204 if (pos < 0)
205 return NULL;
206 return commit_graft[pos];
207}
208
Johannes Schindelined09aef2006-10-30 20:09:06 +0100209int write_shallow_commits(int fd, int use_pack_protocol)
210{
211 int i, count = 0;
212 for (i = 0; i < commit_graft_nr; i++)
213 if (commit_graft[i]->nr_parent < 0) {
214 const char *hex =
215 sha1_to_hex(commit_graft[i]->sha1);
216 count++;
217 if (use_pack_protocol)
218 packet_write(fd, "shallow %s", hex);
219 else {
Andy Whitcroft93822c22007-01-08 15:58:23 +0000220 if (write_in_full(fd, hex, 40) != 40)
221 break;
222 if (write_in_full(fd, "\n", 1) != 1)
223 break;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100224 }
225 }
226 return count;
227}
228
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100229int unregister_shallow(const unsigned char *sha1)
230{
231 int pos = commit_graft_pos(sha1);
232 if (pos < 0)
233 return -1;
234 if (pos + 1 < commit_graft_nr)
235 memcpy(commit_graft + pos, commit_graft + pos + 1,
236 sizeof(struct commit_graft *)
237 * (commit_graft_nr - pos - 1));
238 commit_graft_nr--;
239 return 0;
240}
241
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400242int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700243{
Junio C Hamano3b44f152006-06-28 03:51:00 -0700244 char *tail = buffer;
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700245 char *bufptr = buffer;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700246 unsigned char parent[20];
Linus Torvalds6c88be12005-06-20 20:26:03 -0700247 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700248 struct commit_graft *graft;
Junio C Hamano27dedf02005-11-16 21:32:44 -0800249 unsigned n_refs = 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400250
Daniel Barkalow175785e2005-04-18 11:39:48 -0700251 if (item->object.parsed)
252 return 0;
253 item->object.parsed = 1;
Junio C Hamano3b44f152006-06-28 03:51:00 -0700254 tail += size;
255 if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700256 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
Junio C Hamano3b44f152006-06-28 03:51:00 -0700257 if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800258 return error("bad tree pointer in commit %s",
259 sha1_to_hex(item->object.sha1));
Daniel Barkalow175785e2005-04-18 11:39:48 -0700260 item->tree = lookup_tree(parent);
Linus Torvalds235ac402005-04-24 14:31:57 -0700261 if (item->tree)
Junio C Hamano27dedf02005-11-16 21:32:44 -0800262 n_refs++;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700263 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700264 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700265
266 graft = lookup_commit_graft(item->object.sha1);
Junio C Hamano3b44f152006-06-28 03:51:00 -0700267 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700268 struct commit *new_parent;
269
Junio C Hamano3b44f152006-06-28 03:51:00 -0700270 if (tail <= bufptr + 48 ||
271 get_sha1_hex(bufptr + 7, parent) ||
272 bufptr[47] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700273 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700274 bufptr += 48;
275 if (graft)
276 continue;
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700277 new_parent = lookup_commit(parent);
Linus Torvalds235ac402005-04-24 14:31:57 -0700278 if (new_parent) {
Linus Torvalds6c88be12005-06-20 20:26:03 -0700279 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano27dedf02005-11-16 21:32:44 -0800280 n_refs++;
Linus Torvalds235ac402005-04-24 14:31:57 -0700281 }
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700282 }
283 if (graft) {
284 int i;
285 struct commit *new_parent;
286 for (i = 0; i < graft->nr_parent; i++) {
287 new_parent = lookup_commit(graft->parent[i]);
288 if (!new_parent)
289 continue;
290 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano27dedf02005-11-16 21:32:44 -0800291 n_refs++;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700292 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700293 }
294 item->date = parse_commit_date(bufptr);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800295
296 if (track_object_refs) {
297 unsigned i = 0;
298 struct commit_list *p;
299 struct object_refs *refs = alloc_object_refs(n_refs);
300 if (item->tree)
301 refs->ref[i++] = &item->tree->object;
302 for (p = item->parents; p; p = p->next)
303 refs->ref[i++] = &p->item->object;
304 set_object_refs(&item->object, refs);
305 }
306
Daniel Barkalow175785e2005-04-18 11:39:48 -0700307 return 0;
308}
309
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400310int parse_commit(struct commit *item)
311{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500312 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400313 void *buffer;
314 unsigned long size;
315 int ret;
316
317 if (item->object.parsed)
318 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500319 buffer = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400320 if (!buffer)
321 return error("Could not read %s",
322 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500323 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400324 free(buffer);
325 return error("Object %s not a commit",
326 sha1_to_hex(item->object.sha1));
327 }
328 ret = parse_commit_buffer(item, buffer, size);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700329 if (save_commit_buffer && !ret) {
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700330 item->buffer = buffer;
331 return 0;
332 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400333 free(buffer);
334 return ret;
335}
336
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700337struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700338{
Christopher Li812666c2005-04-26 12:00:58 -0700339 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700340 new_list->item = item;
341 new_list->next = *list_p;
342 *list_p = new_list;
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700343 return new_list;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700344}
345
Daniel Barkalow175785e2005-04-18 11:39:48 -0700346void free_commit_list(struct commit_list *list)
347{
348 while (list) {
349 struct commit_list *temp = list;
350 list = temp->next;
351 free(temp);
352 }
353}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700354
Linus Torvaldsf7554942005-07-06 09:31:17 -0700355struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700356{
357 struct commit_list **pp = list;
358 struct commit_list *p;
359 while ((p = *pp) != NULL) {
360 if (p->item->date < item->date) {
361 break;
362 }
363 pp = &p->next;
364 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700365 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700366}
367
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700368
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700369void sort_by_date(struct commit_list **list)
370{
371 struct commit_list *ret = NULL;
372 while (*list) {
Linus Torvaldsf7554942005-07-06 09:31:17 -0700373 insert_by_date((*list)->item, &ret);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700374 *list = (*list)->next;
375 }
376 *list = ret;
377}
378
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700379struct commit *pop_most_recent_commit(struct commit_list **list,
380 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700381{
382 struct commit *ret = (*list)->item;
383 struct commit_list *parents = ret->parents;
384 struct commit_list *old = *list;
385
386 *list = (*list)->next;
387 free(old);
388
389 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700390 struct commit *commit = parents->item;
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700391 parse_commit(commit);
392 if (!(commit->object.flags & mark)) {
393 commit->object.flags |= mark;
Linus Torvaldsf7554942005-07-06 09:31:17 -0700394 insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700395 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700396 parents = parents->next;
397 }
398 return ret;
399}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700400
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800401void clear_commit_marks(struct commit *commit, unsigned int mark)
402{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100403 while (commit) {
404 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800405
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100406 if (!(mark & commit->object.flags))
407 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700408
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100409 commit->object.flags &= ~mark;
410
411 parents = commit->parents;
412 if (!parents)
413 return;
414
415 while ((parents = parents->next))
416 clear_commit_marks(parents->item, mark);
417
418 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800419 }
420}
421
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000422struct commit *pop_commit(struct commit_list **stack)
423{
424 struct commit_list *top = *stack;
425 struct commit *item = top ? top->item : NULL;
426
427 if (top) {
428 *stack = top->next;
429 free(top);
430 }
431 return item;
432}
433
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100434void topo_sort_default_setter(struct commit *c, void *data)
435{
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -0700436 c->util = data;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100437}
438
439void *topo_sort_default_getter(struct commit *c)
440{
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -0700441 return c->util;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100442}
443
Jon Seymourab580ac2005-07-07 02:39:34 +1000444/*
445 * Performs an in-place topological sort on the list supplied.
446 */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800447void sort_in_topological_order(struct commit_list ** list, int lifo)
Jon Seymourab580ac2005-07-07 02:39:34 +1000448{
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100449 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
450 topo_sort_default_getter);
451}
452
453void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
454 topo_sort_set_fn_t setter,
455 topo_sort_get_fn_t getter)
456{
Jon Seymourab580ac2005-07-07 02:39:34 +1000457 struct commit_list * next = *list;
Linus Torvalds2ed02882005-11-14 10:01:26 -0800458 struct commit_list * work = NULL, **insert;
Jon Seymourab580ac2005-07-07 02:39:34 +1000459 struct commit_list ** pptr = list;
460 struct sort_node * nodes;
461 struct sort_node * next_nodes;
462 int count = 0;
463
464 /* determine the size of the list */
465 while (next) {
466 next = next->next;
467 count++;
468 }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700469
Eric Wong7d6fb372005-12-24 04:12:43 -0800470 if (!count)
471 return;
Jon Seymourab580ac2005-07-07 02:39:34 +1000472 /* allocate an array to help sort the list */
473 nodes = xcalloc(count, sizeof(*nodes));
474 /* link the list to the array */
475 next_nodes = nodes;
476 next=*list;
477 while (next) {
478 next_nodes->list_item = next;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100479 setter(next->item, next_nodes);
Jon Seymourab580ac2005-07-07 02:39:34 +1000480 next_nodes++;
481 next = next->next;
482 }
483 /* update the indegree */
484 next=*list;
485 while (next) {
486 struct commit_list * parents = next->item->parents;
487 while (parents) {
488 struct commit * parent=parents->item;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100489 struct sort_node * pn = (struct sort_node *) getter(parent);
490
Jon Seymourab580ac2005-07-07 02:39:34 +1000491 if (pn)
492 pn->indegree++;
493 parents=parents->next;
494 }
495 next=next->next;
496 }
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700497 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200498 * find the tips
499 *
500 * tips are nodes not reachable from any other node in the list
501 *
502 * the tips serve as a starting set for the work queue.
503 */
Jon Seymourab580ac2005-07-07 02:39:34 +1000504 next=*list;
Linus Torvalds2ed02882005-11-14 10:01:26 -0800505 insert = &work;
Jon Seymourab580ac2005-07-07 02:39:34 +1000506 while (next) {
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100507 struct sort_node * node = (struct sort_node *) getter(next->item);
Jon Seymourab580ac2005-07-07 02:39:34 +1000508
509 if (node->indegree == 0) {
Linus Torvalds2ed02882005-11-14 10:01:26 -0800510 insert = &commit_list_insert(next->item, insert)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000511 }
512 next=next->next;
513 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800514
Jon Seymourab580ac2005-07-07 02:39:34 +1000515 /* process the list in topological order */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800516 if (!lifo)
517 sort_by_date(&work);
Jon Seymourab580ac2005-07-07 02:39:34 +1000518 while (work) {
519 struct commit * work_item = pop_commit(&work);
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100520 struct sort_node * work_node = (struct sort_node *) getter(work_item);
Jon Seymourab580ac2005-07-07 02:39:34 +1000521 struct commit_list * parents = work_item->parents;
522
523 while (parents) {
524 struct commit * parent=parents->item;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100525 struct sort_node * pn = (struct sort_node *) getter(parent);
526
Jon Seymourab580ac2005-07-07 02:39:34 +1000527 if (pn) {
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100528 /*
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700529 * parents are only enqueued for emission
Pierre Habouzit674d1722007-09-10 12:35:06 +0200530 * when all their children have been emitted thereby
531 * guaranteeing topological order.
532 */
Jon Seymourab580ac2005-07-07 02:39:34 +1000533 pn->indegree--;
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800534 if (!pn->indegree) {
535 if (!lifo)
536 insert_by_date(parent, &work);
537 else
538 commit_list_insert(parent, &work);
539 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000540 }
541 parents=parents->next;
542 }
543 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200544 * work_item is a commit all of whose children
545 * have already been emitted. we can emit it now.
546 */
Jon Seymourab580ac2005-07-07 02:39:34 +1000547 *pptr = work_node->list_item;
548 pptr = &(*pptr)->next;
549 *pptr = NULL;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100550 setter(work_item, NULL);
Jon Seymourab580ac2005-07-07 02:39:34 +1000551 }
552 free(nodes);
553}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200554
Junio C Hamano12952282007-01-08 23:10:49 -0800555/* merge-base stuff */
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200556
Junio C Hamano577ed5c2006-10-22 17:32:47 -0700557/* bits #0..15 in revision.h */
558#define PARENT1 (1u<<16)
559#define PARENT2 (1u<<17)
560#define STALE (1u<<18)
561#define RESULT (1u<<19)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200562
Junio C Hamano12952282007-01-08 23:10:49 -0800563static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
564
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200565static struct commit *interesting(struct commit_list *list)
566{
567 while (list) {
568 struct commit *commit = list->item;
569 list = list->next;
Junio C Hamano542ccef2006-07-02 11:34:17 -0700570 if (commit->object.flags & STALE)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200571 continue;
572 return commit;
573 }
574 return NULL;
575}
576
Junio C Hamanof3249432006-07-04 18:46:42 -0700577static struct commit_list *merge_bases(struct commit *one, struct commit *two)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200578{
579 struct commit_list *list = NULL;
580 struct commit_list *result = NULL;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200581
Junio C Hamanof3249432006-07-04 18:46:42 -0700582 if (one == two)
583 /* We do not mark this even with RESULT so we do not
584 * have to clean it up.
585 */
586 return commit_list_insert(one, &result);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200587
Junio C Hamanof3249432006-07-04 18:46:42 -0700588 parse_commit(one);
589 parse_commit(two);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200590
Junio C Hamanof3249432006-07-04 18:46:42 -0700591 one->object.flags |= PARENT1;
592 two->object.flags |= PARENT2;
593 insert_by_date(one, &list);
594 insert_by_date(two, &list);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200595
596 while (interesting(list)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700597 struct commit *commit;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200598 struct commit_list *parents;
Junio C Hamanof3249432006-07-04 18:46:42 -0700599 struct commit_list *n;
600 int flags;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200601
Junio C Hamanof3249432006-07-04 18:46:42 -0700602 commit = list->item;
603 n = list->next;
604 free(list);
605 list = n;
606
607 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200608 if (flags == (PARENT1 | PARENT2)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700609 if (!(commit->object.flags & RESULT)) {
610 commit->object.flags |= RESULT;
611 insert_by_date(commit, &result);
612 }
Junio C Hamano542ccef2006-07-02 11:34:17 -0700613 /* Mark parents of a found merge stale */
614 flags |= STALE;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200615 }
616 parents = commit->parents;
617 while (parents) {
618 struct commit *p = parents->item;
619 parents = parents->next;
620 if ((p->object.flags & flags) == flags)
621 continue;
622 parse_commit(p);
623 p->object.flags |= flags;
624 insert_by_date(p, &list);
625 }
626 }
627
Junio C Hamanof3249432006-07-04 18:46:42 -0700628 /* Clean up the result to remove stale ones */
Junio C Hamano12952282007-01-08 23:10:49 -0800629 free_commit_list(list);
Junio C Hamanof3249432006-07-04 18:46:42 -0700630 list = result; result = NULL;
631 while (list) {
632 struct commit_list *n = list->next;
633 if (!(list->item->object.flags & STALE))
634 insert_by_date(list->item, &result);
635 free(list);
636 list = n;
637 }
638 return result;
639}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200640
Junio C Hamanof3249432006-07-04 18:46:42 -0700641struct commit_list *get_merge_bases(struct commit *one,
Pierre Habouzit674d1722007-09-10 12:35:06 +0200642 struct commit *two, int cleanup)
Junio C Hamanof3249432006-07-04 18:46:42 -0700643{
Junio C Hamanof3249432006-07-04 18:46:42 -0700644 struct commit_list *list;
645 struct commit **rslt;
646 struct commit_list *result;
647 int cnt, i, j;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200648
Junio C Hamanof3249432006-07-04 18:46:42 -0700649 result = merge_bases(one, two);
650 if (one == two)
651 return result;
652 if (!result || !result->next) {
653 if (cleanup) {
654 clear_commit_marks(one, all_flags);
655 clear_commit_marks(two, all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200656 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700657 return result;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200658 }
659
Junio C Hamanof3249432006-07-04 18:46:42 -0700660 /* There are more than one */
661 cnt = 0;
662 list = result;
663 while (list) {
664 list = list->next;
665 cnt++;
666 }
667 rslt = xcalloc(cnt, sizeof(*rslt));
668 for (list = result, i = 0; list; list = list->next)
669 rslt[i++] = list->item;
670 free_commit_list(result);
671
672 clear_commit_marks(one, all_flags);
673 clear_commit_marks(two, all_flags);
674 for (i = 0; i < cnt - 1; i++) {
675 for (j = i+1; j < cnt; j++) {
676 if (!rslt[i] || !rslt[j])
677 continue;
678 result = merge_bases(rslt[i], rslt[j]);
679 clear_commit_marks(rslt[i], all_flags);
680 clear_commit_marks(rslt[j], all_flags);
681 for (list = result; list; list = list->next) {
682 if (rslt[i] == list->item)
683 rslt[i] = NULL;
684 if (rslt[j] == list->item)
685 rslt[j] = NULL;
686 }
687 }
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700688 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200689
Junio C Hamanof3249432006-07-04 18:46:42 -0700690 /* Surviving ones in rslt[] are the independent results */
691 result = NULL;
692 for (i = 0; i < cnt; i++) {
693 if (rslt[i])
694 insert_by_date(rslt[i], &result);
695 }
696 free(rslt);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200697 return result;
698}
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800699
Junio C Hamano03840fc2007-01-08 23:22:31 -0800700int in_merge_bases(struct commit *commit, struct commit **reference, int num)
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800701{
702 struct commit_list *bases, *b;
703 int ret = 0;
704
Junio C Hamano03840fc2007-01-08 23:22:31 -0800705 if (num == 1)
706 bases = get_merge_bases(commit, *reference, 1);
707 else
708 die("not yet");
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800709 for (b = bases; b; b = b->next) {
Junio C Hamano03840fc2007-01-08 23:22:31 -0800710 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800711 ret = 1;
712 break;
713 }
714 }
715
716 free_commit_list(bases);
717 return ret;
718}