blob: 0094ec1c9278332f736d2814c847b272788a7dd3 [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"
Johannes Schindelina97a7462009-10-09 12:21:57 +02008#include "notes.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07009
Linus Torvalds60ab26d2005-09-15 14:43:17 -070010int save_commit_buffer = 1;
11
Daniel Barkalow175785e2005-04-18 11:39:48 -070012const char *commit_type = "commit";
13
Junio C Hamanof76412e2005-08-21 02:51:10 -070014static struct commit *check_commit(struct object *obj,
15 const unsigned char *sha1,
16 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070017{
Linus Torvalds19746322006-07-11 20:45:31 -070018 if (obj->type != OBJ_COMMIT) {
Junio C Hamanof76412e2005-08-21 02:51:10 -070019 if (!quiet)
20 error("Object %s is a %s, not a commit",
Linus Torvalds885a86a2006-06-14 16:45:13 -070021 sha1_to_hex(sha1), typename(obj->type));
Linus Torvalds961784e2005-05-18 16:14:22 -070022 return NULL;
23 }
24 return (struct commit *) obj;
25}
26
Junio C Hamanof76412e2005-08-21 02:51:10 -070027struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
28 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070029{
Junio C Hamano9534f402005-11-02 15:19:13 -080030 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070031
32 if (!obj)
33 return NULL;
Junio C Hamanof76412e2005-08-21 02:51:10 -070034 return check_commit(obj, sha1, quiet);
35}
36
37struct commit *lookup_commit_reference(const unsigned char *sha1)
38{
39 return lookup_commit_reference_gently(sha1, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070040}
41
Jason McMullan5d6ccf52005-06-03 11:05:39 -040042struct commit *lookup_commit(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070043{
44 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -070045 if (!obj)
46 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -040047 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070048 obj->type = OBJ_COMMIT;
Junio C Hamanof76412e2005-08-21 02:51:10 -070049 return check_commit(obj, sha1, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070050}
51
Martin Koegler0a617792008-01-19 18:35:23 +010052static unsigned long parse_commit_date(const char *buf, const char *tail)
Daniel Barkalow175785e2005-04-18 11:39:48 -070053{
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 */
Eric Wongf2909742009-07-02 21:34:54 -070076 return strtoul(dateptr, NULL, 10);
Daniel Barkalow175785e2005-04-18 11:39:48 -070077}
78
Junio C Hamano5040f172006-04-06 23:58:51 -070079static struct commit_graft **commit_graft;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -070080static int commit_graft_alloc, commit_graft_nr;
81
82static int commit_graft_pos(const unsigned char *sha1)
83{
84 int lo, hi;
85 lo = 0;
86 hi = commit_graft_nr;
87 while (lo < hi) {
88 int mi = (lo + hi) / 2;
89 struct commit_graft *graft = commit_graft[mi];
David Rientjesa89fccd2006-08-17 11:54:57 -070090 int cmp = hashcmp(sha1, graft->sha1);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -070091 if (!cmp)
92 return mi;
93 if (cmp < 0)
94 hi = mi;
95 else
96 lo = mi + 1;
97 }
98 return -lo - 1;
99}
100
Junio C Hamano5040f172006-04-06 23:58:51 -0700101int register_commit_graft(struct commit_graft *graft, int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700102{
Junio C Hamano5040f172006-04-06 23:58:51 -0700103 int pos = commit_graft_pos(graft->sha1);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700104
Junio C Hamano5040f172006-04-06 23:58:51 -0700105 if (0 <= pos) {
106 if (ignore_dups)
107 free(graft);
108 else {
109 free(commit_graft[pos]);
110 commit_graft[pos] = graft;
111 }
112 return 1;
113 }
114 pos = -pos - 1;
115 if (commit_graft_alloc <= ++commit_graft_nr) {
116 commit_graft_alloc = alloc_nr(commit_graft_alloc);
117 commit_graft = xrealloc(commit_graft,
118 sizeof(*commit_graft) *
119 commit_graft_alloc);
120 }
121 if (pos < commit_graft_nr)
122 memmove(commit_graft + pos + 1,
123 commit_graft + pos,
124 (commit_graft_nr - pos - 1) *
125 sizeof(*commit_graft));
126 commit_graft[pos] = graft;
127 return 0;
128}
129
130struct commit_graft *read_graft_line(char *buf, int len)
131{
132 /* The format is just "Commit Parent1 Parent2 ...\n" */
133 int i;
134 struct commit_graft *graft = NULL;
135
Junio C Hamanofe0a3cb2009-10-14 11:51:03 -0700136 while (len && isspace(buf[len-1]))
137 buf[--len] = '\0';
Yann Dirson360204c2006-04-17 13:41:49 +0200138 if (buf[0] == '#' || buf[0] == '\0')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700139 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700140 if ((len + 1) % 41) {
141 bad_graft_data:
142 error("bad graft data: %s", buf);
143 free(graft);
144 return NULL;
145 }
146 i = (len + 1) / 41 - 1;
147 graft = xmalloc(sizeof(*graft) + 20 * i);
148 graft->nr_parent = i;
149 if (get_sha1_hex(buf, graft->sha1))
150 goto bad_graft_data;
151 for (i = 40; i < len; i += 41) {
152 if (buf[i] != ' ')
153 goto bad_graft_data;
154 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
155 goto bad_graft_data;
156 }
157 return graft;
158}
159
Nanako Shiraishic5ae6432008-10-02 19:14:30 +0900160static int read_graft_file(const char *graft_file)
Junio C Hamano5040f172006-04-06 23:58:51 -0700161{
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700162 FILE *fp = fopen(graft_file, "r");
163 char buf[1024];
Junio C Hamano5040f172006-04-06 23:58:51 -0700164 if (!fp)
165 return -1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700166 while (fgets(buf, sizeof(buf), fp)) {
167 /* The format is just "Commit Parent1 Parent2 ...\n" */
168 int len = strlen(buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700169 struct commit_graft *graft = read_graft_line(buf, len);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700170 if (!graft)
171 continue;
Junio C Hamano5040f172006-04-06 23:58:51 -0700172 if (register_commit_graft(graft, 1))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700173 error("duplicate graft data: %s", buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700174 }
175 fclose(fp);
Junio C Hamano5040f172006-04-06 23:58:51 -0700176 return 0;
177}
178
179static void prepare_commit_graft(void)
180{
181 static int commit_graft_prepared;
182 char *graft_file;
183
184 if (commit_graft_prepared)
185 return;
186 graft_file = get_graft_file();
187 read_graft_file(graft_file);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100188 /* make sure shallows are read */
189 is_repository_shallow();
Junio C Hamano5040f172006-04-06 23:58:51 -0700190 commit_graft_prepared = 1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700191}
192
Martin Koegler45163382008-02-25 22:46:07 +0100193struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700194{
195 int pos;
Junio C Hamano5040f172006-04-06 23:58:51 -0700196 prepare_commit_graft();
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700197 pos = commit_graft_pos(sha1);
198 if (pos < 0)
199 return NULL;
200 return commit_graft[pos];
201}
202
Shawn O. Pearceedace6f2009-10-30 17:47:23 -0700203int write_shallow_commits(struct strbuf *out, int use_pack_protocol)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100204{
205 int i, count = 0;
206 for (i = 0; i < commit_graft_nr; i++)
207 if (commit_graft[i]->nr_parent < 0) {
208 const char *hex =
209 sha1_to_hex(commit_graft[i]->sha1);
210 count++;
211 if (use_pack_protocol)
Shawn O. Pearceedace6f2009-10-30 17:47:23 -0700212 packet_buf_write(out, "shallow %s", hex);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100213 else {
Shawn O. Pearceedace6f2009-10-30 17:47:23 -0700214 strbuf_addstr(out, hex);
215 strbuf_addch(out, '\n');
Johannes Schindelined09aef2006-10-30 20:09:06 +0100216 }
217 }
218 return count;
219}
220
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100221int unregister_shallow(const unsigned char *sha1)
222{
223 int pos = commit_graft_pos(sha1);
224 if (pos < 0)
225 return -1;
226 if (pos + 1 < commit_graft_nr)
Jeff King65d41d42010-01-29 05:28:44 -0500227 memmove(commit_graft + pos, commit_graft + pos + 1,
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100228 sizeof(struct commit_graft *)
229 * (commit_graft_nr - pos - 1));
230 commit_graft_nr--;
231 return 0;
232}
233
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400234int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700235{
Junio C Hamano3b44f152006-06-28 03:51:00 -0700236 char *tail = buffer;
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700237 char *bufptr = buffer;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700238 unsigned char parent[20];
Linus Torvalds6c88be12005-06-20 20:26:03 -0700239 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700240 struct commit_graft *graft;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400241
Daniel Barkalow175785e2005-04-18 11:39:48 -0700242 if (item->object.parsed)
243 return 0;
244 item->object.parsed = 1;
Junio C Hamano3b44f152006-06-28 03:51:00 -0700245 tail += size;
Martin Koegler0a617792008-01-19 18:35:23 +0100246 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700247 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
Martin Koegler0a617792008-01-19 18:35:23 +0100248 if (get_sha1_hex(bufptr + 5, parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800249 return error("bad tree pointer in commit %s",
250 sha1_to_hex(item->object.sha1));
Daniel Barkalow175785e2005-04-18 11:39:48 -0700251 item->tree = lookup_tree(parent);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700252 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700253 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700254
255 graft = lookup_commit_graft(item->object.sha1);
Junio C Hamano3b44f152006-06-28 03:51:00 -0700256 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700257 struct commit *new_parent;
258
Junio C Hamano3b44f152006-06-28 03:51:00 -0700259 if (tail <= bufptr + 48 ||
260 get_sha1_hex(bufptr + 7, parent) ||
261 bufptr[47] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700262 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700263 bufptr += 48;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +0200264 /*
265 * The clone is shallow if nr_parent < 0, and we must
266 * not traverse its real parents even when we unhide them.
267 */
268 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700269 continue;
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700270 new_parent = lookup_commit(parent);
Miklos Vajna39468de2008-06-07 22:38:37 +0200271 if (new_parent)
Linus Torvalds6c88be12005-06-20 20:26:03 -0700272 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700273 }
274 if (graft) {
275 int i;
276 struct commit *new_parent;
277 for (i = 0; i < graft->nr_parent; i++) {
278 new_parent = lookup_commit(graft->parent[i]);
279 if (!new_parent)
280 continue;
281 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700282 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700283 }
Martin Koegler0a617792008-01-19 18:35:23 +0100284 item->date = parse_commit_date(bufptr, tail);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800285
Daniel Barkalow175785e2005-04-18 11:39:48 -0700286 return 0;
287}
288
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400289int parse_commit(struct commit *item)
290{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500291 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400292 void *buffer;
293 unsigned long size;
294 int ret;
295
Martin Koegler9786f682008-02-18 21:48:02 +0100296 if (!item)
297 return -1;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400298 if (item->object.parsed)
299 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500300 buffer = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400301 if (!buffer)
302 return error("Could not read %s",
303 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500304 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400305 free(buffer);
306 return error("Object %s not a commit",
307 sha1_to_hex(item->object.sha1));
308 }
309 ret = parse_commit_buffer(item, buffer, size);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700310 if (save_commit_buffer && !ret) {
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700311 item->buffer = buffer;
312 return 0;
313 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400314 free(buffer);
315 return ret;
316}
317
Christian Couder11af2aa2010-07-22 15:18:30 +0200318int find_commit_subject(const char *commit_buffer, const char **subject)
319{
320 const char *eol;
321 const char *p = commit_buffer;
322
323 while (*p && (*p != '\n' || p[1] != '\n'))
324 p++;
325 if (*p) {
326 p += 2;
327 for (eol = p; *eol && *eol != '\n'; eol++)
328 ; /* do nothing */
329 } else
330 eol = p;
331
332 *subject = p;
333
334 return eol - p;
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
Miklos Vajna65319472008-06-27 18:21:55 +0200346unsigned commit_list_count(const struct commit_list *l)
347{
348 unsigned c = 0;
349 for (; l; l = l->next )
350 c++;
351 return c;
352}
353
Daniel Barkalow175785e2005-04-18 11:39:48 -0700354void free_commit_list(struct commit_list *list)
355{
356 while (list) {
357 struct commit_list *temp = list;
358 list = temp->next;
359 free(temp);
360 }
361}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700362
Linus Torvaldsf7554942005-07-06 09:31:17 -0700363struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700364{
365 struct commit_list **pp = list;
366 struct commit_list *p;
367 while ((p = *pp) != NULL) {
368 if (p->item->date < item->date) {
369 break;
370 }
371 pp = &p->next;
372 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700373 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700374}
375
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700376
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700377void sort_by_date(struct commit_list **list)
378{
379 struct commit_list *ret = NULL;
380 while (*list) {
Linus Torvaldsf7554942005-07-06 09:31:17 -0700381 insert_by_date((*list)->item, &ret);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700382 *list = (*list)->next;
383 }
384 *list = ret;
385}
386
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700387struct commit *pop_most_recent_commit(struct commit_list **list,
388 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700389{
390 struct commit *ret = (*list)->item;
391 struct commit_list *parents = ret->parents;
392 struct commit_list *old = *list;
393
394 *list = (*list)->next;
395 free(old);
396
397 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700398 struct commit *commit = parents->item;
Martin Koeglerdec38c82008-02-18 21:48:03 +0100399 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700400 commit->object.flags |= mark;
Linus Torvaldsf7554942005-07-06 09:31:17 -0700401 insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700402 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700403 parents = parents->next;
404 }
405 return ret;
406}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700407
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800408void clear_commit_marks(struct commit *commit, unsigned int mark)
409{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100410 while (commit) {
411 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800412
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100413 if (!(mark & commit->object.flags))
414 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700415
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100416 commit->object.flags &= ~mark;
417
418 parents = commit->parents;
419 if (!parents)
420 return;
421
422 while ((parents = parents->next))
423 clear_commit_marks(parents->item, mark);
424
425 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800426 }
427}
428
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000429struct commit *pop_commit(struct commit_list **stack)
430{
431 struct commit_list *top = *stack;
432 struct commit *item = top ? top->item : NULL;
433
434 if (top) {
435 *stack = top->next;
436 free(top);
437 }
438 return item;
439}
440
Jon Seymourab580ac2005-07-07 02:39:34 +1000441/*
442 * Performs an in-place topological sort on the list supplied.
443 */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800444void sort_in_topological_order(struct commit_list ** list, int lifo)
Jon Seymourab580ac2005-07-07 02:39:34 +1000445{
Linus Torvalds23c17d42007-11-02 13:32:58 -0700446 struct commit_list *next, *orig = *list;
447 struct commit_list *work, **insert;
448 struct commit_list **pptr;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100449
Linus Torvalds23c17d42007-11-02 13:32:58 -0700450 if (!orig)
Eric Wong7d6fb372005-12-24 04:12:43 -0800451 return;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700452 *list = NULL;
453
454 /* Mark them and clear the indegree */
455 for (next = orig; next; next = next->next) {
456 struct commit *commit = next->item;
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100457 commit->indegree = 1;
Jon Seymourab580ac2005-07-07 02:39:34 +1000458 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700459
Jon Seymourab580ac2005-07-07 02:39:34 +1000460 /* update the indegree */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700461 for (next = orig; next; next = next->next) {
Jon Seymourab580ac2005-07-07 02:39:34 +1000462 struct commit_list * parents = next->item->parents;
463 while (parents) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700464 struct commit *parent = parents->item;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100465
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100466 if (parent->indegree)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700467 parent->indegree++;
468 parents = parents->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000469 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000470 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700471
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700472 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200473 * find the tips
474 *
475 * tips are nodes not reachable from any other node in the list
476 *
477 * the tips serve as a starting set for the work queue.
478 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700479 work = NULL;
Linus Torvalds2ed02882005-11-14 10:01:26 -0800480 insert = &work;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700481 for (next = orig; next; next = next->next) {
482 struct commit *commit = next->item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000483
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100484 if (commit->indegree == 1)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700485 insert = &commit_list_insert(commit, insert)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000486 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800487
Jon Seymourab580ac2005-07-07 02:39:34 +1000488 /* process the list in topological order */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800489 if (!lifo)
490 sort_by_date(&work);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700491
492 pptr = list;
493 *list = NULL;
Jon Seymourab580ac2005-07-07 02:39:34 +1000494 while (work) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700495 struct commit *commit;
496 struct commit_list *parents, *work_item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000497
Linus Torvalds23c17d42007-11-02 13:32:58 -0700498 work_item = work;
499 work = work_item->next;
500 work_item->next = NULL;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100501
Linus Torvalds23c17d42007-11-02 13:32:58 -0700502 commit = work_item->item;
503 for (parents = commit->parents; parents ; parents = parents->next) {
504 struct commit *parent=parents->item;
505
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100506 if (!parent->indegree)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700507 continue;
508
509 /*
510 * parents are only enqueued for emission
511 * when all their children have been emitted thereby
512 * guaranteeing topological order.
513 */
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100514 if (--parent->indegree == 1) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700515 if (!lifo)
516 insert_by_date(parent, &work);
517 else
518 commit_list_insert(parent, &work);
Jon Seymourab580ac2005-07-07 02:39:34 +1000519 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000520 }
521 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200522 * work_item is a commit all of whose children
523 * have already been emitted. we can emit it now.
524 */
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100525 commit->indegree = 0;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700526 *pptr = work_item;
527 pptr = &work_item->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000528 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000529}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200530
Junio C Hamano12952282007-01-08 23:10:49 -0800531/* merge-base stuff */
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200532
Junio C Hamano577ed5c2006-10-22 17:32:47 -0700533/* bits #0..15 in revision.h */
534#define PARENT1 (1u<<16)
535#define PARENT2 (1u<<17)
536#define STALE (1u<<18)
537#define RESULT (1u<<19)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200538
Junio C Hamano12952282007-01-08 23:10:49 -0800539static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
540
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200541static struct commit *interesting(struct commit_list *list)
542{
543 while (list) {
544 struct commit *commit = list->item;
545 list = list->next;
Junio C Hamano542ccef2006-07-02 11:34:17 -0700546 if (commit->object.flags & STALE)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200547 continue;
548 return commit;
549 }
550 return NULL;
551}
552
Junio C Hamano6a938642008-06-27 18:22:02 +0200553static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200554{
555 struct commit_list *list = NULL;
556 struct commit_list *result = NULL;
Junio C Hamano6a938642008-06-27 18:22:02 +0200557 int i;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200558
Junio C Hamano6a938642008-06-27 18:22:02 +0200559 for (i = 0; i < n; i++) {
560 if (one == twos[i])
561 /*
562 * We do not mark this even with RESULT so we do not
563 * have to clean it up.
564 */
565 return commit_list_insert(one, &result);
566 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200567
Martin Koegler172947e2008-02-18 21:47:57 +0100568 if (parse_commit(one))
569 return NULL;
Junio C Hamano6a938642008-06-27 18:22:02 +0200570 for (i = 0; i < n; i++) {
571 if (parse_commit(twos[i]))
572 return NULL;
573 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200574
Junio C Hamanof3249432006-07-04 18:46:42 -0700575 one->object.flags |= PARENT1;
Junio C Hamanof3249432006-07-04 18:46:42 -0700576 insert_by_date(one, &list);
Junio C Hamano6a938642008-06-27 18:22:02 +0200577 for (i = 0; i < n; i++) {
578 twos[i]->object.flags |= PARENT2;
579 insert_by_date(twos[i], &list);
580 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200581
582 while (interesting(list)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700583 struct commit *commit;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200584 struct commit_list *parents;
Brandon Caseyf203d692009-08-27 11:16:34 -0500585 struct commit_list *next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700586 int flags;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200587
Junio C Hamanof3249432006-07-04 18:46:42 -0700588 commit = list->item;
Brandon Caseyf203d692009-08-27 11:16:34 -0500589 next = list->next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700590 free(list);
Brandon Caseyf203d692009-08-27 11:16:34 -0500591 list = next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700592
593 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200594 if (flags == (PARENT1 | PARENT2)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700595 if (!(commit->object.flags & RESULT)) {
596 commit->object.flags |= RESULT;
597 insert_by_date(commit, &result);
598 }
Junio C Hamano542ccef2006-07-02 11:34:17 -0700599 /* Mark parents of a found merge stale */
600 flags |= STALE;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200601 }
602 parents = commit->parents;
603 while (parents) {
604 struct commit *p = parents->item;
605 parents = parents->next;
606 if ((p->object.flags & flags) == flags)
607 continue;
Martin Koegler172947e2008-02-18 21:47:57 +0100608 if (parse_commit(p))
609 return NULL;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200610 p->object.flags |= flags;
611 insert_by_date(p, &list);
612 }
613 }
614
Junio C Hamanof3249432006-07-04 18:46:42 -0700615 /* Clean up the result to remove stale ones */
Junio C Hamano12952282007-01-08 23:10:49 -0800616 free_commit_list(list);
Junio C Hamanof3249432006-07-04 18:46:42 -0700617 list = result; result = NULL;
618 while (list) {
Brandon Caseyf203d692009-08-27 11:16:34 -0500619 struct commit_list *next = list->next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700620 if (!(list->item->object.flags & STALE))
621 insert_by_date(list->item, &result);
622 free(list);
Brandon Caseyf203d692009-08-27 11:16:34 -0500623 list = next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700624 }
625 return result;
626}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200627
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200628struct commit_list *get_octopus_merge_bases(struct commit_list *in)
629{
630 struct commit_list *i, *j, *k, *ret = NULL;
631 struct commit_list **pptr = &ret;
632
633 for (i = in; i; i = i->next) {
634 if (!ret)
635 pptr = &commit_list_insert(i->item, pptr)->next;
636 else {
637 struct commit_list *new = NULL, *end = NULL;
638
639 for (j = ret; j; j = j->next) {
640 struct commit_list *bases;
641 bases = get_merge_bases(i->item, j->item, 1);
642 if (!new)
643 new = bases;
644 else
645 end->next = bases;
646 for (k = bases; k; k = k->next)
647 end = k;
648 }
649 ret = new;
650 }
651 }
652 return ret;
653}
654
Junio C Hamano6a938642008-06-27 18:22:02 +0200655struct commit_list *get_merge_bases_many(struct commit *one,
656 int n,
657 struct commit **twos,
658 int cleanup)
Junio C Hamanof3249432006-07-04 18:46:42 -0700659{
Junio C Hamanof3249432006-07-04 18:46:42 -0700660 struct commit_list *list;
661 struct commit **rslt;
662 struct commit_list *result;
663 int cnt, i, j;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200664
Junio C Hamano6a938642008-06-27 18:22:02 +0200665 result = merge_bases_many(one, n, twos);
666 for (i = 0; i < n; i++) {
667 if (one == twos[i])
668 return result;
669 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700670 if (!result || !result->next) {
671 if (cleanup) {
672 clear_commit_marks(one, all_flags);
Junio C Hamano6a938642008-06-27 18:22:02 +0200673 for (i = 0; i < n; i++)
674 clear_commit_marks(twos[i], all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200675 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700676 return result;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200677 }
678
Junio C Hamanof3249432006-07-04 18:46:42 -0700679 /* There are more than one */
680 cnt = 0;
681 list = result;
682 while (list) {
683 list = list->next;
684 cnt++;
685 }
686 rslt = xcalloc(cnt, sizeof(*rslt));
687 for (list = result, i = 0; list; list = list->next)
688 rslt[i++] = list->item;
689 free_commit_list(result);
690
691 clear_commit_marks(one, all_flags);
Junio C Hamano6a938642008-06-27 18:22:02 +0200692 for (i = 0; i < n; i++)
693 clear_commit_marks(twos[i], all_flags);
Junio C Hamanof3249432006-07-04 18:46:42 -0700694 for (i = 0; i < cnt - 1; i++) {
695 for (j = i+1; j < cnt; j++) {
696 if (!rslt[i] || !rslt[j])
697 continue;
Junio C Hamano6a938642008-06-27 18:22:02 +0200698 result = merge_bases_many(rslt[i], 1, &rslt[j]);
Junio C Hamanof3249432006-07-04 18:46:42 -0700699 clear_commit_marks(rslt[i], all_flags);
700 clear_commit_marks(rslt[j], all_flags);
701 for (list = result; list; list = list->next) {
702 if (rslt[i] == list->item)
703 rslt[i] = NULL;
704 if (rslt[j] == list->item)
705 rslt[j] = NULL;
706 }
707 }
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700708 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200709
Junio C Hamanof3249432006-07-04 18:46:42 -0700710 /* Surviving ones in rslt[] are the independent results */
711 result = NULL;
712 for (i = 0; i < cnt; i++) {
713 if (rslt[i])
714 insert_by_date(rslt[i], &result);
715 }
716 free(rslt);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200717 return result;
718}
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800719
Junio C Hamano6a938642008-06-27 18:22:02 +0200720struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
721 int cleanup)
722{
723 return get_merge_bases_many(one, 1, &two, cleanup);
724}
725
Jake Goulding7fcdb362009-01-26 09:13:24 -0500726int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
727{
728 if (!with_commit)
729 return 1;
730 while (with_commit) {
731 struct commit *other;
732
733 other = with_commit->item;
734 with_commit = with_commit->next;
735 if (in_merge_bases(other, &commit, 1))
736 return 1;
737 }
738 return 0;
739}
740
Junio C Hamano03840fc2007-01-08 23:22:31 -0800741int in_merge_bases(struct commit *commit, struct commit **reference, int num)
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800742{
743 struct commit_list *bases, *b;
744 int ret = 0;
745
Junio C Hamano03840fc2007-01-08 23:22:31 -0800746 if (num == 1)
747 bases = get_merge_bases(commit, *reference, 1);
748 else
749 die("not yet");
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800750 for (b = bases; b; b = b->next) {
Junio C Hamano03840fc2007-01-08 23:22:31 -0800751 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800752 ret = 1;
753 break;
754 }
755 }
756
757 free_commit_list(bases);
758 return ret;
759}
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200760
761struct commit_list *reduce_heads(struct commit_list *heads)
762{
763 struct commit_list *p;
764 struct commit_list *result = NULL, **tail = &result;
765 struct commit **other;
766 size_t num_head, num_other;
767
768 if (!heads)
769 return NULL;
770
771 /* Avoid unnecessary reallocations */
772 for (p = heads, num_head = 0; p; p = p->next)
773 num_head++;
774 other = xcalloc(sizeof(*other), num_head);
775
776 /* For each commit, see if it can be reached by others */
777 for (p = heads; p; p = p->next) {
778 struct commit_list *q, *base;
779
Junio C Hamano711f6b22008-07-14 00:09:41 -0700780 /* Do we already have this in the result? */
781 for (q = result; q; q = q->next)
782 if (p->item == q->item)
783 break;
784 if (q)
785 continue;
786
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200787 num_other = 0;
788 for (q = heads; q; q = q->next) {
Sverre Hvammen Johansen3d1dd472008-07-13 08:13:55 +0000789 if (p->item == q->item)
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200790 continue;
791 other[num_other++] = q->item;
792 }
Junio C Hamano711f6b22008-07-14 00:09:41 -0700793 if (num_other)
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200794 base = get_merge_bases_many(p->item, num_other, other, 1);
Junio C Hamano711f6b22008-07-14 00:09:41 -0700795 else
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200796 base = NULL;
797 /*
798 * If p->item does not have anything common with other
799 * commits, there won't be any merge base. If it is
800 * reachable from some of the others, p->item will be
801 * the merge base. If its history is connected with
802 * others, but p->item is not reachable by others, we
803 * will get something other than p->item back.
804 */
805 if (!base || (base->item != p->item))
806 tail = &(commit_list_insert(p->item, tail)->next);
807 free_commit_list(base);
808 }
809 free(other);
810 return result;
811}
Jeff King40d52ff2010-04-01 20:05:23 -0400812
813static const char commit_utf8_warn[] =
814"Warning: commit message does not conform to UTF-8.\n"
815"You may want to amend it after fixing the message, or set the config\n"
816"variable i18n.commitencoding to the encoding your project uses.\n";
817
818int commit_tree(const char *msg, unsigned char *tree,
819 struct commit_list *parents, unsigned char *ret,
820 const char *author)
821{
822 int result;
823 int encoding_is_utf8;
824 struct strbuf buffer;
825
826 assert_sha1_type(tree, OBJ_TREE);
827
828 /* Not having i18n.commitencoding is the same as having utf-8 */
829 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
830
831 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
832 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
833
834 /*
835 * NOTE! This ordering means that the same exact tree merged with a
836 * different order of parents will be a _different_ changeset even
837 * if everything else stays the same.
838 */
839 while (parents) {
840 struct commit_list *next = parents->next;
841 strbuf_addf(&buffer, "parent %s\n",
842 sha1_to_hex(parents->item->object.sha1));
843 free(parents);
844 parents = next;
845 }
846
847 /* Person/date information */
848 if (!author)
849 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
850 strbuf_addf(&buffer, "author %s\n", author);
851 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
852 if (!encoding_is_utf8)
853 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
854 strbuf_addch(&buffer, '\n');
855
856 /* And add the comment */
857 strbuf_addstr(&buffer, msg);
858
859 /* And check the encoding */
860 if (encoding_is_utf8 && !is_utf8(buffer.buf))
861 fprintf(stderr, commit_utf8_warn);
862
863 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
864 strbuf_release(&buffer);
865 return result;
866}