blob: 0030e79940ff8564b5c8c5ebab462eb1bd745174 [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"
Johannes Schindelined09aef2006-10-30 20:09:06 +01005#include "pkt-line.h"
Junio C Hamano52883fb2006-12-25 11:48:35 -08006#include "utf8.h"
Junio C Hamano199c45b2007-04-09 02:34:05 -07007#include "diff.h"
8#include "revision.h"
Johannes Schindelina97a7462009-10-09 12:21:57 +02009#include "notes.h"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070010#include "gpg-interface.h"
René Scharfe46905892012-04-01 00:10:39 +020011#include "mergesort.h"
Junio C Hamanoa84b7942013-04-13 11:56:41 -070012#include "commit-slab.h"
Junio C Hamanoda24b102013-06-06 21:58:12 -070013#include "prio-queue.h"
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +040014#include "sha1-lookup.h"
Brian Malehornd76650b2017-05-15 23:06:49 -070015#include "wt-status.h"
Johannes Schindelinf9f99b32018-04-29 00:44:44 +020016#include "advice.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -070017
Junio C Hamano82a75292012-09-15 13:58:15 -070018static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
19
Linus Torvalds60ab26d2005-09-15 14:43:17 -070020int save_commit_buffer = 1;
21
Daniel Barkalow175785e2005-04-18 11:39:48 -070022const char *commit_type = "commit";
23
brian m. carlsonbc832662017-05-06 22:10:10 +000024struct commit *lookup_commit_reference_gently(const struct object_id *oid,
Junio C Hamanof76412e2005-08-21 02:51:10 -070025 int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070026{
brian m. carlsonc251c832017-05-06 22:10:38 +000027 struct object *obj = deref_tag(parse_object(oid), NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070028
29 if (!obj)
30 return NULL;
Jeff King8ff226a2014-07-13 02:42:03 -040031 return object_as_type(obj, OBJ_COMMIT, quiet);
Junio C Hamanof76412e2005-08-21 02:51:10 -070032}
33
brian m. carlsonbc832662017-05-06 22:10:10 +000034struct commit *lookup_commit_reference(const struct object_id *oid)
Junio C Hamanof76412e2005-08-21 02:51:10 -070035{
brian m. carlsonbc832662017-05-06 22:10:10 +000036 return lookup_commit_reference_gently(oid, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070037}
38
brian m. carlsonbc832662017-05-06 22:10:10 +000039struct 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 +100040{
brian m. carlsonbc832662017-05-06 22:10:10 +000041 struct commit *c = lookup_commit_reference(oid);
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100042 if (!c)
43 die(_("could not parse %s"), ref_name);
brian m. carlsonbc832662017-05-06 22:10:10 +000044 if (oidcmp(oid, &c->object.oid)) {
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100045 warning(_("%s %s is not a commit!"),
brian m. carlsonbc832662017-05-06 22:10:10 +000046 ref_name, oid_to_hex(oid));
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100047 }
48 return c;
49}
50
brian m. carlsonbc832662017-05-06 22:10:10 +000051struct commit *lookup_commit(const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 11:39:48 -070052{
brian m. carlsonbc832662017-05-06 22:10:10 +000053 struct object *obj = lookup_object(oid->hash);
Jeff Kingd36f51c2014-07-13 02:41:55 -040054 if (!obj)
brian m. carlsonbc832662017-05-06 22:10:10 +000055 return create_object(oid->hash, alloc_commit_node());
Jeff King8ff226a2014-07-13 02:42:03 -040056 return object_as_type(obj, OBJ_COMMIT, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070057}
58
Pat Notza6fa5992010-11-02 13:59:07 -060059struct commit *lookup_commit_reference_by_name(const char *name)
60{
brian m. carlson7683e2e2015-03-13 23:39:34 +000061 struct object_id oid;
Pat Notza6fa5992010-11-02 13:59:07 -060062 struct commit *commit;
63
brian m. carlsone82caf32017-07-13 23:49:28 +000064 if (get_oid_committish(name, &oid))
Pat Notza6fa5992010-11-02 13:59:07 -060065 return NULL;
brian m. carlsonbc832662017-05-06 22:10:10 +000066 commit = lookup_commit_reference(&oid);
Jeff King5e7d4d32013-10-24 04:53:19 -040067 if (parse_commit(commit))
Pat Notza6fa5992010-11-02 13:59:07 -060068 return NULL;
69 return commit;
70}
71
Johannes Schindelindddbad72017-04-26 21:29:31 +020072static timestamp_t parse_commit_date(const char *buf, const char *tail)
Daniel Barkalow175785e2005-04-18 11:39:48 -070073{
Martin Koegler0a617792008-01-19 18:35:23 +010074 const char *dateptr;
Daniel Barkalow175785e2005-04-18 11:39:48 -070075
Martin Koegler0a617792008-01-19 18:35:23 +010076 if (buf + 6 >= tail)
77 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070078 if (memcmp(buf, "author", 6))
79 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010080 while (buf < tail && *buf++ != '\n')
Daniel Barkalow175785e2005-04-18 11:39:48 -070081 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010082 if (buf + 9 >= tail)
83 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070084 if (memcmp(buf, "committer", 9))
85 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010086 while (buf < tail && *buf++ != '>')
Daniel Barkalow175785e2005-04-18 11:39:48 -070087 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010088 if (buf >= tail)
89 return 0;
90 dateptr = buf;
91 while (buf < tail && *buf++ != '\n')
92 /* nada */;
93 if (buf >= tail)
94 return 0;
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +020095 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
96 return parse_timestamp(dateptr, NULL, 10);
Daniel Barkalow175785e2005-04-18 11:39:48 -070097}
98
Junio C Hamano5040f172006-04-06 23:58:51 -070099static struct commit_graft **commit_graft;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700100static int commit_graft_alloc, commit_graft_nr;
101
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400102static const unsigned char *commit_graft_sha1_access(size_t index, void *table)
103{
104 struct commit_graft **commit_graft_table = table;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000105 return commit_graft_table[index]->oid.hash;
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400106}
107
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700108static int commit_graft_pos(const unsigned char *sha1)
109{
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400110 return sha1_pos(sha1, commit_graft, commit_graft_nr,
111 commit_graft_sha1_access);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700112}
113
Junio C Hamano5040f172006-04-06 23:58:51 -0700114int register_commit_graft(struct commit_graft *graft, int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700115{
brian m. carlson7683e2e2015-03-13 23:39:34 +0000116 int pos = commit_graft_pos(graft->oid.hash);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700117
Junio C Hamano5040f172006-04-06 23:58:51 -0700118 if (0 <= pos) {
119 if (ignore_dups)
120 free(graft);
121 else {
122 free(commit_graft[pos]);
123 commit_graft[pos] = graft;
124 }
125 return 1;
126 }
127 pos = -pos - 1;
Dmitry S. Dolzhenkod6e82b52014-03-04 02:31:52 +0400128 ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc);
129 commit_graft_nr++;
Junio C Hamano5040f172006-04-06 23:58:51 -0700130 if (pos < commit_graft_nr)
SZEDER Gáborf919ffe2018-01-22 18:50:09 +0100131 MOVE_ARRAY(commit_graft + pos + 1, commit_graft + pos,
132 commit_graft_nr - pos - 1);
Junio C Hamano5040f172006-04-06 23:58:51 -0700133 commit_graft[pos] = graft;
134 return 0;
135}
136
Patryk Obara9a934032017-08-18 20:33:12 +0200137struct commit_graft *read_graft_line(struct strbuf *line)
Junio C Hamano5040f172006-04-06 23:58:51 -0700138{
139 /* The format is just "Commit Parent1 Parent2 ...\n" */
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200140 int i, phase;
141 const char *tail = NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700142 struct commit_graft *graft = NULL;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200143 struct object_id dummy_oid, *oid;
Junio C Hamano5040f172006-04-06 23:58:51 -0700144
Patryk Obara9a934032017-08-18 20:33:12 +0200145 strbuf_rtrim(line);
146 if (!line->len || line->buf[0] == '#')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700147 return NULL;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200148 /*
149 * phase 0 verifies line, counts hashes in line and allocates graft
150 * phase 1 fills graft
151 */
152 for (phase = 0; phase < 2; phase++) {
153 oid = graft ? &graft->oid : &dummy_oid;
154 if (parse_oid_hex(line->buf, oid, &tail))
Junio C Hamano5040f172006-04-06 23:58:51 -0700155 goto bad_graft_data;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200156 for (i = 0; *tail != '\0'; i++) {
157 oid = graft ? &graft->parent[i] : &dummy_oid;
158 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
159 goto bad_graft_data;
160 }
161 if (!graft) {
162 graft = xmalloc(st_add(sizeof(*graft),
163 st_mult(sizeof(struct object_id), i)));
164 graft->nr_parent = i;
165 }
Junio C Hamano5040f172006-04-06 23:58:51 -0700166 }
167 return graft;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100168
169bad_graft_data:
Patryk Obara9a934032017-08-18 20:33:12 +0200170 error("bad graft data: %s", line->buf);
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200171 assert(!graft);
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100172 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700173}
174
Nanako Shiraishic5ae6432008-10-02 19:14:30 +0900175static int read_graft_file(const char *graft_file)
Junio C Hamano5040f172006-04-06 23:58:51 -0700176{
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700177 FILE *fp = fopen_or_warn(graft_file, "r");
Johannes Schindeline228c172013-12-27 21:49:57 +0100178 struct strbuf buf = STRBUF_INIT;
Junio C Hamano5040f172006-04-06 23:58:51 -0700179 if (!fp)
180 return -1;
Johannes Schindelinf9f99b32018-04-29 00:44:44 +0200181 if (advice_graft_file_deprecated)
182 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
183 "and will be removed in a future Git version.\n"
184 "\n"
185 "Please use \"git replace --convert-graft-file\"\n"
186 "to convert the grafts into replace refs.\n"
187 "\n"
188 "Turn this message off by running\n"
189 "\"git config advice.graftFileDeprecated false\""));
Johannes Schindeline228c172013-12-27 21:49:57 +0100190 while (!strbuf_getwholeline(&buf, fp, '\n')) {
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700191 /* The format is just "Commit Parent1 Parent2 ...\n" */
Patryk Obara9a934032017-08-18 20:33:12 +0200192 struct commit_graft *graft = read_graft_line(&buf);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700193 if (!graft)
194 continue;
Junio C Hamano5040f172006-04-06 23:58:51 -0700195 if (register_commit_graft(graft, 1))
Johannes Schindeline228c172013-12-27 21:49:57 +0100196 error("duplicate graft data: %s", buf.buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700197 }
198 fclose(fp);
Johannes Schindeline228c172013-12-27 21:49:57 +0100199 strbuf_release(&buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700200 return 0;
201}
202
203static void prepare_commit_graft(void)
204{
205 static int commit_graft_prepared;
206 char *graft_file;
207
208 if (commit_graft_prepared)
209 return;
Jeff King14a9bd22018-05-31 18:42:53 -0400210 if (!startup_info->have_repository)
211 return;
212
Junio C Hamano5040f172006-04-06 23:58:51 -0700213 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
Stefan Beller8b65a342017-07-12 17:44:14 -0700220struct commit_graft *lookup_commit_graft(const struct object_id *oid)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700221{
222 int pos;
Junio C Hamano5040f172006-04-06 23:58:51 -0700223 prepare_commit_graft();
Stefan Beller8b65a342017-07-12 17:44:14 -0700224 pos = commit_graft_pos(oid->hash);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700225 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
brian m. carlsone92b8482017-05-06 22:10:06 +0000238int unregister_shallow(const struct object_id *oid)
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100239{
brian m. carlsone92b8482017-05-06 22:10:06 +0000240 int pos = commit_graft_pos(oid->hash);
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100241 if (pos < 0)
242 return -1;
243 if (pos + 1 < commit_graft_nr)
René Scharfef331ab92017-07-15 22:00:45 +0200244 MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1,
245 commit_graft_nr - pos - 1);
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100246 commit_graft_nr--;
247 return 0;
248}
249
Jeff King8597ea32014-06-10 17:44:13 -0400250struct commit_buffer {
251 void *buffer;
252 unsigned long size;
253};
254define_commit_slab(buffer_slab, struct commit_buffer);
Jeff Kingc1b3c712014-06-10 17:43:02 -0400255static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab);
256
Jeff King8597ea32014-06-10 17:44:13 -0400257void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size)
Jeff King66c28272014-06-10 17:40:14 -0400258{
Jeff King8597ea32014-06-10 17:44:13 -0400259 struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit);
260 v->buffer = buffer;
261 v->size = size;
Jeff King66c28272014-06-10 17:40:14 -0400262}
263
Jeff King8597ea32014-06-10 17:44:13 -0400264const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep)
Jeff King152ff1c2014-06-10 17:40:39 -0400265{
Junio C Hamano862e7302015-05-14 15:25:52 -0700266 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
267 if (!v) {
268 if (sizep)
269 *sizep = 0;
270 return NULL;
271 }
Jeff King8597ea32014-06-10 17:44:13 -0400272 if (sizep)
273 *sizep = v->size;
274 return v->buffer;
Jeff King152ff1c2014-06-10 17:40:39 -0400275}
276
Jeff King8597ea32014-06-10 17:44:13 -0400277const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)
Jeff King152ff1c2014-06-10 17:40:39 -0400278{
Jeff King8597ea32014-06-10 17:44:13 -0400279 const void *ret = get_cached_commit_buffer(commit, sizep);
Jeff King152ff1c2014-06-10 17:40:39 -0400280 if (!ret) {
281 enum object_type type;
282 unsigned long size;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000283 ret = read_object_file(&commit->object.oid, &type, &size);
Jeff King152ff1c2014-06-10 17:40:39 -0400284 if (!ret)
285 die("cannot read commit object %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000286 oid_to_hex(&commit->object.oid));
Jeff King152ff1c2014-06-10 17:40:39 -0400287 if (type != OBJ_COMMIT)
288 die("expected commit for %s, got %s",
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800289 oid_to_hex(&commit->object.oid), type_name(type));
Jeff King8597ea32014-06-10 17:44:13 -0400290 if (sizep)
291 *sizep = size;
Jeff King152ff1c2014-06-10 17:40:39 -0400292 }
293 return ret;
294}
295
296void unuse_commit_buffer(const struct commit *commit, const void *buffer)
297{
Junio C Hamano862e7302015-05-14 15:25:52 -0700298 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
299 if (!(v && v->buffer == buffer))
Jeff King152ff1c2014-06-10 17:40:39 -0400300 free((void *)buffer);
301}
302
Jeff King0fb370d2014-06-12 18:05:37 -0400303void free_commit_buffer(struct commit *commit)
304{
Junio C Hamano862e7302015-05-14 15:25:52 -0700305 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
306 if (v) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000307 FREE_AND_NULL(v->buffer);
Junio C Hamano862e7302015-05-14 15:25:52 -0700308 v->size = 0;
309 }
Jeff King0fb370d2014-06-12 18:05:37 -0400310}
311
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000312struct tree *get_commit_tree(const struct commit *commit)
313{
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000314 if (commit->maybe_tree || !commit->object.parsed)
315 return commit->maybe_tree;
316
317 if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH)
318 BUG("commit has NULL tree, but was not loaded from commit-graph");
319
320 return get_commit_tree_in_graph(commit);
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000321}
322
323struct object_id *get_commit_tree_oid(const struct commit *commit)
324{
325 return &get_commit_tree(commit)->object.oid;
326}
327
Jeff King8597ea32014-06-10 17:44:13 -0400328const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
Jeff King0fb370d2014-06-12 18:05:37 -0400329{
Junio C Hamano862e7302015-05-14 15:25:52 -0700330 struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit);
Jeff King8597ea32014-06-10 17:44:13 -0400331 void *ret;
332
Junio C Hamano862e7302015-05-14 15:25:52 -0700333 if (!v) {
334 if (sizep)
335 *sizep = 0;
336 return NULL;
337 }
Jeff King8597ea32014-06-10 17:44:13 -0400338 ret = v->buffer;
339 if (sizep)
340 *sizep = v->size;
341
342 v->buffer = NULL;
343 v->size = 0;
Jeff King0fb370d2014-06-12 18:05:37 -0400344 return ret;
345}
346
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700347int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700348{
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700349 const char *tail = buffer;
350 const char *bufptr = buffer;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000351 struct object_id parent;
Linus Torvalds6c88be12005-06-20 20:26:03 -0700352 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700353 struct commit_graft *graft;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000354 const int tree_entry_len = GIT_SHA1_HEXSZ + 5;
355 const int parent_entry_len = GIT_SHA1_HEXSZ + 7;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400356
Daniel Barkalow175785e2005-04-18 11:39:48 -0700357 if (item->object.parsed)
358 return 0;
359 item->object.parsed = 1;
Junio C Hamano3b44f152006-06-28 03:51:00 -0700360 tail += size;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000361 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
362 bufptr[tree_entry_len] != '\n')
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000363 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
brian m. carlson26ea3e72018-05-02 00:25:47 +0000364 if (get_oid_hex(bufptr + 5, &parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800365 return error("bad tree pointer in commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000366 oid_to_hex(&item->object.oid));
Derrick Stolee891435d2018-04-06 19:09:32 +0000367 item->maybe_tree = lookup_tree(&parent);
brian m. carlson7683e2e2015-03-13 23:39:34 +0000368 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700369 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700370
Stefan Beller8b65a342017-07-12 17:44:14 -0700371 graft = lookup_commit_graft(&item->object.oid);
brian m. carlson7683e2e2015-03-13 23:39:34 +0000372 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700373 struct commit *new_parent;
374
brian m. carlson7683e2e2015-03-13 23:39:34 +0000375 if (tail <= bufptr + parent_entry_len + 1 ||
brian m. carlson26ea3e72018-05-02 00:25:47 +0000376 get_oid_hex(bufptr + 7, &parent) ||
brian m. carlson7683e2e2015-03-13 23:39:34 +0000377 bufptr[parent_entry_len] != '\n')
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000378 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
brian m. carlson7683e2e2015-03-13 23:39:34 +0000379 bufptr += parent_entry_len + 1;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +0200380 /*
381 * The clone is shallow if nr_parent < 0, and we must
382 * not traverse its real parents even when we unhide them.
383 */
384 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700385 continue;
brian m. carlsonbc832662017-05-06 22:10:10 +0000386 new_parent = lookup_commit(&parent);
Miklos Vajna39468de2008-06-07 22:38:37 +0200387 if (new_parent)
Linus Torvalds6c88be12005-06-20 20:26:03 -0700388 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700389 }
390 if (graft) {
391 int i;
392 struct commit *new_parent;
393 for (i = 0; i < graft->nr_parent; i++) {
brian m. carlsonbc832662017-05-06 22:10:10 +0000394 new_parent = lookup_commit(&graft->parent[i]);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700395 if (!new_parent)
396 continue;
397 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700398 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700399 }
Martin Koegler0a617792008-01-19 18:35:23 +0100400 item->date = parse_commit_date(bufptr, tail);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800401
Daniel Barkalow175785e2005-04-18 11:39:48 -0700402 return 0;
403}
404
Jeff King9cc2b072015-06-01 05:56:26 -0400405int parse_commit_gently(struct commit *item, int quiet_on_missing)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400406{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500407 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400408 void *buffer;
409 unsigned long size;
410 int ret;
411
Martin Koegler9786f682008-02-18 21:48:02 +0100412 if (!item)
413 return -1;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400414 if (item->object.parsed)
415 return 0;
Derrick Stolee177722b2018-04-10 08:56:05 -0400416 if (parse_commit_in_graph(item))
417 return 0;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000418 buffer = read_object_file(&item->object.oid, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400419 if (!buffer)
Jeff King9cc2b072015-06-01 05:56:26 -0400420 return quiet_on_missing ? -1 :
421 error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000422 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500423 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400424 free(buffer);
425 return error("Object %s not a commit",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000426 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400427 }
428 ret = parse_commit_buffer(item, buffer, size);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700429 if (save_commit_buffer && !ret) {
Jeff King8597ea32014-06-10 17:44:13 -0400430 set_commit_buffer(item, buffer, size);
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700431 return 0;
432 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400433 free(buffer);
434 return ret;
435}
436
Jeff King7059dcc2013-10-24 04:52:36 -0400437void parse_commit_or_die(struct commit *item)
438{
439 if (parse_commit(item))
440 die("unable to parse commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000441 item ? oid_to_hex(&item->object.oid) : "(null)");
Jeff King7059dcc2013-10-24 04:52:36 -0400442}
443
Christian Couder11af2aa2010-07-22 15:18:30 +0200444int find_commit_subject(const char *commit_buffer, const char **subject)
445{
446 const char *eol;
447 const char *p = commit_buffer;
448
449 while (*p && (*p != '\n' || p[1] != '\n'))
450 p++;
451 if (*p) {
Johannes Schindelin4e1b06d2016-06-22 22:20:20 +0200452 p = skip_blank_lines(p + 2);
Junio C Hamano9c9b03b2017-01-27 18:01:41 -0800453 eol = strchrnul(p, '\n');
Christian Couder11af2aa2010-07-22 15:18:30 +0200454 } else
455 eol = p;
456
457 *subject = p;
458
459 return eol - p;
460}
461
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700462struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700463{
Christopher Li812666c2005-04-26 12:00:58 -0700464 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700465 new_list->item = item;
466 new_list->next = *list_p;
467 *list_p = new_list;
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700468 return new_list;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700469}
470
Miklos Vajna65319472008-06-27 18:21:55 +0200471unsigned commit_list_count(const struct commit_list *l)
472{
473 unsigned c = 0;
474 for (; l; l = l->next )
475 c++;
476 return c;
477}
478
Thomas Rast53d00b32013-07-31 22:13:20 +0200479struct commit_list *copy_commit_list(struct commit_list *list)
480{
481 struct commit_list *head = NULL;
482 struct commit_list **pp = &head;
483 while (list) {
René Scharfecb979db2014-07-10 11:47:47 +0200484 pp = commit_list_append(list->item, pp);
Thomas Rast53d00b32013-07-31 22:13:20 +0200485 list = list->next;
486 }
487 return head;
488}
489
Daniel Barkalow175785e2005-04-18 11:39:48 -0700490void free_commit_list(struct commit_list *list)
491{
René Scharfee510ab82015-10-24 18:21:31 +0200492 while (list)
493 pop_commit(&list);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700494}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700495
Thiago Farina47e44ed2010-11-26 23:58:14 -0200496struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700497{
498 struct commit_list **pp = list;
499 struct commit_list *p;
500 while ((p = *pp) != NULL) {
501 if (p->item->date < item->date) {
502 break;
503 }
504 pp = &p->next;
505 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700506 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700507}
508
René Scharfe46905892012-04-01 00:10:39 +0200509static int commit_list_compare_by_date(const void *a, const void *b)
510{
Johannes Schindelindddbad72017-04-26 21:29:31 +0200511 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
512 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
René Scharfe46905892012-04-01 00:10:39 +0200513 if (a_date < b_date)
514 return 1;
515 if (a_date > b_date)
516 return -1;
517 return 0;
518}
519
520static void *commit_list_get_next(const void *a)
521{
522 return ((const struct commit_list *)a)->next;
523}
524
525static void commit_list_set_next(void *a, void *next)
526{
527 ((struct commit_list *)a)->next = next;
528}
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700529
Thiago Farina47e44ed2010-11-26 23:58:14 -0200530void commit_list_sort_by_date(struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700531{
Junio C Hamano7365c952012-04-17 11:07:01 -0700532 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
533 commit_list_compare_by_date);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700534}
535
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700536struct commit *pop_most_recent_commit(struct commit_list **list,
537 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700538{
René Scharfee510ab82015-10-24 18:21:31 +0200539 struct commit *ret = pop_commit(list);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700540 struct commit_list *parents = ret->parents;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700541
542 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700543 struct commit *commit = parents->item;
Martin Koeglerdec38c82008-02-18 21:48:03 +0100544 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700545 commit->object.flags |= mark;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200546 commit_list_insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700547 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700548 parents = parents->next;
549 }
550 return ret;
551}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700552
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700553static void clear_commit_marks_1(struct commit_list **plist,
554 struct commit *commit, unsigned int mark)
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800555{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100556 while (commit) {
557 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800558
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100559 if (!(mark & commit->object.flags))
560 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700561
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100562 commit->object.flags &= ~mark;
563
564 parents = commit->parents;
565 if (!parents)
566 return;
567
568 while ((parents = parents->next))
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700569 commit_list_insert(parents->item, plist);
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100570
571 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800572 }
573}
574
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800575void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700576{
577 struct commit_list *list = NULL;
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800578
579 while (nr--) {
René Scharfe07f7d552017-12-25 18:43:37 +0100580 clear_commit_marks_1(&list, *commit, mark);
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800581 commit++;
582 }
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700583 while (list)
584 clear_commit_marks_1(&list, pop_commit(&list), mark);
585}
586
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800587void clear_commit_marks(struct commit *commit, unsigned int mark)
588{
589 clear_commit_marks_many(1, &commit, mark);
590}
591
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000592struct commit *pop_commit(struct commit_list **stack)
593{
594 struct commit_list *top = *stack;
595 struct commit *item = top ? top->item : NULL;
596
597 if (top) {
598 *stack = top->next;
599 free(top);
600 }
601 return item;
602}
603
Jon Seymourab580ac2005-07-07 02:39:34 +1000604/*
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700605 * Topological sort support
606 */
Junio C Hamano66eb3752013-04-12 23:25:49 -0700607
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700608/* count number of children that have not been emitted */
609define_commit_slab(indegree_slab, int);
Jeff King96c4f4a2013-04-09 02:52:56 -0400610
Junio C Hamano81c6b382013-06-07 10:35:54 -0700611/* record author-date for each commit object */
612define_commit_slab(author_date_slab, unsigned long);
613
614static void record_author_date(struct author_date_slab *author_date,
615 struct commit *commit)
616{
Jeff King8597ea32014-06-10 17:44:13 -0400617 const char *buffer = get_commit_buffer(commit, NULL);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700618 struct ident_split ident;
Jeff Kingea5517f2014-08-27 03:56:55 -0400619 const char *ident_line;
620 size_t ident_len;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700621 char *date_end;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200622 timestamp_t date;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700623
Jeff Kingea5517f2014-08-27 03:56:55 -0400624 ident_line = find_commit_header(buffer, "author", &ident_len);
625 if (!ident_line)
626 goto fail_exit; /* no author line */
627 if (split_ident_line(&ident, ident_line, ident_len) ||
628 !ident.date_begin || !ident.date_end)
629 goto fail_exit; /* malformed "author" line */
Junio C Hamano81c6b382013-06-07 10:35:54 -0700630
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200631 date = parse_timestamp(ident.date_begin, &date_end, 10);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700632 if (date_end != ident.date_end)
633 goto fail_exit; /* malformed date */
634 *(author_date_slab_at(author_date, commit)) = date;
635
636fail_exit:
Jeff Kingba41c1c2014-06-10 17:41:02 -0400637 unuse_commit_buffer(commit, buffer);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700638}
639
640static int compare_commits_by_author_date(const void *a_, const void *b_,
641 void *cb_data)
642{
643 const struct commit *a = a_, *b = b_;
644 struct author_date_slab *author_date = cb_data;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200645 timestamp_t a_date = *(author_date_slab_at(author_date, a));
646 timestamp_t b_date = *(author_date_slab_at(author_date, b));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700647
648 /* newer commits with larger date first */
649 if (a_date < b_date)
650 return 1;
651 else if (a_date > b_date)
652 return -1;
653 return 0;
654}
655
Jeff King727377f2013-07-02 02:21:48 -0400656int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700657{
658 const struct commit *a = a_, *b = b_;
659 /* newer commits with larger date first */
660 if (a->date < b->date)
661 return 1;
662 else if (a->date > b->date)
663 return -1;
664 return 0;
665}
666
Jon Seymourab580ac2005-07-07 02:39:34 +1000667/*
668 * Performs an in-place topological sort on the list supplied.
669 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700670void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
Jon Seymourab580ac2005-07-07 02:39:34 +1000671{
Linus Torvalds23c17d42007-11-02 13:32:58 -0700672 struct commit_list *next, *orig = *list;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700673 struct commit_list **pptr;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700674 struct indegree_slab indegree;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700675 struct prio_queue queue;
676 struct commit *commit;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700677 struct author_date_slab author_date;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100678
Linus Torvalds23c17d42007-11-02 13:32:58 -0700679 if (!orig)
Eric Wong7d6fb372005-12-24 04:12:43 -0800680 return;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700681 *list = NULL;
682
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700683 init_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700684 memset(&queue, '\0', sizeof(queue));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700685
Junio C Hamanoda24b102013-06-06 21:58:12 -0700686 switch (sort_order) {
687 default: /* REV_SORT_IN_GRAPH_ORDER */
688 queue.compare = NULL;
689 break;
690 case REV_SORT_BY_COMMIT_DATE:
691 queue.compare = compare_commits_by_commit_date;
692 break;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700693 case REV_SORT_BY_AUTHOR_DATE:
694 init_author_date_slab(&author_date);
695 queue.compare = compare_commits_by_author_date;
696 queue.cb_data = &author_date;
697 break;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700698 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400699
Linus Torvalds23c17d42007-11-02 13:32:58 -0700700 /* Mark them and clear the indegree */
701 for (next = orig; next; next = next->next) {
702 struct commit *commit = next->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700703 *(indegree_slab_at(&indegree, commit)) = 1;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700704 /* also record the author dates, if needed */
705 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
706 record_author_date(&author_date, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000707 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700708
Jon Seymourab580ac2005-07-07 02:39:34 +1000709 /* update the indegree */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700710 for (next = orig; next; next = next->next) {
Junio C Hamanoda24b102013-06-06 21:58:12 -0700711 struct commit_list *parents = next->item->parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000712 while (parents) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700713 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700714 int *pi = indegree_slab_at(&indegree, parent);
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100715
Jeff King96c4f4a2013-04-09 02:52:56 -0400716 if (*pi)
717 (*pi)++;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700718 parents = parents->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000719 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000720 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700721
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700722 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200723 * find the tips
724 *
725 * tips are nodes not reachable from any other node in the list
726 *
727 * the tips serve as a starting set for the work queue.
728 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700729 for (next = orig; next; next = next->next) {
730 struct commit *commit = next->item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000731
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700732 if (*(indegree_slab_at(&indegree, commit)) == 1)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700733 prio_queue_put(&queue, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000734 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800735
Junio C Hamanoda24b102013-06-06 21:58:12 -0700736 /*
737 * This is unfortunate; the initial tips need to be shown
738 * in the order given from the revision traversal machinery.
739 */
740 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
741 prio_queue_reverse(&queue);
742
743 /* We no longer need the commit list */
744 free_commit_list(orig);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700745
746 pptr = list;
747 *list = NULL;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700748 while ((commit = prio_queue_get(&queue)) != NULL) {
749 struct commit_list *parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000750
Linus Torvalds23c17d42007-11-02 13:32:58 -0700751 for (parents = commit->parents; parents ; parents = parents->next) {
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -0700752 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700753 int *pi = indegree_slab_at(&indegree, parent);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700754
Jeff King96c4f4a2013-04-09 02:52:56 -0400755 if (!*pi)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700756 continue;
757
758 /*
759 * parents are only enqueued for emission
760 * when all their children have been emitted thereby
761 * guaranteeing topological order.
762 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700763 if (--(*pi) == 1)
764 prio_queue_put(&queue, parent);
Jon Seymourab580ac2005-07-07 02:39:34 +1000765 }
766 /*
Junio C Hamanoda24b102013-06-06 21:58:12 -0700767 * all children of commit have already been
768 * emitted. we can emit it now.
Pierre Habouzit674d1722007-09-10 12:35:06 +0200769 */
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700770 *(indegree_slab_at(&indegree, commit)) = 0;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700771
772 pptr = &commit_list_insert(commit, pptr)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000773 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400774
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700775 clear_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700776 clear_prio_queue(&queue);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700777 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
778 clear_author_date_slab(&author_date);
Jon Seymourab580ac2005-07-07 02:39:34 +1000779}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200780
Junio C Hamano12952282007-01-08 23:10:49 -0800781/* merge-base stuff */
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200782
Nguyễn Thái Ngọc Duy208acbf2014-03-25 20:23:26 +0700783/* Remember to update object flag allocation in object.h */
Junio C Hamano577ed5c2006-10-22 17:32:47 -0700784#define PARENT1 (1u<<16)
785#define PARENT2 (1u<<17)
786#define STALE (1u<<18)
787#define RESULT (1u<<19)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200788
Junio C Hamano12952282007-01-08 23:10:49 -0800789static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
790
Jeff King73f43f22014-07-14 01:53:54 -0400791static int queue_has_nonstale(struct prio_queue *queue)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200792{
Jeff King73f43f22014-07-14 01:53:54 -0400793 int i;
794 for (i = 0; i < queue->nr; i++) {
795 struct commit *commit = queue->array[i].data;
796 if (!(commit->object.flags & STALE))
797 return 1;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200798 }
Jeff King73f43f22014-07-14 01:53:54 -0400799 return 0;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200800}
801
Junio C Hamanod8669242012-10-04 15:37:15 -0700802/* all input commits in one and twos[] must have been parsed! */
Junio C Hamanoda1f5152012-08-30 14:39:03 -0700803static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos)
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200804{
Jeff King73f43f22014-07-14 01:53:54 -0400805 struct prio_queue queue = { compare_commits_by_commit_date };
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200806 struct commit_list *result = NULL;
Junio C Hamano6a938642008-06-27 18:22:02 +0200807 int i;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200808
Junio C Hamanof3249432006-07-04 18:46:42 -0700809 one->object.flags |= PARENT1;
Jeff King73f43f22014-07-14 01:53:54 -0400810 if (!n) {
811 commit_list_append(one, &result);
812 return result;
813 }
814 prio_queue_put(&queue, one);
815
Junio C Hamano6a938642008-06-27 18:22:02 +0200816 for (i = 0; i < n; i++) {
817 twos[i]->object.flags |= PARENT2;
Jeff King73f43f22014-07-14 01:53:54 -0400818 prio_queue_put(&queue, twos[i]);
Junio C Hamano6a938642008-06-27 18:22:02 +0200819 }
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200820
Jeff King73f43f22014-07-14 01:53:54 -0400821 while (queue_has_nonstale(&queue)) {
822 struct commit *commit = prio_queue_get(&queue);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200823 struct commit_list *parents;
Junio C Hamanof3249432006-07-04 18:46:42 -0700824 int flags;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200825
Junio C Hamanof3249432006-07-04 18:46:42 -0700826 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200827 if (flags == (PARENT1 | PARENT2)) {
Junio C Hamanof3249432006-07-04 18:46:42 -0700828 if (!(commit->object.flags & RESULT)) {
829 commit->object.flags |= RESULT;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200830 commit_list_insert_by_date(commit, &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700831 }
Junio C Hamano542ccef2006-07-02 11:34:17 -0700832 /* Mark parents of a found merge stale */
833 flags |= STALE;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200834 }
835 parents = commit->parents;
836 while (parents) {
837 struct commit *p = parents->item;
838 parents = parents->next;
839 if ((p->object.flags & flags) == flags)
840 continue;
Martin Koegler172947e2008-02-18 21:47:57 +0100841 if (parse_commit(p))
842 return NULL;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200843 p->object.flags |= flags;
Jeff King73f43f22014-07-14 01:53:54 -0400844 prio_queue_put(&queue, p);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200845 }
846 }
847
Jeff King73f43f22014-07-14 01:53:54 -0400848 clear_prio_queue(&queue);
Junio C Hamanoda1f5152012-08-30 14:39:03 -0700849 return result;
850}
851
852static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos)
853{
854 struct commit_list *list = NULL;
855 struct commit_list *result = NULL;
856 int i;
857
858 for (i = 0; i < n; i++) {
859 if (one == twos[i])
860 /*
861 * We do not mark this even with RESULT so we do not
862 * have to clean it up.
863 */
864 return commit_list_insert(one, &result);
865 }
866
867 if (parse_commit(one))
868 return NULL;
869 for (i = 0; i < n; i++) {
870 if (parse_commit(twos[i]))
871 return NULL;
872 }
873
874 list = paint_down_to_common(one, n, twos);
875
Junio C Hamanof3249432006-07-04 18:46:42 -0700876 while (list) {
René Scharfee510ab82015-10-24 18:21:31 +0200877 struct commit *commit = pop_commit(&list);
878 if (!(commit->object.flags & STALE))
879 commit_list_insert_by_date(commit, &result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700880 }
881 return result;
882}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200883
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200884struct commit_list *get_octopus_merge_bases(struct commit_list *in)
885{
886 struct commit_list *i, *j, *k, *ret = NULL;
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200887
Vasily Makarov6bc76722014-01-03 18:45:46 +0400888 if (!in)
889 return ret;
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200890
Vasily Makarov6bc76722014-01-03 18:45:46 +0400891 commit_list_insert(in->item, &ret);
892
893 for (i = in->next; i; i = i->next) {
Brandon Williams258c03e2018-02-14 10:59:37 -0800894 struct commit_list *new_commits = NULL, *end = NULL;
Vasily Makarov6bc76722014-01-03 18:45:46 +0400895
896 for (j = ret; j; j = j->next) {
897 struct commit_list *bases;
Junio C Hamano2ce406c2014-10-30 12:20:44 -0700898 bases = get_merge_bases(i->item, j->item);
Brandon Williams258c03e2018-02-14 10:59:37 -0800899 if (!new_commits)
900 new_commits = bases;
Vasily Makarov6bc76722014-01-03 18:45:46 +0400901 else
902 end->next = bases;
903 for (k = bases; k; k = k->next)
904 end = k;
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200905 }
Brandon Williams258c03e2018-02-14 10:59:37 -0800906 ret = new_commits;
Miklos Vajna5240c9d2008-06-27 18:22:00 +0200907 }
908 return ret;
909}
910
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700911static int remove_redundant(struct commit **array, int cnt)
912{
913 /*
914 * Some commit in the array may be an ancestor of
915 * another commit. Move such commit to the end of
916 * the array, and return the number of commits that
917 * are independent from each other.
918 */
919 struct commit **work;
920 unsigned char *redundant;
921 int *filled_index;
922 int i, j, filled;
923
924 work = xcalloc(cnt, sizeof(*work));
925 redundant = xcalloc(cnt, 1);
Jeff Kingb32fa952016-02-22 17:44:25 -0500926 ALLOC_ARRAY(filled_index, cnt - 1);
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700927
Junio C Hamanod8669242012-10-04 15:37:15 -0700928 for (i = 0; i < cnt; i++)
929 parse_commit(array[i]);
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700930 for (i = 0; i < cnt; i++) {
931 struct commit_list *common;
932
933 if (redundant[i])
934 continue;
935 for (j = filled = 0; j < cnt; j++) {
936 if (i == j || redundant[j])
937 continue;
938 filled_index[filled] = j;
939 work[filled++] = array[j];
940 }
941 common = paint_down_to_common(array[i], filled, work);
942 if (array[i]->object.flags & PARENT2)
943 redundant[i] = 1;
944 for (j = 0; j < filled; j++)
945 if (work[j]->object.flags & PARENT1)
946 redundant[filled_index[j]] = 1;
947 clear_commit_marks(array[i], all_flags);
René Scharfeabc03512017-12-25 18:44:03 +0100948 clear_commit_marks_many(filled, work, all_flags);
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700949 free_commit_list(common);
950 }
951
952 /* Now collect the result */
René Scharfe45ccef82016-09-25 09:24:03 +0200953 COPY_ARRAY(work, array, cnt);
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700954 for (i = filled = 0; i < cnt; i++)
955 if (!redundant[i])
956 array[filled++] = work[i];
957 for (j = filled, i = 0; i < cnt; i++)
958 if (redundant[i])
959 array[j++] = work[i];
960 free(work);
961 free(redundant);
962 free(filled_index);
963 return filled;
964}
965
Junio C Hamano2ce406c2014-10-30 12:20:44 -0700966static struct commit_list *get_merge_bases_many_0(struct commit *one,
967 int n,
968 struct commit **twos,
969 int cleanup)
Junio C Hamanof3249432006-07-04 18:46:42 -0700970{
Junio C Hamanof3249432006-07-04 18:46:42 -0700971 struct commit_list *list;
972 struct commit **rslt;
973 struct commit_list *result;
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700974 int cnt, i;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200975
Junio C Hamano6a938642008-06-27 18:22:02 +0200976 result = merge_bases_many(one, n, twos);
977 for (i = 0; i < n; i++) {
978 if (one == twos[i])
979 return result;
980 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700981 if (!result || !result->next) {
982 if (cleanup) {
983 clear_commit_marks(one, all_flags);
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800984 clear_commit_marks_many(n, twos, all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200985 }
Junio C Hamanof3249432006-07-04 18:46:42 -0700986 return result;
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200987 }
988
Junio C Hamanof3249432006-07-04 18:46:42 -0700989 /* There are more than one */
René Scharfe4bbaa1e2014-07-17 01:52:09 +0200990 cnt = commit_list_count(result);
Junio C Hamanof3249432006-07-04 18:46:42 -0700991 rslt = xcalloc(cnt, sizeof(*rslt));
992 for (list = result, i = 0; list; list = list->next)
993 rslt[i++] = list->item;
994 free_commit_list(result);
995
996 clear_commit_marks(one, all_flags);
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800997 clear_commit_marks_many(n, twos, all_flags);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200998
Junio C Hamano94f0ced2012-08-30 15:35:39 -0700999 cnt = remove_redundant(rslt, cnt);
Junio C Hamanof3249432006-07-04 18:46:42 -07001000 result = NULL;
Junio C Hamano94f0ced2012-08-30 15:35:39 -07001001 for (i = 0; i < cnt; i++)
1002 commit_list_insert_by_date(rslt[i], &result);
Junio C Hamanof3249432006-07-04 18:46:42 -07001003 free(rslt);
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +02001004 return result;
1005}
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -08001006
Junio C Hamano2ce406c2014-10-30 12:20:44 -07001007struct commit_list *get_merge_bases_many(struct commit *one,
1008 int n,
1009 struct commit **twos)
Junio C Hamano6a938642008-06-27 18:22:02 +02001010{
Junio C Hamano2ce406c2014-10-30 12:20:44 -07001011 return get_merge_bases_many_0(one, n, twos, 1);
1012}
1013
1014struct commit_list *get_merge_bases_many_dirty(struct commit *one,
1015 int n,
1016 struct commit **twos)
1017{
1018 return get_merge_bases_many_0(one, n, twos, 0);
1019}
1020
1021struct commit_list *get_merge_bases(struct commit *one, struct commit *two)
1022{
1023 return get_merge_bases_many_0(one, 1, &two, 1);
Junio C Hamano6a938642008-06-27 18:22:02 +02001024}
1025
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001026/*
Stefano Lattarini41ccfdd2013-04-12 00:36:10 +02001027 * Is "commit" a descendant of one of the elements on the "with_commit" list?
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001028 */
Jake Goulding7fcdb362009-01-26 09:13:24 -05001029int is_descendant_of(struct commit *commit, struct commit_list *with_commit)
1030{
1031 if (!with_commit)
1032 return 1;
1033 while (with_commit) {
1034 struct commit *other;
1035
1036 other = with_commit->item;
1037 with_commit = with_commit->next;
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001038 if (in_merge_bases(other, commit))
Jake Goulding7fcdb362009-01-26 09:13:24 -05001039 return 1;
1040 }
1041 return 0;
1042}
1043
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001044/*
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001045 * Is "commit" an ancestor of one of the "references"?
1046 */
1047int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference)
1048{
1049 struct commit_list *bases;
1050 int ret = 0, i;
1051
1052 if (parse_commit(commit))
1053 return ret;
1054 for (i = 0; i < nr_reference; i++)
1055 if (parse_commit(reference[i]))
1056 return ret;
1057
1058 bases = paint_down_to_common(commit, nr_reference, reference);
1059 if (commit->object.flags & PARENT2)
1060 ret = 1;
1061 clear_commit_marks(commit, all_flags);
Junio C Hamano557899f2013-03-05 11:56:03 -08001062 clear_commit_marks_many(nr_reference, reference, all_flags);
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001063 free_commit_list(bases);
1064 return ret;
1065}
1066
1067/*
Junio C Hamanoa20efee2012-08-27 14:46:01 -07001068 * Is "commit" an ancestor of (i.e. reachable from) the "reference"?
1069 */
1070int in_merge_bases(struct commit *commit, struct commit *reference)
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -08001071{
Junio C Hamano4c4b27e2013-03-04 10:16:42 -08001072 return in_merge_bases_many(commit, 1, &reference);
Junio C Hamano2ecd2bb2006-12-19 00:14:04 -08001073}
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001074
1075struct commit_list *reduce_heads(struct commit_list *heads)
1076{
1077 struct commit_list *p;
1078 struct commit_list *result = NULL, **tail = &result;
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001079 struct commit **array;
1080 int num_head, i;
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001081
1082 if (!heads)
1083 return NULL;
1084
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001085 /* Uniquify */
1086 for (p = heads; p; p = p->next)
1087 p->item->object.flags &= ~STALE;
1088 for (p = heads, num_head = 0; p; p = p->next) {
1089 if (p->item->object.flags & STALE)
Junio C Hamano711f6b22008-07-14 00:09:41 -07001090 continue;
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001091 p->item->object.flags |= STALE;
1092 num_head++;
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001093 }
Brian Gesiakc4a7b002014-05-27 00:33:45 +09001094 array = xcalloc(num_head, sizeof(*array));
Junio C Hamanof37d3c72012-08-30 23:20:40 -07001095 for (p = heads, i = 0; p; p = p->next) {
1096 if (p->item->object.flags & STALE) {
1097 array[i++] = p->item;
1098 p->item->object.flags &= ~STALE;
1099 }
1100 }
1101 num_head = remove_redundant(array, num_head);
1102 for (i = 0; i < num_head; i++)
1103 tail = &commit_list_insert(array[i], tail)->next;
Martin Ågrencb7b29e2017-09-23 01:34:50 +02001104 free(array);
Junio C Hamano98cf9c32008-06-27 18:22:03 +02001105 return result;
1106}
Jeff King40d52ff2010-04-01 20:05:23 -04001107
Martin Ågren4da72642017-11-07 21:39:45 +01001108void reduce_heads_replace(struct commit_list **heads)
1109{
1110 struct commit_list *result = reduce_heads(*heads);
1111 free_commit_list(*heads);
1112 *heads = result;
1113}
1114
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001115static const char gpg_sig_header[] = "gpgsig";
1116static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1;
1117
1118static int do_sign_commit(struct strbuf *buf, const char *keyid)
1119{
1120 struct strbuf sig = STRBUF_INIT;
1121 int inspos, copypos;
Johannes Schindelin3324dd82016-06-29 16:14:54 +02001122 const char *eoh;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001123
1124 /* find the end of the header */
Johannes Schindelin3324dd82016-06-29 16:14:54 +02001125 eoh = strstr(buf->buf, "\n\n");
1126 if (!eoh)
1127 inspos = buf->len;
1128 else
1129 inspos = eoh - buf->buf + 1;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001130
1131 if (!keyid || !*keyid)
1132 keyid = get_signing_key();
1133 if (sign_buffer(buf, &sig, keyid)) {
1134 strbuf_release(&sig);
1135 return -1;
1136 }
1137
1138 for (copypos = 0; sig.buf[copypos]; ) {
1139 const char *bol = sig.buf + copypos;
1140 const char *eol = strchrnul(bol, '\n');
1141 int len = (eol - bol) + !!*eol;
1142
1143 if (!copypos) {
1144 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1145 inspos += gpg_sig_header_len;
1146 }
1147 strbuf_insert(buf, inspos++, " ", 1);
1148 strbuf_insert(buf, inspos, bol, len);
1149 inspos += len;
1150 copypos += len;
1151 }
1152 strbuf_release(&sig);
1153 return 0;
1154}
1155
Jeff King218aa3a2014-06-13 02:32:11 -04001156int parse_signed_commit(const struct commit *commit,
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001157 struct strbuf *payload, struct strbuf *signature)
1158{
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001159
Jeff King218aa3a2014-06-13 02:32:11 -04001160 unsigned long size;
1161 const char *buffer = get_commit_buffer(commit, &size);
1162 int in_signature, saw_signature = -1;
1163 const char *line, *tail;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001164
1165 line = buffer;
1166 tail = buffer + size;
1167 in_signature = 0;
1168 saw_signature = 0;
1169 while (line < tail) {
1170 const char *sig = NULL;
Jeff King218aa3a2014-06-13 02:32:11 -04001171 const char *next = memchr(line, '\n', tail - line);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001172
1173 next = next ? next + 1 : tail;
1174 if (in_signature && line[0] == ' ')
1175 sig = line + 1;
Christian Couder59556542013-11-30 21:55:40 +01001176 else if (starts_with(line, gpg_sig_header) &&
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001177 line[gpg_sig_header_len] == ' ')
1178 sig = line + gpg_sig_header_len + 1;
1179 if (sig) {
1180 strbuf_add(signature, sig, next - sig);
1181 saw_signature = 1;
1182 in_signature = 1;
1183 } else {
1184 if (*line == '\n')
1185 /* dump the whole remainder of the buffer */
1186 next = tail;
1187 strbuf_add(payload, line, next - line);
1188 in_signature = 0;
1189 }
1190 line = next;
1191 }
Jeff King218aa3a2014-06-13 02:32:11 -04001192 unuse_commit_buffer(commit, buffer);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001193 return saw_signature;
1194}
1195
Christian Couder0b05ab62014-07-19 17:01:12 +02001196int remove_signature(struct strbuf *buf)
1197{
1198 const char *line = buf->buf;
1199 const char *tail = buf->buf + buf->len;
1200 int in_signature = 0;
1201 const char *sig_start = NULL;
1202 const char *sig_end = NULL;
1203
1204 while (line < tail) {
1205 const char *next = memchr(line, '\n', tail - line);
1206 next = next ? next + 1 : tail;
1207
1208 if (in_signature && line[0] == ' ')
1209 sig_end = next;
1210 else if (starts_with(line, gpg_sig_header) &&
1211 line[gpg_sig_header_len] == ' ') {
1212 sig_start = line;
1213 sig_end = next;
1214 in_signature = 1;
1215 } else {
1216 if (*line == '\n')
1217 /* dump the whole remainder of the buffer */
1218 next = tail;
1219 in_signature = 0;
1220 }
1221 line = next;
1222 }
1223
1224 if (sig_start)
1225 strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start);
1226
1227 return sig_start != NULL;
1228}
1229
Junio C Hamano5231c632011-11-07 16:21:32 -08001230static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1231{
1232 struct merge_remote_desc *desc;
1233 struct commit_extra_header *mergetag;
1234 char *buf;
1235 unsigned long size, len;
1236 enum object_type type;
1237
1238 desc = merge_remote_util(parent);
1239 if (!desc || !desc->obj)
1240 return;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001241 buf = read_object_file(&desc->obj->oid, &type, &size);
Junio C Hamano5231c632011-11-07 16:21:32 -08001242 if (!buf || type != OBJ_TAG)
1243 goto free_return;
1244 len = parse_signature(buf, size);
1245 if (size == len)
1246 goto free_return;
1247 /*
1248 * We could verify this signature and either omit the tag when
1249 * it does not validate, but the integrator may not have the
1250 * public key of the signer of the tag he is merging, while a
1251 * later auditor may have it while auditing, so let's not run
1252 * verify-signed-buffer here for now...
1253 *
1254 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1255 * warn("warning: signed tag unverified.");
1256 */
1257 mergetag = xcalloc(1, sizeof(*mergetag));
1258 mergetag->key = xstrdup("mergetag");
1259 mergetag->value = buf;
1260 mergetag->len = size;
1261
1262 **tail = mergetag;
1263 *tail = &mergetag->next;
1264 return;
1265
1266free_return:
1267 free(buf);
1268}
1269
brian m. carlson434060e2015-06-21 23:14:40 +00001270int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001271{
1272 struct strbuf payload = STRBUF_INIT;
1273 struct strbuf signature = STRBUF_INIT;
brian m. carlson434060e2015-06-21 23:14:40 +00001274 int ret = 1;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001275
1276 sigc->result = 'N';
1277
Jeff King218aa3a2014-06-13 02:32:11 -04001278 if (parse_signed_commit(commit, &payload, &signature) <= 0)
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001279 goto out;
brian m. carlson434060e2015-06-21 23:14:40 +00001280 ret = check_signature(payload.buf, payload.len, signature.buf,
1281 signature.len, sigc);
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001282
1283 out:
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001284 strbuf_release(&payload);
1285 strbuf_release(&signature);
brian m. carlson434060e2015-06-21 23:14:40 +00001286
1287 return ret;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001288}
1289
1290
1291
Junio C Hamano5231c632011-11-07 16:21:32 -08001292void append_merge_tag_headers(struct commit_list *parents,
1293 struct commit_extra_header ***tail)
1294{
1295 while (parents) {
1296 struct commit *parent = parents->item;
1297 handle_signed_tag(parent, tail);
1298 parents = parents->next;
1299 }
1300}
1301
1302static void add_extra_header(struct strbuf *buffer,
1303 struct commit_extra_header *extra)
1304{
1305 strbuf_addstr(buffer, extra->key);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001306 if (extra->len)
1307 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1308 else
1309 strbuf_addch(buffer, '\n');
1310}
1311
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001312struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1313 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001314{
1315 struct commit_extra_header *extra = NULL;
1316 unsigned long size;
Jeff King218aa3a2014-06-13 02:32:11 -04001317 const char *buffer = get_commit_buffer(commit, &size);
1318 extra = read_commit_extra_header_lines(buffer, size, exclude);
1319 unuse_commit_buffer(commit, buffer);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001320 return extra;
1321}
1322
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001323int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
Christian Couder063da622014-07-07 08:35:37 +02001324{
1325 struct commit_extra_header *extra, *to_free;
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001326 int res = 0;
Christian Couder063da622014-07-07 08:35:37 +02001327
1328 to_free = read_commit_extra_headers(commit, NULL);
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001329 for (extra = to_free; !res && extra; extra = extra->next) {
Christian Couder063da622014-07-07 08:35:37 +02001330 if (strcmp(extra->key, "mergetag"))
1331 continue; /* not a merge tag */
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001332 res = fn(commit, extra, data);
Christian Couder063da622014-07-07 08:35:37 +02001333 }
1334 free_commit_extra_headers(to_free);
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001335 return res;
Christian Couder063da622014-07-07 08:35:37 +02001336}
1337
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001338static inline int standard_header_field(const char *field, size_t len)
1339{
René Scharfeb0725042017-02-25 20:27:40 +01001340 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1341 (len == 6 && !memcmp(field, "parent", 6)) ||
1342 (len == 6 && !memcmp(field, "author", 6)) ||
1343 (len == 9 && !memcmp(field, "committer", 9)) ||
1344 (len == 8 && !memcmp(field, "encoding", 8)));
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001345}
1346
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001347static int excluded_header_field(const char *field, size_t len, const char **exclude)
1348{
1349 if (!exclude)
1350 return 0;
1351
1352 while (*exclude) {
1353 size_t xlen = strlen(*exclude);
René Scharfeb0725042017-02-25 20:27:40 +01001354 if (len == xlen && !memcmp(field, *exclude, xlen))
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001355 return 1;
1356 exclude++;
1357 }
1358 return 0;
1359}
1360
Junio C Hamano82a75292012-09-15 13:58:15 -07001361static struct commit_extra_header *read_commit_extra_header_lines(
1362 const char *buffer, size_t size,
1363 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001364{
1365 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1366 const char *line, *next, *eof, *eob;
1367 struct strbuf buf = STRBUF_INIT;
1368
1369 for (line = buffer, eob = line + size;
1370 line < eob && *line != '\n';
1371 line = next) {
1372 next = memchr(line, '\n', eob - line);
1373 next = next ? next + 1 : eob;
1374 if (*line == ' ') {
1375 /* continuation */
1376 if (it)
1377 strbuf_add(&buf, line + 1, next - (line + 1));
1378 continue;
1379 }
1380 if (it)
1381 it->value = strbuf_detach(&buf, &it->len);
1382 strbuf_reset(&buf);
1383 it = NULL;
1384
René Scharfe50a01cc2017-02-25 20:21:52 +01001385 eof = memchr(line, ' ', next - line);
1386 if (!eof)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001387 eof = next;
René Scharfeb0725042017-02-25 20:27:40 +01001388 else if (standard_header_field(line, eof - line) ||
1389 excluded_header_field(line, eof - line, exclude))
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001390 continue;
1391
1392 it = xcalloc(1, sizeof(*it));
1393 it->key = xmemdupz(line, eof-line);
1394 *tail = it;
1395 tail = &it->next;
1396 if (eof + 1 < next)
1397 strbuf_add(&buf, eof + 1, next - (eof + 1));
1398 }
1399 if (it)
1400 it->value = strbuf_detach(&buf, &it->len);
1401 return extra;
Junio C Hamano5231c632011-11-07 16:21:32 -08001402}
1403
1404void free_commit_extra_headers(struct commit_extra_header *extra)
1405{
1406 while (extra) {
1407 struct commit_extra_header *next = extra->next;
1408 free(extra->key);
1409 free(extra->value);
1410 free(extra);
1411 extra = next;
1412 }
1413}
1414
Patryk Obara5078f342018-01-28 01:13:16 +01001415int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1416 struct commit_list *parents, struct object_id *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001417 const char *author, const char *sign_commit)
Junio C Hamano5231c632011-11-07 16:21:32 -08001418{
1419 struct commit_extra_header *extra = NULL, **tail = &extra;
1420 int result;
1421
1422 append_merge_tag_headers(parents, &tail);
Jeff King3ffefb52014-06-10 17:36:52 -04001423 result = commit_tree_extended(msg, msg_len, tree, parents, ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001424 author, sign_commit, extra);
Junio C Hamano5231c632011-11-07 16:21:32 -08001425 free_commit_extra_headers(extra);
1426 return result;
1427}
1428
Linus Torvalds08a94a12012-06-28 11:24:14 -07001429static int find_invalid_utf8(const char *buf, int len)
1430{
1431 int offset = 0;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001432 static const unsigned int max_codepoint[] = {
1433 0x7f, 0x7ff, 0xffff, 0x10ffff
1434 };
Linus Torvalds08a94a12012-06-28 11:24:14 -07001435
1436 while (len) {
1437 unsigned char c = *buf++;
1438 int bytes, bad_offset;
brian m. carlson28110d42013-07-04 17:19:43 +00001439 unsigned int codepoint;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001440 unsigned int min_val, max_val;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001441
1442 len--;
1443 offset++;
1444
1445 /* Simple US-ASCII? No worries. */
1446 if (c < 0x80)
1447 continue;
1448
1449 bad_offset = offset-1;
1450
1451 /*
1452 * Count how many more high bits set: that's how
1453 * many more bytes this sequence should have.
1454 */
1455 bytes = 0;
1456 while (c & 0x40) {
1457 c <<= 1;
1458 bytes++;
1459 }
1460
brian m. carlson28110d42013-07-04 17:19:43 +00001461 /*
1462 * Must be between 1 and 3 more bytes. Longer sequences result in
1463 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1464 */
1465 if (bytes < 1 || 3 < bytes)
Linus Torvalds08a94a12012-06-28 11:24:14 -07001466 return bad_offset;
1467
1468 /* Do we *have* that many bytes? */
1469 if (len < bytes)
1470 return bad_offset;
1471
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001472 /*
1473 * Place the encoded bits at the bottom of the value and compute the
1474 * valid range.
1475 */
brian m. carlson28110d42013-07-04 17:19:43 +00001476 codepoint = (c & 0x7f) >> bytes;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001477 min_val = max_codepoint[bytes-1] + 1;
1478 max_val = max_codepoint[bytes];
brian m. carlson28110d42013-07-04 17:19:43 +00001479
Linus Torvalds08a94a12012-06-28 11:24:14 -07001480 offset += bytes;
1481 len -= bytes;
1482
1483 /* And verify that they are good continuation bytes */
1484 do {
brian m. carlson28110d42013-07-04 17:19:43 +00001485 codepoint <<= 6;
1486 codepoint |= *buf & 0x3f;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001487 if ((*buf++ & 0xc0) != 0x80)
1488 return bad_offset;
1489 } while (--bytes);
1490
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001491 /* Reject codepoints that are out of range for the sequence length. */
1492 if (codepoint < min_val || codepoint > max_val)
brian m. carlson28110d42013-07-04 17:19:43 +00001493 return bad_offset;
1494 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1495 if ((codepoint & 0x1ff800) == 0xd800)
1496 return bad_offset;
Peter Krefting81050ac2013-07-09 12:16:33 +01001497 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
Junio C Hamanodc773a62013-08-05 09:52:28 -07001498 if ((codepoint & 0xfffe) == 0xfffe)
Peter Krefting81050ac2013-07-09 12:16:33 +01001499 return bad_offset;
1500 /* So are anything in the range U+FDD0..U+FDEF. */
1501 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
brian m. carlson28110d42013-07-04 17:19:43 +00001502 return bad_offset;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001503 }
1504 return -1;
1505}
1506
1507/*
1508 * This verifies that the buffer is in proper utf8 format.
1509 *
1510 * If it isn't, it assumes any non-utf8 characters are Latin1,
1511 * and does the conversion.
Linus Torvalds08a94a12012-06-28 11:24:14 -07001512 */
1513static int verify_utf8(struct strbuf *buf)
1514{
1515 int ok = 1;
1516 long pos = 0;
1517
1518 for (;;) {
1519 int bad;
1520 unsigned char c;
1521 unsigned char replace[2];
1522
1523 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1524 if (bad < 0)
1525 return ok;
1526 pos += bad;
1527 ok = 0;
1528 c = buf->buf[pos];
1529 strbuf_remove(buf, pos, 1);
1530
1531 /* We know 'c' must be in the range 128-255 */
1532 replace[0] = 0xc0 + (c >> 6);
1533 replace[1] = 0x80 + (c & 0x3f);
1534 strbuf_insert(buf, pos, replace, 2);
1535 pos += 2;
1536 }
1537}
1538
Jeff King40d52ff2010-04-01 20:05:23 -04001539static const char commit_utf8_warn[] =
Vasco Almeida4fa4b312016-09-19 13:08:16 +00001540N_("Warning: commit message did not conform to UTF-8.\n"
1541 "You may want to amend it after fixing the message, or set the config\n"
1542 "variable i18n.commitencoding to the encoding your project uses.\n");
Jeff King40d52ff2010-04-01 20:05:23 -04001543
Jeff King3ffefb52014-06-10 17:36:52 -04001544int commit_tree_extended(const char *msg, size_t msg_len,
Patryk Obara5078f342018-01-28 01:13:16 +01001545 const struct object_id *tree,
1546 struct commit_list *parents, struct object_id *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001547 const char *author, const char *sign_commit,
1548 struct commit_extra_header *extra)
Jeff King40d52ff2010-04-01 20:05:23 -04001549{
1550 int result;
1551 int encoding_is_utf8;
1552 struct strbuf buffer;
1553
brian m. carlsone816caa2018-03-12 02:27:42 +00001554 assert_oid_type(tree, OBJ_TREE);
Jeff King40d52ff2010-04-01 20:05:23 -04001555
Jeff King3ffefb52014-06-10 17:36:52 -04001556 if (memchr(msg, '\0', msg_len))
Nguyễn Thái Ngọc Duy37576c12011-12-15 20:47:23 +07001557 return error("a NUL byte in commit log message not allowed.");
1558
Jeff King40d52ff2010-04-01 20:05:23 -04001559 /* Not having i18n.commitencoding is the same as having utf-8 */
1560 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1561
1562 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
Patryk Obara5078f342018-01-28 01:13:16 +01001563 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
Jeff King40d52ff2010-04-01 20:05:23 -04001564
1565 /*
1566 * NOTE! This ordering means that the same exact tree merged with a
1567 * different order of parents will be a _different_ changeset even
1568 * if everything else stays the same.
1569 */
1570 while (parents) {
René Scharfee510ab82015-10-24 18:21:31 +02001571 struct commit *parent = pop_commit(&parents);
Jeff King40d52ff2010-04-01 20:05:23 -04001572 strbuf_addf(&buffer, "parent %s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001573 oid_to_hex(&parent->object.oid));
Jeff King40d52ff2010-04-01 20:05:23 -04001574 }
1575
1576 /* Person/date information */
1577 if (!author)
Jeff Kingf9bc5732012-05-24 19:28:40 -04001578 author = git_author_info(IDENT_STRICT);
Jeff King40d52ff2010-04-01 20:05:23 -04001579 strbuf_addf(&buffer, "author %s\n", author);
Jeff Kingf9bc5732012-05-24 19:28:40 -04001580 strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT));
Jeff King40d52ff2010-04-01 20:05:23 -04001581 if (!encoding_is_utf8)
1582 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
Junio C Hamano5231c632011-11-07 16:21:32 -08001583
1584 while (extra) {
1585 add_extra_header(&buffer, extra);
1586 extra = extra->next;
1587 }
Jeff King40d52ff2010-04-01 20:05:23 -04001588 strbuf_addch(&buffer, '\n');
1589
1590 /* And add the comment */
Jeff King3ffefb52014-06-10 17:36:52 -04001591 strbuf_add(&buffer, msg, msg_len);
Jeff King40d52ff2010-04-01 20:05:23 -04001592
1593 /* And check the encoding */
Linus Torvalds08a94a12012-06-28 11:24:14 -07001594 if (encoding_is_utf8 && !verify_utf8(&buffer))
Vasco Almeida4fa4b312016-09-19 13:08:16 +00001595 fprintf(stderr, _(commit_utf8_warn));
Jeff King40d52ff2010-04-01 20:05:23 -04001596
Rene Scharfee5051462017-08-30 19:49:38 +02001597 if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1598 result = -1;
1599 goto out;
1600 }
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001601
Patryk Obaraa09c9852018-01-28 01:13:19 +01001602 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
Rene Scharfee5051462017-08-30 19:49:38 +02001603out:
Jeff King40d52ff2010-04-01 20:05:23 -04001604 strbuf_release(&buffer);
1605 return result;
1606}
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001607
René Scharfebeb518c2016-08-13 14:11:27 +02001608void set_merge_remote_desc(struct commit *commit,
1609 const char *name, struct object *obj)
1610{
1611 struct merge_remote_desc *desc;
René Scharfe5447a762016-08-13 14:21:27 +02001612 FLEX_ALLOC_STR(desc, name, name);
René Scharfebeb518c2016-08-13 14:11:27 +02001613 desc->obj = obj;
René Scharfebeb518c2016-08-13 14:11:27 +02001614 commit->util = desc;
1615}
1616
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001617struct commit *get_merge_parent(const char *name)
1618{
1619 struct object *obj;
1620 struct commit *commit;
brian m. carlson7683e2e2015-03-13 23:39:34 +00001621 struct object_id oid;
brian m. carlsone82caf32017-07-13 23:49:28 +00001622 if (get_oid(name, &oid))
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001623 return NULL;
brian m. carlsonc251c832017-05-06 22:10:38 +00001624 obj = parse_object(&oid);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001625 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
René Scharfebeb518c2016-08-13 14:11:27 +02001626 if (commit && !commit->util)
1627 set_merge_remote_desc(commit, name, obj);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001628 return commit;
1629}
René Scharfe89b5f1d2012-04-25 22:35:27 +02001630
1631/*
1632 * Append a commit to the end of the commit_list.
1633 *
1634 * next starts by pointing to the variable that holds the head of an
1635 * empty commit_list, and is updated to point to the "next" field of
1636 * the last item on the list as new commits are appended.
1637 *
1638 * Usage example:
1639 *
1640 * struct commit_list *list;
1641 * struct commit_list **next = &list;
1642 *
1643 * next = commit_list_append(c1, next);
1644 * next = commit_list_append(c2, next);
1645 * assert(commit_list_count(list) == 2);
1646 * return list;
1647 */
1648struct commit_list **commit_list_append(struct commit *commit,
1649 struct commit_list **next)
1650{
Brandon Williams258c03e2018-02-14 10:59:37 -08001651 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1652 new_commit->item = commit;
1653 *next = new_commit;
1654 new_commit->next = NULL;
1655 return &new_commit->next;
René Scharfe89b5f1d2012-04-25 22:35:27 +02001656}
Nguyễn Thái Ngọc Duyefc7df42012-10-26 22:53:51 +07001657
Jeff Kingfe6eb7f2014-08-27 03:56:01 -04001658const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1659{
1660 int key_len = strlen(key);
1661 const char *line = msg;
1662
1663 while (line) {
1664 const char *eol = strchrnul(line, '\n');
1665
1666 if (line == eol)
1667 return NULL;
1668
1669 if (eol - line > key_len &&
1670 !strncmp(line, key, key_len) &&
1671 line[key_len] == ' ') {
1672 *out_len = eol - line - key_len - 1;
1673 return line + key_len + 1;
1674 }
1675 line = *eol ? eol + 1 : NULL;
1676 }
1677 return NULL;
1678}
Junio C Hamano0ed8a4e2014-12-22 12:26:23 -08001679
Christian Couder8c384582014-11-09 10:23:41 +01001680/*
Jonathan Tan710714a2016-11-02 10:29:17 -07001681 * Inspect the given string and determine the true "end" of the log message, in
Christian Couder8c384582014-11-09 10:23:41 +01001682 * order to find where to put a new Signed-off-by: line. Ignored are
Brian Malehornd76650b2017-05-15 23:06:49 -07001683 * trailing comment lines and blank lines. To support "git commit -s
1684 * --amend" on an existing commit, we also ignore "Conflicts:". To
1685 * support "git commit -v", we truncate at cut lines.
Christian Couder8c384582014-11-09 10:23:41 +01001686 *
1687 * Returns the number of bytes from the tail to ignore, to be fed as
1688 * the second parameter to append_signoff().
1689 */
Jonathan Tan710714a2016-11-02 10:29:17 -07001690int ignore_non_trailer(const char *buf, size_t len)
Christian Couder8c384582014-11-09 10:23:41 +01001691{
1692 int boc = 0;
1693 int bol = 0;
1694 int in_old_conflicts_block = 0;
Brian Malehornd76650b2017-05-15 23:06:49 -07001695 size_t cutoff = wt_status_locate_end(buf, len);
Christian Couder8c384582014-11-09 10:23:41 +01001696
Brian Malehornd76650b2017-05-15 23:06:49 -07001697 while (bol < cutoff) {
Jonathan Tan710714a2016-11-02 10:29:17 -07001698 const char *next_line = memchr(buf + bol, '\n', len - bol);
Christian Couder8c384582014-11-09 10:23:41 +01001699
Jonathan Tan710714a2016-11-02 10:29:17 -07001700 if (!next_line)
1701 next_line = buf + len;
Christian Couder8c384582014-11-09 10:23:41 +01001702 else
1703 next_line++;
1704
Jonathan Tan710714a2016-11-02 10:29:17 -07001705 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
Christian Couder8c384582014-11-09 10:23:41 +01001706 /* is this the first of the run of comments? */
1707 if (!boc)
1708 boc = bol;
1709 /* otherwise, it is just continuing */
Jonathan Tan710714a2016-11-02 10:29:17 -07001710 } else if (starts_with(buf + bol, "Conflicts:\n")) {
Christian Couder8c384582014-11-09 10:23:41 +01001711 in_old_conflicts_block = 1;
1712 if (!boc)
1713 boc = bol;
Jonathan Tan710714a2016-11-02 10:29:17 -07001714 } else if (in_old_conflicts_block && buf[bol] == '\t') {
Christian Couder8c384582014-11-09 10:23:41 +01001715 ; /* a pathname in the conflicts block */
1716 } else if (boc) {
1717 /* the previous was not trailing comment */
1718 boc = 0;
1719 in_old_conflicts_block = 0;
1720 }
Jonathan Tan710714a2016-11-02 10:29:17 -07001721 bol = next_line - buf;
Christian Couder8c384582014-11-09 10:23:41 +01001722 }
Brian Malehornd76650b2017-05-15 23:06:49 -07001723 return boc ? len - boc : len - cutoff;
Christian Couder8c384582014-11-09 10:23:41 +01001724}