blob: a7c0b5f8c6ce1d1d41c7ede357116fee4a643bbc [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"
Derrick Stolee177722b2018-04-10 08:56:05 -04004#include "commit-graph.h"
Jonathan Nieder6a1a79f2018-05-15 16:42:16 -07005#include "repository.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07006#include "object-store.h"
Johannes Schindelined09aef2006-10-30 20:09:06 +01007#include "pkt-line.h"
Junio C Hamano52883fb2006-12-25 11:48:35 -08008#include "utf8.h"
Junio C Hamano199c45b2007-04-09 02:34:05 -07009#include "diff.h"
10#include "revision.h"
Johannes Schindelina97a7462009-10-09 12:21:57 +020011#include "notes.h"
Stefan Beller14ba97f2018-05-15 14:48:42 -070012#include "alloc.h"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070013#include "gpg-interface.h"
René Scharfe46905892012-04-01 00:10:39 +020014#include "mergesort.h"
Junio C Hamanoa84b7942013-04-13 11:56:41 -070015#include "commit-slab.h"
Junio C Hamanoda24b102013-06-06 21:58:12 -070016#include "prio-queue.h"
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +040017#include "sha1-lookup.h"
Brian Malehornd76650b2017-05-15 23:06:49 -070018#include "wt-status.h"
Johannes Schindelinf9f99b32018-04-29 00:44:44 +020019#include "advice.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -070020
Junio C Hamano82a75292012-09-15 13:58:15 -070021static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
22
Linus Torvalds60ab26d2005-09-15 14:43:17 -070023int save_commit_buffer = 1;
24
Daniel Barkalow175785e2005-04-18 11:39:48 -070025const char *commit_type = "commit";
26
brian m. carlsonbc832662017-05-06 22:10:10 +000027struct commit *lookup_commit_reference_gently(const struct object_id *oid,
Junio C Hamanof76412e2005-08-21 02:51:10 -070028 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070029{
brian m. carlsonc251c832017-05-06 22:10:38 +000030 struct object *obj = deref_tag(parse_object(oid), NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070031
32 if (!obj)
33 return NULL;
Jeff King8ff226a2014-07-13 02:42:03 -040034 return object_as_type(obj, OBJ_COMMIT, quiet);
Junio C Hamanof76412e2005-08-21 02:51:10 -070035}
36
brian m. carlsonbc832662017-05-06 22:10:10 +000037struct commit *lookup_commit_reference(const struct object_id *oid)
Junio C Hamanof76412e2005-08-21 02:51:10 -070038{
brian m. carlsonbc832662017-05-06 22:10:10 +000039 return lookup_commit_reference_gently(oid, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070040}
41
brian m. carlsonbc832662017-05-06 22:10:10 +000042struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100043{
brian m. carlsonbc832662017-05-06 22:10:10 +000044 struct commit *c = lookup_commit_reference(oid);
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100045 if (!c)
46 die(_("could not parse %s"), ref_name);
brian m. carlsonbc832662017-05-06 22:10:10 +000047 if (oidcmp(oid, &c->object.oid)) {
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100048 warning(_("%s %s is not a commit!"),
brian m. carlsonbc832662017-05-06 22:10:10 +000049 ref_name, oid_to_hex(oid));
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100050 }
51 return c;
52}
53
brian m. carlsonbc832662017-05-06 22:10:10 +000054struct commit *lookup_commit(const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 11:39:48 -070055{
brian m. carlsonbc832662017-05-06 22:10:10 +000056 struct object *obj = lookup_object(oid->hash);
Jeff Kingd36f51c2014-07-13 02:41:55 -040057 if (!obj)
Stefan Beller68f95d32018-05-08 12:37:25 -070058 return create_object(the_repository, oid->hash,
Stefan Beller8ba0e5e2018-05-08 12:37:29 -070059 alloc_commit_node(the_repository));
Jeff King8ff226a2014-07-13 02:42:03 -040060 return object_as_type(obj, OBJ_COMMIT, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070061}
62
Pat Notza6fa5992010-11-02 13:59:07 -060063struct commit *lookup_commit_reference_by_name(const char *name)
64{
brian m. carlson7683e2e2015-03-13 23:39:34 +000065 struct object_id oid;
Pat Notza6fa5992010-11-02 13:59:07 -060066 struct commit *commit;
67
brian m. carlsone82caf32017-07-13 23:49:28 +000068 if (get_oid_committish(name, &oid))
Pat Notza6fa5992010-11-02 13:59:07 -060069 return NULL;
brian m. carlsonbc832662017-05-06 22:10:10 +000070 commit = lookup_commit_reference(&oid);
Jeff King5e7d4d32013-10-24 04:53:19 -040071 if (parse_commit(commit))
Pat Notza6fa5992010-11-02 13:59:07 -060072 return NULL;
73 return commit;
74}
75
Johannes Schindelindddbad72017-04-26 21:29:31 +020076static timestamp_t parse_commit_date(const char *buf, const char *tail)
Daniel Barkalow175785e2005-04-18 11:39:48 -070077{
Martin Koegler0a617792008-01-19 18:35:23 +010078 const char *dateptr;
Daniel Barkalow175785e2005-04-18 11:39:48 -070079
Martin Koegler0a617792008-01-19 18:35:23 +010080 if (buf + 6 >= tail)
81 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070082 if (memcmp(buf, "author", 6))
83 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010084 while (buf < tail && *buf++ != '\n')
Daniel Barkalow175785e2005-04-18 11:39:48 -070085 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010086 if (buf + 9 >= tail)
87 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070088 if (memcmp(buf, "committer", 9))
89 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010090 while (buf < tail && *buf++ != '>')
Daniel Barkalow175785e2005-04-18 11:39:48 -070091 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010092 if (buf >= tail)
93 return 0;
94 dateptr = buf;
95 while (buf < tail && *buf++ != '\n')
96 /* nada */;
97 if (buf >= tail)
98 return 0;
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +020099 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
100 return parse_timestamp(dateptr, NULL, 10);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700101}
102
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400103static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
104{
105 struct commit_graft **commit_graft_table = table;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000106 return commit_graft_table[index]->oid.hash;
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400107}
108
Brandon Williamse8086562018-05-17 15:51:47 -0700109static int commit_graft_pos(struct repository *r, const unsigned char *sha1)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700110{
Brandon Williamse8086562018-05-17 15:51:47 -0700111 return sha1_pos(sha1, r->parsed_objects->grafts,
112 r->parsed_objects->grafts_nr,
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400113 commit_graft_sha1_access);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700114}
115
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700116int register_commit_graft(struct repository *r, struct commit_graft *graft,
117 int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700118{
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700119 int pos = commit_graft_pos(r, graft->oid.hash);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700120
Junio C Hamano5040f172006-04-06 23:58:51 -0700121 if (0 <= pos) {
122 if (ignore_dups)
123 free(graft);
124 else {
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700125 free(r->parsed_objects->grafts[pos]);
126 r->parsed_objects->grafts[pos] = graft;
Junio C Hamano5040f172006-04-06 23:58:51 -0700127 }
128 return 1;
129 }
130 pos = -pos - 1;
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700131 ALLOC_GROW(r->parsed_objects->grafts,
132 r->parsed_objects->grafts_nr + 1,
133 r->parsed_objects->grafts_alloc);
134 r->parsed_objects->grafts_nr++;
135 if (pos < r->parsed_objects->grafts_nr)
136 memmove(r->parsed_objects->grafts + pos + 1,
137 r->parsed_objects->grafts + pos,
138 (r->parsed_objects->grafts_nr - pos - 1) *
139 sizeof(*r->parsed_objects->grafts));
140 r->parsed_objects->grafts[pos] = graft;
Junio C Hamano5040f172006-04-06 23:58:51 -0700141 return 0;
142}
143
Patryk Obara9a934032017-08-18 20:33:12 +0200144struct commit_graft *read_graft_line(struct strbuf *line)
Junio C Hamano5040f172006-04-06 23:58:51 -0700145{
146 /* The format is just "Commit Parent1 Parent2 ...\n" */
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200147 int i, phase;
148 const char *tail = NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700149 struct commit_graft *graft = NULL;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200150 struct object_id dummy_oid, *oid;
Junio C Hamano5040f172006-04-06 23:58:51 -0700151
Patryk Obara9a934032017-08-18 20:33:12 +0200152 strbuf_rtrim(line);
153 if (!line->len || line->buf[0] == '#')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700154 return NULL;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200155 /*
156 * phase 0 verifies line, counts hashes in line and allocates graft
157 * phase 1 fills graft
158 */
159 for (phase = 0; phase < 2; phase++) {
160 oid = graft ? &graft->oid : &dummy_oid;
161 if (parse_oid_hex(line->buf, oid, &tail))
Junio C Hamano5040f172006-04-06 23:58:51 -0700162 goto bad_graft_data;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200163 for (i = 0; *tail != '\0'; i++) {
164 oid = graft ? &graft->parent[i] : &dummy_oid;
165 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
166 goto bad_graft_data;
167 }
168 if (!graft) {
169 graft = xmalloc(st_add(sizeof(*graft),
170 st_mult(sizeof(struct object_id), i)));
171 graft->nr_parent = i;
172 }
Junio C Hamano5040f172006-04-06 23:58:51 -0700173 }
174 return graft;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100175
176bad_graft_data:
Patryk Obara9a934032017-08-18 20:33:12 +0200177 error("bad graft data: %s", line->buf);
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200178 assert(!graft);
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100179 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700180}
181
Brandon Williamsd0e5dd02018-05-17 15:51:49 -0700182static int read_graft_file(struct repository *r, const char *graft_file)
Junio C Hamano5040f172006-04-06 23:58:51 -0700183{
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700184 FILE *fp = fopen_or_warn(graft_file, "r");
Johannes Schindeline228c172013-12-27 21:49:57 +0100185 struct strbuf buf = STRBUF_INIT;
Junio C Hamano5040f172006-04-06 23:58:51 -0700186 if (!fp)
187 return -1;
Johannes Schindelinf9f99b32018-04-29 00:44:44 +0200188 if (advice_graft_file_deprecated)
189 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
190 "and will be removed in a future Git version.\n"
191 "\n"
192 "Please use \"git replace --convert-graft-file\"\n"
193 "to convert the grafts into replace refs.\n"
194 "\n"
195 "Turn this message off by running\n"
196 "\"git config advice.graftFileDeprecated false\""));
Johannes Schindeline228c172013-12-27 21:49:57 +0100197 while (!strbuf_getwholeline(&buf, fp, '\n')) {
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700198 /* The format is just "Commit Parent1 Parent2 ...\n" */
Patryk Obara9a934032017-08-18 20:33:12 +0200199 struct commit_graft *graft = read_graft_line(&buf);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700200 if (!graft)
201 continue;
Brandon Williamsd0e5dd02018-05-17 15:51:49 -0700202 if (register_commit_graft(r, graft, 1))
Johannes Schindeline228c172013-12-27 21:49:57 +0100203 error("duplicate graft data: %s", buf.buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700204 }
205 fclose(fp);
Johannes Schindeline228c172013-12-27 21:49:57 +0100206 strbuf_release(&buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700207 return 0;
208}
209
Stefan Beller2f6c7672018-05-17 15:51:53 -0700210static void prepare_commit_graft(struct repository *r)
Junio C Hamano5040f172006-04-06 23:58:51 -0700211{
Junio C Hamano5040f172006-04-06 23:58:51 -0700212 char *graft_file;
213
Stefan Beller2f6c7672018-05-17 15:51:53 -0700214 if (r->parsed_objects->commit_graft_prepared)
Junio C Hamano5040f172006-04-06 23:58:51 -0700215 return;
Jeff King14a9bd22018-05-31 18:42:53 -0400216 if (!startup_info->have_repository)
217 return;
218
Stefan Beller2f6c7672018-05-17 15:51:53 -0700219 graft_file = get_graft_file(r);
220 read_graft_file(r, graft_file);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100221 /* make sure shallows are read */
Stefan Beller2f6c7672018-05-17 15:51:53 -0700222 is_repository_shallow(r);
223 r->parsed_objects->commit_graft_prepared = 1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700224}
225
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700226struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700227{
228 int pos;
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700229 prepare_commit_graft(r);
230 pos = commit_graft_pos(r, oid->hash);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700231 if (pos < 0)
232 return NULL;
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700233 return r->parsed_objects->grafts[pos];
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700234}
235
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700236int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100237{
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700238 int i, ret;
Jonathan Nieder6a1a79f2018-05-15 16:42:16 -0700239 for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++)
240 ret = fn(the_repository->parsed_objects->grafts[i], cb_data);
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700241 return ret;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100242}
243
brian m. carlsone92b8482017-05-06 22:10:06 +0000244int unregister_shallow(const struct object_id *oid)
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100245{
Jonathan Niederbe479e82018-05-15 16:42:17 -0700246 int pos = commit_graft_pos(the_repository, oid->hash);
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100247 if (pos < 0)
248 return -1;
Jonathan Nieder6a1a79f2018-05-15 16:42:16 -0700249 if (pos + 1 < the_repository->parsed_objects->grafts_nr)
250 MOVE_ARRAY(the_repository->parsed_objects->grafts + pos,
251 the_repository->parsed_objects->grafts + pos + 1,
252 the_repository->parsed_objects->grafts_nr - pos - 1);
253 the_repository->parsed_objects->grafts_nr--;
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100254 return 0;
255}
256
Jeff King8597ea32014-06-10 17:44:13 -0400257struct commit_buffer {
258 void *buffer;
259 unsigned long size;
260};
261define_commit_slab(buffer_slab, struct commit_buffer);
Jeff Kingc1b3c712014-06-10 17:43:02 -0400262static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
263
Jeff King8597ea32014-06-10 17:44:13 -0400264void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
Jeff King66c28272014-06-10 17:40:14 -0400265{
Jeff King8597ea32014-06-10 17:44:13 -0400266 struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
267 v->buffer = buffer;
268 v->size = size;
Jeff King66c28272014-06-10 17:40:14 -0400269}
270
Jeff King8597ea32014-06-10 17:44:13 -0400271const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
Jeff King152ff1c2014-06-10 17:40:39 -0400272{
Junio C Hamano862e7302015-05-14 15:25:52 -0700273 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
274 if (!v) {
275 if (sizep)
276 *sizep = 0;
277 return NULL;
278 }
Jeff King8597ea32014-06-10 17:44:13 -0400279 if (sizep)
280 *sizep = v->size;
281 return v->buffer;
Jeff King152ff1c2014-06-10 17:40:39 -0400282}
283
Jeff King8597ea32014-06-10 17:44:13 -0400284const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
Jeff King152ff1c2014-06-10 17:40:39 -0400285{
Jeff King8597ea32014-06-10 17:44:13 -0400286 const void *ret = get_cached_commit_buffer(commit, sizep);
Jeff King152ff1c2014-06-10 17:40:39 -0400287 if (!ret) {
288 enum object_type type;
289 unsigned long size;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000290 ret = read_object_file(&commit->object.oid, &type, &size);
Jeff King152ff1c2014-06-10 17:40:39 -0400291 if (!ret)
292 die("cannot read commit object %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000293 oid_to_hex(&commit->object.oid));
Jeff King152ff1c2014-06-10 17:40:39 -0400294 if (type != OBJ_COMMIT)
295 die("expected commit for %s, got %s",
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800296 oid_to_hex(&commit->object.oid), type_name(type));
Jeff King8597ea32014-06-10 17:44:13 -0400297 if (sizep)
298 *sizep = size;
Jeff King152ff1c2014-06-10 17:40:39 -0400299 }
300 return ret;
301}
302
303void unuse_commit_buffer(const struct commit *commit, const void *buffer)
304{
Junio C Hamano862e7302015-05-14 15:25:52 -0700305 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
306 if (!(v && v->buffer == buffer))
Jeff King152ff1c2014-06-10 17:40:39 -0400307 free((void *)buffer);
308}
309
Jeff King0fb370d2014-06-12 18:05:37 -0400310void free_commit_buffer(struct commit *commit)
311{
Junio C Hamano862e7302015-05-14 15:25:52 -0700312 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
313 if (v) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000314 FREE_AND_NULL(v->buffer);
Junio C Hamano862e7302015-05-14 15:25:52 -0700315 v->size = 0;
316 }
Jeff King0fb370d2014-06-12 18:05:37 -0400317}
318
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000319struct tree *get_commit_tree(const struct commit *commit)
320{
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000321 if (commit->maybe_tree || !commit->object.parsed)
322 return commit->maybe_tree;
323
324 if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
325 BUG("commit has NULL tree, but was not loaded from commit-graph");
326
327 return get_commit_tree_in_graph(commit);
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000328}
329
330struct object_id *get_commit_tree_oid(const struct commit *commit)
331{
332 return &get_commit_tree(commit)->object.oid;
333}
334
Stefan Beller14ba97f2018-05-15 14:48:42 -0700335void release_commit_memory(struct commit *c)
336{
Junio C Hamano11024052018-06-25 13:22:38 -0700337 c->maybe_tree = NULL;
Stefan Beller14ba97f2018-05-15 14:48:42 -0700338 c->index = 0;
339 free_commit_buffer(c);
340 free_commit_list(c->parents);
341 /* TODO: what about commit->util? */
342
343 c->object.parsed = 0;
344}
345
Jeff King8597ea32014-06-10 17:44:13 -0400346const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
Jeff King0fb370d2014-06-12 18:05:37 -0400347{
Junio C Hamano862e7302015-05-14 15:25:52 -0700348 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
Jeff King8597ea32014-06-10 17:44:13 -0400349 void *ret;
350
Junio C Hamano862e7302015-05-14 15:25:52 -0700351 if (!v) {
352 if (sizep)
353 *sizep = 0;
354 return NULL;
355 }
Jeff King8597ea32014-06-10 17:44:13 -0400356 ret = v->buffer;
357 if (sizep)
358 *sizep = v->size;
359
360 v->buffer = NULL;
361 v->size = 0;
Jeff King0fb370d2014-06-12 18:05:37 -0400362 return ret;
363}
364
Derrick Stoleee2838d82018-05-01 12:47:13 +0000365int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size, int check_graph)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700366{
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700367 const char *tail = buffer;
368 const char *bufptr = buffer;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000369 struct object_id parent;
Linus Torvalds6c88be12005-06-20 20:26:03 -0700370 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700371 struct commit_graft *graft;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000372 const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
373 const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400374
Daniel Barkalow175785e2005-04-18 11:39:48 -0700375 if (item->object.parsed)
376 return 0;
377 item->object.parsed = 1;
Junio C Hamano3b44f152006-06-28 03:51:00 -0700378 tail += size;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000379 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
380 bufptr[tree_entry_len] != '\n')
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000381 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
brian m. carlson26ea3e72018-05-02 00:25:47 +0000382 if (get_oid_hex(bufptr + 5, &parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800383 return error("bad tree pointer in commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000384 oid_to_hex(&item->object.oid));
Derrick Stolee891435d2018-04-06 19:09:32 +0000385 item->maybe_tree = lookup_tree(&parent);
brian m. carlson7683e2e2015-03-13 23:39:34 +0000386 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700387 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700388
Jonathan Nieder1f93ecd2018-05-17 15:51:42 -0700389 graft = lookup_commit_graft(the_repository, &item->object.oid);
brian m. carlson7683e2e2015-03-13 23:39:34 +0000390 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700391 struct commit *new_parent;
392
brian m. carlson7683e2e2015-03-13 23:39:34 +0000393 if (tail <= bufptr + parent_entry_len + 1 ||
brian m. carlson26ea3e72018-05-02 00:25:47 +0000394 get_oid_hex(bufptr + 7, &parent) ||
brian m. carlson7683e2e2015-03-13 23:39:34 +0000395 bufptr[parent_entry_len] != '\n')
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000396 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
brian m. carlson7683e2e2015-03-13 23:39:34 +0000397 bufptr += parent_entry_len + 1;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +0200398 /*
399 * The clone is shallow if nr_parent < 0, and we must
400 * not traverse its real parents even when we unhide them.
401 */
402 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700403 continue;
brian m. carlsonbc832662017-05-06 22:10:10 +0000404 new_parent = lookup_commit(&parent);
Miklos Vajna39468de2008-06-07 22:38:37 +0200405 if (new_parent)
Linus Torvalds6c88be12005-06-20 20:26:03 -0700406 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700407 }
408 if (graft) {
409 int i;
410 struct commit *new_parent;
411 for (i = 0; i < graft->nr_parent; i++) {
brian m. carlsonbc832662017-05-06 22:10:10 +0000412 new_parent = lookup_commit(&graft->parent[i]);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700413 if (!new_parent)
414 continue;
415 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700416 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700417 }
Martin Koegler0a617792008-01-19 18:35:23 +0100418 item->date = parse_commit_date(bufptr, tail);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800419
Derrick Stoleee2838d82018-05-01 12:47:13 +0000420 if (check_graph)
421 load_commit_graph_info(item);
422
Daniel Barkalow175785e2005-04-18 11:39:48 -0700423 return 0;
424}
425
Jeff King9cc2b072015-06-01 05:56:26 -0400426int parse_commit_gently(struct commit *item, int quiet_on_missing)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400427{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500428 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400429 void *buffer;
430 unsigned long size;
431 int ret;
432
Martin Koegler9786f682008-02-18 21:48:02 +0100433 if (!item)
434 return -1;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400435 if (item->object.parsed)
436 return 0;
Derrick Stolee177722b2018-04-10 08:56:05 -0400437 if (parse_commit_in_graph(item))
438 return 0;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000439 buffer = read_object_file(&item->object.oid, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400440 if (!buffer)
Jeff King9cc2b072015-06-01 05:56:26 -0400441 return quiet_on_missing ? -1 :
442 error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000443 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500444 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400445 free(buffer);
446 return error("Object %s not a commit",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000447 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400448 }
Derrick Stoleee2838d82018-05-01 12:47:13 +0000449 ret = parse_commit_buffer(item, buffer, size, 0);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700450 if (save_commit_buffer && !ret) {
Jeff King8597ea32014-06-10 17:44:13 -0400451 set_commit_buffer(item, buffer, size);
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700452 return 0;
453 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400454 free(buffer);
455 return ret;
456}
457
Jeff King7059dcc2013-10-24 04:52:36 -0400458void parse_commit_or_die(struct commit *item)
459{
460 if (parse_commit(item))
461 die("unable to parse commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000462 item ? oid_to_hex(&item->object.oid) : "(null)");
Jeff King7059dcc2013-10-24 04:52:36 -0400463}
464
Christian Couder11af2aa2010-07-22 15:18:30 +0200465int find_commit_subject(const char *commit_buffer, const char **subject)
466{
467 const char *eol;
468 const char *p = commit_buffer;
469
470 while (*p && (*p != '\n' || p[1] != '\n'))
471 p++;
472 if (*p) {
Johannes Schindelin4e1b06d2016-06-22 22:20:20 +0200473 p = skip_blank_lines(p + 2);
Junio C Hamano9c9b03b2017-01-27 18:01:41 -0800474 eol = strchrnul(p, '\n');
Christian Couder11af2aa2010-07-22 15:18:30 +0200475 } else
476 eol = p;
477
478 *subject = p;
479
480 return eol - p;
481}
482
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700483struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700484{
Christopher Li812666c2005-04-26 12:00:58 -0700485 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700486 new_list->item = item;
487 new_list->next = *list_p;
488 *list_p = new_list;
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700489 return new_list;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700490}
491
Miklos Vajna65319472008-06-27 18:21:55 +0200492unsigned commit_list_count(const struct commit_list *l)
493{
494 unsigned c = 0;
495 for (; l; l = l->next )
496 c++;
497 return c;
498}
499
Thomas Rast53d00b32013-07-31 22:13:20 +0200500struct commit_list *copy_commit_list(struct commit_list *list)
501{
502 struct commit_list *head = NULL;
503 struct commit_list **pp = &head;
504 while (list) {
René Scharfecb979db2014-07-10 11:47:47 +0200505 pp = commit_list_append(list->item, pp);
Thomas Rast53d00b32013-07-31 22:13:20 +0200506 list = list->next;
507 }
508 return head;
509}
510
Daniel Barkalow175785e2005-04-18 11:39:48 -0700511void free_commit_list(struct commit_list *list)
512{
René Scharfee510ab82015-10-24 18:21:31 +0200513 while (list)
514 pop_commit(&list);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700515}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700516
Thiago Farina47e44ed2010-11-26 23:58:14 -0200517struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700518{
519 struct commit_list **pp = list;
520 struct commit_list *p;
521 while ((p = *pp) != NULL) {
522 if (p->item->date < item->date) {
523 break;
524 }
525 pp = &p->next;
526 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700527 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700528}
529
René Scharfe46905892012-04-01 00:10:39 +0200530static int commit_list_compare_by_date(const void *a, const void *b)
531{
Johannes Schindelindddbad72017-04-26 21:29:31 +0200532 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
533 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
René Scharfe46905892012-04-01 00:10:39 +0200534 if (a_date < b_date)
535 return 1;
536 if (a_date > b_date)
537 return -1;
538 return 0;
539}
540
541static void *commit_list_get_next(const void *a)
542{
543 return ((const struct commit_list *)a)->next;
544}
545
546static void commit_list_set_next(void *a, void *next)
547{
548 ((struct commit_list *)a)->next = next;
549}
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700550
Thiago Farina47e44ed2010-11-26 23:58:14 -0200551void commit_list_sort_by_date(struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700552{
Junio C Hamano7365c952012-04-17 11:07:01 -0700553 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
554 commit_list_compare_by_date);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700555}
556
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700557struct commit *pop_most_recent_commit(struct commit_list **list,
558 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700559{
René Scharfee510ab82015-10-24 18:21:31 +0200560 struct commit *ret = pop_commit(list);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700561 struct commit_list *parents = ret->parents;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700562
563 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700564 struct commit *commit = parents->item;
Martin Koeglerdec38c82008-02-18 21:48:03 +0100565 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700566 commit->object.flags |= mark;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200567 commit_list_insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700568 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700569 parents = parents->next;
570 }
571 return ret;
572}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700573
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700574static void clear_commit_marks_1(struct commit_list **plist,
575 struct commit *commit, unsigned int mark)
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800576{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100577 while (commit) {
578 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800579
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100580 if (!(mark & commit->object.flags))
581 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700582
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100583 commit->object.flags &= ~mark;
584
585 parents = commit->parents;
586 if (!parents)
587 return;
588
589 while ((parents = parents->next))
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700590 commit_list_insert(parents->item, plist);
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100591
592 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800593 }
594}
595
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800596void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700597{
598 struct commit_list *list = NULL;
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800599
600 while (nr--) {
René Scharfe07f7d552017-12-25 18:43:37 +0100601 clear_commit_marks_1(&list, *commit, mark);
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800602 commit++;
603 }
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700604 while (list)
605 clear_commit_marks_1(&list, pop_commit(&list), mark);
606}
607
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800608void clear_commit_marks(struct commit *commit, unsigned int mark)
609{
610 clear_commit_marks_many(1, &commit, mark);
611}
612
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000613struct commit *pop_commit(struct commit_list **stack)
614{
615 struct commit_list *top = *stack;
616 struct commit *item = top ? top->item : NULL;
617
618 if (top) {
619 *stack = top->next;
620 free(top);
621 }
622 return item;
623}
624
Jon Seymourab580ac2005-07-07 02:39:34 +1000625/*
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700626 * Topological sort support
627 */
Junio C Hamano66eb3752013-04-12 23:25:49 -0700628
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700629/* count number of children that have not been emitted */
630define_commit_slab(indegree_slab, int);
Jeff King96c4f4a2013-04-09 02:52:56 -0400631
Junio C Hamano81c6b382013-06-07 10:35:54 -0700632/* record author-date for each commit object */
633define_commit_slab(author_date_slab, unsigned long);
634
635static void record_author_date(struct author_date_slab *author_date,
636 struct commit *commit)
637{
Jeff King8597ea32014-06-10 17:44:13 -0400638 const char *buffer = get_commit_buffer(commit, NULL);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700639 struct ident_split ident;
Jeff Kingea5517f2014-08-27 03:56:55 -0400640 const char *ident_line;
641 size_t ident_len;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700642 char *date_end;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200643 timestamp_t date;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700644
Jeff Kingea5517f2014-08-27 03:56:55 -0400645 ident_line = find_commit_header(buffer, "author", &ident_len);
646 if (!ident_line)
647 goto fail_exit; /* no author line */
648 if (split_ident_line(&ident, ident_line, ident_len) ||
649 !ident.date_begin || !ident.date_end)
650 goto fail_exit; /* malformed "author" line */
Junio C Hamano81c6b382013-06-07 10:35:54 -0700651
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200652 date = parse_timestamp(ident.date_begin, &date_end, 10);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700653 if (date_end != ident.date_end)
654 goto fail_exit; /* malformed date */
655 *(author_date_slab_at(author_date, commit)) = date;
656
657fail_exit:
Jeff Kingba41c1c2014-06-10 17:41:02 -0400658 unuse_commit_buffer(commit, buffer);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700659}
660
661static int compare_commits_by_author_date(const void *a_, const void *b_,
662 void *cb_data)
663{
664 const struct commit *a = a_, *b = b_;
665 struct author_date_slab *author_date = cb_data;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200666 timestamp_t a_date = *(author_date_slab_at(author_date, a));
667 timestamp_t b_date = *(author_date_slab_at(author_date, b));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700668
669 /* newer commits with larger date first */
670 if (a_date < b_date)
671 return 1;
672 else if (a_date > b_date)
673 return -1;
674 return 0;
675}
676
Derrick Stolee3afc6792018-05-01 12:47:11 +0000677int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused)
678{
679 const struct commit *a = a_, *b = b_;
680
681 /* newer commits first */
682 if (a->generation < b->generation)
683 return 1;
684 else if (a->generation > b->generation)
685 return -1;
686
687 /* use date as a heuristic when generations are equal */
688 if (a->date < b->date)
689 return 1;
690 else if (a->date > b->date)
691 return -1;
692 return 0;
693}
694
Jeff King727377f2013-07-02 02:21:48 -0400695int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700696{
697 const struct commit *a = a_, *b = b_;
698 /* newer commits with larger date first */
699 if (a->date < b->date)
700 return 1;
701 else if (a->date > b->date)
702 return -1;
703 return 0;
704}
705
Jon Seymourab580ac2005-07-07 02:39:34 +1000706/*
707 * Performs an in-place topological sort on the list supplied.
708 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700709void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
Jon Seymourab580ac2005-07-07 02:39:34 +1000710{
Linus Torvalds23c17d42007-11-02 13:32:58 -0700711 struct commit_list *next, *orig = *list;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700712 struct commit_list **pptr;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700713 struct indegree_slab indegree;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700714 struct prio_queue queue;
715 struct commit *commit;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700716 struct author_date_slab author_date;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100717
Linus Torvalds23c17d42007-11-02 13:32:58 -0700718 if (!orig)
Eric Wong7d6fb372005-12-24 04:12:43 -0800719 return;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700720 *list = NULL;
721
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700722 init_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700723 memset(&queue, '\0', sizeof(queue));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700724
Junio C Hamanoda24b102013-06-06 21:58:12 -0700725 switch (sort_order) {
726 default: /* REV_SORT_IN_GRAPH_ORDER */
727 queue.compare = NULL;
728 break;
729 case REV_SORT_BY_COMMIT_DATE:
730 queue.compare = compare_commits_by_commit_date;
731 break;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700732 case REV_SORT_BY_AUTHOR_DATE:
733 init_author_date_slab(&author_date);
734 queue.compare = compare_commits_by_author_date;
735 queue.cb_data = &author_date;
736 break;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700737 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400738
Linus Torvalds23c17d42007-11-02 13:32:58 -0700739 /* Mark them and clear the indegree */
740 for (next = orig; next; next = next->next) {
741 struct commit *commit = next->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700742 *(indegree_slab_at(&indegree, commit)) = 1;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700743 /* also record the author dates, if needed */
744 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
745 record_author_date(&author_date, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000746 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700747
Jon Seymourab580ac2005-07-07 02:39:34 +1000748 /* update the indegree */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700749 for (next = orig; next; next = next->next) {
Junio C Hamanoda24b102013-06-06 21:58:12 -0700750 struct commit_list *parents = next->item->parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000751 while (parents) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700752 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700753 int *pi = indegree_slab_at(&indegree, parent);
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100754
Jeff King96c4f4a2013-04-09 02:52:56 -0400755 if (*pi)
756 (*pi)++;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700757 parents = parents->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000758 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000759 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700760
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700761 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200762 * find the tips
763 *
764 * tips are nodes not reachable from any other node in the list
765 *
766 * the tips serve as a starting set for the work queue.
767 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700768 for (next = orig; next; next = next->next) {
769 struct commit *commit = next->item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000770
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700771 if (*(indegree_slab_at(&indegree, commit)) == 1)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700772 prio_queue_put(&queue, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000773 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800774
Junio C Hamanoda24b102013-06-06 21:58:12 -0700775 /*
776 * This is unfortunate; the initial tips need to be shown
777 * in the order given from the revision traversal machinery.
778 */
779 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
780 prio_queue_reverse(&queue);
781
782 /* We no longer need the commit list */
783 free_commit_list(orig);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700784
785 pptr = list;
786 *list = NULL;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700787 while ((commit = prio_queue_get(&queue)) != NULL) {
788 struct commit_list *parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000789
Linus Torvalds23c17d42007-11-02 13:32:58 -0700790 for (parents = commit->parents; parents ; parents = parents->next) {
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -0700791 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700792 int *pi = indegree_slab_at(&indegree, parent);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700793
Jeff King96c4f4a2013-04-09 02:52:56 -0400794 if (!*pi)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700795 continue;
796
797 /*
798 * parents are only enqueued for emission
799 * when all their children have been emitted thereby
800 * guaranteeing topological order.
801 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700802 if (--(*pi) == 1)
803 prio_queue_put(&queue, parent);
Jon Seymourab580ac2005-07-07 02:39:34 +1000804 }
805 /*
Junio C Hamanoda24b102013-06-06 21:58:12 -0700806 * all children of commit have already been
807 * emitted. we can emit it now.
Pierre Habouzit674d1722007-09-10 12:35:06 +0200808 */
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700809 *(indegree_slab_at(&indegree, commit)) = 0;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700810
811 pptr = &commit_list_insert(commit, pptr)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000812 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400813
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700814 clear_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700815 clear_prio_queue(&queue);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700816 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
817 clear_author_date_slab(&author_date);
Jon Seymourab580ac2005-07-07 02:39:34 +1000818}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200819
Junio C Hamano12952282007-01-08 23:10:49 -0800820/* merge-base stuff */
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200821
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +0700822/* Remember to update object flag allocation in object.h */
Junio C Hamano577ed5c2006-10-22 17:32:47 -0700823#define PARENT1 (1u<<16)
824#define PARENT2 (1u<<17)
825#define STALE (1u<<18)
826#define RESULT (1u<<19)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200827
Junio C Hamano12952282007-01-08 23:10:49 -0800828static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
829
Jeff King73f43f22014-07-14 01:53:54 -0400830static int queue_has_nonstale(struct prio_queue *queue)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200831{
Jeff King73f43f22014-07-14 01:53:54 -0400832 int i;
833 for (i = 0; i < queue->nr; i++) {
834 struct commit *commit = queue->array[i].data;
835 if (!(commit->object.flags & STALE))
836 return 1;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200837 }
Jeff King73f43f22014-07-14 01:53:54 -0400838 return 0;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200839}
840
Junio C Hamanod8669242012-10-04 15:37:15 -0700841/* all input commits in one and twos[] must have been parsed! */
Derrick Stoleed7c1ec32018-05-01 12:47:19 +0000842static struct commit_list *paint_down_to_common(struct commit *one, int n,
843 struct commit **twos,
844 int min_generation)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200845{
Derrick Stolee3afc6792018-05-01 12:47:11 +0000846 struct prio_queue queue = { compare_commits_by_gen_then_commit_date };
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200847 struct commit_list *result = NULL;
Junio C Hamano6a938642008-06-27 18:22:02 +0200848 int i;
Derrick Stoleed7c1ec32018-05-01 12:47:19 +0000849 uint32_t last_gen = GENERATION_NUMBER_INFINITY;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200850
Junio C Hamanof3249432006-07-04 18:46:42 -0700851 one->object.flags |= PARENT1;
Jeff King73f43f22014-07-14 01:53:54 -0400852 if (!n) {
853 commit_list_append(one, &result);
854 return result;
855 }
856 prio_queue_put(&queue, one);
857
Junio C Hamano6a938642008-06-27 18:22:02 +0200858 for (i = 0; i < n; i++) {
859 twos[i]->object.flags |= PARENT2;
Jeff King73f43f22014-07-14 01:53:54 -0400860 prio_queue_put(&queue, twos[i]);
Junio C Hamano6a938642008-06-27 18:22:02 +0200861 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200862
Jeff King73f43f22014-07-14 01:53:54 -0400863 while (queue_has_nonstale(&queue)) {
864 struct commit *commit = prio_queue_get(&queue);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200865 struct commit_list *parents;
Junio C Hamanof3249432006-07-04 18:46:42 -0700866 int flags;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200867
Derrick Stoleed7c1ec32018-05-01 12:47:19 +0000868 if (commit->generation > last_gen)
869 BUG("bad generation skip %8x > %8x at %s",
870 commit->generation, last_gen,
871 oid_to_hex(&commit->object.oid));
872 last_gen = commit->generation;
873
874 if (commit->generation < min_generation)
875 break;
876
Junio C Hamanof3249432006-07-04 18:46:42 -0700877 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200878 if (flags == (PARENT1 | PARENT2)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700879 if (!(commit->object.flags & RESULT)) {
880 commit->object.flags |= RESULT;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200881 commit_list_insert_by_date(commit, &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700882 }
Junio C Hamano542ccef2006-07-02 11:34:17 -0700883 /* Mark parents of a found merge stale */
884 flags |= STALE;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200885 }
886 parents = commit->parents;
887 while (parents) {
888 struct commit *p = parents->item;
889 parents = parents->next;
890 if ((p->object.flags & flags) == flags)
891 continue;
Martin Koegler172947e2008-02-18 21:47:57 +0100892 if (parse_commit(p))
893 return NULL;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200894 p->object.flags |= flags;
Jeff King73f43f22014-07-14 01:53:54 -0400895 prio_queue_put(&queue, p);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200896 }
897 }
898
Jeff King73f43f22014-07-14 01:53:54 -0400899 clear_prio_queue(&queue);
Junio C Hamanoda1f5152012-08-30 14:39:03 -0700900 return result;
901}
902
903static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
904{
905 struct commit_list *list = NULL;
906 struct commit_list *result = NULL;
907 int i;
908
909 for (i = 0; i < n; i++) {
910 if (one == twos[i])
911 /*
912 * We do not mark this even with RESULT so we do not
913 * have to clean it up.
914 */
915 return commit_list_insert(one, &result);
916 }
917
918 if (parse_commit(one))
919 return NULL;
920 for (i = 0; i < n; i++) {
921 if (parse_commit(twos[i]))
922 return NULL;
923 }
924
Derrick Stoleed7c1ec32018-05-01 12:47:19 +0000925 list = paint_down_to_common(one, n, twos, 0);
Junio C Hamanoda1f5152012-08-30 14:39:03 -0700926
Junio C Hamanof3249432006-07-04 18:46:42 -0700927 while (list) {
René Scharfee510ab82015-10-24 18:21:31 +0200928 struct commit *commit = pop_commit(&list);
929 if (!(commit->object.flags & STALE))
930 commit_list_insert_by_date(commit, &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700931 }
932 return result;
933}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200934
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200935struct commit_list *get_octopus_merge_bases(struct commit_list *in)
936{
937 struct commit_list *i, *j, *k, *ret = NULL;
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200938
Vasily Makarov6bc76722014-01-03 18:45:46 +0400939 if (!in)
940 return ret;
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200941
Vasily Makarov6bc76722014-01-03 18:45:46 +0400942 commit_list_insert(in->item, &ret);
943
944 for (i = in->next; i; i = i->next) {
Brandon Williams258c03e2018-02-14 10:59:37 -0800945 struct commit_list *new_commits = NULL, *end = NULL;
Vasily Makarov6bc76722014-01-03 18:45:46 +0400946
947 for (j = ret; j; j = j->next) {
948 struct commit_list *bases;
Junio C Hamano2ce406c2014-10-30 12:20:44 -0700949 bases = get_merge_bases(i->item, j->item);
Brandon Williams258c03e2018-02-14 10:59:37 -0800950 if (!new_commits)
951 new_commits = bases;
Vasily Makarov6bc76722014-01-03 18:45:46 +0400952 else
953 end->next = bases;
954 for (k = bases; k; k = k->next)
955 end = k;
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200956 }
Brandon Williams258c03e2018-02-14 10:59:37 -0800957 ret = new_commits;
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200958 }
959 return ret;
960}
961
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700962static int remove_redundant(struct commit **array, int cnt)
963{
964 /*
965 * Some commit in the array may be an ancestor of
966 * another commit. Move such commit to the end of
967 * the array, and return the number of commits that
968 * are independent from each other.
969 */
970 struct commit **work;
971 unsigned char *redundant;
972 int *filled_index;
973 int i, j, filled;
974
975 work = xcalloc(cnt, sizeof(*work));
976 redundant = xcalloc(cnt, 1);
Jeff Kingb32fa952016-02-22 17:44:25 -0500977 ALLOC_ARRAY(filled_index, cnt - 1);
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700978
Junio C Hamanod8669242012-10-04 15:37:15 -0700979 for (i = 0; i < cnt; i++)
980 parse_commit(array[i]);
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700981 for (i = 0; i < cnt; i++) {
982 struct commit_list *common;
Derrick Stolee04bc8d12018-05-01 12:47:21 +0000983 uint32_t min_generation = array[i]->generation;
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700984
985 if (redundant[i])
986 continue;
987 for (j = filled = 0; j < cnt; j++) {
988 if (i == j || redundant[j])
989 continue;
990 filled_index[filled] = j;
991 work[filled++] = array[j];
Derrick Stolee04bc8d12018-05-01 12:47:21 +0000992
993 if (array[j]->generation < min_generation)
994 min_generation = array[j]->generation;
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700995 }
Derrick Stolee04bc8d12018-05-01 12:47:21 +0000996 common = paint_down_to_common(array[i], filled, work,
997 min_generation);
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700998 if (array[i]->object.flags & PARENT2)
999 redundant[i] = 1;
1000 for (j = 0; j < filled; j++)
1001 if (work[j]->object.flags & PARENT1)
1002 redundant[filled_index[j]] = 1;
1003 clear_commit_marks(array[i], all_flags);
René Scharfeabc03512017-12-25 18:44:03 +01001004 clear_commit_marks_many(filled, work, all_flags);
Junio C Hamano94f0ced2012-08-30 15:35:39 -07001005 free_commit_list(common);
1006 }
1007
1008 /* Now collect the result */
René Scharfe45ccef82016-09-25 09:24:03 +02001009 COPY_ARRAY(work, array, cnt);
Junio C Hamano94f0ced2012-08-30 15:35:39 -07001010 for (i = filled = 0; i < cnt; i++)
1011 if (!redundant[i])
1012 array[filled++] = work[i];
1013 for (j = filled, i = 0; i < cnt; i++)
1014 if (redundant[i])
1015 array[j++] = work[i];
1016 free(work);
1017 free(redundant);
1018 free(filled_index);
1019 return filled;
1020}
1021
Junio C Hamano2ce406c2014-10-30 12:20:44 -07001022static struct commit_list *get_merge_bases_many_0(struct commit *one,
1023 int n,
1024 struct commit **twos,
1025 int cleanup)
Junio C Hamanof3249432006-07-04 18:46:42 -07001026{
Junio C Hamanof3249432006-07-04 18:46:42 -07001027 struct commit_list *list;
1028 struct commit **rslt;
1029 struct commit_list *result;
Junio C Hamano94f0ced2012-08-30 15:35:39 -07001030 int cnt, i;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +02001031
Junio C Hamano6a938642008-06-27 18:22:02 +02001032 result = merge_bases_many(one, n, twos);
1033 for (i = 0; i < n; i++) {
1034 if (one == twos[i])
1035 return result;
1036 }
Junio C Hamanof3249432006-07-04 18:46:42 -07001037 if (!result || !result->next) {
1038 if (cleanup) {
1039 clear_commit_marks(one, all_flags);
Junio C Hamanoe895cb52013-03-05 11:42:20 -08001040 clear_commit_marks_many(n, twos, all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +02001041 }
Junio C Hamanof3249432006-07-04 18:46:42 -07001042 return result;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +02001043 }
1044
Junio C Hamanof3249432006-07-04 18:46:42 -07001045 /* There are more than one */
René Scharfe4bbaa1e2014-07-17 01:52:09 +02001046 cnt = commit_list_count(result);
Junio C Hamanof3249432006-07-04 18:46:42 -07001047 rslt = xcalloc(cnt, sizeof(*rslt));
1048 for (list = result, i = 0; list; list = list->next)
1049 rslt[i++] = list->item;
1050 free_commit_list(result);
1051
1052 clear_commit_marks(one, all_flags);
Junio C Hamanoe895cb52013-03-05 11:42:20 -08001053 clear_commit_marks_many(n, twos, all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +02001054
Junio C Hamano94f0ced2012-08-30 15:35:39 -07001055 cnt = remove_redundant(rslt, cnt);
Junio C Hamanof3249432006-07-04 18:46:42 -07001056 result = NULL;
Junio C Hamano94f0ced2012-08-30 15:35:39 -07001057 for (i = 0; i < cnt; i++)
1058 commit_list_insert_by_date(rslt[i], &result);
Junio C Hamanof3249432006-07-04 18:46:42 -07001059 free(rslt);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +02001060 return result;
1061}
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -08001062
Junio C Hamano2ce406c2014-10-30 12:20:44 -07001063struct commit_list *get_merge_bases_many(struct commit *one,
1064 int n,
1065 struct commit **twos)
Junio C Hamano6a938642008-06-27 18:22:02 +02001066{
Junio C Hamano2ce406c2014-10-30 12:20:44 -07001067 return get_merge_bases_many_0(one, n, twos, 1);
1068}
1069
1070struct commit_list *get_merge_bases_many_dirty(struct commit *one,
1071 int n,
1072 struct commit **twos)
1073{
1074 return get_merge_bases_many_0(one, n, twos, 0);
1075}
1076
1077struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
1078{
1079 return get_merge_bases_many_0(one, 1, &two, 1);
Junio C Hamano6a938642008-06-27 18:22:02 +02001080}
1081
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001082/*
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02001083 * Is "commit" a descendant of one of the elements on the "with_commit" list?
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001084 */
Jake Goulding7fcdb362009-01-26 09:13:24 -05001085int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
1086{
1087 if (!with_commit)
1088 return 1;
1089 while (with_commit) {
1090 struct commit *other;
1091
1092 other = with_commit->item;
1093 with_commit = with_commit->next;
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001094 if (in_merge_bases(other, commit))
Jake Goulding7fcdb362009-01-26 09:13:24 -05001095 return 1;
1096 }
1097 return 0;
1098}
1099
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001100/*
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001101 * Is "commit" an ancestor of one of the "references"?
1102 */
1103int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1104{
1105 struct commit_list *bases;
1106 int ret = 0, i;
Derrick Stoleef9b89082018-05-01 12:47:17 +00001107 uint32_t min_generation = GENERATION_NUMBER_INFINITY;
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001108
1109 if (parse_commit(commit))
1110 return ret;
Derrick Stoleef9b89082018-05-01 12:47:17 +00001111 for (i = 0; i < nr_reference; i++) {
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001112 if (parse_commit(reference[i]))
1113 return ret;
Derrick Stoleef9b89082018-05-01 12:47:17 +00001114 if (reference[i]->generation < min_generation)
1115 min_generation = reference[i]->generation;
1116 }
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001117
Derrick Stoleef9b89082018-05-01 12:47:17 +00001118 if (commit->generation > min_generation)
1119 return ret;
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001120
Derrick Stoleed7c1ec32018-05-01 12:47:19 +00001121 bases = paint_down_to_common(commit, nr_reference, reference, commit->generation);
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001122 if (commit->object.flags & PARENT2)
1123 ret = 1;
1124 clear_commit_marks(commit, all_flags);
Junio C Hamano557899f2013-03-05 11:56:03 -08001125 clear_commit_marks_many(nr_reference, reference, all_flags);
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001126 free_commit_list(bases);
1127 return ret;
1128}
1129
1130/*
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001131 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1132 */
1133int in_merge_bases(struct commit *commit, struct commit *reference)
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -08001134{
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001135 return in_merge_bases_many(commit, 1, &reference);
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -08001136}
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001137
1138struct commit_list *reduce_heads(struct commit_list *heads)
1139{
1140 struct commit_list *p;
1141 struct commit_list *result = NULL, **tail = &result;
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001142 struct commit **array;
1143 int num_head, i;
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001144
1145 if (!heads)
1146 return NULL;
1147
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001148 /* Uniquify */
1149 for (p = heads; p; p = p->next)
1150 p->item->object.flags &= ~STALE;
1151 for (p = heads, num_head = 0; p; p = p->next) {
1152 if (p->item->object.flags & STALE)
Junio C Hamano711f6b22008-07-14 00:09:41 -07001153 continue;
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001154 p->item->object.flags |= STALE;
1155 num_head++;
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001156 }
Brian Gesiakc4a7b002014-05-27 00:33:45 +09001157 array = xcalloc(num_head, sizeof(*array));
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001158 for (p = heads, i = 0; p; p = p->next) {
1159 if (p->item->object.flags & STALE) {
1160 array[i++] = p->item;
1161 p->item->object.flags &= ~STALE;
1162 }
1163 }
1164 num_head = remove_redundant(array, num_head);
1165 for (i = 0; i < num_head; i++)
1166 tail = &commit_list_insert(array[i], tail)->next;
Martin Ågrencb7b29e2017-09-23 01:34:50 +02001167 free(array);
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001168 return result;
1169}
Jeff King40d52ff2010-04-01 20:05:23 -04001170
Martin Ågren4da72642017-11-07 21:39:45 +01001171void reduce_heads_replace(struct commit_list **heads)
1172{
1173 struct commit_list *result = reduce_heads(*heads);
1174 free_commit_list(*heads);
1175 *heads = result;
1176}
1177
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001178static const char gpg_sig_header[] = "gpgsig";
1179static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1180
1181static int do_sign_commit(struct strbuf *buf, const char *keyid)
1182{
1183 struct strbuf sig = STRBUF_INIT;
1184 int inspos, copypos;
Johannes Schindelin3324dd82016-06-29 16:14:54 +02001185 const char *eoh;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001186
1187 /* find the end of the header */
Johannes Schindelin3324dd82016-06-29 16:14:54 +02001188 eoh = strstr(buf->buf, "\n\n");
1189 if (!eoh)
1190 inspos = buf->len;
1191 else
1192 inspos = eoh - buf->buf + 1;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001193
1194 if (!keyid || !*keyid)
1195 keyid = get_signing_key();
1196 if (sign_buffer(buf, &sig, keyid)) {
1197 strbuf_release(&sig);
1198 return -1;
1199 }
1200
1201 for (copypos = 0; sig.buf[copypos]; ) {
1202 const char *bol = sig.buf + copypos;
1203 const char *eol = strchrnul(bol, '\n');
1204 int len = (eol - bol) + !!*eol;
1205
1206 if (!copypos) {
1207 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1208 inspos += gpg_sig_header_len;
1209 }
1210 strbuf_insert(buf, inspos++, " ", 1);
1211 strbuf_insert(buf, inspos, bol, len);
1212 inspos += len;
1213 copypos += len;
1214 }
1215 strbuf_release(&sig);
1216 return 0;
1217}
1218
Jeff King218aa3a2014-06-13 02:32:11 -04001219int parse_signed_commit(const struct commit *commit,
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001220 struct strbuf *payload, struct strbuf *signature)
1221{
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001222
Jeff King218aa3a2014-06-13 02:32:11 -04001223 unsigned long size;
1224 const char *buffer = get_commit_buffer(commit, &size);
1225 int in_signature, saw_signature = -1;
1226 const char *line, *tail;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001227
1228 line = buffer;
1229 tail = buffer + size;
1230 in_signature = 0;
1231 saw_signature = 0;
1232 while (line < tail) {
1233 const char *sig = NULL;
Jeff King218aa3a2014-06-13 02:32:11 -04001234 const char *next = memchr(line, '\n', tail - line);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001235
1236 next = next ? next + 1 : tail;
1237 if (in_signature && line[0] == ' ')
1238 sig = line + 1;
Christian Couder59556542013-11-30 21:55:40 +01001239 else if (starts_with(line, gpg_sig_header) &&
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001240 line[gpg_sig_header_len] == ' ')
1241 sig = line + gpg_sig_header_len + 1;
1242 if (sig) {
1243 strbuf_add(signature, sig, next - sig);
1244 saw_signature = 1;
1245 in_signature = 1;
1246 } else {
1247 if (*line == '\n')
1248 /* dump the whole remainder of the buffer */
1249 next = tail;
1250 strbuf_add(payload, line, next - line);
1251 in_signature = 0;
1252 }
1253 line = next;
1254 }
Jeff King218aa3a2014-06-13 02:32:11 -04001255 unuse_commit_buffer(commit, buffer);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001256 return saw_signature;
1257}
1258
Christian Couder0b05ab62014-07-19 17:01:12 +02001259int remove_signature(struct strbuf *buf)
1260{
1261 const char *line = buf->buf;
1262 const char *tail = buf->buf + buf->len;
1263 int in_signature = 0;
1264 const char *sig_start = NULL;
1265 const char *sig_end = NULL;
1266
1267 while (line < tail) {
1268 const char *next = memchr(line, '\n', tail - line);
1269 next = next ? next + 1 : tail;
1270
1271 if (in_signature && line[0] == ' ')
1272 sig_end = next;
1273 else if (starts_with(line, gpg_sig_header) &&
1274 line[gpg_sig_header_len] == ' ') {
1275 sig_start = line;
1276 sig_end = next;
1277 in_signature = 1;
1278 } else {
1279 if (*line == '\n')
1280 /* dump the whole remainder of the buffer */
1281 next = tail;
1282 in_signature = 0;
1283 }
1284 line = next;
1285 }
1286
1287 if (sig_start)
1288 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1289
1290 return sig_start != NULL;
1291}
1292
Junio C Hamano5231c632011-11-07 16:21:32 -08001293static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1294{
1295 struct merge_remote_desc *desc;
1296 struct commit_extra_header *mergetag;
1297 char *buf;
1298 unsigned long size, len;
1299 enum object_type type;
1300
1301 desc = merge_remote_util(parent);
1302 if (!desc || !desc->obj)
1303 return;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001304 buf = read_object_file(&desc->obj->oid, &type, &size);
Junio C Hamano5231c632011-11-07 16:21:32 -08001305 if (!buf || type != OBJ_TAG)
1306 goto free_return;
1307 len = parse_signature(buf, size);
1308 if (size == len)
1309 goto free_return;
1310 /*
1311 * We could verify this signature and either omit the tag when
1312 * it does not validate, but the integrator may not have the
1313 * public key of the signer of the tag he is merging, while a
1314 * later auditor may have it while auditing, so let's not run
1315 * verify-signed-buffer here for now...
1316 *
1317 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1318 * warn("warning: signed tag unverified.");
1319 */
1320 mergetag = xcalloc(1, sizeof(*mergetag));
1321 mergetag->key = xstrdup("mergetag");
1322 mergetag->value = buf;
1323 mergetag->len = size;
1324
1325 **tail = mergetag;
1326 *tail = &mergetag->next;
1327 return;
1328
1329free_return:
1330 free(buf);
1331}
1332
brian m. carlson434060e2015-06-21 23:14:40 +00001333int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001334{
1335 struct strbuf payload = STRBUF_INIT;
1336 struct strbuf signature = STRBUF_INIT;
brian m. carlson434060e2015-06-21 23:14:40 +00001337 int ret = 1;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001338
1339 sigc->result = 'N';
1340
Jeff King218aa3a2014-06-13 02:32:11 -04001341 if (parse_signed_commit(commit, &payload, &signature) <= 0)
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001342 goto out;
brian m. carlson434060e2015-06-21 23:14:40 +00001343 ret = check_signature(payload.buf, payload.len, signature.buf,
1344 signature.len, sigc);
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001345
1346 out:
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001347 strbuf_release(&payload);
1348 strbuf_release(&signature);
brian m. carlson434060e2015-06-21 23:14:40 +00001349
1350 return ret;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001351}
1352
1353
1354
Junio C Hamano5231c632011-11-07 16:21:32 -08001355void append_merge_tag_headers(struct commit_list *parents,
1356 struct commit_extra_header ***tail)
1357{
1358 while (parents) {
1359 struct commit *parent = parents->item;
1360 handle_signed_tag(parent, tail);
1361 parents = parents->next;
1362 }
1363}
1364
1365static void add_extra_header(struct strbuf *buffer,
1366 struct commit_extra_header *extra)
1367{
1368 strbuf_addstr(buffer, extra->key);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001369 if (extra->len)
1370 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1371 else
1372 strbuf_addch(buffer, '\n');
1373}
1374
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001375struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1376 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001377{
1378 struct commit_extra_header *extra = NULL;
1379 unsigned long size;
Jeff King218aa3a2014-06-13 02:32:11 -04001380 const char *buffer = get_commit_buffer(commit, &size);
1381 extra = read_commit_extra_header_lines(buffer, size, exclude);
1382 unuse_commit_buffer(commit, buffer);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001383 return extra;
1384}
1385
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001386int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
Christian Couder063da622014-07-07 08:35:37 +02001387{
1388 struct commit_extra_header *extra, *to_free;
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001389 int res = 0;
Christian Couder063da622014-07-07 08:35:37 +02001390
1391 to_free = read_commit_extra_headers(commit, NULL);
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001392 for (extra = to_free; !res && extra; extra = extra->next) {
Christian Couder063da622014-07-07 08:35:37 +02001393 if (strcmp(extra->key, "mergetag"))
1394 continue; /* not a merge tag */
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001395 res = fn(commit, extra, data);
Christian Couder063da622014-07-07 08:35:37 +02001396 }
1397 free_commit_extra_headers(to_free);
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001398 return res;
Christian Couder063da622014-07-07 08:35:37 +02001399}
1400
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001401static inline int standard_header_field(const char *field, size_t len)
1402{
René Scharfeb0725042017-02-25 20:27:40 +01001403 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1404 (len == 6 && !memcmp(field, "parent", 6)) ||
1405 (len == 6 && !memcmp(field, "author", 6)) ||
1406 (len == 9 && !memcmp(field, "committer", 9)) ||
1407 (len == 8 && !memcmp(field, "encoding", 8)));
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001408}
1409
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001410static int excluded_header_field(const char *field, size_t len, const char **exclude)
1411{
1412 if (!exclude)
1413 return 0;
1414
1415 while (*exclude) {
1416 size_t xlen = strlen(*exclude);
René Scharfeb0725042017-02-25 20:27:40 +01001417 if (len == xlen && !memcmp(field, *exclude, xlen))
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001418 return 1;
1419 exclude++;
1420 }
1421 return 0;
1422}
1423
Junio C Hamano82a75292012-09-15 13:58:15 -07001424static struct commit_extra_header *read_commit_extra_header_lines(
1425 const char *buffer, size_t size,
1426 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001427{
1428 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1429 const char *line, *next, *eof, *eob;
1430 struct strbuf buf = STRBUF_INIT;
1431
1432 for (line = buffer, eob = line + size;
1433 line < eob && *line != '\n';
1434 line = next) {
1435 next = memchr(line, '\n', eob - line);
1436 next = next ? next + 1 : eob;
1437 if (*line == ' ') {
1438 /* continuation */
1439 if (it)
1440 strbuf_add(&buf, line + 1, next - (line + 1));
1441 continue;
1442 }
1443 if (it)
1444 it->value = strbuf_detach(&buf, &it->len);
1445 strbuf_reset(&buf);
1446 it = NULL;
1447
René Scharfe50a01cc2017-02-25 20:21:52 +01001448 eof = memchr(line, ' ', next - line);
1449 if (!eof)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001450 eof = next;
René Scharfeb0725042017-02-25 20:27:40 +01001451 else if (standard_header_field(line, eof - line) ||
1452 excluded_header_field(line, eof - line, exclude))
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001453 continue;
1454
1455 it = xcalloc(1, sizeof(*it));
1456 it->key = xmemdupz(line, eof-line);
1457 *tail = it;
1458 tail = &it->next;
1459 if (eof + 1 < next)
1460 strbuf_add(&buf, eof + 1, next - (eof + 1));
1461 }
1462 if (it)
1463 it->value = strbuf_detach(&buf, &it->len);
1464 return extra;
Junio C Hamano5231c632011-11-07 16:21:32 -08001465}
1466
1467void free_commit_extra_headers(struct commit_extra_header *extra)
1468{
1469 while (extra) {
1470 struct commit_extra_header *next = extra->next;
1471 free(extra->key);
1472 free(extra->value);
1473 free(extra);
1474 extra = next;
1475 }
1476}
1477
Patryk Obara5078f342018-01-28 01:13:16 +01001478int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1479 struct commit_list *parents, struct object_id *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001480 const char *author, const char *sign_commit)
Junio C Hamano5231c632011-11-07 16:21:32 -08001481{
1482 struct commit_extra_header *extra = NULL, **tail = &extra;
1483 int result;
1484
1485 append_merge_tag_headers(parents, &tail);
Jeff King3ffefb52014-06-10 17:36:52 -04001486 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001487 author, sign_commit, extra);
Junio C Hamano5231c632011-11-07 16:21:32 -08001488 free_commit_extra_headers(extra);
1489 return result;
1490}
1491
Linus Torvalds08a94a12012-06-28 11:24:14 -07001492static int find_invalid_utf8(const char *buf, int len)
1493{
1494 int offset = 0;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001495 static const unsigned int max_codepoint[] = {
1496 0x7f, 0x7ff, 0xffff, 0x10ffff
1497 };
Linus Torvalds08a94a12012-06-28 11:24:14 -07001498
1499 while (len) {
1500 unsigned char c = *buf++;
1501 int bytes, bad_offset;
brian m. carlson28110d42013-07-04 17:19:43 +00001502 unsigned int codepoint;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001503 unsigned int min_val, max_val;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001504
1505 len--;
1506 offset++;
1507
1508 /* Simple US-ASCII? No worries. */
1509 if (c < 0x80)
1510 continue;
1511
1512 bad_offset = offset-1;
1513
1514 /*
1515 * Count how many more high bits set: that's how
1516 * many more bytes this sequence should have.
1517 */
1518 bytes = 0;
1519 while (c & 0x40) {
1520 c <<= 1;
1521 bytes++;
1522 }
1523
brian m. carlson28110d42013-07-04 17:19:43 +00001524 /*
1525 * Must be between 1 and 3 more bytes. Longer sequences result in
1526 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1527 */
1528 if (bytes < 1 || 3 < bytes)
Linus Torvalds08a94a12012-06-28 11:24:14 -07001529 return bad_offset;
1530
1531 /* Do we *have* that many bytes? */
1532 if (len < bytes)
1533 return bad_offset;
1534
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001535 /*
1536 * Place the encoded bits at the bottom of the value and compute the
1537 * valid range.
1538 */
brian m. carlson28110d42013-07-04 17:19:43 +00001539 codepoint = (c & 0x7f) >> bytes;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001540 min_val = max_codepoint[bytes-1] + 1;
1541 max_val = max_codepoint[bytes];
brian m. carlson28110d42013-07-04 17:19:43 +00001542
Linus Torvalds08a94a12012-06-28 11:24:14 -07001543 offset += bytes;
1544 len -= bytes;
1545
1546 /* And verify that they are good continuation bytes */
1547 do {
brian m. carlson28110d42013-07-04 17:19:43 +00001548 codepoint <<= 6;
1549 codepoint |= *buf & 0x3f;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001550 if ((*buf++ & 0xc0) != 0x80)
1551 return bad_offset;
1552 } while (--bytes);
1553
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001554 /* Reject codepoints that are out of range for the sequence length. */
1555 if (codepoint < min_val || codepoint > max_val)
brian m. carlson28110d42013-07-04 17:19:43 +00001556 return bad_offset;
1557 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1558 if ((codepoint & 0x1ff800) == 0xd800)
1559 return bad_offset;
Peter Krefting81050ac2013-07-09 12:16:33 +01001560 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
Junio C Hamanodc773a62013-08-05 09:52:28 -07001561 if ((codepoint & 0xfffe) == 0xfffe)
Peter Krefting81050ac2013-07-09 12:16:33 +01001562 return bad_offset;
1563 /* So are anything in the range U+FDD0..U+FDEF. */
1564 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
brian m. carlson28110d42013-07-04 17:19:43 +00001565 return bad_offset;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001566 }
1567 return -1;
1568}
1569
1570/*
1571 * This verifies that the buffer is in proper utf8 format.
1572 *
1573 * If it isn't, it assumes any non-utf8 characters are Latin1,
1574 * and does the conversion.
Linus Torvalds08a94a12012-06-28 11:24:14 -07001575 */
1576static int verify_utf8(struct strbuf *buf)
1577{
1578 int ok = 1;
1579 long pos = 0;
1580
1581 for (;;) {
1582 int bad;
1583 unsigned char c;
1584 unsigned char replace[2];
1585
1586 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1587 if (bad < 0)
1588 return ok;
1589 pos += bad;
1590 ok = 0;
1591 c = buf->buf[pos];
1592 strbuf_remove(buf, pos, 1);
1593
1594 /* We know 'c' must be in the range 128-255 */
1595 replace[0] = 0xc0 + (c >> 6);
1596 replace[1] = 0x80 + (c & 0x3f);
1597 strbuf_insert(buf, pos, replace, 2);
1598 pos += 2;
1599 }
1600}
1601
Jeff King40d52ff2010-04-01 20:05:23 -04001602static const char commit_utf8_warn[] =
Vasco Almeida4fa4b312016-09-19 13:08:16 +00001603N_("Warning: commit message did not conform to UTF-8.\n"
1604 "You may want to amend it after fixing the message, or set the config\n"
1605 "variable i18n.commitencoding to the encoding your project uses.\n");
Jeff King40d52ff2010-04-01 20:05:23 -04001606
Jeff King3ffefb52014-06-10 17:36:52 -04001607int commit_tree_extended(const char *msg, size_t msg_len,
Patryk Obara5078f342018-01-28 01:13:16 +01001608 const struct object_id *tree,
1609 struct commit_list *parents, struct object_id *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001610 const char *author, const char *sign_commit,
1611 struct commit_extra_header *extra)
Jeff King40d52ff2010-04-01 20:05:23 -04001612{
1613 int result;
1614 int encoding_is_utf8;
1615 struct strbuf buffer;
1616
brian m. carlsone816caa2018-03-12 02:27:42 +00001617 assert_oid_type(tree, OBJ_TREE);
Jeff King40d52ff2010-04-01 20:05:23 -04001618
Jeff King3ffefb52014-06-10 17:36:52 -04001619 if (memchr(msg, '\0', msg_len))
Nguyễn Thái Ngọc Duy37576c12011-12-15 20:47:23 +07001620 return error("a NUL byte in commit log message not allowed.");
1621
Jeff King40d52ff2010-04-01 20:05:23 -04001622 /* Not having i18n.commitencoding is the same as having utf-8 */
1623 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1624
1625 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
Patryk Obara5078f342018-01-28 01:13:16 +01001626 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
Jeff King40d52ff2010-04-01 20:05:23 -04001627
1628 /*
1629 * NOTE! This ordering means that the same exact tree merged with a
1630 * different order of parents will be a _different_ changeset even
1631 * if everything else stays the same.
1632 */
1633 while (parents) {
René Scharfee510ab82015-10-24 18:21:31 +02001634 struct commit *parent = pop_commit(&parents);
Jeff King40d52ff2010-04-01 20:05:23 -04001635 strbuf_addf(&buffer, "parent %s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001636 oid_to_hex(&parent->object.oid));
Jeff King40d52ff2010-04-01 20:05:23 -04001637 }
1638
1639 /* Person/date information */
1640 if (!author)
Jeff Kingf9bc5732012-05-24 19:28:40 -04001641 author = git_author_info(IDENT_STRICT);
Jeff King40d52ff2010-04-01 20:05:23 -04001642 strbuf_addf(&buffer, "author %s\n", author);
Jeff Kingf9bc5732012-05-24 19:28:40 -04001643 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
Jeff King40d52ff2010-04-01 20:05:23 -04001644 if (!encoding_is_utf8)
1645 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
Junio C Hamano5231c632011-11-07 16:21:32 -08001646
1647 while (extra) {
1648 add_extra_header(&buffer, extra);
1649 extra = extra->next;
1650 }
Jeff King40d52ff2010-04-01 20:05:23 -04001651 strbuf_addch(&buffer, '\n');
1652
1653 /* And add the comment */
Jeff King3ffefb52014-06-10 17:36:52 -04001654 strbuf_add(&buffer, msg, msg_len);
Jeff King40d52ff2010-04-01 20:05:23 -04001655
1656 /* And check the encoding */
Linus Torvalds08a94a12012-06-28 11:24:14 -07001657 if (encoding_is_utf8 && !verify_utf8(&buffer))
Vasco Almeida4fa4b312016-09-19 13:08:16 +00001658 fprintf(stderr, _(commit_utf8_warn));
Jeff King40d52ff2010-04-01 20:05:23 -04001659
Rene Scharfee5051462017-08-30 19:49:38 +02001660 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1661 result = -1;
1662 goto out;
1663 }
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001664
Patryk Obaraa09c9852018-01-28 01:13:19 +01001665 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
Rene Scharfee5051462017-08-30 19:49:38 +02001666out:
Jeff King40d52ff2010-04-01 20:05:23 -04001667 strbuf_release(&buffer);
1668 return result;
1669}
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001670
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001671define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1672static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1673
1674struct merge_remote_desc *merge_remote_util(struct commit *commit)
1675{
1676 return *merge_desc_slab_at(&merge_desc_slab, commit);
1677}
1678
René Scharfebeb518c2016-08-13 14:11:27 +02001679void set_merge_remote_desc(struct commit *commit,
1680 const char *name, struct object *obj)
1681{
1682 struct merge_remote_desc *desc;
René Scharfe5447a762016-08-13 14:21:27 +02001683 FLEX_ALLOC_STR(desc, name, name);
René Scharfebeb518c2016-08-13 14:11:27 +02001684 desc->obj = obj;
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001685 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
René Scharfebeb518c2016-08-13 14:11:27 +02001686}
1687
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001688struct commit *get_merge_parent(const char *name)
1689{
1690 struct object *obj;
1691 struct commit *commit;
brian m. carlson7683e2e2015-03-13 23:39:34 +00001692 struct object_id oid;
brian m. carlsone82caf32017-07-13 23:49:28 +00001693 if (get_oid(name, &oid))
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001694 return NULL;
brian m. carlsonc251c832017-05-06 22:10:38 +00001695 obj = parse_object(&oid);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001696 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001697 if (commit && !merge_remote_util(commit))
René Scharfebeb518c2016-08-13 14:11:27 +02001698 set_merge_remote_desc(commit, name, obj);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001699 return commit;
1700}
René Scharfe89b5f1d2012-04-25 22:35:27 +02001701
1702/*
1703 * Append a commit to the end of the commit_list.
1704 *
1705 * next starts by pointing to the variable that holds the head of an
1706 * empty commit_list, and is updated to point to the "next" field of
1707 * the last item on the list as new commits are appended.
1708 *
1709 * Usage example:
1710 *
1711 * struct commit_list *list;
1712 * struct commit_list **next = &list;
1713 *
1714 * next = commit_list_append(c1, next);
1715 * next = commit_list_append(c2, next);
1716 * assert(commit_list_count(list) == 2);
1717 * return list;
1718 */
1719struct commit_list **commit_list_append(struct commit *commit,
1720 struct commit_list **next)
1721{
Brandon Williams258c03e2018-02-14 10:59:37 -08001722 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1723 new_commit->item = commit;
1724 *next = new_commit;
1725 new_commit->next = NULL;
1726 return &new_commit->next;
René Scharfe89b5f1d2012-04-25 22:35:27 +02001727}
Nguyễn Thái Ngọc Duyefc7df42012-10-26 22:53:51 +07001728
Jeff Kingfe6eb7f2014-08-27 03:56:01 -04001729const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1730{
1731 int key_len = strlen(key);
1732 const char *line = msg;
1733
1734 while (line) {
1735 const char *eol = strchrnul(line, '\n');
1736
1737 if (line == eol)
1738 return NULL;
1739
1740 if (eol - line > key_len &&
1741 !strncmp(line, key, key_len) &&
1742 line[key_len] == ' ') {
1743 *out_len = eol - line - key_len - 1;
1744 return line + key_len + 1;
1745 }
1746 line = *eol ? eol + 1 : NULL;
1747 }
1748 return NULL;
1749}
Junio C Hamano0ed8a4e2014-12-22 12:26:23 -08001750
Christian Couder8c384582014-11-09 10:23:41 +01001751/*
Jonathan Tan710714a2016-11-02 10:29:17 -07001752 * Inspect the given string and determine the true "end" of the log message, in
Christian Couder8c384582014-11-09 10:23:41 +01001753 * order to find where to put a new Signed-off-by: line. Ignored are
Brian Malehornd76650b2017-05-15 23:06:49 -07001754 * trailing comment lines and blank lines. To support "git commit -s
1755 * --amend" on an existing commit, we also ignore "Conflicts:". To
1756 * support "git commit -v", we truncate at cut lines.
Christian Couder8c384582014-11-09 10:23:41 +01001757 *
1758 * Returns the number of bytes from the tail to ignore, to be fed as
1759 * the second parameter to append_signoff().
1760 */
Jonathan Tan710714a2016-11-02 10:29:17 -07001761int ignore_non_trailer(const char *buf, size_t len)
Christian Couder8c384582014-11-09 10:23:41 +01001762{
1763 int boc = 0;
1764 int bol = 0;
1765 int in_old_conflicts_block = 0;
Brian Malehornd76650b2017-05-15 23:06:49 -07001766 size_t cutoff = wt_status_locate_end(buf, len);
Christian Couder8c384582014-11-09 10:23:41 +01001767
Brian Malehornd76650b2017-05-15 23:06:49 -07001768 while (bol < cutoff) {
Jonathan Tan710714a2016-11-02 10:29:17 -07001769 const char *next_line = memchr(buf + bol, '\n', len - bol);
Christian Couder8c384582014-11-09 10:23:41 +01001770
Jonathan Tan710714a2016-11-02 10:29:17 -07001771 if (!next_line)
1772 next_line = buf + len;
Christian Couder8c384582014-11-09 10:23:41 +01001773 else
1774 next_line++;
1775
Jonathan Tan710714a2016-11-02 10:29:17 -07001776 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
Christian Couder8c384582014-11-09 10:23:41 +01001777 /* is this the first of the run of comments? */
1778 if (!boc)
1779 boc = bol;
1780 /* otherwise, it is just continuing */
Jonathan Tan710714a2016-11-02 10:29:17 -07001781 } else if (starts_with(buf + bol, "Conflicts:\n")) {
Christian Couder8c384582014-11-09 10:23:41 +01001782 in_old_conflicts_block = 1;
1783 if (!boc)
1784 boc = bol;
Jonathan Tan710714a2016-11-02 10:29:17 -07001785 } else if (in_old_conflicts_block && buf[bol] == '\t') {
Christian Couder8c384582014-11-09 10:23:41 +01001786 ; /* a pathname in the conflicts block */
1787 } else if (boc) {
1788 /* the previous was not trailing comment */
1789 boc = 0;
1790 in_old_conflicts_block = 0;
1791 }
Jonathan Tan710714a2016-11-02 10:29:17 -07001792 bol = next_line - buf;
Christian Couder8c384582014-11-09 10:23:41 +01001793 }
Brian Malehornd76650b2017-05-15 23:06:49 -07001794 return boc ? len - boc : len - cutoff;
Christian Couder8c384582014-11-09 10:23:41 +01001795}