blob: 94d5b3d2618d335585084170ef6a3a8e67f34c62 [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
Daniel Barkalow175785e2005-04-18 11:39:48 -070011const char *commit_type = "commit";
12
Junio C Hamanof76412e2005-08-21 02:51:10 -070013static struct commit *check_commit(struct object *obj,
14 const unsigned char *sha1,
15 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070016{
Linus Torvalds19746322006-07-11 20:45:31 -070017 if (obj->type != OBJ_COMMIT) {
Junio C Hamanof76412e2005-08-21 02:51:10 -070018 if (!quiet)
19 error("Object %s is a %s, not a commit",
Linus Torvalds885a86a2006-06-14 16:45:13 -070020 sha1_to_hex(sha1), typename(obj->type));
Linus Torvalds961784e2005-05-18 16:14:22 -070021 return NULL;
22 }
23 return (struct commit *) obj;
24}
25
Junio C Hamanof76412e2005-08-21 02:51:10 -070026struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
27 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070028{
Junio C Hamano9534f402005-11-02 15:19:13 -080029 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070030
31 if (!obj)
32 return NULL;
Junio C Hamanof76412e2005-08-21 02:51:10 -070033 return check_commit(obj, sha1, quiet);
34}
35
36struct commit *lookup_commit_reference(const unsigned char *sha1)
37{
38 return lookup_commit_reference_gently(sha1, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070039}
40
Jason McMullan5d6ccf52005-06-03 11:05:39 -040041struct commit *lookup_commit(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070042{
43 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -070044 if (!obj)
45 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -040046 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070047 obj->type = OBJ_COMMIT;
Junio C Hamanof76412e2005-08-21 02:51:10 -070048 return check_commit(obj, sha1, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070049}
50
Martin Koegler0a617792008-01-19 18:35:23 +010051static unsigned long parse_commit_date(const char *buf, const char *tail)
Daniel Barkalow175785e2005-04-18 11:39:48 -070052{
53 unsigned long date;
Martin Koegler0a617792008-01-19 18:35:23 +010054 const char *dateptr;
Daniel Barkalow175785e2005-04-18 11:39:48 -070055
Martin Koegler0a617792008-01-19 18:35:23 +010056 if (buf + 6 >= tail)
57 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070058 if (memcmp(buf, "author", 6))
59 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010060 while (buf < tail && *buf++ != '\n')
Daniel Barkalow175785e2005-04-18 11:39:48 -070061 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010062 if (buf + 9 >= tail)
63 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070064 if (memcmp(buf, "committer", 9))
65 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010066 while (buf < tail && *buf++ != '>')
Daniel Barkalow175785e2005-04-18 11:39:48 -070067 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010068 if (buf >= tail)
69 return 0;
70 dateptr = buf;
71 while (buf < tail && *buf++ != '\n')
72 /* nada */;
73 if (buf >= tail)
74 return 0;
75 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
76 date = strtoul(dateptr, NULL, 10);
Daniel Barkalow175785e2005-04-18 11:39:48 -070077 if (date == ULONG_MAX)
78 date = 0;
79 return date;
80}
81
Junio C Hamano5040f172006-04-06 23:58:51 -070082static struct commit_graft **commit_graft;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -070083static int commit_graft_alloc, commit_graft_nr;
84
85static int commit_graft_pos(const unsigned char *sha1)
86{
87 int lo, hi;
88 lo = 0;
89 hi = commit_graft_nr;
90 while (lo < hi) {
91 int mi = (lo + hi) / 2;
92 struct commit_graft *graft = commit_graft[mi];
David Rientjesa89fccd2006-08-17 11:54:57 -070093 int cmp = hashcmp(sha1, graft->sha1);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -070094 if (!cmp)
95 return mi;
96 if (cmp < 0)
97 hi = mi;
98 else
99 lo = mi + 1;
100 }
101 return -lo - 1;
102}
103
Junio C Hamano5040f172006-04-06 23:58:51 -0700104int register_commit_graft(struct commit_graft *graft, int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700105{
Junio C Hamano5040f172006-04-06 23:58:51 -0700106 int pos = commit_graft_pos(graft->sha1);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700107
Junio C Hamano5040f172006-04-06 23:58:51 -0700108 if (0 <= pos) {
109 if (ignore_dups)
110 free(graft);
111 else {
112 free(commit_graft[pos]);
113 commit_graft[pos] = graft;
114 }
115 return 1;
116 }
117 pos = -pos - 1;
118 if (commit_graft_alloc <= ++commit_graft_nr) {
119 commit_graft_alloc = alloc_nr(commit_graft_alloc);
120 commit_graft = xrealloc(commit_graft,
121 sizeof(*commit_graft) *
122 commit_graft_alloc);
123 }
124 if (pos < commit_graft_nr)
125 memmove(commit_graft + pos + 1,
126 commit_graft + pos,
127 (commit_graft_nr - pos - 1) *
128 sizeof(*commit_graft));
129 commit_graft[pos] = graft;
130 return 0;
131}
132
133struct commit_graft *read_graft_line(char *buf, int len)
134{
135 /* The format is just "Commit Parent1 Parent2 ...\n" */
136 int i;
137 struct commit_graft *graft = NULL;
138
139 if (buf[len-1] == '\n')
140 buf[--len] = 0;
Yann Dirson360204c2006-04-17 13:41:49 +0200141 if (buf[0] == '#' || buf[0] == '\0')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700142 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700143 if ((len + 1) % 41) {
144 bad_graft_data:
145 error("bad graft data: %s", buf);
146 free(graft);
147 return NULL;
148 }
149 i = (len + 1) / 41 - 1;
150 graft = xmalloc(sizeof(*graft) + 20 * i);
151 graft->nr_parent = i;
152 if (get_sha1_hex(buf, graft->sha1))
153 goto bad_graft_data;
154 for (i = 40; i < len; i += 41) {
155 if (buf[i] != ' ')
156 goto bad_graft_data;
157 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
158 goto bad_graft_data;
159 }
160 return graft;
161}
162
163int read_graft_file(const char *graft_file)
164{
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700165 FILE *fp = fopen(graft_file, "r");
166 char buf[1024];
Junio C Hamano5040f172006-04-06 23:58:51 -0700167 if (!fp)
168 return -1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700169 while (fgets(buf, sizeof(buf), fp)) {
170 /* The format is just "Commit Parent1 Parent2 ...\n" */
171 int len = strlen(buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700172 struct commit_graft *graft = read_graft_line(buf, len);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700173 if (!graft)
174 continue;
Junio C Hamano5040f172006-04-06 23:58:51 -0700175 if (register_commit_graft(graft, 1))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700176 error("duplicate graft data: %s", buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700177 }
178 fclose(fp);
Junio C Hamano5040f172006-04-06 23:58:51 -0700179 return 0;
180}
181
182static void prepare_commit_graft(void)
183{
184 static int commit_graft_prepared;
185 char *graft_file;
186
187 if (commit_graft_prepared)
188 return;
189 graft_file = get_graft_file();
190 read_graft_file(graft_file);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100191 /* make sure shallows are read */
192 is_repository_shallow();
Junio C Hamano5040f172006-04-06 23:58:51 -0700193 commit_graft_prepared = 1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700194}
195
Martin Koegler45163382008-02-25 22:46:07 +0100196struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700197{
198 int pos;
Junio C Hamano5040f172006-04-06 23:58:51 -0700199 prepare_commit_graft();
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700200 pos = commit_graft_pos(sha1);
201 if (pos < 0)
202 return NULL;
203 return commit_graft[pos];
204}
205
Johannes Schindelined09aef2006-10-30 20:09:06 +0100206int write_shallow_commits(int fd, int use_pack_protocol)
207{
208 int i, count = 0;
209 for (i = 0; i < commit_graft_nr; i++)
210 if (commit_graft[i]->nr_parent < 0) {
211 const char *hex =
212 sha1_to_hex(commit_graft[i]->sha1);
213 count++;
214 if (use_pack_protocol)
215 packet_write(fd, "shallow %s", hex);
216 else {
Andy Whitcroft93822c22007-01-08 15:58:23 +0000217 if (write_in_full(fd, hex, 40) != 40)
218 break;
219 if (write_in_full(fd, "\n", 1) != 1)
220 break;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100221 }
222 }
223 return count;
224}
225
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100226int unregister_shallow(const unsigned char *sha1)
227{
228 int pos = commit_graft_pos(sha1);
229 if (pos < 0)
230 return -1;
231 if (pos + 1 < commit_graft_nr)
232 memcpy(commit_graft + pos, commit_graft + pos + 1,
233 sizeof(struct commit_graft *)
234 * (commit_graft_nr - pos - 1));
235 commit_graft_nr--;
236 return 0;
237}
238
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400239int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700240{
Junio C Hamano3b44f152006-06-28 03:51:00 -0700241 char *tail = buffer;
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700242 char *bufptr = buffer;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700243 unsigned char parent[20];
Linus Torvalds6c88be12005-06-20 20:26:03 -0700244 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700245 struct commit_graft *graft;
Junio C Hamano27dedf02005-11-16 21:32:44 -0800246 unsigned n_refs = 0;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400247
Daniel Barkalow175785e2005-04-18 11:39:48 -0700248 if (item->object.parsed)
249 return 0;
250 item->object.parsed = 1;
Junio C Hamano3b44f152006-06-28 03:51:00 -0700251 tail += size;
Martin Koegler0a617792008-01-19 18:35:23 +0100252 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700253 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
Martin Koegler0a617792008-01-19 18:35:23 +0100254 if (get_sha1_hex(bufptr + 5, parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800255 return error("bad tree pointer in commit %s",
256 sha1_to_hex(item->object.sha1));
Daniel Barkalow175785e2005-04-18 11:39:48 -0700257 item->tree = lookup_tree(parent);
Linus Torvalds235ac402005-04-24 14:31:57 -0700258 if (item->tree)
Junio C Hamano27dedf02005-11-16 21:32:44 -0800259 n_refs++;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700260 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700261 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700262
263 graft = lookup_commit_graft(item->object.sha1);
Junio C Hamano3b44f152006-06-28 03:51:00 -0700264 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700265 struct commit *new_parent;
266
Junio C Hamano3b44f152006-06-28 03:51:00 -0700267 if (tail <= bufptr + 48 ||
268 get_sha1_hex(bufptr + 7, parent) ||
269 bufptr[47] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700270 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700271 bufptr += 48;
272 if (graft)
273 continue;
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700274 new_parent = lookup_commit(parent);
Linus Torvalds235ac402005-04-24 14:31:57 -0700275 if (new_parent) {
Linus Torvalds6c88be12005-06-20 20:26:03 -0700276 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano27dedf02005-11-16 21:32:44 -0800277 n_refs++;
Linus Torvalds235ac402005-04-24 14:31:57 -0700278 }
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700279 }
280 if (graft) {
281 int i;
282 struct commit *new_parent;
283 for (i = 0; i < graft->nr_parent; i++) {
284 new_parent = lookup_commit(graft->parent[i]);
285 if (!new_parent)
286 continue;
287 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano27dedf02005-11-16 21:32:44 -0800288 n_refs++;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700289 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700290 }
Martin Koegler0a617792008-01-19 18:35:23 +0100291 item->date = parse_commit_date(bufptr, tail);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800292
Daniel Barkalow175785e2005-04-18 11:39:48 -0700293 return 0;
294}
295
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400296int parse_commit(struct commit *item)
297{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500298 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400299 void *buffer;
300 unsigned long size;
301 int ret;
302
Martin Koegler9786f682008-02-18 21:48:02 +0100303 if (!item)
304 return -1;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400305 if (item->object.parsed)
306 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500307 buffer = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400308 if (!buffer)
309 return error("Could not read %s",
310 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500311 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400312 free(buffer);
313 return error("Object %s not a commit",
314 sha1_to_hex(item->object.sha1));
315 }
316 ret = parse_commit_buffer(item, buffer, size);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700317 if (save_commit_buffer && !ret) {
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700318 item->buffer = buffer;
319 return 0;
320 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400321 free(buffer);
322 return ret;
323}
324
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700325struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700326{
Christopher Li812666c2005-04-26 12:00:58 -0700327 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700328 new_list->item = item;
329 new_list->next = *list_p;
330 *list_p = new_list;
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700331 return new_list;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700332}
333
Daniel Barkalow175785e2005-04-18 11:39:48 -0700334void free_commit_list(struct commit_list *list)
335{
336 while (list) {
337 struct commit_list *temp = list;
338 list = temp->next;
339 free(temp);
340 }
341}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700342
Linus Torvaldsf7554942005-07-06 09:31:17 -0700343struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700344{
345 struct commit_list **pp = list;
346 struct commit_list *p;
347 while ((p = *pp) != NULL) {
348 if (p->item->date < item->date) {
349 break;
350 }
351 pp = &p->next;
352 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700353 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700354}
355
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700356
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700357void sort_by_date(struct commit_list **list)
358{
359 struct commit_list *ret = NULL;
360 while (*list) {
Linus Torvaldsf7554942005-07-06 09:31:17 -0700361 insert_by_date((*list)->item, &ret);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700362 *list = (*list)->next;
363 }
364 *list = ret;
365}
366
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700367struct commit *pop_most_recent_commit(struct commit_list **list,
368 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700369{
370 struct commit *ret = (*list)->item;
371 struct commit_list *parents = ret->parents;
372 struct commit_list *old = *list;
373
374 *list = (*list)->next;
375 free(old);
376
377 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700378 struct commit *commit = parents->item;
Martin Koeglerdec38c82008-02-18 21:48:03 +0100379 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700380 commit->object.flags |= mark;
Linus Torvaldsf7554942005-07-06 09:31:17 -0700381 insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700382 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700383 parents = parents->next;
384 }
385 return ret;
386}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700387
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800388void clear_commit_marks(struct commit *commit, unsigned int mark)
389{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100390 while (commit) {
391 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800392
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100393 if (!(mark & commit->object.flags))
394 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700395
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100396 commit->object.flags &= ~mark;
397
398 parents = commit->parents;
399 if (!parents)
400 return;
401
402 while ((parents = parents->next))
403 clear_commit_marks(parents->item, mark);
404
405 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800406 }
407}
408
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000409struct commit *pop_commit(struct commit_list **stack)
410{
411 struct commit_list *top = *stack;
412 struct commit *item = top ? top->item : NULL;
413
414 if (top) {
415 *stack = top->next;
416 free(top);
417 }
418 return item;
419}
420
Jon Seymourab580ac2005-07-07 02:39:34 +1000421/*
422 * Performs an in-place topological sort on the list supplied.
423 */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800424void sort_in_topological_order(struct commit_list ** list, int lifo)
Jon Seymourab580ac2005-07-07 02:39:34 +1000425{
Linus Torvalds23c17d42007-11-02 13:32:58 -0700426 struct commit_list *next, *orig = *list;
427 struct commit_list *work, **insert;
428 struct commit_list **pptr;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100429
Linus Torvalds23c17d42007-11-02 13:32:58 -0700430 if (!orig)
Eric Wong7d6fb372005-12-24 04:12:43 -0800431 return;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700432 *list = NULL;
433
434 /* Mark them and clear the indegree */
435 for (next = orig; next; next = next->next) {
436 struct commit *commit = next->item;
437 commit->object.flags |= TOPOSORT;
438 commit->indegree = 0;
Jon Seymourab580ac2005-07-07 02:39:34 +1000439 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700440
Jon Seymourab580ac2005-07-07 02:39:34 +1000441 /* update the indegree */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700442 for (next = orig; next; next = next->next) {
Jon Seymourab580ac2005-07-07 02:39:34 +1000443 struct commit_list * parents = next->item->parents;
444 while (parents) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700445 struct commit *parent = parents->item;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100446
Linus Torvalds23c17d42007-11-02 13:32:58 -0700447 if (parent->object.flags & TOPOSORT)
448 parent->indegree++;
449 parents = parents->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000450 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000451 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700452
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700453 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200454 * find the tips
455 *
456 * tips are nodes not reachable from any other node in the list
457 *
458 * the tips serve as a starting set for the work queue.
459 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700460 work = NULL;
Linus Torvalds2ed02882005-11-14 10:01:26 -0800461 insert = &work;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700462 for (next = orig; next; next = next->next) {
463 struct commit *commit = next->item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000464
Linus Torvalds23c17d42007-11-02 13:32:58 -0700465 if (!commit->indegree)
466 insert = &commit_list_insert(commit, insert)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000467 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800468
Jon Seymourab580ac2005-07-07 02:39:34 +1000469 /* process the list in topological order */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800470 if (!lifo)
471 sort_by_date(&work);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700472
473 pptr = list;
474 *list = NULL;
Jon Seymourab580ac2005-07-07 02:39:34 +1000475 while (work) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700476 struct commit *commit;
477 struct commit_list *parents, *work_item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000478
Linus Torvalds23c17d42007-11-02 13:32:58 -0700479 work_item = work;
480 work = work_item->next;
481 work_item->next = NULL;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100482
Linus Torvalds23c17d42007-11-02 13:32:58 -0700483 commit = work_item->item;
484 for (parents = commit->parents; parents ; parents = parents->next) {
485 struct commit *parent=parents->item;
486
487 if (!(parent->object.flags & TOPOSORT))
488 continue;
489
490 /*
491 * parents are only enqueued for emission
492 * when all their children have been emitted thereby
493 * guaranteeing topological order.
494 */
495 if (!--parent->indegree) {
496 if (!lifo)
497 insert_by_date(parent, &work);
498 else
499 commit_list_insert(parent, &work);
Jon Seymourab580ac2005-07-07 02:39:34 +1000500 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000501 }
502 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200503 * work_item is a commit all of whose children
504 * have already been emitted. we can emit it now.
505 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700506 commit->object.flags &= ~TOPOSORT;
507 *pptr = work_item;
508 pptr = &work_item->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000509 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000510}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200511
Junio C Hamano12952282007-01-08 23:10:49 -0800512/* merge-base stuff */
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200513
Junio C Hamano577ed5c2006-10-22 17:32:47 -0700514/* bits #0..15 in revision.h */
515#define PARENT1 (1u<<16)
516#define PARENT2 (1u<<17)
517#define STALE (1u<<18)
518#define RESULT (1u<<19)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200519
Junio C Hamano12952282007-01-08 23:10:49 -0800520static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
521
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200522static struct commit *interesting(struct commit_list *list)
523{
524 while (list) {
525 struct commit *commit = list->item;
526 list = list->next;
Junio C Hamano542ccef2006-07-02 11:34:17 -0700527 if (commit->object.flags & STALE)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200528 continue;
529 return commit;
530 }
531 return NULL;
532}
533
Junio C Hamanof3249432006-07-04 18:46:42 -0700534static struct commit_list *merge_bases(struct commit *one, struct commit *two)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200535{
536 struct commit_list *list = NULL;
537 struct commit_list *result = NULL;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200538
Junio C Hamanof3249432006-07-04 18:46:42 -0700539 if (one == two)
540 /* We do not mark this even with RESULT so we do not
541 * have to clean it up.
542 */
543 return commit_list_insert(one, &result);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200544
Martin Koegler172947e2008-02-18 21:47:57 +0100545 if (parse_commit(one))
546 return NULL;
547 if (parse_commit(two))
548 return NULL;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200549
Junio C Hamanof3249432006-07-04 18:46:42 -0700550 one->object.flags |= PARENT1;
551 two->object.flags |= PARENT2;
552 insert_by_date(one, &list);
553 insert_by_date(two, &list);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200554
555 while (interesting(list)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700556 struct commit *commit;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200557 struct commit_list *parents;
Junio C Hamanof3249432006-07-04 18:46:42 -0700558 struct commit_list *n;
559 int flags;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200560
Junio C Hamanof3249432006-07-04 18:46:42 -0700561 commit = list->item;
562 n = list->next;
563 free(list);
564 list = n;
565
566 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200567 if (flags == (PARENT1 | PARENT2)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700568 if (!(commit->object.flags & RESULT)) {
569 commit->object.flags |= RESULT;
570 insert_by_date(commit, &result);
571 }
Junio C Hamano542ccef2006-07-02 11:34:17 -0700572 /* Mark parents of a found merge stale */
573 flags |= STALE;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200574 }
575 parents = commit->parents;
576 while (parents) {
577 struct commit *p = parents->item;
578 parents = parents->next;
579 if ((p->object.flags & flags) == flags)
580 continue;
Martin Koegler172947e2008-02-18 21:47:57 +0100581 if (parse_commit(p))
582 return NULL;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200583 p->object.flags |= flags;
584 insert_by_date(p, &list);
585 }
586 }
587
Junio C Hamanof3249432006-07-04 18:46:42 -0700588 /* Clean up the result to remove stale ones */
Junio C Hamano12952282007-01-08 23:10:49 -0800589 free_commit_list(list);
Junio C Hamanof3249432006-07-04 18:46:42 -0700590 list = result; result = NULL;
591 while (list) {
592 struct commit_list *n = list->next;
593 if (!(list->item->object.flags & STALE))
594 insert_by_date(list->item, &result);
595 free(list);
596 list = n;
597 }
598 return result;
599}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200600
Junio C Hamanof3249432006-07-04 18:46:42 -0700601struct commit_list *get_merge_bases(struct commit *one,
Pierre Habouzit674d1722007-09-10 12:35:06 +0200602 struct commit *two, int cleanup)
Junio C Hamanof3249432006-07-04 18:46:42 -0700603{
Junio C Hamanof3249432006-07-04 18:46:42 -0700604 struct commit_list *list;
605 struct commit **rslt;
606 struct commit_list *result;
607 int cnt, i, j;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200608
Junio C Hamanof3249432006-07-04 18:46:42 -0700609 result = merge_bases(one, two);
610 if (one == two)
611 return result;
612 if (!result || !result->next) {
613 if (cleanup) {
614 clear_commit_marks(one, all_flags);
615 clear_commit_marks(two, all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200616 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700617 return result;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200618 }
619
Junio C Hamanof3249432006-07-04 18:46:42 -0700620 /* There are more than one */
621 cnt = 0;
622 list = result;
623 while (list) {
624 list = list->next;
625 cnt++;
626 }
627 rslt = xcalloc(cnt, sizeof(*rslt));
628 for (list = result, i = 0; list; list = list->next)
629 rslt[i++] = list->item;
630 free_commit_list(result);
631
632 clear_commit_marks(one, all_flags);
633 clear_commit_marks(two, all_flags);
634 for (i = 0; i < cnt - 1; i++) {
635 for (j = i+1; j < cnt; j++) {
636 if (!rslt[i] || !rslt[j])
637 continue;
638 result = merge_bases(rslt[i], rslt[j]);
639 clear_commit_marks(rslt[i], all_flags);
640 clear_commit_marks(rslt[j], all_flags);
641 for (list = result; list; list = list->next) {
642 if (rslt[i] == list->item)
643 rslt[i] = NULL;
644 if (rslt[j] == list->item)
645 rslt[j] = NULL;
646 }
647 }
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700648 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200649
Junio C Hamanof3249432006-07-04 18:46:42 -0700650 /* Surviving ones in rslt[] are the independent results */
651 result = NULL;
652 for (i = 0; i < cnt; i++) {
653 if (rslt[i])
654 insert_by_date(rslt[i], &result);
655 }
656 free(rslt);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200657 return result;
658}
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800659
Junio C Hamano03840fc2007-01-08 23:22:31 -0800660int in_merge_bases(struct commit *commit, struct commit **reference, int num)
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800661{
662 struct commit_list *bases, *b;
663 int ret = 0;
664
Junio C Hamano03840fc2007-01-08 23:22:31 -0800665 if (num == 1)
666 bases = get_merge_bases(commit, *reference, 1);
667 else
668 die("not yet");
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800669 for (b = bases; b; b = b->next) {
Junio C Hamano03840fc2007-01-08 23:22:31 -0800670 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800671 ret = 1;
672 break;
673 }
674 }
675
676 free_commit_list(bases);
677 return ret;
678}