blob: 0ea441d13b1430231f30d9524aed88b9719290b6 [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"
Daniel Barkalow175785e2005-04-18 11:39:48 -070011
Linus Torvalds60ab26d2005-09-15 14:43:17 -070012int save_commit_buffer = 1;
13
Daniel Barkalow175785e2005-04-18 11:39:48 -070014const char *commit_type = "commit";
15
Junio C Hamanof76412e2005-08-21 02:51:10 -070016static struct commit *check_commit(struct object *obj,
17 const unsigned char *sha1,
18 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070019{
Linus Torvalds19746322006-07-11 20:45:31 -070020 if (obj->type != OBJ_COMMIT) {
Junio C Hamanof76412e2005-08-21 02:51:10 -070021 if (!quiet)
22 error("Object %s is a %s, not a commit",
Linus Torvalds885a86a2006-06-14 16:45:13 -070023 sha1_to_hex(sha1), typename(obj->type));
Linus Torvalds961784e2005-05-18 16:14:22 -070024 return NULL;
25 }
26 return (struct commit *) obj;
27}
28
Junio C Hamanof76412e2005-08-21 02:51:10 -070029struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
30 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070031{
Junio C Hamano9534f402005-11-02 15:19:13 -080032 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070033
34 if (!obj)
35 return NULL;
Junio C Hamanof76412e2005-08-21 02:51:10 -070036 return check_commit(obj, sha1, quiet);
37}
38
39struct commit *lookup_commit_reference(const unsigned char *sha1)
40{
41 return lookup_commit_reference_gently(sha1, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070042}
43
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100044struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
45{
46 struct commit *c = lookup_commit_reference(sha1);
47 if (!c)
48 die(_("could not parse %s"), ref_name);
49 if (hashcmp(sha1, c->object.sha1)) {
50 warning(_("%s %s is not a commit!"),
51 ref_name, sha1_to_hex(sha1));
52 }
53 return c;
54}
55
Jason McMullan5d6ccf52005-06-03 11:05:39 -040056struct commit *lookup_commit(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070057{
58 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -070059 if (!obj)
60 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -040061 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070062 obj->type = OBJ_COMMIT;
Junio C Hamanof76412e2005-08-21 02:51:10 -070063 return check_commit(obj, sha1, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070064}
65
Pat Notza6fa5992010-11-02 13:59:07 -060066struct commit *lookup_commit_reference_by_name(const char *name)
67{
68 unsigned char sha1[20];
69 struct commit *commit;
70
Junio C Hamanocd74e472012-07-02 12:04:52 -070071 if (get_sha1_committish(name, sha1))
Pat Notza6fa5992010-11-02 13:59:07 -060072 return NULL;
73 commit = lookup_commit_reference(sha1);
74 if (!commit || parse_commit(commit))
75 return NULL;
76 return commit;
77}
78
Martin Koegler0a617792008-01-19 18:35:23 +010079static unsigned long parse_commit_date(const char *buf, const char *tail)
Daniel Barkalow175785e2005-04-18 11:39:48 -070080{
Martin Koegler0a617792008-01-19 18:35:23 +010081 const char *dateptr;
Daniel Barkalow175785e2005-04-18 11:39:48 -070082
Martin Koegler0a617792008-01-19 18:35:23 +010083 if (buf + 6 >= tail)
84 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070085 if (memcmp(buf, "author", 6))
86 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010087 while (buf < tail && *buf++ != '\n')
Daniel Barkalow175785e2005-04-18 11:39:48 -070088 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010089 if (buf + 9 >= tail)
90 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070091 if (memcmp(buf, "committer", 9))
92 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010093 while (buf < tail && *buf++ != '>')
Daniel Barkalow175785e2005-04-18 11:39:48 -070094 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010095 if (buf >= tail)
96 return 0;
97 dateptr = buf;
98 while (buf < tail && *buf++ != '\n')
99 /* nada */;
100 if (buf >= tail)
101 return 0;
102 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
Eric Wongf2909742009-07-02 21:34:54 -0700103 return strtoul(dateptr, NULL, 10);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700104}
105
Junio C Hamano5040f172006-04-06 23:58:51 -0700106static struct commit_graft **commit_graft;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700107static int commit_graft_alloc, commit_graft_nr;
108
109static int commit_graft_pos(const unsigned char *sha1)
110{
111 int lo, hi;
112 lo = 0;
113 hi = commit_graft_nr;
114 while (lo < hi) {
115 int mi = (lo + hi) / 2;
116 struct commit_graft *graft = commit_graft[mi];
David Rientjesa89fccd2006-08-17 11:54:57 -0700117 int cmp = hashcmp(sha1, graft->sha1);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700118 if (!cmp)
119 return mi;
120 if (cmp < 0)
121 hi = mi;
122 else
123 lo = mi + 1;
124 }
125 return -lo - 1;
126}
127
Junio C Hamano5040f172006-04-06 23:58:51 -0700128int register_commit_graft(struct commit_graft *graft, int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700129{
Junio C Hamano5040f172006-04-06 23:58:51 -0700130 int pos = commit_graft_pos(graft->sha1);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700131
Junio C Hamano5040f172006-04-06 23:58:51 -0700132 if (0 <= pos) {
133 if (ignore_dups)
134 free(graft);
135 else {
136 free(commit_graft[pos]);
137 commit_graft[pos] = graft;
138 }
139 return 1;
140 }
141 pos = -pos - 1;
142 if (commit_graft_alloc <= ++commit_graft_nr) {
143 commit_graft_alloc = alloc_nr(commit_graft_alloc);
144 commit_graft = xrealloc(commit_graft,
145 sizeof(*commit_graft) *
146 commit_graft_alloc);
147 }
148 if (pos < commit_graft_nr)
149 memmove(commit_graft + pos + 1,
150 commit_graft + pos,
151 (commit_graft_nr - pos - 1) *
152 sizeof(*commit_graft));
153 commit_graft[pos] = graft;
154 return 0;
155}
156
157struct commit_graft *read_graft_line(char *buf, int len)
158{
159 /* The format is just "Commit Parent1 Parent2 ...\n" */
160 int i;
161 struct commit_graft *graft = NULL;
162
Junio C Hamanofe0a3cb2009-10-14 11:51:03 -0700163 while (len && isspace(buf[len-1]))
164 buf[--len] = '\0';
Yann Dirson360204c2006-04-17 13:41:49 +0200165 if (buf[0] == '#' || buf[0] == '\0')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700166 return NULL;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100167 if ((len + 1) % 41)
168 goto bad_graft_data;
Junio C Hamano5040f172006-04-06 23:58:51 -0700169 i = (len + 1) / 41 - 1;
170 graft = xmalloc(sizeof(*graft) + 20 * i);
171 graft->nr_parent = i;
172 if (get_sha1_hex(buf, graft->sha1))
173 goto bad_graft_data;
174 for (i = 40; i < len; i += 41) {
175 if (buf[i] != ' ')
176 goto bad_graft_data;
177 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
178 goto bad_graft_data;
179 }
180 return graft;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100181
182bad_graft_data:
183 error("bad graft data: %s", buf);
184 free(graft);
185 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700186}
187
Nanako Shiraishic5ae6432008-10-02 19:14:30 +0900188static int read_graft_file(const char *graft_file)
Junio C Hamano5040f172006-04-06 23:58:51 -0700189{
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700190 FILE *fp = fopen(graft_file, "r");
191 char buf[1024];
Junio C Hamano5040f172006-04-06 23:58:51 -0700192 if (!fp)
193 return -1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700194 while (fgets(buf, sizeof(buf), fp)) {
195 /* The format is just "Commit Parent1 Parent2 ...\n" */
196 int len = strlen(buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700197 struct commit_graft *graft = read_graft_line(buf, len);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700198 if (!graft)
199 continue;
Junio C Hamano5040f172006-04-06 23:58:51 -0700200 if (register_commit_graft(graft, 1))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700201 error("duplicate graft data: %s", buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700202 }
203 fclose(fp);
Junio C Hamano5040f172006-04-06 23:58:51 -0700204 return 0;
205}
206
207static void prepare_commit_graft(void)
208{
209 static int commit_graft_prepared;
210 char *graft_file;
211
212 if (commit_graft_prepared)
213 return;
214 graft_file = get_graft_file();
215 read_graft_file(graft_file);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100216 /* make sure shallows are read */
217 is_repository_shallow();
Junio C Hamano5040f172006-04-06 23:58:51 -0700218 commit_graft_prepared = 1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700219}
220
Martin Koegler45163382008-02-25 22:46:07 +0100221struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700222{
223 int pos;
Junio C Hamano5040f172006-04-06 23:58:51 -0700224 prepare_commit_graft();
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700225 pos = commit_graft_pos(sha1);
226 if (pos < 0)
227 return NULL;
228 return commit_graft[pos];
229}
230
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700231int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100232{
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700233 int i, ret;
234 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
235 ret = fn(commit_graft[i], cb_data);
236 return ret;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100237}
238
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100239int unregister_shallow(const unsigned char *sha1)
240{
241 int pos = commit_graft_pos(sha1);
242 if (pos < 0)
243 return -1;
244 if (pos + 1 < commit_graft_nr)
Jeff King65d41d42010-01-29 05:28:44 -0500245 memmove(commit_graft + pos, commit_graft + pos + 1,
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100246 sizeof(struct commit_graft *)
247 * (commit_graft_nr - pos - 1));
248 commit_graft_nr--;
249 return 0;
250}
251
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700252int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700253{
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700254 const char *tail = buffer;
255 const char *bufptr = buffer;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700256 unsigned char parent[20];
Linus Torvalds6c88be12005-06-20 20:26:03 -0700257 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700258 struct commit_graft *graft;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400259
Daniel Barkalow175785e2005-04-18 11:39:48 -0700260 if (item->object.parsed)
261 return 0;
262 item->object.parsed = 1;
Junio C Hamano3b44f152006-06-28 03:51:00 -0700263 tail += size;
Martin Koegler0a617792008-01-19 18:35:23 +0100264 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700265 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
Martin Koegler0a617792008-01-19 18:35:23 +0100266 if (get_sha1_hex(bufptr + 5, parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800267 return error("bad tree pointer in commit %s",
268 sha1_to_hex(item->object.sha1));
Daniel Barkalow175785e2005-04-18 11:39:48 -0700269 item->tree = lookup_tree(parent);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700270 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700271 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700272
273 graft = lookup_commit_graft(item->object.sha1);
Junio C Hamano3b44f152006-06-28 03:51:00 -0700274 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700275 struct commit *new_parent;
276
Junio C Hamano3b44f152006-06-28 03:51:00 -0700277 if (tail <= bufptr + 48 ||
278 get_sha1_hex(bufptr + 7, parent) ||
279 bufptr[47] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700280 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700281 bufptr += 48;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +0200282 /*
283 * The clone is shallow if nr_parent < 0, and we must
284 * not traverse its real parents even when we unhide them.
285 */
286 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700287 continue;
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700288 new_parent = lookup_commit(parent);
Miklos Vajna39468de2008-06-07 22:38:37 +0200289 if (new_parent)
Linus Torvalds6c88be12005-06-20 20:26:03 -0700290 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700291 }
292 if (graft) {
293 int i;
294 struct commit *new_parent;
295 for (i = 0; i < graft->nr_parent; i++) {
296 new_parent = lookup_commit(graft->parent[i]);
297 if (!new_parent)
298 continue;
299 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700300 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700301 }
Martin Koegler0a617792008-01-19 18:35:23 +0100302 item->date = parse_commit_date(bufptr, tail);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800303
Daniel Barkalow175785e2005-04-18 11:39:48 -0700304 return 0;
305}
306
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400307int parse_commit(struct commit *item)
308{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500309 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400310 void *buffer;
311 unsigned long size;
312 int ret;
313
Martin Koegler9786f682008-02-18 21:48:02 +0100314 if (!item)
315 return -1;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400316 if (item->object.parsed)
317 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500318 buffer = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400319 if (!buffer)
320 return error("Could not read %s",
321 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500322 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400323 free(buffer);
324 return error("Object %s not a commit",
325 sha1_to_hex(item->object.sha1));
326 }
327 ret = parse_commit_buffer(item, buffer, size);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700328 if (save_commit_buffer && !ret) {
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700329 item->buffer = buffer;
330 return 0;
331 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400332 free(buffer);
333 return ret;
334}
335
Christian Couder11af2aa2010-07-22 15:18:30 +0200336int find_commit_subject(const char *commit_buffer, const char **subject)
337{
338 const char *eol;
339 const char *p = commit_buffer;
340
341 while (*p && (*p != '\n' || p[1] != '\n'))
342 p++;
343 if (*p) {
344 p += 2;
345 for (eol = p; *eol && *eol != '\n'; eol++)
346 ; /* do nothing */
347 } else
348 eol = p;
349
350 *subject = p;
351
352 return eol - p;
353}
354
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700355struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700356{
Christopher Li812666c2005-04-26 12:00:58 -0700357 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700358 new_list->item = item;
359 new_list->next = *list_p;
360 *list_p = new_list;
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700361 return new_list;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700362}
363
Miklos Vajna65319472008-06-27 18:21:55 +0200364unsigned commit_list_count(const struct commit_list *l)
365{
366 unsigned c = 0;
367 for (; l; l = l->next )
368 c++;
369 return c;
370}
371
Daniel Barkalow175785e2005-04-18 11:39:48 -0700372void free_commit_list(struct commit_list *list)
373{
374 while (list) {
375 struct commit_list *temp = list;
376 list = temp->next;
377 free(temp);
378 }
379}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700380
Thiago Farina47e44ed2010-11-26 23:58:14 -0200381struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700382{
383 struct commit_list **pp = list;
384 struct commit_list *p;
385 while ((p = *pp) != NULL) {
386 if (p->item->date < item->date) {
387 break;
388 }
389 pp = &p->next;
390 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700391 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700392}
393
René Scharfe46905892012-04-01 00:10:39 +0200394static int commit_list_compare_by_date(const void *a, const void *b)
395{
396 unsigned long a_date = ((const struct commit_list *)a)->item->date;
397 unsigned long b_date = ((const struct commit_list *)b)->item->date;
398 if (a_date < b_date)
399 return 1;
400 if (a_date > b_date)
401 return -1;
402 return 0;
403}
404
405static void *commit_list_get_next(const void *a)
406{
407 return ((const struct commit_list *)a)->next;
408}
409
410static void commit_list_set_next(void *a, void *next)
411{
412 ((struct commit_list *)a)->next = next;
413}
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700414
Thiago Farina47e44ed2010-11-26 23:58:14 -0200415void commit_list_sort_by_date(struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700416{
Junio C Hamano7365c952012-04-17 11:07:01 -0700417 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
418 commit_list_compare_by_date);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700419}
420
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700421struct commit *pop_most_recent_commit(struct commit_list **list,
422 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700423{
424 struct commit *ret = (*list)->item;
425 struct commit_list *parents = ret->parents;
426 struct commit_list *old = *list;
427
428 *list = (*list)->next;
429 free(old);
430
431 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700432 struct commit *commit = parents->item;
Martin Koeglerdec38c82008-02-18 21:48:03 +0100433 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700434 commit->object.flags |= mark;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200435 commit_list_insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700436 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700437 parents = parents->next;
438 }
439 return ret;
440}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700441
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700442static void clear_commit_marks_1(struct commit_list **plist,
443 struct commit *commit, unsigned int mark)
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800444{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100445 while (commit) {
446 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800447
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100448 if (!(mark & commit->object.flags))
449 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700450
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100451 commit->object.flags &= ~mark;
452
453 parents = commit->parents;
454 if (!parents)
455 return;
456
457 while ((parents = parents->next))
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700458 commit_list_insert(parents->item, plist);
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100459
460 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800461 }
462}
463
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700464void clear_commit_marks(struct commit *commit, unsigned int mark)
465{
466 struct commit_list *list = NULL;
467 commit_list_insert(commit, &list);
468 while (list)
469 clear_commit_marks_1(&list, pop_commit(&list), mark);
470}
471
René Scharfe86a0a402011-10-01 18:16:08 +0200472void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
473{
474 struct object *object;
475 struct commit *commit;
476 unsigned int i;
477
478 for (i = 0; i < a->nr; i++) {
479 object = a->objects[i].item;
480 commit = lookup_commit_reference_gently(object->sha1, 1);
481 if (commit)
482 clear_commit_marks(commit, mark);
483 }
484}
485
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000486struct commit *pop_commit(struct commit_list **stack)
487{
488 struct commit_list *top = *stack;
489 struct commit *item = top ? top->item : NULL;
490
491 if (top) {
492 *stack = top->next;
493 free(top);
494 }
495 return item;
496}
497
Jon Seymourab580ac2005-07-07 02:39:34 +1000498/*
499 * Performs an in-place topological sort on the list supplied.
500 */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800501void sort_in_topological_order(struct commit_list ** list, int lifo)
Jon Seymourab580ac2005-07-07 02:39:34 +1000502{
Linus Torvalds23c17d42007-11-02 13:32:58 -0700503 struct commit_list *next, *orig = *list;
504 struct commit_list *work, **insert;
505 struct commit_list **pptr;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100506
Linus Torvalds23c17d42007-11-02 13:32:58 -0700507 if (!orig)
Eric Wong7d6fb372005-12-24 04:12:43 -0800508 return;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700509 *list = NULL;
510
511 /* Mark them and clear the indegree */
512 for (next = orig; next; next = next->next) {
513 struct commit *commit = next->item;
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100514 commit->indegree = 1;
Jon Seymourab580ac2005-07-07 02:39:34 +1000515 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700516
Jon Seymourab580ac2005-07-07 02:39:34 +1000517 /* update the indegree */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700518 for (next = orig; next; next = next->next) {
Jon Seymourab580ac2005-07-07 02:39:34 +1000519 struct commit_list * parents = next->item->parents;
520 while (parents) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700521 struct commit *parent = parents->item;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100522
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100523 if (parent->indegree)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700524 parent->indegree++;
525 parents = parents->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000526 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000527 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700528
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700529 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200530 * find the tips
531 *
532 * tips are nodes not reachable from any other node in the list
533 *
534 * the tips serve as a starting set for the work queue.
535 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700536 work = NULL;
Linus Torvalds2ed02882005-11-14 10:01:26 -0800537 insert = &work;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700538 for (next = orig; next; next = next->next) {
539 struct commit *commit = next->item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000540
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100541 if (commit->indegree == 1)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700542 insert = &commit_list_insert(commit, insert)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000543 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800544
Jon Seymourab580ac2005-07-07 02:39:34 +1000545 /* process the list in topological order */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800546 if (!lifo)
Thiago Farina47e44ed2010-11-26 23:58:14 -0200547 commit_list_sort_by_date(&work);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700548
549 pptr = list;
550 *list = NULL;
Jon Seymourab580ac2005-07-07 02:39:34 +1000551 while (work) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700552 struct commit *commit;
553 struct commit_list *parents, *work_item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000554
Linus Torvalds23c17d42007-11-02 13:32:58 -0700555 work_item = work;
556 work = work_item->next;
557 work_item->next = NULL;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100558
Linus Torvalds23c17d42007-11-02 13:32:58 -0700559 commit = work_item->item;
560 for (parents = commit->parents; parents ; parents = parents->next) {
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -0700561 struct commit *parent = parents->item;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700562
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100563 if (!parent->indegree)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700564 continue;
565
566 /*
567 * parents are only enqueued for emission
568 * when all their children have been emitted thereby
569 * guaranteeing topological order.
570 */
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100571 if (--parent->indegree == 1) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700572 if (!lifo)
Thiago Farina47e44ed2010-11-26 23:58:14 -0200573 commit_list_insert_by_date(parent, &work);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700574 else
575 commit_list_insert(parent, &work);
Jon Seymourab580ac2005-07-07 02:39:34 +1000576 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000577 }
578 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200579 * work_item is a commit all of whose children
580 * have already been emitted. we can emit it now.
581 */
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100582 commit->indegree = 0;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700583 *pptr = work_item;
584 pptr = &work_item->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000585 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000586}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200587
Junio C Hamano12952282007-01-08 23:10:49 -0800588/* merge-base stuff */
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200589
Junio C Hamano577ed5c2006-10-22 17:32:47 -0700590/* bits #0..15 in revision.h */
591#define PARENT1 (1u<<16)
592#define PARENT2 (1u<<17)
593#define STALE (1u<<18)
594#define RESULT (1u<<19)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200595
Junio C Hamano12952282007-01-08 23:10:49 -0800596static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
597
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200598static struct commit *interesting(struct commit_list *list)
599{
600 while (list) {
601 struct commit *commit = list->item;
602 list = list->next;
Junio C Hamano542ccef2006-07-02 11:34:17 -0700603 if (commit->object.flags & STALE)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200604 continue;
605 return commit;
606 }
607 return NULL;
608}
609
Junio C Hamanoda1f5152012-08-30 14:39:03 -0700610static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200611{
612 struct commit_list *list = NULL;
613 struct commit_list *result = NULL;
Junio C Hamano6a938642008-06-27 18:22:02 +0200614 int i;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200615
Junio C Hamanof3249432006-07-04 18:46:42 -0700616 one->object.flags |= PARENT1;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200617 commit_list_insert_by_date(one, &list);
Junio C Hamano6a938642008-06-27 18:22:02 +0200618 for (i = 0; i < n; i++) {
619 twos[i]->object.flags |= PARENT2;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200620 commit_list_insert_by_date(twos[i], &list);
Junio C Hamano6a938642008-06-27 18:22:02 +0200621 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200622
623 while (interesting(list)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700624 struct commit *commit;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200625 struct commit_list *parents;
Brandon Caseyf203d692009-08-27 11:16:34 -0500626 struct commit_list *next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700627 int flags;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200628
Junio C Hamanof3249432006-07-04 18:46:42 -0700629 commit = list->item;
Brandon Caseyf203d692009-08-27 11:16:34 -0500630 next = list->next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700631 free(list);
Brandon Caseyf203d692009-08-27 11:16:34 -0500632 list = next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700633
634 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200635 if (flags == (PARENT1 | PARENT2)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700636 if (!(commit->object.flags & RESULT)) {
637 commit->object.flags |= RESULT;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200638 commit_list_insert_by_date(commit, &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700639 }
Junio C Hamano542ccef2006-07-02 11:34:17 -0700640 /* Mark parents of a found merge stale */
641 flags |= STALE;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200642 }
643 parents = commit->parents;
644 while (parents) {
645 struct commit *p = parents->item;
646 parents = parents->next;
647 if ((p->object.flags & flags) == flags)
648 continue;
Martin Koegler172947e2008-02-18 21:47:57 +0100649 if (parse_commit(p))
650 return NULL;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200651 p->object.flags |= flags;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200652 commit_list_insert_by_date(p, &list);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200653 }
654 }
655
Junio C Hamano12952282007-01-08 23:10:49 -0800656 free_commit_list(list);
Junio C Hamanoda1f5152012-08-30 14:39:03 -0700657 return result;
658}
659
660static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
661{
662 struct commit_list *list = NULL;
663 struct commit_list *result = NULL;
664 int i;
665
666 for (i = 0; i < n; i++) {
667 if (one == twos[i])
668 /*
669 * We do not mark this even with RESULT so we do not
670 * have to clean it up.
671 */
672 return commit_list_insert(one, &result);
673 }
674
675 if (parse_commit(one))
676 return NULL;
677 for (i = 0; i < n; i++) {
678 if (parse_commit(twos[i]))
679 return NULL;
680 }
681
682 list = paint_down_to_common(one, n, twos);
683
Junio C Hamanof3249432006-07-04 18:46:42 -0700684 while (list) {
Brandon Caseyf203d692009-08-27 11:16:34 -0500685 struct commit_list *next = list->next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700686 if (!(list->item->object.flags & STALE))
Thiago Farina47e44ed2010-11-26 23:58:14 -0200687 commit_list_insert_by_date(list->item, &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700688 free(list);
Brandon Caseyf203d692009-08-27 11:16:34 -0500689 list = next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700690 }
691 return result;
692}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200693
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200694struct commit_list *get_octopus_merge_bases(struct commit_list *in)
695{
696 struct commit_list *i, *j, *k, *ret = NULL;
697 struct commit_list **pptr = &ret;
698
699 for (i = in; i; i = i->next) {
700 if (!ret)
701 pptr = &commit_list_insert(i->item, pptr)->next;
702 else {
703 struct commit_list *new = NULL, *end = NULL;
704
705 for (j = ret; j; j = j->next) {
706 struct commit_list *bases;
707 bases = get_merge_bases(i->item, j->item, 1);
708 if (!new)
709 new = bases;
710 else
711 end->next = bases;
712 for (k = bases; k; k = k->next)
713 end = k;
714 }
715 ret = new;
716 }
717 }
718 return ret;
719}
720
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700721static int remove_redundant(struct commit **array, int cnt)
722{
723 /*
724 * Some commit in the array may be an ancestor of
725 * another commit. Move such commit to the end of
726 * the array, and return the number of commits that
727 * are independent from each other.
728 */
729 struct commit **work;
730 unsigned char *redundant;
731 int *filled_index;
732 int i, j, filled;
733
734 work = xcalloc(cnt, sizeof(*work));
735 redundant = xcalloc(cnt, 1);
736 filled_index = xmalloc(sizeof(*filled_index) * (cnt - 1));
737
738 for (i = 0; i < cnt; i++) {
739 struct commit_list *common;
740
741 if (redundant[i])
742 continue;
743 for (j = filled = 0; j < cnt; j++) {
744 if (i == j || redundant[j])
745 continue;
746 filled_index[filled] = j;
747 work[filled++] = array[j];
748 }
749 common = paint_down_to_common(array[i], filled, work);
750 if (array[i]->object.flags & PARENT2)
751 redundant[i] = 1;
752 for (j = 0; j < filled; j++)
753 if (work[j]->object.flags & PARENT1)
754 redundant[filled_index[j]] = 1;
755 clear_commit_marks(array[i], all_flags);
756 for (j = 0; j < filled; j++)
757 clear_commit_marks(work[j], all_flags);
758 free_commit_list(common);
759 }
760
761 /* Now collect the result */
762 memcpy(work, array, sizeof(*array) * cnt);
763 for (i = filled = 0; i < cnt; i++)
764 if (!redundant[i])
765 array[filled++] = work[i];
766 for (j = filled, i = 0; i < cnt; i++)
767 if (redundant[i])
768 array[j++] = work[i];
769 free(work);
770 free(redundant);
771 free(filled_index);
772 return filled;
773}
774
Junio C Hamano6a938642008-06-27 18:22:02 +0200775struct commit_list *get_merge_bases_many(struct commit *one,
776 int n,
777 struct commit **twos,
778 int cleanup)
Junio C Hamanof3249432006-07-04 18:46:42 -0700779{
Junio C Hamanof3249432006-07-04 18:46:42 -0700780 struct commit_list *list;
781 struct commit **rslt;
782 struct commit_list *result;
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700783 int cnt, i;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200784
Junio C Hamano6a938642008-06-27 18:22:02 +0200785 result = merge_bases_many(one, n, twos);
786 for (i = 0; i < n; i++) {
787 if (one == twos[i])
788 return result;
789 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700790 if (!result || !result->next) {
791 if (cleanup) {
792 clear_commit_marks(one, all_flags);
Junio C Hamano6a938642008-06-27 18:22:02 +0200793 for (i = 0; i < n; i++)
794 clear_commit_marks(twos[i], all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200795 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700796 return result;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200797 }
798
Junio C Hamanof3249432006-07-04 18:46:42 -0700799 /* There are more than one */
800 cnt = 0;
801 list = result;
802 while (list) {
803 list = list->next;
804 cnt++;
805 }
806 rslt = xcalloc(cnt, sizeof(*rslt));
807 for (list = result, i = 0; list; list = list->next)
808 rslt[i++] = list->item;
809 free_commit_list(result);
810
811 clear_commit_marks(one, all_flags);
Junio C Hamano6a938642008-06-27 18:22:02 +0200812 for (i = 0; i < n; i++)
813 clear_commit_marks(twos[i], all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200814
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700815 cnt = remove_redundant(rslt, cnt);
Junio C Hamanof3249432006-07-04 18:46:42 -0700816 result = NULL;
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700817 for (i = 0; i < cnt; i++)
818 commit_list_insert_by_date(rslt[i], &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700819 free(rslt);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200820 return result;
821}
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800822
Junio C Hamano6a938642008-06-27 18:22:02 +0200823struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
824 int cleanup)
825{
826 return get_merge_bases_many(one, 1, &two, cleanup);
827}
828
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700829/*
830 * Is "commit" a decendant of one of the elements on the "with_commit" list?
831 */
Jake Goulding7fcdb362009-01-26 09:13:24 -0500832int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
833{
834 if (!with_commit)
835 return 1;
836 while (with_commit) {
837 struct commit *other;
838
839 other = with_commit->item;
840 with_commit = with_commit->next;
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700841 if (in_merge_bases(other, commit))
Jake Goulding7fcdb362009-01-26 09:13:24 -0500842 return 1;
843 }
844 return 0;
845}
846
Junio C Hamanoa20efee2012-08-27 14:46:01 -0700847/*
848 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
849 */
850int in_merge_bases(struct commit *commit, struct commit *reference)
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800851{
Junio C Hamano6440fdb2012-08-30 15:04:13 -0700852 struct commit_list *bases;
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800853 int ret = 0;
854
Junio C Hamano6440fdb2012-08-30 15:04:13 -0700855 if (parse_commit(commit) || parse_commit(reference))
856 return ret;
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800857
Junio C Hamano6440fdb2012-08-30 15:04:13 -0700858 bases = paint_down_to_common(commit, 1, &reference);
859 if (commit->object.flags & PARENT2)
860 ret = 1;
Thomas Rastb0f9e9e2012-08-23 16:20:41 +0200861 clear_commit_marks(commit, all_flags);
862 clear_commit_marks(reference, all_flags);
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800863 free_commit_list(bases);
864 return ret;
865}
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200866
867struct commit_list *reduce_heads(struct commit_list *heads)
868{
869 struct commit_list *p;
870 struct commit_list *result = NULL, **tail = &result;
Junio C Hamanof37d3c72012-08-30 23:20:40 -0700871 struct commit **array;
872 int num_head, i;
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200873
874 if (!heads)
875 return NULL;
876
Junio C Hamanof37d3c72012-08-30 23:20:40 -0700877 /* Uniquify */
878 for (p = heads; p; p = p->next)
879 p->item->object.flags &= ~STALE;
880 for (p = heads, num_head = 0; p; p = p->next) {
881 if (p->item->object.flags & STALE)
Junio C Hamano711f6b22008-07-14 00:09:41 -0700882 continue;
Junio C Hamanof37d3c72012-08-30 23:20:40 -0700883 p->item->object.flags |= STALE;
884 num_head++;
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200885 }
Junio C Hamanof37d3c72012-08-30 23:20:40 -0700886 array = xcalloc(sizeof(*array), num_head);
887 for (p = heads, i = 0; p; p = p->next) {
888 if (p->item->object.flags & STALE) {
889 array[i++] = p->item;
890 p->item->object.flags &= ~STALE;
891 }
892 }
893 num_head = remove_redundant(array, num_head);
894 for (i = 0; i < num_head; i++)
895 tail = &commit_list_insert(array[i], tail)->next;
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200896 return result;
897}
Jeff King40d52ff2010-04-01 20:05:23 -0400898
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700899static const char gpg_sig_header[] = "gpgsig";
900static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
901
902static int do_sign_commit(struct strbuf *buf, const char *keyid)
903{
904 struct strbuf sig = STRBUF_INIT;
905 int inspos, copypos;
906
907 /* find the end of the header */
908 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
909
910 if (!keyid || !*keyid)
911 keyid = get_signing_key();
912 if (sign_buffer(buf, &sig, keyid)) {
913 strbuf_release(&sig);
914 return -1;
915 }
916
917 for (copypos = 0; sig.buf[copypos]; ) {
918 const char *bol = sig.buf + copypos;
919 const char *eol = strchrnul(bol, '\n');
920 int len = (eol - bol) + !!*eol;
921
922 if (!copypos) {
923 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
924 inspos += gpg_sig_header_len;
925 }
926 strbuf_insert(buf, inspos++, " ", 1);
927 strbuf_insert(buf, inspos, bol, len);
928 inspos += len;
929 copypos += len;
930 }
931 strbuf_release(&sig);
932 return 0;
933}
934
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700935int parse_signed_commit(const unsigned char *sha1,
936 struct strbuf *payload, struct strbuf *signature)
937{
938 unsigned long size;
939 enum object_type type;
940 char *buffer = read_sha1_file(sha1, &type, &size);
941 int in_signature, saw_signature = -1;
942 char *line, *tail;
943
944 if (!buffer || type != OBJ_COMMIT)
945 goto cleanup;
946
947 line = buffer;
948 tail = buffer + size;
949 in_signature = 0;
950 saw_signature = 0;
951 while (line < tail) {
952 const char *sig = NULL;
953 char *next = memchr(line, '\n', tail - line);
954
955 next = next ? next + 1 : tail;
956 if (in_signature && line[0] == ' ')
957 sig = line + 1;
958 else if (!prefixcmp(line, gpg_sig_header) &&
959 line[gpg_sig_header_len] == ' ')
960 sig = line + gpg_sig_header_len + 1;
961 if (sig) {
962 strbuf_add(signature, sig, next - sig);
963 saw_signature = 1;
964 in_signature = 1;
965 } else {
966 if (*line == '\n')
967 /* dump the whole remainder of the buffer */
968 next = tail;
969 strbuf_add(payload, line, next - line);
970 in_signature = 0;
971 }
972 line = next;
973 }
974 cleanup:
975 free(buffer);
976 return saw_signature;
977}
978
Junio C Hamano5231c632011-11-07 16:21:32 -0800979static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
980{
981 struct merge_remote_desc *desc;
982 struct commit_extra_header *mergetag;
983 char *buf;
984 unsigned long size, len;
985 enum object_type type;
986
987 desc = merge_remote_util(parent);
988 if (!desc || !desc->obj)
989 return;
990 buf = read_sha1_file(desc->obj->sha1, &type, &size);
991 if (!buf || type != OBJ_TAG)
992 goto free_return;
993 len = parse_signature(buf, size);
994 if (size == len)
995 goto free_return;
996 /*
997 * We could verify this signature and either omit the tag when
998 * it does not validate, but the integrator may not have the
999 * public key of the signer of the tag he is merging, while a
1000 * later auditor may have it while auditing, so let's not run
1001 * verify-signed-buffer here for now...
1002 *
1003 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1004 * warn("warning: signed tag unverified.");
1005 */
1006 mergetag = xcalloc(1, sizeof(*mergetag));
1007 mergetag->key = xstrdup("mergetag");
1008 mergetag->value = buf;
1009 mergetag->len = size;
1010
1011 **tail = mergetag;
1012 *tail = &mergetag->next;
1013 return;
1014
1015free_return:
1016 free(buf);
1017}
1018
1019void append_merge_tag_headers(struct commit_list *parents,
1020 struct commit_extra_header ***tail)
1021{
1022 while (parents) {
1023 struct commit *parent = parents->item;
1024 handle_signed_tag(parent, tail);
1025 parents = parents->next;
1026 }
1027}
1028
1029static void add_extra_header(struct strbuf *buffer,
1030 struct commit_extra_header *extra)
1031{
1032 strbuf_addstr(buffer, extra->key);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001033 if (extra->len)
1034 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1035 else
1036 strbuf_addch(buffer, '\n');
1037}
1038
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001039struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1040 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001041{
1042 struct commit_extra_header *extra = NULL;
1043 unsigned long size;
1044 enum object_type type;
1045 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
1046 if (buffer && type == OBJ_COMMIT)
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001047 extra = read_commit_extra_header_lines(buffer, size, exclude);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001048 free(buffer);
1049 return extra;
1050}
1051
1052static inline int standard_header_field(const char *field, size_t len)
1053{
1054 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1055 (len == 6 && !memcmp(field, "parent ", 7)) ||
1056 (len == 6 && !memcmp(field, "author ", 7)) ||
1057 (len == 9 && !memcmp(field, "committer ", 10)) ||
1058 (len == 8 && !memcmp(field, "encoding ", 9)));
1059}
1060
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001061static int excluded_header_field(const char *field, size_t len, const char **exclude)
1062{
1063 if (!exclude)
1064 return 0;
1065
1066 while (*exclude) {
1067 size_t xlen = strlen(*exclude);
1068 if (len == xlen &&
1069 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1070 return 1;
1071 exclude++;
1072 }
1073 return 0;
1074}
1075
1076struct commit_extra_header *read_commit_extra_header_lines(const char *buffer, size_t size,
1077 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001078{
1079 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1080 const char *line, *next, *eof, *eob;
1081 struct strbuf buf = STRBUF_INIT;
1082
1083 for (line = buffer, eob = line + size;
1084 line < eob && *line != '\n';
1085 line = next) {
1086 next = memchr(line, '\n', eob - line);
1087 next = next ? next + 1 : eob;
1088 if (*line == ' ') {
1089 /* continuation */
1090 if (it)
1091 strbuf_add(&buf, line + 1, next - (line + 1));
1092 continue;
1093 }
1094 if (it)
1095 it->value = strbuf_detach(&buf, &it->len);
1096 strbuf_reset(&buf);
1097 it = NULL;
1098
1099 eof = strchr(line, ' ');
1100 if (next <= eof)
1101 eof = next;
1102
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001103 if (standard_header_field(line, eof - line) ||
1104 excluded_header_field(line, eof - line, exclude))
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001105 continue;
1106
1107 it = xcalloc(1, sizeof(*it));
1108 it->key = xmemdupz(line, eof-line);
1109 *tail = it;
1110 tail = &it->next;
1111 if (eof + 1 < next)
1112 strbuf_add(&buf, eof + 1, next - (eof + 1));
1113 }
1114 if (it)
1115 it->value = strbuf_detach(&buf, &it->len);
1116 return extra;
Junio C Hamano5231c632011-11-07 16:21:32 -08001117}
1118
1119void free_commit_extra_headers(struct commit_extra_header *extra)
1120{
1121 while (extra) {
1122 struct commit_extra_header *next = extra->next;
1123 free(extra->key);
1124 free(extra->value);
1125 free(extra);
1126 extra = next;
1127 }
1128}
1129
Junio C Hamanof35ccd92011-12-22 11:27:26 -08001130int commit_tree(const struct strbuf *msg, unsigned char *tree,
Junio C Hamano5231c632011-11-07 16:21:32 -08001131 struct commit_list *parents, unsigned char *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001132 const char *author, const char *sign_commit)
Junio C Hamano5231c632011-11-07 16:21:32 -08001133{
1134 struct commit_extra_header *extra = NULL, **tail = &extra;
1135 int result;
1136
1137 append_merge_tag_headers(parents, &tail);
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001138 result = commit_tree_extended(msg, tree, parents, ret,
1139 author, sign_commit, extra);
Junio C Hamano5231c632011-11-07 16:21:32 -08001140 free_commit_extra_headers(extra);
1141 return result;
1142}
1143
Linus Torvalds08a94a12012-06-28 11:24:14 -07001144static int find_invalid_utf8(const char *buf, int len)
1145{
1146 int offset = 0;
1147
1148 while (len) {
1149 unsigned char c = *buf++;
1150 int bytes, bad_offset;
1151
1152 len--;
1153 offset++;
1154
1155 /* Simple US-ASCII? No worries. */
1156 if (c < 0x80)
1157 continue;
1158
1159 bad_offset = offset-1;
1160
1161 /*
1162 * Count how many more high bits set: that's how
1163 * many more bytes this sequence should have.
1164 */
1165 bytes = 0;
1166 while (c & 0x40) {
1167 c <<= 1;
1168 bytes++;
1169 }
1170
1171 /* Must be between 1 and 5 more bytes */
1172 if (bytes < 1 || bytes > 5)
1173 return bad_offset;
1174
1175 /* Do we *have* that many bytes? */
1176 if (len < bytes)
1177 return bad_offset;
1178
1179 offset += bytes;
1180 len -= bytes;
1181
1182 /* And verify that they are good continuation bytes */
1183 do {
1184 if ((*buf++ & 0xc0) != 0x80)
1185 return bad_offset;
1186 } while (--bytes);
1187
1188 /* We could/should check the value and length here too */
1189 }
1190 return -1;
1191}
1192
1193/*
1194 * This verifies that the buffer is in proper utf8 format.
1195 *
1196 * If it isn't, it assumes any non-utf8 characters are Latin1,
1197 * and does the conversion.
1198 *
1199 * Fixme: we should probably also disallow overlong forms and
1200 * invalid characters. But we don't do that currently.
1201 */
1202static int verify_utf8(struct strbuf *buf)
1203{
1204 int ok = 1;
1205 long pos = 0;
1206
1207 for (;;) {
1208 int bad;
1209 unsigned char c;
1210 unsigned char replace[2];
1211
1212 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1213 if (bad < 0)
1214 return ok;
1215 pos += bad;
1216 ok = 0;
1217 c = buf->buf[pos];
1218 strbuf_remove(buf, pos, 1);
1219
1220 /* We know 'c' must be in the range 128-255 */
1221 replace[0] = 0xc0 + (c >> 6);
1222 replace[1] = 0x80 + (c & 0x3f);
1223 strbuf_insert(buf, pos, replace, 2);
1224 pos += 2;
1225 }
1226}
1227
Jeff King40d52ff2010-04-01 20:05:23 -04001228static const char commit_utf8_warn[] =
Linus Torvalds08a94a12012-06-28 11:24:14 -07001229"Warning: commit message did not conform to UTF-8.\n"
Jeff King40d52ff2010-04-01 20:05:23 -04001230"You may want to amend it after fixing the message, or set the config\n"
1231"variable i18n.commitencoding to the encoding your project uses.\n";
1232
Junio C Hamanof35ccd92011-12-22 11:27:26 -08001233int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
Junio C Hamano5231c632011-11-07 16:21:32 -08001234 struct commit_list *parents, unsigned char *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001235 const char *author, const char *sign_commit,
1236 struct commit_extra_header *extra)
Jeff King40d52ff2010-04-01 20:05:23 -04001237{
1238 int result;
1239 int encoding_is_utf8;
1240 struct strbuf buffer;
1241
1242 assert_sha1_type(tree, OBJ_TREE);
1243
Nguyễn Thái Ngọc Duy37576c12011-12-15 20:47:23 +07001244 if (memchr(msg->buf, '\0', msg->len))
1245 return error("a NUL byte in commit log message not allowed.");
1246
Jeff King40d52ff2010-04-01 20:05:23 -04001247 /* Not having i18n.commitencoding is the same as having utf-8 */
1248 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1249
1250 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1251 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1252
1253 /*
1254 * NOTE! This ordering means that the same exact tree merged with a
1255 * different order of parents will be a _different_ changeset even
1256 * if everything else stays the same.
1257 */
1258 while (parents) {
1259 struct commit_list *next = parents->next;
Junio C Hamano5231c632011-11-07 16:21:32 -08001260 struct commit *parent = parents->item;
1261
Jeff King40d52ff2010-04-01 20:05:23 -04001262 strbuf_addf(&buffer, "parent %s\n",
Junio C Hamano5231c632011-11-07 16:21:32 -08001263 sha1_to_hex(parent->object.sha1));
Jeff King40d52ff2010-04-01 20:05:23 -04001264 free(parents);
1265 parents = next;
1266 }
1267
1268 /* Person/date information */
1269 if (!author)
Jeff Kingf9bc5732012-05-24 19:28:40 -04001270 author = git_author_info(IDENT_STRICT);
Jeff King40d52ff2010-04-01 20:05:23 -04001271 strbuf_addf(&buffer, "author %s\n", author);
Jeff Kingf9bc5732012-05-24 19:28:40 -04001272 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
Jeff King40d52ff2010-04-01 20:05:23 -04001273 if (!encoding_is_utf8)
1274 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
Junio C Hamano5231c632011-11-07 16:21:32 -08001275
1276 while (extra) {
1277 add_extra_header(&buffer, extra);
1278 extra = extra->next;
1279 }
Jeff King40d52ff2010-04-01 20:05:23 -04001280 strbuf_addch(&buffer, '\n');
1281
1282 /* And add the comment */
Nguyễn Thái Ngọc Duy13f8b722011-12-15 20:47:22 +07001283 strbuf_addbuf(&buffer, msg);
Jeff King40d52ff2010-04-01 20:05:23 -04001284
1285 /* And check the encoding */
Linus Torvalds08a94a12012-06-28 11:24:14 -07001286 if (encoding_is_utf8 && !verify_utf8(&buffer))
Jeff King40d52ff2010-04-01 20:05:23 -04001287 fprintf(stderr, commit_utf8_warn);
1288
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001289 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1290 return -1;
1291
Jeff King40d52ff2010-04-01 20:05:23 -04001292 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1293 strbuf_release(&buffer);
1294 return result;
1295}
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001296
1297struct commit *get_merge_parent(const char *name)
1298{
1299 struct object *obj;
1300 struct commit *commit;
1301 unsigned char sha1[20];
1302 if (get_sha1(name, sha1))
1303 return NULL;
1304 obj = parse_object(sha1);
1305 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1306 if (commit && !commit->util) {
1307 struct merge_remote_desc *desc;
1308 desc = xmalloc(sizeof(*desc));
1309 desc->obj = obj;
1310 desc->name = strdup(name);
1311 commit->util = desc;
1312 }
1313 return commit;
1314}
René Scharfe89b5f1d2012-04-25 22:35:27 +02001315
1316/*
1317 * Append a commit to the end of the commit_list.
1318 *
1319 * next starts by pointing to the variable that holds the head of an
1320 * empty commit_list, and is updated to point to the "next" field of
1321 * the last item on the list as new commits are appended.
1322 *
1323 * Usage example:
1324 *
1325 * struct commit_list *list;
1326 * struct commit_list **next = &list;
1327 *
1328 * next = commit_list_append(c1, next);
1329 * next = commit_list_append(c2, next);
1330 * assert(commit_list_count(list) == 2);
1331 * return list;
1332 */
1333struct commit_list **commit_list_append(struct commit *commit,
1334 struct commit_list **next)
1335{
1336 struct commit_list *new = xmalloc(sizeof(struct commit_list));
1337 new->item = commit;
1338 *next = new;
1339 new->next = NULL;
1340 return &new->next;
1341}