blob: 57ebea2aee30b00e86a620f7efc793420f214185 [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"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07009#include "gpg-interface.h"
René Scharfe46905892012-04-01 00:10:39 +020010#include "mergesort.h"
Junio C Hamanoa84b7942013-04-13 11:56:41 -070011#include "commit-slab.h"
Junio C Hamanoda24b102013-06-06 21:58:12 -070012#include "prio-queue.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -070013
Junio C Hamano82a75292012-09-15 13:58:15 -070014static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
15
Linus Torvalds60ab26d2005-09-15 14:43:17 -070016int save_commit_buffer = 1;
17
Daniel Barkalow175785e2005-04-18 11:39:48 -070018const char *commit_type = "commit";
Jeff King96c4f4a2013-04-09 02:52:56 -040019static int commit_count;
Daniel Barkalow175785e2005-04-18 11:39:48 -070020
Junio C Hamanof76412e2005-08-21 02:51:10 -070021static struct commit *check_commit(struct object *obj,
22 const unsigned char *sha1,
23 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070024{
Linus Torvalds19746322006-07-11 20:45:31 -070025 if (obj->type != OBJ_COMMIT) {
Junio C Hamanof76412e2005-08-21 02:51:10 -070026 if (!quiet)
27 error("Object %s is a %s, not a commit",
Linus Torvalds885a86a2006-06-14 16:45:13 -070028 sha1_to_hex(sha1), typename(obj->type));
Linus Torvalds961784e2005-05-18 16:14:22 -070029 return NULL;
30 }
31 return (struct commit *) obj;
32}
33
Junio C Hamanof76412e2005-08-21 02:51:10 -070034struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
35 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070036{
Junio C Hamano9534f402005-11-02 15:19:13 -080037 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070038
39 if (!obj)
40 return NULL;
Junio C Hamanof76412e2005-08-21 02:51:10 -070041 return check_commit(obj, sha1, quiet);
42}
43
44struct commit *lookup_commit_reference(const unsigned char *sha1)
45{
46 return lookup_commit_reference_gently(sha1, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070047}
48
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100049struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
50{
51 struct commit *c = lookup_commit_reference(sha1);
52 if (!c)
53 die(_("could not parse %s"), ref_name);
54 if (hashcmp(sha1, c->object.sha1)) {
55 warning(_("%s %s is not a commit!"),
56 ref_name, sha1_to_hex(sha1));
57 }
58 return c;
59}
60
Jason McMullan5d6ccf52005-06-03 11:05:39 -040061struct commit *lookup_commit(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070062{
63 struct object *obj = lookup_object(sha1);
Jeff King96c4f4a2013-04-09 02:52:56 -040064 if (!obj) {
65 struct commit *c = alloc_commit_node();
66 c->index = commit_count++;
67 return create_object(sha1, OBJ_COMMIT, c);
68 }
Nicolas Pitred1af0022005-05-20 16:59:17 -040069 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070070 obj->type = OBJ_COMMIT;
Junio C Hamanof76412e2005-08-21 02:51:10 -070071 return check_commit(obj, sha1, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070072}
73
Pat Notza6fa5992010-11-02 13:59:07 -060074struct commit *lookup_commit_reference_by_name(const char *name)
75{
76 unsigned char sha1[20];
77 struct commit *commit;
78
Junio C Hamanocd74e472012-07-02 12:04:52 -070079 if (get_sha1_committish(name, sha1))
Pat Notza6fa5992010-11-02 13:59:07 -060080 return NULL;
81 commit = lookup_commit_reference(sha1);
82 if (!commit || parse_commit(commit))
83 return NULL;
84 return commit;
85}
86
Martin Koegler0a617792008-01-19 18:35:23 +010087static unsigned long parse_commit_date(const char *buf, const char *tail)
Daniel Barkalow175785e2005-04-18 11:39:48 -070088{
Martin Koegler0a617792008-01-19 18:35:23 +010089 const char *dateptr;
Daniel Barkalow175785e2005-04-18 11:39:48 -070090
Martin Koegler0a617792008-01-19 18:35:23 +010091 if (buf + 6 >= tail)
92 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070093 if (memcmp(buf, "author", 6))
94 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010095 while (buf < tail && *buf++ != '\n')
Daniel Barkalow175785e2005-04-18 11:39:48 -070096 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010097 if (buf + 9 >= tail)
98 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070099 if (memcmp(buf, "committer", 9))
100 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +0100101 while (buf < tail && *buf++ != '>')
Daniel Barkalow175785e2005-04-18 11:39:48 -0700102 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +0100103 if (buf >= tail)
104 return 0;
105 dateptr = buf;
106 while (buf < tail && *buf++ != '\n')
107 /* nada */;
108 if (buf >= tail)
109 return 0;
110 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
Eric Wongf2909742009-07-02 21:34:54 -0700111 return strtoul(dateptr, NULL, 10);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700112}
113
Junio C Hamano5040f172006-04-06 23:58:51 -0700114static struct commit_graft **commit_graft;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700115static int commit_graft_alloc, commit_graft_nr;
116
117static int commit_graft_pos(const unsigned char *sha1)
118{
119 int lo, hi;
120 lo = 0;
121 hi = commit_graft_nr;
122 while (lo < hi) {
123 int mi = (lo + hi) / 2;
124 struct commit_graft *graft = commit_graft[mi];
David Rientjesa89fccd2006-08-17 11:54:57 -0700125 int cmp = hashcmp(sha1, graft->sha1);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700126 if (!cmp)
127 return mi;
128 if (cmp < 0)
129 hi = mi;
130 else
131 lo = mi + 1;
132 }
133 return -lo - 1;
134}
135
Junio C Hamano5040f172006-04-06 23:58:51 -0700136int register_commit_graft(struct commit_graft *graft, int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700137{
Junio C Hamano5040f172006-04-06 23:58:51 -0700138 int pos = commit_graft_pos(graft->sha1);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700139
Junio C Hamano5040f172006-04-06 23:58:51 -0700140 if (0 <= pos) {
141 if (ignore_dups)
142 free(graft);
143 else {
144 free(commit_graft[pos]);
145 commit_graft[pos] = graft;
146 }
147 return 1;
148 }
149 pos = -pos - 1;
150 if (commit_graft_alloc <= ++commit_graft_nr) {
151 commit_graft_alloc = alloc_nr(commit_graft_alloc);
152 commit_graft = xrealloc(commit_graft,
153 sizeof(*commit_graft) *
154 commit_graft_alloc);
155 }
156 if (pos < commit_graft_nr)
157 memmove(commit_graft + pos + 1,
158 commit_graft + pos,
159 (commit_graft_nr - pos - 1) *
160 sizeof(*commit_graft));
161 commit_graft[pos] = graft;
162 return 0;
163}
164
165struct commit_graft *read_graft_line(char *buf, int len)
166{
167 /* The format is just "Commit Parent1 Parent2 ...\n" */
168 int i;
169 struct commit_graft *graft = NULL;
170
Junio C Hamanofe0a3cb2009-10-14 11:51:03 -0700171 while (len && isspace(buf[len-1]))
172 buf[--len] = '\0';
Yann Dirson360204c2006-04-17 13:41:49 +0200173 if (buf[0] == '#' || buf[0] == '\0')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700174 return NULL;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100175 if ((len + 1) % 41)
176 goto bad_graft_data;
Junio C Hamano5040f172006-04-06 23:58:51 -0700177 i = (len + 1) / 41 - 1;
178 graft = xmalloc(sizeof(*graft) + 20 * i);
179 graft->nr_parent = i;
180 if (get_sha1_hex(buf, graft->sha1))
181 goto bad_graft_data;
182 for (i = 40; i < len; i += 41) {
183 if (buf[i] != ' ')
184 goto bad_graft_data;
185 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
186 goto bad_graft_data;
187 }
188 return graft;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100189
190bad_graft_data:
191 error("bad graft data: %s", buf);
192 free(graft);
193 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700194}
195
Nanako Shiraishic5ae6432008-10-02 19:14:30 +0900196static int read_graft_file(const char *graft_file)
Junio C Hamano5040f172006-04-06 23:58:51 -0700197{
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700198 FILE *fp = fopen(graft_file, "r");
Johannes Schindeline228c172013-12-27 21:49:57 +0100199 struct strbuf buf = STRBUF_INIT;
Junio C Hamano5040f172006-04-06 23:58:51 -0700200 if (!fp)
201 return -1;
Johannes Schindeline228c172013-12-27 21:49:57 +0100202 while (!strbuf_getwholeline(&buf, fp, '\n')) {
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700203 /* The format is just "Commit Parent1 Parent2 ...\n" */
Johannes Schindeline228c172013-12-27 21:49:57 +0100204 struct commit_graft *graft = read_graft_line(buf.buf, buf.len);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700205 if (!graft)
206 continue;
Junio C Hamano5040f172006-04-06 23:58:51 -0700207 if (register_commit_graft(graft, 1))
Johannes Schindeline228c172013-12-27 21:49:57 +0100208 error("duplicate graft data: %s", buf.buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700209 }
210 fclose(fp);
Johannes Schindeline228c172013-12-27 21:49:57 +0100211 strbuf_release(&buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700212 return 0;
213}
214
215static void prepare_commit_graft(void)
216{
217 static int commit_graft_prepared;
218 char *graft_file;
219
220 if (commit_graft_prepared)
221 return;
222 graft_file = get_graft_file();
223 read_graft_file(graft_file);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100224 /* make sure shallows are read */
225 is_repository_shallow();
Junio C Hamano5040f172006-04-06 23:58:51 -0700226 commit_graft_prepared = 1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700227}
228
Martin Koegler45163382008-02-25 22:46:07 +0100229struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700230{
231 int pos;
Junio C Hamano5040f172006-04-06 23:58:51 -0700232 prepare_commit_graft();
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700233 pos = commit_graft_pos(sha1);
234 if (pos < 0)
235 return NULL;
236 return commit_graft[pos];
237}
238
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700239int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100240{
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700241 int i, ret;
242 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
243 ret = fn(commit_graft[i], cb_data);
244 return ret;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100245}
246
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100247int unregister_shallow(const unsigned char *sha1)
248{
249 int pos = commit_graft_pos(sha1);
250 if (pos < 0)
251 return -1;
252 if (pos + 1 < commit_graft_nr)
Jeff King65d41d42010-01-29 05:28:44 -0500253 memmove(commit_graft + pos, commit_graft + pos + 1,
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100254 sizeof(struct commit_graft *)
255 * (commit_graft_nr - pos - 1));
256 commit_graft_nr--;
257 return 0;
258}
259
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700260int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700261{
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700262 const char *tail = buffer;
263 const char *bufptr = buffer;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700264 unsigned char parent[20];
Linus Torvalds6c88be12005-06-20 20:26:03 -0700265 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700266 struct commit_graft *graft;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400267
Daniel Barkalow175785e2005-04-18 11:39:48 -0700268 if (item->object.parsed)
269 return 0;
270 item->object.parsed = 1;
Junio C Hamano3b44f152006-06-28 03:51:00 -0700271 tail += size;
Martin Koegler0a617792008-01-19 18:35:23 +0100272 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700273 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
Martin Koegler0a617792008-01-19 18:35:23 +0100274 if (get_sha1_hex(bufptr + 5, parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800275 return error("bad tree pointer in commit %s",
276 sha1_to_hex(item->object.sha1));
Daniel Barkalow175785e2005-04-18 11:39:48 -0700277 item->tree = lookup_tree(parent);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700278 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700279 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700280
281 graft = lookup_commit_graft(item->object.sha1);
Junio C Hamano3b44f152006-06-28 03:51:00 -0700282 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700283 struct commit *new_parent;
284
Junio C Hamano3b44f152006-06-28 03:51:00 -0700285 if (tail <= bufptr + 48 ||
286 get_sha1_hex(bufptr + 7, parent) ||
287 bufptr[47] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700288 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700289 bufptr += 48;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +0200290 /*
291 * The clone is shallow if nr_parent < 0, and we must
292 * not traverse its real parents even when we unhide them.
293 */
294 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700295 continue;
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700296 new_parent = lookup_commit(parent);
Miklos Vajna39468de2008-06-07 22:38:37 +0200297 if (new_parent)
Linus Torvalds6c88be12005-06-20 20:26:03 -0700298 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700299 }
300 if (graft) {
301 int i;
302 struct commit *new_parent;
303 for (i = 0; i < graft->nr_parent; i++) {
304 new_parent = lookup_commit(graft->parent[i]);
305 if (!new_parent)
306 continue;
307 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700308 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700309 }
Martin Koegler0a617792008-01-19 18:35:23 +0100310 item->date = parse_commit_date(bufptr, tail);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800311
Daniel Barkalow175785e2005-04-18 11:39:48 -0700312 return 0;
313}
314
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400315int parse_commit(struct commit *item)
316{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500317 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400318 void *buffer;
319 unsigned long size;
320 int ret;
321
Martin Koegler9786f682008-02-18 21:48:02 +0100322 if (!item)
323 return -1;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400324 if (item->object.parsed)
325 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500326 buffer = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400327 if (!buffer)
328 return error("Could not read %s",
329 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500330 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400331 free(buffer);
332 return error("Object %s not a commit",
333 sha1_to_hex(item->object.sha1));
334 }
335 ret = parse_commit_buffer(item, buffer, size);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700336 if (save_commit_buffer && !ret) {
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700337 item->buffer = buffer;
338 return 0;
339 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400340 free(buffer);
341 return ret;
342}
343
Christian Couder11af2aa2010-07-22 15:18:30 +0200344int find_commit_subject(const char *commit_buffer, const char **subject)
345{
346 const char *eol;
347 const char *p = commit_buffer;
348
349 while (*p && (*p != '\n' || p[1] != '\n'))
350 p++;
351 if (*p) {
352 p += 2;
353 for (eol = p; *eol && *eol != '\n'; eol++)
354 ; /* do nothing */
355 } else
356 eol = p;
357
358 *subject = p;
359
360 return eol - p;
361}
362
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700363struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700364{
Christopher Li812666c2005-04-26 12:00:58 -0700365 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700366 new_list->item = item;
367 new_list->next = *list_p;
368 *list_p = new_list;
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700369 return new_list;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700370}
371
Miklos Vajna65319472008-06-27 18:21:55 +0200372unsigned commit_list_count(const struct commit_list *l)
373{
374 unsigned c = 0;
375 for (; l; l = l->next )
376 c++;
377 return c;
378}
379
Thomas Rast53d00b32013-07-31 22:13:20 +0200380struct commit_list *copy_commit_list(struct commit_list *list)
381{
382 struct commit_list *head = NULL;
383 struct commit_list **pp = &head;
384 while (list) {
385 struct commit_list *new;
386 new = xmalloc(sizeof(struct commit_list));
387 new->item = list->item;
388 new->next = NULL;
389 *pp = new;
390 pp = &new->next;
391 list = list->next;
392 }
393 return head;
394}
395
Daniel Barkalow175785e2005-04-18 11:39:48 -0700396void free_commit_list(struct commit_list *list)
397{
398 while (list) {
399 struct commit_list *temp = list;
400 list = temp->next;
401 free(temp);
402 }
403}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700404
Thiago Farina47e44ed2010-11-26 23:58:14 -0200405struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700406{
407 struct commit_list **pp = list;
408 struct commit_list *p;
409 while ((p = *pp) != NULL) {
410 if (p->item->date < item->date) {
411 break;
412 }
413 pp = &p->next;
414 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700415 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700416}
417
René Scharfe46905892012-04-01 00:10:39 +0200418static int commit_list_compare_by_date(const void *a, const void *b)
419{
420 unsigned long a_date = ((const struct commit_list *)a)->item->date;
421 unsigned long b_date = ((const struct commit_list *)b)->item->date;
422 if (a_date < b_date)
423 return 1;
424 if (a_date > b_date)
425 return -1;
426 return 0;
427}
428
429static void *commit_list_get_next(const void *a)
430{
431 return ((const struct commit_list *)a)->next;
432}
433
434static void commit_list_set_next(void *a, void *next)
435{
436 ((struct commit_list *)a)->next = next;
437}
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700438
Thiago Farina47e44ed2010-11-26 23:58:14 -0200439void commit_list_sort_by_date(struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700440{
Junio C Hamano7365c952012-04-17 11:07:01 -0700441 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
442 commit_list_compare_by_date);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700443}
444
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700445struct commit *pop_most_recent_commit(struct commit_list **list,
446 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700447{
448 struct commit *ret = (*list)->item;
449 struct commit_list *parents = ret->parents;
450 struct commit_list *old = *list;
451
452 *list = (*list)->next;
453 free(old);
454
455 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700456 struct commit *commit = parents->item;
Martin Koeglerdec38c82008-02-18 21:48:03 +0100457 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700458 commit->object.flags |= mark;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200459 commit_list_insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700460 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700461 parents = parents->next;
462 }
463 return ret;
464}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700465
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700466static void clear_commit_marks_1(struct commit_list **plist,
467 struct commit *commit, unsigned int mark)
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800468{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100469 while (commit) {
470 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800471
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100472 if (!(mark & commit->object.flags))
473 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700474
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100475 commit->object.flags &= ~mark;
476
477 parents = commit->parents;
478 if (!parents)
479 return;
480
481 while ((parents = parents->next))
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700482 commit_list_insert(parents->item, plist);
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100483
484 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800485 }
486}
487
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800488void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700489{
490 struct commit_list *list = NULL;
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800491
492 while (nr--) {
493 commit_list_insert(*commit, &list);
494 commit++;
495 }
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700496 while (list)
497 clear_commit_marks_1(&list, pop_commit(&list), mark);
498}
499
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800500void clear_commit_marks(struct commit *commit, unsigned int mark)
501{
502 clear_commit_marks_many(1, &commit, mark);
503}
504
René Scharfe86a0a402011-10-01 18:16:08 +0200505void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
506{
507 struct object *object;
508 struct commit *commit;
509 unsigned int i;
510
511 for (i = 0; i < a->nr; i++) {
512 object = a->objects[i].item;
513 commit = lookup_commit_reference_gently(object->sha1, 1);
514 if (commit)
515 clear_commit_marks(commit, mark);
516 }
517}
518
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000519struct commit *pop_commit(struct commit_list **stack)
520{
521 struct commit_list *top = *stack;
522 struct commit *item = top ? top->item : NULL;
523
524 if (top) {
525 *stack = top->next;
526 free(top);
527 }
528 return item;
529}
530
Jon Seymourab580ac2005-07-07 02:39:34 +1000531/*
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700532 * Topological sort support
533 */
Junio C Hamano66eb3752013-04-12 23:25:49 -0700534
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700535/* count number of children that have not been emitted */
536define_commit_slab(indegree_slab, int);
Jeff King96c4f4a2013-04-09 02:52:56 -0400537
Junio C Hamano81c6b382013-06-07 10:35:54 -0700538/* record author-date for each commit object */
539define_commit_slab(author_date_slab, unsigned long);
540
541static void record_author_date(struct author_date_slab *author_date,
542 struct commit *commit)
543{
544 const char *buf, *line_end;
545 char *buffer = NULL;
546 struct ident_split ident;
547 char *date_end;
548 unsigned long date;
549
550 if (!commit->buffer) {
551 unsigned long size;
552 enum object_type type;
553 buffer = read_sha1_file(commit->object.sha1, &type, &size);
554 if (!buffer)
555 return;
556 }
557
558 for (buf = commit->buffer ? commit->buffer : buffer;
559 buf;
560 buf = line_end + 1) {
561 line_end = strchrnul(buf, '\n');
562 if (prefixcmp(buf, "author ")) {
563 if (!line_end[0] || line_end[1] == '\n')
564 return; /* end of header */
565 continue;
566 }
567 if (split_ident_line(&ident,
568 buf + strlen("author "),
569 line_end - (buf + strlen("author "))) ||
570 !ident.date_begin || !ident.date_end)
571 goto fail_exit; /* malformed "author" line */
572 break;
573 }
574
575 date = strtoul(ident.date_begin, &date_end, 10);
576 if (date_end != ident.date_end)
577 goto fail_exit; /* malformed date */
578 *(author_date_slab_at(author_date, commit)) = date;
579
580fail_exit:
581 free(buffer);
582}
583
584static int compare_commits_by_author_date(const void *a_, const void *b_,
585 void *cb_data)
586{
587 const struct commit *a = a_, *b = b_;
588 struct author_date_slab *author_date = cb_data;
589 unsigned long a_date = *(author_date_slab_at(author_date, a));
590 unsigned long b_date = *(author_date_slab_at(author_date, b));
591
592 /* newer commits with larger date first */
593 if (a_date < b_date)
594 return 1;
595 else if (a_date > b_date)
596 return -1;
597 return 0;
598}
599
Jeff King727377f2013-07-02 02:21:48 -0400600int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700601{
602 const struct commit *a = a_, *b = b_;
603 /* newer commits with larger date first */
604 if (a->date < b->date)
605 return 1;
606 else if (a->date > b->date)
607 return -1;
608 return 0;
609}
610
Jon Seymourab580ac2005-07-07 02:39:34 +1000611/*
612 * Performs an in-place topological sort on the list supplied.
613 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700614void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
Jon Seymourab580ac2005-07-07 02:39:34 +1000615{
Linus Torvalds23c17d42007-11-02 13:32:58 -0700616 struct commit_list *next, *orig = *list;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700617 struct commit_list **pptr;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700618 struct indegree_slab indegree;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700619 struct prio_queue queue;
620 struct commit *commit;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700621 struct author_date_slab author_date;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100622
Linus Torvalds23c17d42007-11-02 13:32:58 -0700623 if (!orig)
Eric Wong7d6fb372005-12-24 04:12:43 -0800624 return;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700625 *list = NULL;
626
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700627 init_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700628 memset(&queue, '\0', sizeof(queue));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700629
Junio C Hamanoda24b102013-06-06 21:58:12 -0700630 switch (sort_order) {
631 default: /* REV_SORT_IN_GRAPH_ORDER */
632 queue.compare = NULL;
633 break;
634 case REV_SORT_BY_COMMIT_DATE:
635 queue.compare = compare_commits_by_commit_date;
636 break;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700637 case REV_SORT_BY_AUTHOR_DATE:
638 init_author_date_slab(&author_date);
639 queue.compare = compare_commits_by_author_date;
640 queue.cb_data = &author_date;
641 break;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700642 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400643
Linus Torvalds23c17d42007-11-02 13:32:58 -0700644 /* Mark them and clear the indegree */
645 for (next = orig; next; next = next->next) {
646 struct commit *commit = next->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700647 *(indegree_slab_at(&indegree, commit)) = 1;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700648 /* also record the author dates, if needed */
649 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
650 record_author_date(&author_date, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000651 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700652
Jon Seymourab580ac2005-07-07 02:39:34 +1000653 /* update the indegree */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700654 for (next = orig; next; next = next->next) {
Junio C Hamanoda24b102013-06-06 21:58:12 -0700655 struct commit_list *parents = next->item->parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000656 while (parents) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700657 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700658 int *pi = indegree_slab_at(&indegree, parent);
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100659
Jeff King96c4f4a2013-04-09 02:52:56 -0400660 if (*pi)
661 (*pi)++;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700662 parents = parents->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000663 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000664 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700665
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700666 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200667 * find the tips
668 *
669 * tips are nodes not reachable from any other node in the list
670 *
671 * the tips serve as a starting set for the work queue.
672 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700673 for (next = orig; next; next = next->next) {
674 struct commit *commit = next->item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000675
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700676 if (*(indegree_slab_at(&indegree, commit)) == 1)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700677 prio_queue_put(&queue, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000678 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800679
Junio C Hamanoda24b102013-06-06 21:58:12 -0700680 /*
681 * This is unfortunate; the initial tips need to be shown
682 * in the order given from the revision traversal machinery.
683 */
684 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
685 prio_queue_reverse(&queue);
686
687 /* We no longer need the commit list */
688 free_commit_list(orig);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700689
690 pptr = list;
691 *list = NULL;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700692 while ((commit = prio_queue_get(&queue)) != NULL) {
693 struct commit_list *parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000694
Linus Torvalds23c17d42007-11-02 13:32:58 -0700695 for (parents = commit->parents; parents ; parents = parents->next) {
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -0700696 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700697 int *pi = indegree_slab_at(&indegree, parent);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700698
Jeff King96c4f4a2013-04-09 02:52:56 -0400699 if (!*pi)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700700 continue;
701
702 /*
703 * parents are only enqueued for emission
704 * when all their children have been emitted thereby
705 * guaranteeing topological order.
706 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700707 if (--(*pi) == 1)
708 prio_queue_put(&queue, parent);
Jon Seymourab580ac2005-07-07 02:39:34 +1000709 }
710 /*
Junio C Hamanoda24b102013-06-06 21:58:12 -0700711 * all children of commit have already been
712 * emitted. we can emit it now.
Pierre Habouzit674d1722007-09-10 12:35:06 +0200713 */
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700714 *(indegree_slab_at(&indegree, commit)) = 0;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700715
716 pptr = &commit_list_insert(commit, pptr)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000717 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400718
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700719 clear_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700720 clear_prio_queue(&queue);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700721 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
722 clear_author_date_slab(&author_date);
Jon Seymourab580ac2005-07-07 02:39:34 +1000723}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200724
Junio C Hamano12952282007-01-08 23:10:49 -0800725/* merge-base stuff */
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200726
Junio C Hamano577ed5c2006-10-22 17:32:47 -0700727/* bits #0..15 in revision.h */
728#define PARENT1 (1u<<16)
729#define PARENT2 (1u<<17)
730#define STALE (1u<<18)
731#define RESULT (1u<<19)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200732
Junio C Hamano12952282007-01-08 23:10:49 -0800733static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
734
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200735static struct commit *interesting(struct commit_list *list)
736{
737 while (list) {
738 struct commit *commit = list->item;
739 list = list->next;
Junio C Hamano542ccef2006-07-02 11:34:17 -0700740 if (commit->object.flags & STALE)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200741 continue;
742 return commit;
743 }
744 return NULL;
745}
746
Junio C Hamanod8669242012-10-04 15:37:15 -0700747/* all input commits in one and twos[] must have been parsed! */
Junio C Hamanoda1f5152012-08-30 14:39:03 -0700748static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200749{
750 struct commit_list *list = NULL;
751 struct commit_list *result = NULL;
Junio C Hamano6a938642008-06-27 18:22:02 +0200752 int i;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200753
Junio C Hamanof3249432006-07-04 18:46:42 -0700754 one->object.flags |= PARENT1;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200755 commit_list_insert_by_date(one, &list);
Junio C Hamanod8669242012-10-04 15:37:15 -0700756 if (!n)
757 return list;
Junio C Hamano6a938642008-06-27 18:22:02 +0200758 for (i = 0; i < n; i++) {
759 twos[i]->object.flags |= PARENT2;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200760 commit_list_insert_by_date(twos[i], &list);
Junio C Hamano6a938642008-06-27 18:22:02 +0200761 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200762
763 while (interesting(list)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700764 struct commit *commit;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200765 struct commit_list *parents;
Brandon Caseyf203d692009-08-27 11:16:34 -0500766 struct commit_list *next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700767 int flags;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200768
Junio C Hamanof3249432006-07-04 18:46:42 -0700769 commit = list->item;
Brandon Caseyf203d692009-08-27 11:16:34 -0500770 next = list->next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700771 free(list);
Brandon Caseyf203d692009-08-27 11:16:34 -0500772 list = next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700773
774 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200775 if (flags == (PARENT1 | PARENT2)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700776 if (!(commit->object.flags & RESULT)) {
777 commit->object.flags |= RESULT;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200778 commit_list_insert_by_date(commit, &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700779 }
Junio C Hamano542ccef2006-07-02 11:34:17 -0700780 /* Mark parents of a found merge stale */
781 flags |= STALE;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200782 }
783 parents = commit->parents;
784 while (parents) {
785 struct commit *p = parents->item;
786 parents = parents->next;
787 if ((p->object.flags & flags) == flags)
788 continue;
Martin Koegler172947e2008-02-18 21:47:57 +0100789 if (parse_commit(p))
790 return NULL;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200791 p->object.flags |= flags;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200792 commit_list_insert_by_date(p, &list);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200793 }
794 }
795
Junio C Hamano12952282007-01-08 23:10:49 -0800796 free_commit_list(list);
Junio C Hamanoda1f5152012-08-30 14:39:03 -0700797 return result;
798}
799
800static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
801{
802 struct commit_list *list = NULL;
803 struct commit_list *result = NULL;
804 int i;
805
806 for (i = 0; i < n; i++) {
807 if (one == twos[i])
808 /*
809 * We do not mark this even with RESULT so we do not
810 * have to clean it up.
811 */
812 return commit_list_insert(one, &result);
813 }
814
815 if (parse_commit(one))
816 return NULL;
817 for (i = 0; i < n; i++) {
818 if (parse_commit(twos[i]))
819 return NULL;
820 }
821
822 list = paint_down_to_common(one, n, twos);
823
Junio C Hamanof3249432006-07-04 18:46:42 -0700824 while (list) {
Brandon Caseyf203d692009-08-27 11:16:34 -0500825 struct commit_list *next = list->next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700826 if (!(list->item->object.flags & STALE))
Thiago Farina47e44ed2010-11-26 23:58:14 -0200827 commit_list_insert_by_date(list->item, &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700828 free(list);
Brandon Caseyf203d692009-08-27 11:16:34 -0500829 list = next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700830 }
831 return result;
832}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200833
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200834struct commit_list *get_octopus_merge_bases(struct commit_list *in)
835{
836 struct commit_list *i, *j, *k, *ret = NULL;
837 struct commit_list **pptr = &ret;
838
839 for (i = in; i; i = i->next) {
840 if (!ret)
841 pptr = &commit_list_insert(i->item, pptr)->next;
842 else {
843 struct commit_list *new = NULL, *end = NULL;
844
845 for (j = ret; j; j = j->next) {
846 struct commit_list *bases;
847 bases = get_merge_bases(i->item, j->item, 1);
848 if (!new)
849 new = bases;
850 else
851 end->next = bases;
852 for (k = bases; k; k = k->next)
853 end = k;
854 }
855 ret = new;
856 }
857 }
858 return ret;
859}
860
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700861static int remove_redundant(struct commit **array, int cnt)
862{
863 /*
864 * Some commit in the array may be an ancestor of
865 * another commit. Move such commit to the end of
866 * the array, and return the number of commits that
867 * are independent from each other.
868 */
869 struct commit **work;
870 unsigned char *redundant;
871 int *filled_index;
872 int i, j, filled;
873
874 work = xcalloc(cnt, sizeof(*work));
875 redundant = xcalloc(cnt, 1);
876 filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
877
Junio C Hamanod8669242012-10-04 15:37:15 -0700878 for (i = 0; i < cnt; i++)
879 parse_commit(array[i]);
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700880 for (i = 0; i < cnt; i++) {
881 struct commit_list *common;
882
883 if (redundant[i])
884 continue;
885 for (j = filled = 0; j < cnt; j++) {
886 if (i == j || redundant[j])
887 continue;
888 filled_index[filled] = j;
889 work[filled++] = array[j];
890 }
891 common = paint_down_to_common(array[i], filled, work);
892 if (array[i]->object.flags & PARENT2)
893 redundant[i] = 1;
894 for (j = 0; j < filled; j++)
895 if (work[j]->object.flags & PARENT1)
896 redundant[filled_index[j]] = 1;
897 clear_commit_marks(array[i], all_flags);
898 for (j = 0; j < filled; j++)
899 clear_commit_marks(work[j], all_flags);
900 free_commit_list(common);
901 }
902
903 /* Now collect the result */
904 memcpy(work, array, sizeof(*array) * cnt);
905 for (i = filled = 0; i < cnt; i++)
906 if (!redundant[i])
907 array[filled++] = work[i];
908 for (j = filled, i = 0; i < cnt; i++)
909 if (redundant[i])
910 array[j++] = work[i];
911 free(work);
912 free(redundant);
913 free(filled_index);
914 return filled;
915}
916
Junio C Hamano6a938642008-06-27 18:22:02 +0200917struct commit_list *get_merge_bases_many(struct commit *one,
918 int n,
919 struct commit **twos,
920 int cleanup)
Junio C Hamanof3249432006-07-04 18:46:42 -0700921{
Junio C Hamanof3249432006-07-04 18:46:42 -0700922 struct commit_list *list;
923 struct commit **rslt;
924 struct commit_list *result;
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700925 int cnt, i;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200926
Junio C Hamano6a938642008-06-27 18:22:02 +0200927 result = merge_bases_many(one, n, twos);
928 for (i = 0; i < n; i++) {
929 if (one == twos[i])
930 return result;
931 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700932 if (!result || !result->next) {
933 if (cleanup) {
934 clear_commit_marks(one, all_flags);
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800935 clear_commit_marks_many(n, twos, all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200936 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700937 return result;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200938 }
939
Junio C Hamanof3249432006-07-04 18:46:42 -0700940 /* There are more than one */
941 cnt = 0;
942 list = result;
943 while (list) {
944 list = list->next;
945 cnt++;
946 }
947 rslt = xcalloc(cnt, sizeof(*rslt));
948 for (list = result, i = 0; list; list = list->next)
949 rslt[i++] = list->item;
950 free_commit_list(result);
951
952 clear_commit_marks(one, all_flags);
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800953 clear_commit_marks_many(n, twos, all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200954
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700955 cnt = remove_redundant(rslt, cnt);
Junio C Hamanof3249432006-07-04 18:46:42 -0700956 result = NULL;
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700957 for (i = 0; i < cnt; i++)
958 commit_list_insert_by_date(rslt[i], &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700959 free(rslt);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200960 return result;
961}
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800962
Junio C Hamano6a938642008-06-27 18:22:02 +0200963struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
964 int cleanup)
965{
966 return get_merge_bases_many(one, 1, &two, cleanup);
967}
968
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700969/*
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +0200970 * Is "commit" a descendant of one of the elements on the "with_commit" list?
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700971 */
Jake Goulding7fcdb362009-01-26 09:13:24 -0500972int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
973{
974 if (!with_commit)
975 return 1;
976 while (with_commit) {
977 struct commit *other;
978
979 other = with_commit->item;
980 with_commit = with_commit->next;
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700981 if (in_merge_bases(other, commit))
Jake Goulding7fcdb362009-01-26 09:13:24 -0500982 return 1;
983 }
984 return 0;
985}
986
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700987/*
Junio C Hamano4c4b27e2013-03-04 10:16:42 -0800988 * Is "commit" an ancestor of one of the "references"?
989 */
990int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
991{
992 struct commit_list *bases;
993 int ret = 0, i;
994
995 if (parse_commit(commit))
996 return ret;
997 for (i = 0; i < nr_reference; i++)
998 if (parse_commit(reference[i]))
999 return ret;
1000
1001 bases = paint_down_to_common(commit, nr_reference, reference);
1002 if (commit->object.flags & PARENT2)
1003 ret = 1;
1004 clear_commit_marks(commit, all_flags);
Junio C Hamano557899f2013-03-05 11:56:03 -08001005 clear_commit_marks_many(nr_reference, reference, all_flags);
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001006 free_commit_list(bases);
1007 return ret;
1008}
1009
1010/*
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001011 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1012 */
1013int in_merge_bases(struct commit *commit, struct commit *reference)
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -08001014{
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001015 return in_merge_bases_many(commit, 1, &reference);
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -08001016}
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001017
1018struct commit_list *reduce_heads(struct commit_list *heads)
1019{
1020 struct commit_list *p;
1021 struct commit_list *result = NULL, **tail = &result;
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001022 struct commit **array;
1023 int num_head, i;
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001024
1025 if (!heads)
1026 return NULL;
1027
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001028 /* Uniquify */
1029 for (p = heads; p; p = p->next)
1030 p->item->object.flags &= ~STALE;
1031 for (p = heads, num_head = 0; p; p = p->next) {
1032 if (p->item->object.flags & STALE)
Junio C Hamano711f6b22008-07-14 00:09:41 -07001033 continue;
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001034 p->item->object.flags |= STALE;
1035 num_head++;
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001036 }
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001037 array = xcalloc(sizeof(*array), num_head);
1038 for (p = heads, i = 0; p; p = p->next) {
1039 if (p->item->object.flags & STALE) {
1040 array[i++] = p->item;
1041 p->item->object.flags &= ~STALE;
1042 }
1043 }
1044 num_head = remove_redundant(array, num_head);
1045 for (i = 0; i < num_head; i++)
1046 tail = &commit_list_insert(array[i], tail)->next;
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001047 return result;
1048}
Jeff King40d52ff2010-04-01 20:05:23 -04001049
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001050static const char gpg_sig_header[] = "gpgsig";
1051static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1052
1053static int do_sign_commit(struct strbuf *buf, const char *keyid)
1054{
1055 struct strbuf sig = STRBUF_INIT;
1056 int inspos, copypos;
1057
1058 /* find the end of the header */
1059 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
1060
1061 if (!keyid || !*keyid)
1062 keyid = get_signing_key();
1063 if (sign_buffer(buf, &sig, keyid)) {
1064 strbuf_release(&sig);
1065 return -1;
1066 }
1067
1068 for (copypos = 0; sig.buf[copypos]; ) {
1069 const char *bol = sig.buf + copypos;
1070 const char *eol = strchrnul(bol, '\n');
1071 int len = (eol - bol) + !!*eol;
1072
1073 if (!copypos) {
1074 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1075 inspos += gpg_sig_header_len;
1076 }
1077 strbuf_insert(buf, inspos++, " ", 1);
1078 strbuf_insert(buf, inspos, bol, len);
1079 inspos += len;
1080 copypos += len;
1081 }
1082 strbuf_release(&sig);
1083 return 0;
1084}
1085
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001086int parse_signed_commit(const unsigned char *sha1,
1087 struct strbuf *payload, struct strbuf *signature)
1088{
1089 unsigned long size;
1090 enum object_type type;
1091 char *buffer = read_sha1_file(sha1, &type, &size);
1092 int in_signature, saw_signature = -1;
1093 char *line, *tail;
1094
1095 if (!buffer || type != OBJ_COMMIT)
1096 goto cleanup;
1097
1098 line = buffer;
1099 tail = buffer + size;
1100 in_signature = 0;
1101 saw_signature = 0;
1102 while (line < tail) {
1103 const char *sig = NULL;
1104 char *next = memchr(line, '\n', tail - line);
1105
1106 next = next ? next + 1 : tail;
1107 if (in_signature && line[0] == ' ')
1108 sig = line + 1;
1109 else if (!prefixcmp(line, gpg_sig_header) &&
1110 line[gpg_sig_header_len] == ' ')
1111 sig = line + gpg_sig_header_len + 1;
1112 if (sig) {
1113 strbuf_add(signature, sig, next - sig);
1114 saw_signature = 1;
1115 in_signature = 1;
1116 } else {
1117 if (*line == '\n')
1118 /* dump the whole remainder of the buffer */
1119 next = tail;
1120 strbuf_add(payload, line, next - line);
1121 in_signature = 0;
1122 }
1123 line = next;
1124 }
1125 cleanup:
1126 free(buffer);
1127 return saw_signature;
1128}
1129
Junio C Hamano5231c632011-11-07 16:21:32 -08001130static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1131{
1132 struct merge_remote_desc *desc;
1133 struct commit_extra_header *mergetag;
1134 char *buf;
1135 unsigned long size, len;
1136 enum object_type type;
1137
1138 desc = merge_remote_util(parent);
1139 if (!desc || !desc->obj)
1140 return;
1141 buf = read_sha1_file(desc->obj->sha1, &type, &size);
1142 if (!buf || type != OBJ_TAG)
1143 goto free_return;
1144 len = parse_signature(buf, size);
1145 if (size == len)
1146 goto free_return;
1147 /*
1148 * We could verify this signature and either omit the tag when
1149 * it does not validate, but the integrator may not have the
1150 * public key of the signer of the tag he is merging, while a
1151 * later auditor may have it while auditing, so let's not run
1152 * verify-signed-buffer here for now...
1153 *
1154 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1155 * warn("warning: signed tag unverified.");
1156 */
1157 mergetag = xcalloc(1, sizeof(*mergetag));
1158 mergetag->key = xstrdup("mergetag");
1159 mergetag->value = buf;
1160 mergetag->len = size;
1161
1162 **tail = mergetag;
1163 *tail = &mergetag->next;
1164 return;
1165
1166free_return:
1167 free(buf);
1168}
1169
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001170static struct {
1171 char result;
1172 const char *check;
1173} sigcheck_gpg_status[] = {
1174 { 'G', "\n[GNUPG:] GOODSIG " },
1175 { 'B', "\n[GNUPG:] BADSIG " },
Sebastian Götteeb307ae2013-03-31 18:02:46 +02001176 { 'U', "\n[GNUPG:] TRUST_NEVER" },
1177 { 'U', "\n[GNUPG:] TRUST_UNDEFINED" },
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001178};
1179
1180static void parse_gpg_output(struct signature_check *sigc)
1181{
1182 const char *buf = sigc->gpg_status;
1183 int i;
1184
Sebastian Göttef8aae8d2013-03-31 18:01:34 +02001185 /* Iterate over all search strings */
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001186 for (i = 0; i < ARRAY_SIZE(sigcheck_gpg_status); i++) {
Sebastian Göttef8aae8d2013-03-31 18:01:34 +02001187 const char *found, *next;
1188
1189 if (!prefixcmp(buf, sigcheck_gpg_status[i].check + 1)) {
1190 /* At the very beginning of the buffer */
1191 found = buf + strlen(sigcheck_gpg_status[i].check + 1);
1192 } else {
1193 found = strstr(buf, sigcheck_gpg_status[i].check);
1194 if (!found)
1195 continue;
1196 found += strlen(sigcheck_gpg_status[i].check);
1197 }
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001198 sigc->result = sigcheck_gpg_status[i].result;
Sebastian Götteeb307ae2013-03-31 18:02:46 +02001199 /* The trust messages are not followed by key/signer information */
1200 if (sigc->result != 'U') {
1201 sigc->key = xmemdupz(found, 16);
1202 found += 17;
1203 next = strchrnul(found, '\n');
1204 sigc->signer = xmemdupz(found, next - found);
1205 }
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001206 }
1207}
1208
1209void check_commit_signature(const struct commit* commit, struct signature_check *sigc)
1210{
1211 struct strbuf payload = STRBUF_INIT;
1212 struct strbuf signature = STRBUF_INIT;
1213 struct strbuf gpg_output = STRBUF_INIT;
1214 struct strbuf gpg_status = STRBUF_INIT;
1215 int status;
1216
1217 sigc->result = 'N';
1218
1219 if (parse_signed_commit(commit->object.sha1,
1220 &payload, &signature) <= 0)
1221 goto out;
1222 status = verify_signed_buffer(payload.buf, payload.len,
1223 signature.buf, signature.len,
1224 &gpg_output, &gpg_status);
1225 if (status && !gpg_output.len)
1226 goto out;
1227 sigc->gpg_output = strbuf_detach(&gpg_output, NULL);
1228 sigc->gpg_status = strbuf_detach(&gpg_status, NULL);
1229 parse_gpg_output(sigc);
1230
1231 out:
1232 strbuf_release(&gpg_status);
1233 strbuf_release(&gpg_output);
1234 strbuf_release(&payload);
1235 strbuf_release(&signature);
1236}
1237
1238
1239
Junio C Hamano5231c632011-11-07 16:21:32 -08001240void append_merge_tag_headers(struct commit_list *parents,
1241 struct commit_extra_header ***tail)
1242{
1243 while (parents) {
1244 struct commit *parent = parents->item;
1245 handle_signed_tag(parent, tail);
1246 parents = parents->next;
1247 }
1248}
1249
1250static void add_extra_header(struct strbuf *buffer,
1251 struct commit_extra_header *extra)
1252{
1253 strbuf_addstr(buffer, extra->key);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001254 if (extra->len)
1255 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1256 else
1257 strbuf_addch(buffer, '\n');
1258}
1259
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001260struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1261 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001262{
1263 struct commit_extra_header *extra = NULL;
1264 unsigned long size;
1265 enum object_type type;
1266 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1267 if (buffer && type == OBJ_COMMIT)
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001268 extra = read_commit_extra_header_lines(buffer, size, exclude);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001269 free(buffer);
1270 return extra;
1271}
1272
1273static inline int standard_header_field(const char *field, size_t len)
1274{
1275 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1276 (len == 6 && !memcmp(field, "parent ", 7)) ||
1277 (len == 6 && !memcmp(field, "author ", 7)) ||
1278 (len == 9 && !memcmp(field, "committer ", 10)) ||
1279 (len == 8 && !memcmp(field, "encoding ", 9)));
1280}
1281
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001282static int excluded_header_field(const char *field, size_t len, const char **exclude)
1283{
1284 if (!exclude)
1285 return 0;
1286
1287 while (*exclude) {
1288 size_t xlen = strlen(*exclude);
1289 if (len == xlen &&
1290 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1291 return 1;
1292 exclude++;
1293 }
1294 return 0;
1295}
1296
Junio C Hamano82a75292012-09-15 13:58:15 -07001297static struct commit_extra_header *read_commit_extra_header_lines(
1298 const char *buffer, size_t size,
1299 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001300{
1301 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1302 const char *line, *next, *eof, *eob;
1303 struct strbuf buf = STRBUF_INIT;
1304
1305 for (line = buffer, eob = line + size;
1306 line < eob && *line != '\n';
1307 line = next) {
1308 next = memchr(line, '\n', eob - line);
1309 next = next ? next + 1 : eob;
1310 if (*line == ' ') {
1311 /* continuation */
1312 if (it)
1313 strbuf_add(&buf, line + 1, next - (line + 1));
1314 continue;
1315 }
1316 if (it)
1317 it->value = strbuf_detach(&buf, &it->len);
1318 strbuf_reset(&buf);
1319 it = NULL;
1320
1321 eof = strchr(line, ' ');
1322 if (next <= eof)
1323 eof = next;
1324
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001325 if (standard_header_field(line, eof - line) ||
1326 excluded_header_field(line, eof - line, exclude))
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001327 continue;
1328
1329 it = xcalloc(1, sizeof(*it));
1330 it->key = xmemdupz(line, eof-line);
1331 *tail = it;
1332 tail = &it->next;
1333 if (eof + 1 < next)
1334 strbuf_add(&buf, eof + 1, next - (eof + 1));
1335 }
1336 if (it)
1337 it->value = strbuf_detach(&buf, &it->len);
1338 return extra;
Junio C Hamano5231c632011-11-07 16:21:32 -08001339}
1340
1341void free_commit_extra_headers(struct commit_extra_header *extra)
1342{
1343 while (extra) {
1344 struct commit_extra_header *next = extra->next;
1345 free(extra->key);
1346 free(extra->value);
1347 free(extra);
1348 extra = next;
1349 }
1350}
1351
Junio C Hamanof35ccd92011-12-22 11:27:26 -08001352int commit_tree(const struct strbuf *msg, unsigned char *tree,
Junio C Hamano5231c632011-11-07 16:21:32 -08001353 struct commit_list *parents, unsigned char *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001354 const char *author, const char *sign_commit)
Junio C Hamano5231c632011-11-07 16:21:32 -08001355{
1356 struct commit_extra_header *extra = NULL, **tail = &extra;
1357 int result;
1358
1359 append_merge_tag_headers(parents, &tail);
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001360 result = commit_tree_extended(msg, tree, parents, ret,
1361 author, sign_commit, extra);
Junio C Hamano5231c632011-11-07 16:21:32 -08001362 free_commit_extra_headers(extra);
1363 return result;
1364}
1365
Linus Torvalds08a94a12012-06-28 11:24:14 -07001366static int find_invalid_utf8(const char *buf, int len)
1367{
1368 int offset = 0;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001369 static const unsigned int max_codepoint[] = {
1370 0x7f, 0x7ff, 0xffff, 0x10ffff
1371 };
Linus Torvalds08a94a12012-06-28 11:24:14 -07001372
1373 while (len) {
1374 unsigned char c = *buf++;
1375 int bytes, bad_offset;
brian m. carlson28110d42013-07-04 17:19:43 +00001376 unsigned int codepoint;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001377 unsigned int min_val, max_val;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001378
1379 len--;
1380 offset++;
1381
1382 /* Simple US-ASCII? No worries. */
1383 if (c < 0x80)
1384 continue;
1385
1386 bad_offset = offset-1;
1387
1388 /*
1389 * Count how many more high bits set: that's how
1390 * many more bytes this sequence should have.
1391 */
1392 bytes = 0;
1393 while (c & 0x40) {
1394 c <<= 1;
1395 bytes++;
1396 }
1397
brian m. carlson28110d42013-07-04 17:19:43 +00001398 /*
1399 * Must be between 1 and 3 more bytes. Longer sequences result in
1400 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1401 */
1402 if (bytes < 1 || 3 < bytes)
Linus Torvalds08a94a12012-06-28 11:24:14 -07001403 return bad_offset;
1404
1405 /* Do we *have* that many bytes? */
1406 if (len < bytes)
1407 return bad_offset;
1408
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001409 /*
1410 * Place the encoded bits at the bottom of the value and compute the
1411 * valid range.
1412 */
brian m. carlson28110d42013-07-04 17:19:43 +00001413 codepoint = (c & 0x7f) >> bytes;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001414 min_val = max_codepoint[bytes-1] + 1;
1415 max_val = max_codepoint[bytes];
brian m. carlson28110d42013-07-04 17:19:43 +00001416
Linus Torvalds08a94a12012-06-28 11:24:14 -07001417 offset += bytes;
1418 len -= bytes;
1419
1420 /* And verify that they are good continuation bytes */
1421 do {
brian m. carlson28110d42013-07-04 17:19:43 +00001422 codepoint <<= 6;
1423 codepoint |= *buf & 0x3f;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001424 if ((*buf++ & 0xc0) != 0x80)
1425 return bad_offset;
1426 } while (--bytes);
1427
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001428 /* Reject codepoints that are out of range for the sequence length. */
1429 if (codepoint < min_val || codepoint > max_val)
brian m. carlson28110d42013-07-04 17:19:43 +00001430 return bad_offset;
1431 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1432 if ((codepoint & 0x1ff800) == 0xd800)
1433 return bad_offset;
Peter Krefting81050ac2013-07-09 12:16:33 +01001434 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
Junio C Hamanodc773a62013-08-05 09:52:28 -07001435 if ((codepoint & 0xfffe) == 0xfffe)
Peter Krefting81050ac2013-07-09 12:16:33 +01001436 return bad_offset;
1437 /* So are anything in the range U+FDD0..U+FDEF. */
1438 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
brian m. carlson28110d42013-07-04 17:19:43 +00001439 return bad_offset;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001440 }
1441 return -1;
1442}
1443
1444/*
1445 * This verifies that the buffer is in proper utf8 format.
1446 *
1447 * If it isn't, it assumes any non-utf8 characters are Latin1,
1448 * and does the conversion.
Linus Torvalds08a94a12012-06-28 11:24:14 -07001449 */
1450static int verify_utf8(struct strbuf *buf)
1451{
1452 int ok = 1;
1453 long pos = 0;
1454
1455 for (;;) {
1456 int bad;
1457 unsigned char c;
1458 unsigned char replace[2];
1459
1460 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1461 if (bad < 0)
1462 return ok;
1463 pos += bad;
1464 ok = 0;
1465 c = buf->buf[pos];
1466 strbuf_remove(buf, pos, 1);
1467
1468 /* We know 'c' must be in the range 128-255 */
1469 replace[0] = 0xc0 + (c >> 6);
1470 replace[1] = 0x80 + (c & 0x3f);
1471 strbuf_insert(buf, pos, replace, 2);
1472 pos += 2;
1473 }
1474}
1475
Jeff King40d52ff2010-04-01 20:05:23 -04001476static const char commit_utf8_warn[] =
Linus Torvalds08a94a12012-06-28 11:24:14 -07001477"Warning: commit message did not conform to UTF-8.\n"
Jeff King40d52ff2010-04-01 20:05:23 -04001478"You may want to amend it after fixing the message, or set the config\n"
1479"variable i18n.commitencoding to the encoding your project uses.\n";
1480
Junio C Hamanof35ccd92011-12-22 11:27:26 -08001481int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
Junio C Hamano5231c632011-11-07 16:21:32 -08001482 struct commit_list *parents, unsigned char *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001483 const char *author, const char *sign_commit,
1484 struct commit_extra_header *extra)
Jeff King40d52ff2010-04-01 20:05:23 -04001485{
1486 int result;
1487 int encoding_is_utf8;
1488 struct strbuf buffer;
1489
1490 assert_sha1_type(tree, OBJ_TREE);
1491
Nguyễn Thái Ngọc Duy37576c12011-12-15 20:47:23 +07001492 if (memchr(msg->buf, '\0', msg->len))
1493 return error("a NUL byte in commit log message not allowed.");
1494
Jeff King40d52ff2010-04-01 20:05:23 -04001495 /* Not having i18n.commitencoding is the same as having utf-8 */
1496 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1497
1498 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1499 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1500
1501 /*
1502 * NOTE! This ordering means that the same exact tree merged with a
1503 * different order of parents will be a _different_ changeset even
1504 * if everything else stays the same.
1505 */
1506 while (parents) {
1507 struct commit_list *next = parents->next;
Junio C Hamano5231c632011-11-07 16:21:32 -08001508 struct commit *parent = parents->item;
1509
Jeff King40d52ff2010-04-01 20:05:23 -04001510 strbuf_addf(&buffer, "parent %s\n",
Junio C Hamano5231c632011-11-07 16:21:32 -08001511 sha1_to_hex(parent->object.sha1));
Jeff King40d52ff2010-04-01 20:05:23 -04001512 free(parents);
1513 parents = next;
1514 }
1515
1516 /* Person/date information */
1517 if (!author)
Jeff Kingf9bc5732012-05-24 19:28:40 -04001518 author = git_author_info(IDENT_STRICT);
Jeff King40d52ff2010-04-01 20:05:23 -04001519 strbuf_addf(&buffer, "author %s\n", author);
Jeff Kingf9bc5732012-05-24 19:28:40 -04001520 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
Jeff King40d52ff2010-04-01 20:05:23 -04001521 if (!encoding_is_utf8)
1522 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
Junio C Hamano5231c632011-11-07 16:21:32 -08001523
1524 while (extra) {
1525 add_extra_header(&buffer, extra);
1526 extra = extra->next;
1527 }
Jeff King40d52ff2010-04-01 20:05:23 -04001528 strbuf_addch(&buffer, '\n');
1529
1530 /* And add the comment */
Nguyễn Thái Ngọc Duy13f8b722011-12-15 20:47:22 +07001531 strbuf_addbuf(&buffer, msg);
Jeff King40d52ff2010-04-01 20:05:23 -04001532
1533 /* And check the encoding */
Linus Torvalds08a94a12012-06-28 11:24:14 -07001534 if (encoding_is_utf8 && !verify_utf8(&buffer))
Jeff King40d52ff2010-04-01 20:05:23 -04001535 fprintf(stderr, commit_utf8_warn);
1536
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001537 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1538 return -1;
1539
Jeff King40d52ff2010-04-01 20:05:23 -04001540 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1541 strbuf_release(&buffer);
1542 return result;
1543}
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001544
1545struct commit *get_merge_parent(const char *name)
1546{
1547 struct object *obj;
1548 struct commit *commit;
1549 unsigned char sha1[20];
1550 if (get_sha1(name, sha1))
1551 return NULL;
1552 obj = parse_object(sha1);
1553 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1554 if (commit && !commit->util) {
1555 struct merge_remote_desc *desc;
1556 desc = xmalloc(sizeof(*desc));
1557 desc->obj = obj;
1558 desc->name = strdup(name);
1559 commit->util = desc;
1560 }
1561 return commit;
1562}
René Scharfe89b5f1d2012-04-25 22:35:27 +02001563
1564/*
1565 * Append a commit to the end of the commit_list.
1566 *
1567 * next starts by pointing to the variable that holds the head of an
1568 * empty commit_list, and is updated to point to the "next" field of
1569 * the last item on the list as new commits are appended.
1570 *
1571 * Usage example:
1572 *
1573 * struct commit_list *list;
1574 * struct commit_list **next = &list;
1575 *
1576 * next = commit_list_append(c1, next);
1577 * next = commit_list_append(c2, next);
1578 * assert(commit_list_count(list) == 2);
1579 * return list;
1580 */
1581struct commit_list **commit_list_append(struct commit *commit,
1582 struct commit_list **next)
1583{
1584 struct commit_list *new = xmalloc(sizeof(struct commit_list));
1585 new->item = commit;
1586 *next = new;
1587 new->next = NULL;
1588 return &new->next;
1589}
Nguyễn Thái Ngọc Duyefc7df42012-10-26 22:53:51 +07001590
1591void print_commit_list(struct commit_list *list,
1592 const char *format_cur,
1593 const char *format_last)
1594{
1595 for ( ; list; list = list->next) {
1596 const char *format = list->next ? format_cur : format_last;
1597 printf(format, sha1_to_hex(list->item->object.sha1));
1598 }
1599}