blob: 35af4988f0ff83c6a3379ea9f6de4e4e1568c39f [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"
Daniel Barkalow175785e2005-04-18 11:39:48 -070010
Linus Torvalds60ab26d2005-09-15 14:43:17 -070011int save_commit_buffer = 1;
12
Daniel Barkalow175785e2005-04-18 11:39:48 -070013const char *commit_type = "commit";
14
Junio C Hamanof76412e2005-08-21 02:51:10 -070015static struct commit *check_commit(struct object *obj,
16 const unsigned char *sha1,
17 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070018{
Linus Torvalds19746322006-07-11 20:45:31 -070019 if (obj->type != OBJ_COMMIT) {
Junio C Hamanof76412e2005-08-21 02:51:10 -070020 if (!quiet)
21 error("Object %s is a %s, not a commit",
Linus Torvalds885a86a2006-06-14 16:45:13 -070022 sha1_to_hex(sha1), typename(obj->type));
Linus Torvalds961784e2005-05-18 16:14:22 -070023 return NULL;
24 }
25 return (struct commit *) obj;
26}
27
Junio C Hamanof76412e2005-08-21 02:51:10 -070028struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
29 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070030{
Junio C Hamano9534f402005-11-02 15:19:13 -080031 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070032
33 if (!obj)
34 return NULL;
Junio C Hamanof76412e2005-08-21 02:51:10 -070035 return check_commit(obj, sha1, quiet);
36}
37
38struct commit *lookup_commit_reference(const unsigned char *sha1)
39{
40 return lookup_commit_reference_gently(sha1, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070041}
42
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100043struct commit *lookup_commit_or_die(const unsigned char *sha1, const char *ref_name)
44{
45 struct commit *c = lookup_commit_reference(sha1);
46 if (!c)
47 die(_("could not parse %s"), ref_name);
48 if (hashcmp(sha1, c->object.sha1)) {
49 warning(_("%s %s is not a commit!"),
50 ref_name, sha1_to_hex(sha1));
51 }
52 return c;
53}
54
Jason McMullan5d6ccf52005-06-03 11:05:39 -040055struct commit *lookup_commit(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070056{
57 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -070058 if (!obj)
59 return create_object(sha1, OBJ_COMMIT, alloc_commit_node());
Nicolas Pitred1af0022005-05-20 16:59:17 -040060 if (!obj->type)
Linus Torvalds19746322006-07-11 20:45:31 -070061 obj->type = OBJ_COMMIT;
Junio C Hamanof76412e2005-08-21 02:51:10 -070062 return check_commit(obj, sha1, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070063}
64
Pat Notza6fa5992010-11-02 13:59:07 -060065struct commit *lookup_commit_reference_by_name(const char *name)
66{
67 unsigned char sha1[20];
68 struct commit *commit;
69
70 if (get_sha1(name, sha1))
71 return NULL;
72 commit = lookup_commit_reference(sha1);
73 if (!commit || parse_commit(commit))
74 return NULL;
75 return commit;
76}
77
Martin Koegler0a617792008-01-19 18:35:23 +010078static unsigned long parse_commit_date(const char *buf, const char *tail)
Daniel Barkalow175785e2005-04-18 11:39:48 -070079{
Martin Koegler0a617792008-01-19 18:35:23 +010080 const char *dateptr;
Daniel Barkalow175785e2005-04-18 11:39:48 -070081
Martin Koegler0a617792008-01-19 18:35:23 +010082 if (buf + 6 >= tail)
83 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070084 if (memcmp(buf, "author", 6))
85 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010086 while (buf < tail && *buf++ != '\n')
Daniel Barkalow175785e2005-04-18 11:39:48 -070087 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010088 if (buf + 9 >= tail)
89 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070090 if (memcmp(buf, "committer", 9))
91 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010092 while (buf < tail && *buf++ != '>')
Daniel Barkalow175785e2005-04-18 11:39:48 -070093 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010094 if (buf >= tail)
95 return 0;
96 dateptr = buf;
97 while (buf < tail && *buf++ != '\n')
98 /* nada */;
99 if (buf >= tail)
100 return 0;
101 /* dateptr < buf && buf[-1] == '\n', so strtoul will stop at buf-1 */
Eric Wongf2909742009-07-02 21:34:54 -0700102 return strtoul(dateptr, NULL, 10);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700103}
104
Junio C Hamano5040f172006-04-06 23:58:51 -0700105static struct commit_graft **commit_graft;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700106static int commit_graft_alloc, commit_graft_nr;
107
108static int commit_graft_pos(const unsigned char *sha1)
109{
110 int lo, hi;
111 lo = 0;
112 hi = commit_graft_nr;
113 while (lo < hi) {
114 int mi = (lo + hi) / 2;
115 struct commit_graft *graft = commit_graft[mi];
David Rientjesa89fccd2006-08-17 11:54:57 -0700116 int cmp = hashcmp(sha1, graft->sha1);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700117 if (!cmp)
118 return mi;
119 if (cmp < 0)
120 hi = mi;
121 else
122 lo = mi + 1;
123 }
124 return -lo - 1;
125}
126
Junio C Hamano5040f172006-04-06 23:58:51 -0700127int register_commit_graft(struct commit_graft *graft, int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700128{
Junio C Hamano5040f172006-04-06 23:58:51 -0700129 int pos = commit_graft_pos(graft->sha1);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700130
Junio C Hamano5040f172006-04-06 23:58:51 -0700131 if (0 <= pos) {
132 if (ignore_dups)
133 free(graft);
134 else {
135 free(commit_graft[pos]);
136 commit_graft[pos] = graft;
137 }
138 return 1;
139 }
140 pos = -pos - 1;
141 if (commit_graft_alloc <= ++commit_graft_nr) {
142 commit_graft_alloc = alloc_nr(commit_graft_alloc);
143 commit_graft = xrealloc(commit_graft,
144 sizeof(*commit_graft) *
145 commit_graft_alloc);
146 }
147 if (pos < commit_graft_nr)
148 memmove(commit_graft + pos + 1,
149 commit_graft + pos,
150 (commit_graft_nr - pos - 1) *
151 sizeof(*commit_graft));
152 commit_graft[pos] = graft;
153 return 0;
154}
155
156struct commit_graft *read_graft_line(char *buf, int len)
157{
158 /* The format is just "Commit Parent1 Parent2 ...\n" */
159 int i;
160 struct commit_graft *graft = NULL;
161
Junio C Hamanofe0a3cb2009-10-14 11:51:03 -0700162 while (len && isspace(buf[len-1]))
163 buf[--len] = '\0';
Yann Dirson360204c2006-04-17 13:41:49 +0200164 if (buf[0] == '#' || buf[0] == '\0')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700165 return NULL;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100166 if ((len + 1) % 41)
167 goto bad_graft_data;
Junio C Hamano5040f172006-04-06 23:58:51 -0700168 i = (len + 1) / 41 - 1;
169 graft = xmalloc(sizeof(*graft) + 20 * i);
170 graft->nr_parent = i;
171 if (get_sha1_hex(buf, graft->sha1))
172 goto bad_graft_data;
173 for (i = 40; i < len; i += 41) {
174 if (buf[i] != ' ')
175 goto bad_graft_data;
176 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
177 goto bad_graft_data;
178 }
179 return graft;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100180
181bad_graft_data:
182 error("bad graft data: %s", buf);
183 free(graft);
184 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700185}
186
Nanako Shiraishic5ae6432008-10-02 19:14:30 +0900187static int read_graft_file(const char *graft_file)
Junio C Hamano5040f172006-04-06 23:58:51 -0700188{
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700189 FILE *fp = fopen(graft_file, "r");
190 char buf[1024];
Junio C Hamano5040f172006-04-06 23:58:51 -0700191 if (!fp)
192 return -1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700193 while (fgets(buf, sizeof(buf), fp)) {
194 /* The format is just "Commit Parent1 Parent2 ...\n" */
195 int len = strlen(buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700196 struct commit_graft *graft = read_graft_line(buf, len);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700197 if (!graft)
198 continue;
Junio C Hamano5040f172006-04-06 23:58:51 -0700199 if (register_commit_graft(graft, 1))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700200 error("duplicate graft data: %s", buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700201 }
202 fclose(fp);
Junio C Hamano5040f172006-04-06 23:58:51 -0700203 return 0;
204}
205
206static void prepare_commit_graft(void)
207{
208 static int commit_graft_prepared;
209 char *graft_file;
210
211 if (commit_graft_prepared)
212 return;
213 graft_file = get_graft_file();
214 read_graft_file(graft_file);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100215 /* make sure shallows are read */
216 is_repository_shallow();
Junio C Hamano5040f172006-04-06 23:58:51 -0700217 commit_graft_prepared = 1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700218}
219
Martin Koegler45163382008-02-25 22:46:07 +0100220struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700221{
222 int pos;
Junio C Hamano5040f172006-04-06 23:58:51 -0700223 prepare_commit_graft();
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700224 pos = commit_graft_pos(sha1);
225 if (pos < 0)
226 return NULL;
227 return commit_graft[pos];
228}
229
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700230int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100231{
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700232 int i, ret;
233 for (i = ret = 0; i < commit_graft_nr && !ret; i++)
234 ret = fn(commit_graft[i], cb_data);
235 return ret;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100236}
237
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100238int unregister_shallow(const unsigned char *sha1)
239{
240 int pos = commit_graft_pos(sha1);
241 if (pos < 0)
242 return -1;
243 if (pos + 1 < commit_graft_nr)
Jeff King65d41d42010-01-29 05:28:44 -0500244 memmove(commit_graft + pos, commit_graft + pos + 1,
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100245 sizeof(struct commit_graft *)
246 * (commit_graft_nr - pos - 1));
247 commit_graft_nr--;
248 return 0;
249}
250
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700251int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700252{
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700253 const char *tail = buffer;
254 const char *bufptr = buffer;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700255 unsigned char parent[20];
Linus Torvalds6c88be12005-06-20 20:26:03 -0700256 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700257 struct commit_graft *graft;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400258
Daniel Barkalow175785e2005-04-18 11:39:48 -0700259 if (item->object.parsed)
260 return 0;
261 item->object.parsed = 1;
Junio C Hamano3b44f152006-06-28 03:51:00 -0700262 tail += size;
Martin Koegler0a617792008-01-19 18:35:23 +0100263 if (tail <= bufptr + 46 || memcmp(bufptr, "tree ", 5) || bufptr[45] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700264 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
Martin Koegler0a617792008-01-19 18:35:23 +0100265 if (get_sha1_hex(bufptr + 5, parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800266 return error("bad tree pointer in commit %s",
267 sha1_to_hex(item->object.sha1));
Daniel Barkalow175785e2005-04-18 11:39:48 -0700268 item->tree = lookup_tree(parent);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700269 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700270 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700271
272 graft = lookup_commit_graft(item->object.sha1);
Junio C Hamano3b44f152006-06-28 03:51:00 -0700273 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700274 struct commit *new_parent;
275
Junio C Hamano3b44f152006-06-28 03:51:00 -0700276 if (tail <= bufptr + 48 ||
277 get_sha1_hex(bufptr + 7, parent) ||
278 bufptr[47] != '\n')
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700279 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700280 bufptr += 48;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +0200281 /*
282 * The clone is shallow if nr_parent < 0, and we must
283 * not traverse its real parents even when we unhide them.
284 */
285 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700286 continue;
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700287 new_parent = lookup_commit(parent);
Miklos Vajna39468de2008-06-07 22:38:37 +0200288 if (new_parent)
Linus Torvalds6c88be12005-06-20 20:26:03 -0700289 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700290 }
291 if (graft) {
292 int i;
293 struct commit *new_parent;
294 for (i = 0; i < graft->nr_parent; i++) {
295 new_parent = lookup_commit(graft->parent[i]);
296 if (!new_parent)
297 continue;
298 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700299 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700300 }
Martin Koegler0a617792008-01-19 18:35:23 +0100301 item->date = parse_commit_date(bufptr, tail);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800302
Daniel Barkalow175785e2005-04-18 11:39:48 -0700303 return 0;
304}
305
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400306int parse_commit(struct commit *item)
307{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500308 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400309 void *buffer;
310 unsigned long size;
311 int ret;
312
Martin Koegler9786f682008-02-18 21:48:02 +0100313 if (!item)
314 return -1;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400315 if (item->object.parsed)
316 return 0;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500317 buffer = read_sha1_file(item->object.sha1, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400318 if (!buffer)
319 return error("Could not read %s",
320 sha1_to_hex(item->object.sha1));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500321 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400322 free(buffer);
323 return error("Object %s not a commit",
324 sha1_to_hex(item->object.sha1));
325 }
326 ret = parse_commit_buffer(item, buffer, size);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700327 if (save_commit_buffer && !ret) {
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700328 item->buffer = buffer;
329 return 0;
330 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400331 free(buffer);
332 return ret;
333}
334
Christian Couder11af2aa2010-07-22 15:18:30 +0200335int find_commit_subject(const char *commit_buffer, const char **subject)
336{
337 const char *eol;
338 const char *p = commit_buffer;
339
340 while (*p && (*p != '\n' || p[1] != '\n'))
341 p++;
342 if (*p) {
343 p += 2;
344 for (eol = p; *eol && *eol != '\n'; eol++)
345 ; /* do nothing */
346 } else
347 eol = p;
348
349 *subject = p;
350
351 return eol - p;
352}
353
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700354struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700355{
Christopher Li812666c2005-04-26 12:00:58 -0700356 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700357 new_list->item = item;
358 new_list->next = *list_p;
359 *list_p = new_list;
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700360 return new_list;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700361}
362
Miklos Vajna65319472008-06-27 18:21:55 +0200363unsigned commit_list_count(const struct commit_list *l)
364{
365 unsigned c = 0;
366 for (; l; l = l->next )
367 c++;
368 return c;
369}
370
Daniel Barkalow175785e2005-04-18 11:39:48 -0700371void free_commit_list(struct commit_list *list)
372{
373 while (list) {
374 struct commit_list *temp = list;
375 list = temp->next;
376 free(temp);
377 }
378}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700379
Thiago Farina47e44ed2010-11-26 23:58:14 -0200380struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700381{
382 struct commit_list **pp = list;
383 struct commit_list *p;
384 while ((p = *pp) != NULL) {
385 if (p->item->date < item->date) {
386 break;
387 }
388 pp = &p->next;
389 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700390 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700391}
392
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700393
Thiago Farina47e44ed2010-11-26 23:58:14 -0200394void commit_list_sort_by_date(struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700395{
396 struct commit_list *ret = NULL;
397 while (*list) {
Thiago Farina47e44ed2010-11-26 23:58:14 -0200398 commit_list_insert_by_date((*list)->item, &ret);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700399 *list = (*list)->next;
400 }
401 *list = ret;
402}
403
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700404struct commit *pop_most_recent_commit(struct commit_list **list,
405 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700406{
407 struct commit *ret = (*list)->item;
408 struct commit_list *parents = ret->parents;
409 struct commit_list *old = *list;
410
411 *list = (*list)->next;
412 free(old);
413
414 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700415 struct commit *commit = parents->item;
Martin Koeglerdec38c82008-02-18 21:48:03 +0100416 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700417 commit->object.flags |= mark;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200418 commit_list_insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700419 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700420 parents = parents->next;
421 }
422 return ret;
423}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700424
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800425void clear_commit_marks(struct commit *commit, unsigned int mark)
426{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100427 while (commit) {
428 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800429
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100430 if (!(mark & commit->object.flags))
431 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700432
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100433 commit->object.flags &= ~mark;
434
435 parents = commit->parents;
436 if (!parents)
437 return;
438
439 while ((parents = parents->next))
440 clear_commit_marks(parents->item, mark);
441
442 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800443 }
444}
445
René Scharfe86a0a402011-10-01 18:16:08 +0200446void clear_commit_marks_for_object_array(struct object_array *a, unsigned mark)
447{
448 struct object *object;
449 struct commit *commit;
450 unsigned int i;
451
452 for (i = 0; i < a->nr; i++) {
453 object = a->objects[i].item;
454 commit = lookup_commit_reference_gently(object->sha1, 1);
455 if (commit)
456 clear_commit_marks(commit, mark);
457 }
458}
459
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000460struct commit *pop_commit(struct commit_list **stack)
461{
462 struct commit_list *top = *stack;
463 struct commit *item = top ? top->item : NULL;
464
465 if (top) {
466 *stack = top->next;
467 free(top);
468 }
469 return item;
470}
471
Jon Seymourab580ac2005-07-07 02:39:34 +1000472/*
473 * Performs an in-place topological sort on the list supplied.
474 */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800475void sort_in_topological_order(struct commit_list ** list, int lifo)
Jon Seymourab580ac2005-07-07 02:39:34 +1000476{
Linus Torvalds23c17d42007-11-02 13:32:58 -0700477 struct commit_list *next, *orig = *list;
478 struct commit_list *work, **insert;
479 struct commit_list **pptr;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100480
Linus Torvalds23c17d42007-11-02 13:32:58 -0700481 if (!orig)
Eric Wong7d6fb372005-12-24 04:12:43 -0800482 return;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700483 *list = NULL;
484
485 /* Mark them and clear the indegree */
486 for (next = orig; next; next = next->next) {
487 struct commit *commit = next->item;
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100488 commit->indegree = 1;
Jon Seymourab580ac2005-07-07 02:39:34 +1000489 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700490
Jon Seymourab580ac2005-07-07 02:39:34 +1000491 /* update the indegree */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700492 for (next = orig; next; next = next->next) {
Jon Seymourab580ac2005-07-07 02:39:34 +1000493 struct commit_list * parents = next->item->parents;
494 while (parents) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700495 struct commit *parent = parents->item;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100496
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100497 if (parent->indegree)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700498 parent->indegree++;
499 parents = parents->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000500 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000501 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700502
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700503 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200504 * find the tips
505 *
506 * tips are nodes not reachable from any other node in the list
507 *
508 * the tips serve as a starting set for the work queue.
509 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700510 work = NULL;
Linus Torvalds2ed02882005-11-14 10:01:26 -0800511 insert = &work;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700512 for (next = orig; next; next = next->next) {
513 struct commit *commit = next->item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000514
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100515 if (commit->indegree == 1)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700516 insert = &commit_list_insert(commit, insert)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000517 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800518
Jon Seymourab580ac2005-07-07 02:39:34 +1000519 /* process the list in topological order */
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800520 if (!lifo)
Thiago Farina47e44ed2010-11-26 23:58:14 -0200521 commit_list_sort_by_date(&work);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700522
523 pptr = list;
524 *list = NULL;
Jon Seymourab580ac2005-07-07 02:39:34 +1000525 while (work) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700526 struct commit *commit;
527 struct commit_list *parents, *work_item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000528
Linus Torvalds23c17d42007-11-02 13:32:58 -0700529 work_item = work;
530 work = work_item->next;
531 work_item->next = NULL;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100532
Linus Torvalds23c17d42007-11-02 13:32:58 -0700533 commit = work_item->item;
534 for (parents = commit->parents; parents ; parents = parents->next) {
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -0700535 struct commit *parent = parents->item;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700536
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100537 if (!parent->indegree)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700538 continue;
539
540 /*
541 * parents are only enqueued for emission
542 * when all their children have been emitted thereby
543 * guaranteeing topological order.
544 */
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100545 if (--parent->indegree == 1) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700546 if (!lifo)
Thiago Farina47e44ed2010-11-26 23:58:14 -0200547 commit_list_insert_by_date(parent, &work);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700548 else
549 commit_list_insert(parent, &work);
Jon Seymourab580ac2005-07-07 02:39:34 +1000550 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000551 }
552 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200553 * work_item is a commit all of whose children
554 * have already been emitted. we can emit it now.
555 */
Johannes Schindeline358f3c2008-07-23 01:51:36 +0100556 commit->indegree = 0;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700557 *pptr = work_item;
558 pptr = &work_item->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000559 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000560}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200561
Junio C Hamano12952282007-01-08 23:10:49 -0800562/* merge-base stuff */
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200563
Junio C Hamano577ed5c2006-10-22 17:32:47 -0700564/* bits #0..15 in revision.h */
565#define PARENT1 (1u<<16)
566#define PARENT2 (1u<<17)
567#define STALE (1u<<18)
568#define RESULT (1u<<19)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200569
Junio C Hamano12952282007-01-08 23:10:49 -0800570static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
571
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200572static struct commit *interesting(struct commit_list *list)
573{
574 while (list) {
575 struct commit *commit = list->item;
576 list = list->next;
Junio C Hamano542ccef2006-07-02 11:34:17 -0700577 if (commit->object.flags & STALE)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200578 continue;
579 return commit;
580 }
581 return NULL;
582}
583
Junio C Hamano6a938642008-06-27 18:22:02 +0200584static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200585{
586 struct commit_list *list = NULL;
587 struct commit_list *result = NULL;
Junio C Hamano6a938642008-06-27 18:22:02 +0200588 int i;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200589
Junio C Hamano6a938642008-06-27 18:22:02 +0200590 for (i = 0; i < n; i++) {
591 if (one == twos[i])
592 /*
593 * We do not mark this even with RESULT so we do not
594 * have to clean it up.
595 */
596 return commit_list_insert(one, &result);
597 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200598
Martin Koegler172947e2008-02-18 21:47:57 +0100599 if (parse_commit(one))
600 return NULL;
Junio C Hamano6a938642008-06-27 18:22:02 +0200601 for (i = 0; i < n; i++) {
602 if (parse_commit(twos[i]))
603 return NULL;
604 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200605
Junio C Hamanof3249432006-07-04 18:46:42 -0700606 one->object.flags |= PARENT1;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200607 commit_list_insert_by_date(one, &list);
Junio C Hamano6a938642008-06-27 18:22:02 +0200608 for (i = 0; i < n; i++) {
609 twos[i]->object.flags |= PARENT2;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200610 commit_list_insert_by_date(twos[i], &list);
Junio C Hamano6a938642008-06-27 18:22:02 +0200611 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200612
613 while (interesting(list)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700614 struct commit *commit;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200615 struct commit_list *parents;
Brandon Caseyf203d692009-08-27 11:16:34 -0500616 struct commit_list *next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700617 int flags;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200618
Junio C Hamanof3249432006-07-04 18:46:42 -0700619 commit = list->item;
Brandon Caseyf203d692009-08-27 11:16:34 -0500620 next = list->next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700621 free(list);
Brandon Caseyf203d692009-08-27 11:16:34 -0500622 list = next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700623
624 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200625 if (flags == (PARENT1 | PARENT2)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700626 if (!(commit->object.flags & RESULT)) {
627 commit->object.flags |= RESULT;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200628 commit_list_insert_by_date(commit, &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700629 }
Junio C Hamano542ccef2006-07-02 11:34:17 -0700630 /* Mark parents of a found merge stale */
631 flags |= STALE;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200632 }
633 parents = commit->parents;
634 while (parents) {
635 struct commit *p = parents->item;
636 parents = parents->next;
637 if ((p->object.flags & flags) == flags)
638 continue;
Martin Koegler172947e2008-02-18 21:47:57 +0100639 if (parse_commit(p))
640 return NULL;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200641 p->object.flags |= flags;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200642 commit_list_insert_by_date(p, &list);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200643 }
644 }
645
Junio C Hamanof3249432006-07-04 18:46:42 -0700646 /* Clean up the result to remove stale ones */
Junio C Hamano12952282007-01-08 23:10:49 -0800647 free_commit_list(list);
Junio C Hamanof3249432006-07-04 18:46:42 -0700648 list = result; result = NULL;
649 while (list) {
Brandon Caseyf203d692009-08-27 11:16:34 -0500650 struct commit_list *next = list->next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700651 if (!(list->item->object.flags & STALE))
Thiago Farina47e44ed2010-11-26 23:58:14 -0200652 commit_list_insert_by_date(list->item, &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700653 free(list);
Brandon Caseyf203d692009-08-27 11:16:34 -0500654 list = next;
Junio C Hamanof3249432006-07-04 18:46:42 -0700655 }
656 return result;
657}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200658
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200659struct commit_list *get_octopus_merge_bases(struct commit_list *in)
660{
661 struct commit_list *i, *j, *k, *ret = NULL;
662 struct commit_list **pptr = &ret;
663
664 for (i = in; i; i = i->next) {
665 if (!ret)
666 pptr = &commit_list_insert(i->item, pptr)->next;
667 else {
668 struct commit_list *new = NULL, *end = NULL;
669
670 for (j = ret; j; j = j->next) {
671 struct commit_list *bases;
672 bases = get_merge_bases(i->item, j->item, 1);
673 if (!new)
674 new = bases;
675 else
676 end->next = bases;
677 for (k = bases; k; k = k->next)
678 end = k;
679 }
680 ret = new;
681 }
682 }
683 return ret;
684}
685
Junio C Hamano6a938642008-06-27 18:22:02 +0200686struct commit_list *get_merge_bases_many(struct commit *one,
687 int n,
688 struct commit **twos,
689 int cleanup)
Junio C Hamanof3249432006-07-04 18:46:42 -0700690{
Junio C Hamanof3249432006-07-04 18:46:42 -0700691 struct commit_list *list;
692 struct commit **rslt;
693 struct commit_list *result;
694 int cnt, i, j;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200695
Junio C Hamano6a938642008-06-27 18:22:02 +0200696 result = merge_bases_many(one, n, twos);
697 for (i = 0; i < n; i++) {
698 if (one == twos[i])
699 return result;
700 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700701 if (!result || !result->next) {
702 if (cleanup) {
703 clear_commit_marks(one, all_flags);
Junio C Hamano6a938642008-06-27 18:22:02 +0200704 for (i = 0; i < n; i++)
705 clear_commit_marks(twos[i], all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200706 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700707 return result;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200708 }
709
Junio C Hamanof3249432006-07-04 18:46:42 -0700710 /* There are more than one */
711 cnt = 0;
712 list = result;
713 while (list) {
714 list = list->next;
715 cnt++;
716 }
717 rslt = xcalloc(cnt, sizeof(*rslt));
718 for (list = result, i = 0; list; list = list->next)
719 rslt[i++] = list->item;
720 free_commit_list(result);
721
722 clear_commit_marks(one, all_flags);
Junio C Hamano6a938642008-06-27 18:22:02 +0200723 for (i = 0; i < n; i++)
724 clear_commit_marks(twos[i], all_flags);
Junio C Hamanof3249432006-07-04 18:46:42 -0700725 for (i = 0; i < cnt - 1; i++) {
726 for (j = i+1; j < cnt; j++) {
727 if (!rslt[i] || !rslt[j])
728 continue;
Junio C Hamano6a938642008-06-27 18:22:02 +0200729 result = merge_bases_many(rslt[i], 1, &rslt[j]);
Junio C Hamanof3249432006-07-04 18:46:42 -0700730 clear_commit_marks(rslt[i], all_flags);
731 clear_commit_marks(rslt[j], all_flags);
732 for (list = result; list; list = list->next) {
733 if (rslt[i] == list->item)
734 rslt[i] = NULL;
735 if (rslt[j] == list->item)
736 rslt[j] = NULL;
737 }
738 }
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700739 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200740
Junio C Hamanof3249432006-07-04 18:46:42 -0700741 /* Surviving ones in rslt[] are the independent results */
742 result = NULL;
743 for (i = 0; i < cnt; i++) {
744 if (rslt[i])
Thiago Farina47e44ed2010-11-26 23:58:14 -0200745 commit_list_insert_by_date(rslt[i], &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700746 }
747 free(rslt);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200748 return result;
749}
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800750
Junio C Hamano6a938642008-06-27 18:22:02 +0200751struct commit_list *get_merge_bases(struct commit *one, struct commit *two,
752 int cleanup)
753{
754 return get_merge_bases_many(one, 1, &two, cleanup);
755}
756
Jake Goulding7fcdb362009-01-26 09:13:24 -0500757int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
758{
759 if (!with_commit)
760 return 1;
761 while (with_commit) {
762 struct commit *other;
763
764 other = with_commit->item;
765 with_commit = with_commit->next;
766 if (in_merge_bases(other, &commit, 1))
767 return 1;
768 }
769 return 0;
770}
771
Junio C Hamano03840fc2007-01-08 23:22:31 -0800772int in_merge_bases(struct commit *commit, struct commit **reference, int num)
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800773{
774 struct commit_list *bases, *b;
775 int ret = 0;
776
Junio C Hamano03840fc2007-01-08 23:22:31 -0800777 if (num == 1)
778 bases = get_merge_bases(commit, *reference, 1);
779 else
780 die("not yet");
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800781 for (b = bases; b; b = b->next) {
Junio C Hamano03840fc2007-01-08 23:22:31 -0800782 if (!hashcmp(commit->object.sha1, b->item->object.sha1)) {
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -0800783 ret = 1;
784 break;
785 }
786 }
787
788 free_commit_list(bases);
789 return ret;
790}
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200791
792struct commit_list *reduce_heads(struct commit_list *heads)
793{
794 struct commit_list *p;
795 struct commit_list *result = NULL, **tail = &result;
796 struct commit **other;
797 size_t num_head, num_other;
798
799 if (!heads)
800 return NULL;
801
802 /* Avoid unnecessary reallocations */
803 for (p = heads, num_head = 0; p; p = p->next)
804 num_head++;
805 other = xcalloc(sizeof(*other), num_head);
806
807 /* For each commit, see if it can be reached by others */
808 for (p = heads; p; p = p->next) {
809 struct commit_list *q, *base;
810
Junio C Hamano711f6b22008-07-14 00:09:41 -0700811 /* Do we already have this in the result? */
812 for (q = result; q; q = q->next)
813 if (p->item == q->item)
814 break;
815 if (q)
816 continue;
817
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200818 num_other = 0;
819 for (q = heads; q; q = q->next) {
Sverre Hvammen Johansen3d1dd472008-07-13 08:13:55 +0000820 if (p->item == q->item)
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200821 continue;
822 other[num_other++] = q->item;
823 }
Junio C Hamano711f6b22008-07-14 00:09:41 -0700824 if (num_other)
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200825 base = get_merge_bases_many(p->item, num_other, other, 1);
Junio C Hamano711f6b22008-07-14 00:09:41 -0700826 else
Junio C Hamano98cf9c32008-06-27 18:22:03 +0200827 base = NULL;
828 /*
829 * If p->item does not have anything common with other
830 * commits, there won't be any merge base. If it is
831 * reachable from some of the others, p->item will be
832 * the merge base. If its history is connected with
833 * others, but p->item is not reachable by others, we
834 * will get something other than p->item back.
835 */
836 if (!base || (base->item != p->item))
837 tail = &(commit_list_insert(p->item, tail)->next);
838 free_commit_list(base);
839 }
840 free(other);
841 return result;
842}
Jeff King40d52ff2010-04-01 20:05:23 -0400843
Junio C Hamanoba3c69a2011-10-05 17:23:20 -0700844static const char gpg_sig_header[] = "gpgsig";
845static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
846
847static int do_sign_commit(struct strbuf *buf, const char *keyid)
848{
849 struct strbuf sig = STRBUF_INIT;
850 int inspos, copypos;
851
852 /* find the end of the header */
853 inspos = strstr(buf->buf, "\n\n") - buf->buf + 1;
854
855 if (!keyid || !*keyid)
856 keyid = get_signing_key();
857 if (sign_buffer(buf, &sig, keyid)) {
858 strbuf_release(&sig);
859 return -1;
860 }
861
862 for (copypos = 0; sig.buf[copypos]; ) {
863 const char *bol = sig.buf + copypos;
864 const char *eol = strchrnul(bol, '\n');
865 int len = (eol - bol) + !!*eol;
866
867 if (!copypos) {
868 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
869 inspos += gpg_sig_header_len;
870 }
871 strbuf_insert(buf, inspos++, " ", 1);
872 strbuf_insert(buf, inspos, bol, len);
873 inspos += len;
874 copypos += len;
875 }
876 strbuf_release(&sig);
877 return 0;
878}
879
Junio C Hamano0c37f1f2011-10-18 15:53:23 -0700880int parse_signed_commit(const unsigned char *sha1,
881 struct strbuf *payload, struct strbuf *signature)
882{
883 unsigned long size;
884 enum object_type type;
885 char *buffer = read_sha1_file(sha1, &type, &size);
886 int in_signature, saw_signature = -1;
887 char *line, *tail;
888
889 if (!buffer || type != OBJ_COMMIT)
890 goto cleanup;
891
892 line = buffer;
893 tail = buffer + size;
894 in_signature = 0;
895 saw_signature = 0;
896 while (line < tail) {
897 const char *sig = NULL;
898 char *next = memchr(line, '\n', tail - line);
899
900 next = next ? next + 1 : tail;
901 if (in_signature && line[0] == ' ')
902 sig = line + 1;
903 else if (!prefixcmp(line, gpg_sig_header) &&
904 line[gpg_sig_header_len] == ' ')
905 sig = line + gpg_sig_header_len + 1;
906 if (sig) {
907 strbuf_add(signature, sig, next - sig);
908 saw_signature = 1;
909 in_signature = 1;
910 } else {
911 if (*line == '\n')
912 /* dump the whole remainder of the buffer */
913 next = tail;
914 strbuf_add(payload, line, next - line);
915 in_signature = 0;
916 }
917 line = next;
918 }
919 cleanup:
920 free(buffer);
921 return saw_signature;
922}
923
Junio C Hamano5231c632011-11-07 16:21:32 -0800924static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
925{
926 struct merge_remote_desc *desc;
927 struct commit_extra_header *mergetag;
928 char *buf;
929 unsigned long size, len;
930 enum object_type type;
931
932 desc = merge_remote_util(parent);
933 if (!desc || !desc->obj)
934 return;
935 buf = read_sha1_file(desc->obj->sha1, &type, &size);
936 if (!buf || type != OBJ_TAG)
937 goto free_return;
938 len = parse_signature(buf, size);
939 if (size == len)
940 goto free_return;
941 /*
942 * We could verify this signature and either omit the tag when
943 * it does not validate, but the integrator may not have the
944 * public key of the signer of the tag he is merging, while a
945 * later auditor may have it while auditing, so let's not run
946 * verify-signed-buffer here for now...
947 *
948 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
949 * warn("warning: signed tag unverified.");
950 */
951 mergetag = xcalloc(1, sizeof(*mergetag));
952 mergetag->key = xstrdup("mergetag");
953 mergetag->value = buf;
954 mergetag->len = size;
955
956 **tail = mergetag;
957 *tail = &mergetag->next;
958 return;
959
960free_return:
961 free(buf);
962}
963
964void append_merge_tag_headers(struct commit_list *parents,
965 struct commit_extra_header ***tail)
966{
967 while (parents) {
968 struct commit *parent = parents->item;
969 handle_signed_tag(parent, tail);
970 parents = parents->next;
971 }
972}
973
974static void add_extra_header(struct strbuf *buffer,
975 struct commit_extra_header *extra)
976{
977 strbuf_addstr(buffer, extra->key);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -0800978 if (extra->len)
979 strbuf_add_lines(buffer, " ", extra->value, extra->len);
980 else
981 strbuf_addch(buffer, '\n');
982}
983
Junio C Hamanoc871a1d2012-01-05 10:54:14 -0800984struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
985 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -0800986{
987 struct commit_extra_header *extra = NULL;
988 unsigned long size;
989 enum object_type type;
990 char *buffer = read_sha1_file(commit->object.sha1, &type, &size);
991 if (buffer && type == OBJ_COMMIT)
Junio C Hamanoc871a1d2012-01-05 10:54:14 -0800992 extra = read_commit_extra_header_lines(buffer, size, exclude);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -0800993 free(buffer);
994 return extra;
995}
996
997static inline int standard_header_field(const char *field, size_t len)
998{
999 return ((len == 4 && !memcmp(field, "tree ", 5)) ||
1000 (len == 6 && !memcmp(field, "parent ", 7)) ||
1001 (len == 6 && !memcmp(field, "author ", 7)) ||
1002 (len == 9 && !memcmp(field, "committer ", 10)) ||
1003 (len == 8 && !memcmp(field, "encoding ", 9)));
1004}
1005
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001006static int excluded_header_field(const char *field, size_t len, const char **exclude)
1007{
1008 if (!exclude)
1009 return 0;
1010
1011 while (*exclude) {
1012 size_t xlen = strlen(*exclude);
1013 if (len == xlen &&
1014 !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
1015 return 1;
1016 exclude++;
1017 }
1018 return 0;
1019}
1020
1021struct commit_extra_header *read_commit_extra_header_lines(const char *buffer, size_t size,
1022 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001023{
1024 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1025 const char *line, *next, *eof, *eob;
1026 struct strbuf buf = STRBUF_INIT;
1027
1028 for (line = buffer, eob = line + size;
1029 line < eob && *line != '\n';
1030 line = next) {
1031 next = memchr(line, '\n', eob - line);
1032 next = next ? next + 1 : eob;
1033 if (*line == ' ') {
1034 /* continuation */
1035 if (it)
1036 strbuf_add(&buf, line + 1, next - (line + 1));
1037 continue;
1038 }
1039 if (it)
1040 it->value = strbuf_detach(&buf, &it->len);
1041 strbuf_reset(&buf);
1042 it = NULL;
1043
1044 eof = strchr(line, ' ');
1045 if (next <= eof)
1046 eof = next;
1047
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001048 if (standard_header_field(line, eof - line) ||
1049 excluded_header_field(line, eof - line, exclude))
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001050 continue;
1051
1052 it = xcalloc(1, sizeof(*it));
1053 it->key = xmemdupz(line, eof-line);
1054 *tail = it;
1055 tail = &it->next;
1056 if (eof + 1 < next)
1057 strbuf_add(&buf, eof + 1, next - (eof + 1));
1058 }
1059 if (it)
1060 it->value = strbuf_detach(&buf, &it->len);
1061 return extra;
Junio C Hamano5231c632011-11-07 16:21:32 -08001062}
1063
1064void free_commit_extra_headers(struct commit_extra_header *extra)
1065{
1066 while (extra) {
1067 struct commit_extra_header *next = extra->next;
1068 free(extra->key);
1069 free(extra->value);
1070 free(extra);
1071 extra = next;
1072 }
1073}
1074
Junio C Hamanof35ccd92011-12-22 11:27:26 -08001075int commit_tree(const struct strbuf *msg, unsigned char *tree,
Junio C Hamano5231c632011-11-07 16:21:32 -08001076 struct commit_list *parents, unsigned char *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001077 const char *author, const char *sign_commit)
Junio C Hamano5231c632011-11-07 16:21:32 -08001078{
1079 struct commit_extra_header *extra = NULL, **tail = &extra;
1080 int result;
1081
1082 append_merge_tag_headers(parents, &tail);
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001083 result = commit_tree_extended(msg, tree, parents, ret,
1084 author, sign_commit, extra);
Junio C Hamano5231c632011-11-07 16:21:32 -08001085 free_commit_extra_headers(extra);
1086 return result;
1087}
1088
Jeff King40d52ff2010-04-01 20:05:23 -04001089static const char commit_utf8_warn[] =
1090"Warning: commit message does not conform to UTF-8.\n"
1091"You may want to amend it after fixing the message, or set the config\n"
1092"variable i18n.commitencoding to the encoding your project uses.\n";
1093
Junio C Hamanof35ccd92011-12-22 11:27:26 -08001094int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
Junio C Hamano5231c632011-11-07 16:21:32 -08001095 struct commit_list *parents, unsigned char *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001096 const char *author, const char *sign_commit,
1097 struct commit_extra_header *extra)
Jeff King40d52ff2010-04-01 20:05:23 -04001098{
1099 int result;
1100 int encoding_is_utf8;
1101 struct strbuf buffer;
1102
1103 assert_sha1_type(tree, OBJ_TREE);
1104
Nguyễn Thái Ngọc Duy37576c12011-12-15 20:47:23 +07001105 if (memchr(msg->buf, '\0', msg->len))
1106 return error("a NUL byte in commit log message not allowed.");
1107
Jeff King40d52ff2010-04-01 20:05:23 -04001108 /* Not having i18n.commitencoding is the same as having utf-8 */
1109 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1110
1111 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
1112 strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree));
1113
1114 /*
1115 * NOTE! This ordering means that the same exact tree merged with a
1116 * different order of parents will be a _different_ changeset even
1117 * if everything else stays the same.
1118 */
1119 while (parents) {
1120 struct commit_list *next = parents->next;
Junio C Hamano5231c632011-11-07 16:21:32 -08001121 struct commit *parent = parents->item;
1122
Jeff King40d52ff2010-04-01 20:05:23 -04001123 strbuf_addf(&buffer, "parent %s\n",
Junio C Hamano5231c632011-11-07 16:21:32 -08001124 sha1_to_hex(parent->object.sha1));
Jeff King40d52ff2010-04-01 20:05:23 -04001125 free(parents);
1126 parents = next;
1127 }
1128
1129 /* Person/date information */
1130 if (!author)
1131 author = git_author_info(IDENT_ERROR_ON_NO_NAME);
1132 strbuf_addf(&buffer, "author %s\n", author);
1133 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
1134 if (!encoding_is_utf8)
1135 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
Junio C Hamano5231c632011-11-07 16:21:32 -08001136
1137 while (extra) {
1138 add_extra_header(&buffer, extra);
1139 extra = extra->next;
1140 }
Jeff King40d52ff2010-04-01 20:05:23 -04001141 strbuf_addch(&buffer, '\n');
1142
1143 /* And add the comment */
Nguyễn Thái Ngọc Duy13f8b722011-12-15 20:47:22 +07001144 strbuf_addbuf(&buffer, msg);
Jeff King40d52ff2010-04-01 20:05:23 -04001145
1146 /* And check the encoding */
1147 if (encoding_is_utf8 && !is_utf8(buffer.buf))
1148 fprintf(stderr, commit_utf8_warn);
1149
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001150 if (sign_commit && do_sign_commit(&buffer, sign_commit))
1151 return -1;
1152
Jeff King40d52ff2010-04-01 20:05:23 -04001153 result = write_sha1_file(buffer.buf, buffer.len, commit_type, ret);
1154 strbuf_release(&buffer);
1155 return result;
1156}
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001157
1158struct commit *get_merge_parent(const char *name)
1159{
1160 struct object *obj;
1161 struct commit *commit;
1162 unsigned char sha1[20];
1163 if (get_sha1(name, sha1))
1164 return NULL;
1165 obj = parse_object(sha1);
1166 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
1167 if (commit && !commit->util) {
1168 struct merge_remote_desc *desc;
1169 desc = xmalloc(sizeof(*desc));
1170 desc->obj = obj;
1171 desc->name = strdup(name);
1172 commit->util = desc;
1173 }
1174 return commit;
1175}