blob: 551de4903c1f4f5d21a4e30bc012109c593054c8 [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"
Martin Ågrenbc626922020-12-31 12:56:23 +010017#include "hash-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"
Pratik Karki103148a2018-09-04 15:00:08 -070020#include "refs.h"
Junio C Hamano02931212018-11-02 11:04:55 +090021#include "commit-reach.h"
Phillip Wood49697cb2019-10-15 10:25:31 +000022#include "run-command.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060023#include "shallow.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -070024
Junio C Hamano82a75292012-09-15 13:58:15 -070025static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
26
Linus Torvalds60ab26d2005-09-15 14:43:17 -070027int save_commit_buffer = 1;
Ævar Arnfjörð Bjarmasonab628582021-08-23 12:44:02 +020028int no_graft_file_deprecated_advice;
Linus Torvalds60ab26d2005-09-15 14:43:17 -070029
Daniel Barkalow175785e2005-04-18 11:39:48 -070030const char *commit_type = "commit";
31
Stefan Bellerd9a05e72018-06-28 18:22:21 -070032struct commit *lookup_commit_reference_gently(struct repository *r,
Stefan Beller21e1ee82018-06-28 18:21:57 -070033 const struct object_id *oid, int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070034{
Stefan Bellerd9a05e72018-06-28 18:22:21 -070035 struct object *obj = deref_tag(r,
36 parse_object(r, oid),
Stefan Beller109cd762018-06-28 18:21:51 -070037 NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070038
39 if (!obj)
40 return NULL;
Abhishek Kumar6da43d92020-06-17 14:44:08 +053041 return object_as_type(obj, OBJ_COMMIT, quiet);
Junio C Hamanof76412e2005-08-21 02:51:10 -070042}
43
Stefan Beller1f6c72f2018-06-28 18:22:22 -070044struct commit *lookup_commit_reference(struct repository *r, const struct object_id *oid)
Junio C Hamanof76412e2005-08-21 02:51:10 -070045{
Stefan Beller1f6c72f2018-06-28 18:22:22 -070046 return lookup_commit_reference_gently(r, oid, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070047}
48
brian m. carlsonbc832662017-05-06 22:10:10 +000049struct 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 +100050{
Stefan Beller2122f672018-06-28 18:21:58 -070051 struct commit *c = lookup_commit_reference(the_repository, oid);
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100052 if (!c)
53 die(_("could not parse %s"), ref_name);
Jeff King9001dc22018-08-28 17:22:48 -040054 if (!oideq(oid, &c->object.oid)) {
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100055 warning(_("%s %s is not a commit!"),
brian m. carlsonbc832662017-05-06 22:10:10 +000056 ref_name, oid_to_hex(oid));
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100057 }
58 return c;
59}
60
Stefan Bellerbacf1682018-06-28 18:22:10 -070061struct commit *lookup_commit(struct repository *r, const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 11:39:48 -070062{
Jeff Kingd0229ab2019-06-20 03:41:14 -040063 struct object *obj = lookup_object(r, oid);
Jeff Kingd36f51c2014-07-13 02:41:55 -040064 if (!obj)
Jeff Kinga3785092019-06-20 03:41:21 -040065 return create_object(r, oid, alloc_commit_node(r));
Abhishek Kumar6da43d92020-06-17 14:44:08 +053066 return object_as_type(obj, OBJ_COMMIT, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070067}
68
Pat Notza6fa5992010-11-02 13:59:07 -060069struct commit *lookup_commit_reference_by_name(const char *name)
70{
brian m. carlson7683e2e2015-03-13 23:39:34 +000071 struct object_id oid;
Pat Notza6fa5992010-11-02 13:59:07 -060072 struct commit *commit;
73
brian m. carlsone82caf32017-07-13 23:49:28 +000074 if (get_oid_committish(name, &oid))
Pat Notza6fa5992010-11-02 13:59:07 -060075 return NULL;
Stefan Beller2122f672018-06-28 18:21:58 -070076 commit = lookup_commit_reference(the_repository, &oid);
Jeff King5e7d4d32013-10-24 04:53:19 -040077 if (parse_commit(commit))
Pat Notza6fa5992010-11-02 13:59:07 -060078 return NULL;
79 return commit;
80}
81
Johannes Schindelindddbad72017-04-26 21:29:31 +020082static timestamp_t parse_commit_date(const char *buf, const char *tail)
Daniel Barkalow175785e2005-04-18 11:39:48 -070083{
Martin Koegler0a617792008-01-19 18:35:23 +010084 const char *dateptr;
Daniel Barkalow175785e2005-04-18 11:39:48 -070085
Martin Koegler0a617792008-01-19 18:35:23 +010086 if (buf + 6 >= tail)
87 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070088 if (memcmp(buf, "author", 6))
89 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010090 while (buf < tail && *buf++ != '\n')
Daniel Barkalow175785e2005-04-18 11:39:48 -070091 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010092 if (buf + 9 >= tail)
93 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070094 if (memcmp(buf, "committer", 9))
95 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +010096 while (buf < tail && *buf++ != '>')
Daniel Barkalow175785e2005-04-18 11:39:48 -070097 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +010098 if (buf >= tail)
99 return 0;
100 dateptr = buf;
101 while (buf < tail && *buf++ != '\n')
102 /* nada */;
103 if (buf >= tail)
104 return 0;
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200105 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
106 return parse_timestamp(dateptr, NULL, 10);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700107}
108
Jeff King8380dcd2021-01-28 01:20:23 -0500109static const struct object_id *commit_graft_oid_access(size_t index, const void *table)
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400110{
Jeff King8380dcd2021-01-28 01:20:23 -0500111 const struct commit_graft * const *commit_graft_table = table;
Jeff King45ee13b2021-01-28 01:19:42 -0500112 return &commit_graft_table[index]->oid;
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400113}
114
Jeff King98c431b2021-01-28 01:12:35 -0500115int commit_graft_pos(struct repository *r, const struct object_id *oid)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700116{
Jeff King45ee13b2021-01-28 01:19:42 -0500117 return oid_pos(oid, r->parsed_objects->grafts,
118 r->parsed_objects->grafts_nr,
119 commit_graft_oid_access);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700120}
121
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700122int register_commit_graft(struct repository *r, struct commit_graft *graft,
123 int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700124{
Jeff King98c431b2021-01-28 01:12:35 -0500125 int pos = commit_graft_pos(r, &graft->oid);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700126
Junio C Hamano5040f172006-04-06 23:58:51 -0700127 if (0 <= pos) {
128 if (ignore_dups)
129 free(graft);
130 else {
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700131 free(r->parsed_objects->grafts[pos]);
132 r->parsed_objects->grafts[pos] = graft;
Junio C Hamano5040f172006-04-06 23:58:51 -0700133 }
134 return 1;
135 }
136 pos = -pos - 1;
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700137 ALLOC_GROW(r->parsed_objects->grafts,
138 r->parsed_objects->grafts_nr + 1,
139 r->parsed_objects->grafts_alloc);
140 r->parsed_objects->grafts_nr++;
141 if (pos < r->parsed_objects->grafts_nr)
142 memmove(r->parsed_objects->grafts + pos + 1,
143 r->parsed_objects->grafts + pos,
144 (r->parsed_objects->grafts_nr - pos - 1) *
145 sizeof(*r->parsed_objects->grafts));
146 r->parsed_objects->grafts[pos] = graft;
Junio C Hamano5040f172006-04-06 23:58:51 -0700147 return 0;
148}
149
Patryk Obara9a934032017-08-18 20:33:12 +0200150struct commit_graft *read_graft_line(struct strbuf *line)
Junio C Hamano5040f172006-04-06 23:58:51 -0700151{
152 /* The format is just "Commit Parent1 Parent2 ...\n" */
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200153 int i, phase;
154 const char *tail = NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700155 struct commit_graft *graft = NULL;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200156 struct object_id dummy_oid, *oid;
Junio C Hamano5040f172006-04-06 23:58:51 -0700157
Patryk Obara9a934032017-08-18 20:33:12 +0200158 strbuf_rtrim(line);
159 if (!line->len || line->buf[0] == '#')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700160 return NULL;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200161 /*
162 * phase 0 verifies line, counts hashes in line and allocates graft
163 * phase 1 fills graft
164 */
165 for (phase = 0; phase < 2; phase++) {
166 oid = graft ? &graft->oid : &dummy_oid;
167 if (parse_oid_hex(line->buf, oid, &tail))
Junio C Hamano5040f172006-04-06 23:58:51 -0700168 goto bad_graft_data;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200169 for (i = 0; *tail != '\0'; i++) {
170 oid = graft ? &graft->parent[i] : &dummy_oid;
171 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
172 goto bad_graft_data;
173 }
174 if (!graft) {
175 graft = xmalloc(st_add(sizeof(*graft),
176 st_mult(sizeof(struct object_id), i)));
177 graft->nr_parent = i;
178 }
Junio C Hamano5040f172006-04-06 23:58:51 -0700179 }
180 return graft;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100181
182bad_graft_data:
Patryk Obara9a934032017-08-18 20:33:12 +0200183 error("bad graft data: %s", line->buf);
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200184 assert(!graft);
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100185 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700186}
187
Brandon Williamsd0e5dd02018-05-17 15:51:49 -0700188static int read_graft_file(struct repository *r, const char *graft_file)
Junio C Hamano5040f172006-04-06 23:58:51 -0700189{
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700190 FILE *fp = fopen_or_warn(graft_file, "r");
Johannes Schindeline228c172013-12-27 21:49:57 +0100191 struct strbuf buf = STRBUF_INIT;
Junio C Hamano5040f172006-04-06 23:58:51 -0700192 if (!fp)
193 return -1;
Ævar Arnfjörð Bjarmasonab628582021-08-23 12:44:02 +0200194 if (!no_graft_file_deprecated_advice &&
195 advice_enabled(ADVICE_GRAFT_FILE_DEPRECATED))
Johannes Schindelinf9f99b32018-04-29 00:44:44 +0200196 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
197 "and will be removed in a future Git version.\n"
198 "\n"
199 "Please use \"git replace --convert-graft-file\"\n"
200 "to convert the grafts into replace refs.\n"
201 "\n"
202 "Turn this message off by running\n"
203 "\"git config advice.graftFileDeprecated false\""));
Johannes Schindeline228c172013-12-27 21:49:57 +0100204 while (!strbuf_getwholeline(&buf, fp, '\n')) {
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700205 /* The format is just "Commit Parent1 Parent2 ...\n" */
Patryk Obara9a934032017-08-18 20:33:12 +0200206 struct commit_graft *graft = read_graft_line(&buf);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700207 if (!graft)
208 continue;
Brandon Williamsd0e5dd02018-05-17 15:51:49 -0700209 if (register_commit_graft(r, graft, 1))
Johannes Schindeline228c172013-12-27 21:49:57 +0100210 error("duplicate graft data: %s", buf.buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700211 }
212 fclose(fp);
Johannes Schindeline228c172013-12-27 21:49:57 +0100213 strbuf_release(&buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700214 return 0;
215}
216
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000217void prepare_commit_graft(struct repository *r)
Junio C Hamano5040f172006-04-06 23:58:51 -0700218{
Junio C Hamano5040f172006-04-06 23:58:51 -0700219 char *graft_file;
220
Stefan Beller2f6c7672018-05-17 15:51:53 -0700221 if (r->parsed_objects->commit_graft_prepared)
Junio C Hamano5040f172006-04-06 23:58:51 -0700222 return;
Jeff King14a9bd22018-05-31 18:42:53 -0400223 if (!startup_info->have_repository)
224 return;
225
Stefan Beller2f6c7672018-05-17 15:51:53 -0700226 graft_file = get_graft_file(r);
227 read_graft_file(r, graft_file);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100228 /* make sure shallows are read */
Stefan Beller2f6c7672018-05-17 15:51:53 -0700229 is_repository_shallow(r);
230 r->parsed_objects->commit_graft_prepared = 1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700231}
232
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700233struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700234{
235 int pos;
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700236 prepare_commit_graft(r);
Jeff King98c431b2021-01-28 01:12:35 -0500237 pos = commit_graft_pos(r, oid);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700238 if (pos < 0)
239 return NULL;
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700240 return r->parsed_objects->grafts[pos];
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700241}
242
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700243int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100244{
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700245 int i, ret;
Jonathan Nieder6a1a79f2018-05-15 16:42:16 -0700246 for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++)
247 ret = fn(the_repository->parsed_objects->grafts[i], cb_data);
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700248 return ret;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100249}
250
Jeff King8597ea32014-06-10 17:44:13 -0400251struct commit_buffer {
252 void *buffer;
253 unsigned long size;
254};
255define_commit_slab(buffer_slab, struct commit_buffer);
Jeff Kingc1b3c712014-06-10 17:43:02 -0400256
Stefan Beller65ea9d42018-06-28 18:22:15 -0700257struct buffer_slab *allocate_commit_buffer_slab(void)
Jeff King66c28272014-06-10 17:40:14 -0400258{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700259 struct buffer_slab *bs = xmalloc(sizeof(*bs));
260 init_buffer_slab(bs);
261 return bs;
262}
263
264void free_commit_buffer_slab(struct buffer_slab *bs)
265{
266 clear_buffer_slab(bs);
267 free(bs);
268}
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100269
Stefan Beller1a40fc42018-06-28 18:22:16 -0700270void set_commit_buffer(struct repository *r, struct commit *commit, void *buffer, unsigned long size)
Jeff King66c28272014-06-10 17:40:14 -0400271{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700272 struct commit_buffer *v = buffer_slab_at(
Stefan Beller1a40fc42018-06-28 18:22:16 -0700273 r->parsed_objects->buffer_slab, commit);
Jeff King8597ea32014-06-10 17:44:13 -0400274 v->buffer = buffer;
275 v->size = size;
Jeff King66c28272014-06-10 17:40:14 -0400276}
277
Stefan Beller4ff7e5c2018-06-28 18:22:17 -0700278const void *get_cached_commit_buffer(struct repository *r, const struct commit *commit, unsigned long *sizep)
Jeff King152ff1c2014-06-10 17:40:39 -0400279{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700280 struct commit_buffer *v = buffer_slab_peek(
Stefan Beller4ff7e5c2018-06-28 18:22:17 -0700281 r->parsed_objects->buffer_slab, commit);
Junio C Hamano862e7302015-05-14 15:25:52 -0700282 if (!v) {
283 if (sizep)
284 *sizep = 0;
285 return NULL;
286 }
Jeff King8597ea32014-06-10 17:44:13 -0400287 if (sizep)
288 *sizep = v->size;
289 return v->buffer;
Jeff King152ff1c2014-06-10 17:40:39 -0400290}
291
Stefan Beller07de3fd2018-11-13 16:12:57 -0800292const void *repo_get_commit_buffer(struct repository *r,
293 const struct commit *commit,
294 unsigned long *sizep)
Jeff King152ff1c2014-06-10 17:40:39 -0400295{
Stefan Beller07de3fd2018-11-13 16:12:57 -0800296 const void *ret = get_cached_commit_buffer(r, commit, sizep);
Jeff King152ff1c2014-06-10 17:40:39 -0400297 if (!ret) {
298 enum object_type type;
299 unsigned long size;
Stefan Beller07de3fd2018-11-13 16:12:57 -0800300 ret = repo_read_object_file(r, &commit->object.oid, &type, &size);
Jeff King152ff1c2014-06-10 17:40:39 -0400301 if (!ret)
302 die("cannot read commit object %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000303 oid_to_hex(&commit->object.oid));
Jeff King152ff1c2014-06-10 17:40:39 -0400304 if (type != OBJ_COMMIT)
305 die("expected commit for %s, got %s",
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800306 oid_to_hex(&commit->object.oid), type_name(type));
Jeff King8597ea32014-06-10 17:44:13 -0400307 if (sizep)
308 *sizep = size;
Jeff King152ff1c2014-06-10 17:40:39 -0400309 }
310 return ret;
311}
312
Stefan Beller70315372018-11-13 16:12:58 -0800313void repo_unuse_commit_buffer(struct repository *r,
314 const struct commit *commit,
315 const void *buffer)
Jeff King152ff1c2014-06-10 17:40:39 -0400316{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700317 struct commit_buffer *v = buffer_slab_peek(
Stefan Beller70315372018-11-13 16:12:58 -0800318 r->parsed_objects->buffer_slab, commit);
Junio C Hamano862e7302015-05-14 15:25:52 -0700319 if (!(v && v->buffer == buffer))
Jeff King152ff1c2014-06-10 17:40:39 -0400320 free((void *)buffer);
321}
322
Stefan Beller6a7895f2018-12-14 16:09:40 -0800323void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit)
Jeff King0fb370d2014-06-12 18:05:37 -0400324{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700325 struct commit_buffer *v = buffer_slab_peek(
Stefan Beller6a7895f2018-12-14 16:09:40 -0800326 pool->buffer_slab, commit);
Junio C Hamano862e7302015-05-14 15:25:52 -0700327 if (v) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000328 FREE_AND_NULL(v->buffer);
Junio C Hamano862e7302015-05-14 15:25:52 -0700329 v->size = 0;
330 }
Jeff King0fb370d2014-06-12 18:05:37 -0400331}
332
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700333static inline void set_commit_tree(struct commit *c, struct tree *t)
334{
335 c->maybe_tree = t;
336}
337
Nguyễn Thái Ngọc Duy301b8c72019-04-16 16:33:19 +0700338struct tree *repo_get_commit_tree(struct repository *r,
339 const struct commit *commit)
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000340{
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000341 if (commit->maybe_tree || !commit->object.parsed)
342 return commit->maybe_tree;
343
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530344 if (commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
Junio C Hamanoea2dab12019-05-09 00:37:25 +0900345 return get_commit_tree_in_graph(r, commit);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000346
Jeff King83487662019-04-09 19:13:20 -0700347 return NULL;
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000348}
349
350struct object_id *get_commit_tree_oid(const struct commit *commit)
351{
Taylor Blau806278d2019-09-05 18:04:57 -0400352 struct tree *tree = get_commit_tree(commit);
353 return tree ? &tree->object.oid : NULL;
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000354}
355
Stefan Beller6a7895f2018-12-14 16:09:40 -0800356void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)
Stefan Beller14ba97f2018-05-15 14:48:42 -0700357{
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700358 set_commit_tree(c, NULL);
Stefan Beller6a7895f2018-12-14 16:09:40 -0800359 free_commit_buffer(pool, c);
Mike Hommey9784f972019-08-26 11:01:37 +0900360 c->index = 0;
Stefan Beller14ba97f2018-05-15 14:48:42 -0700361 free_commit_list(c->parents);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700362
363 c->object.parsed = 0;
364}
365
Jeff King8597ea32014-06-10 17:44:13 -0400366const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
Jeff King0fb370d2014-06-12 18:05:37 -0400367{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700368 struct commit_buffer *v = buffer_slab_peek(
369 the_repository->parsed_objects->buffer_slab, commit);
Jeff King8597ea32014-06-10 17:44:13 -0400370 void *ret;
371
Junio C Hamano862e7302015-05-14 15:25:52 -0700372 if (!v) {
373 if (sizep)
374 *sizep = 0;
375 return NULL;
376 }
Jeff King8597ea32014-06-10 17:44:13 -0400377 ret = v->buffer;
378 if (sizep)
379 *sizep = v->size;
380
381 v->buffer = NULL;
382 v->size = 0;
Jeff King0fb370d2014-06-12 18:05:37 -0400383 return ret;
384}
385
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700386int parse_commit_buffer(struct repository *r, struct commit *item, const void *buffer, unsigned long size, int check_graph)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700387{
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700388 const char *tail = buffer;
389 const char *bufptr = buffer;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000390 struct object_id parent;
Linus Torvalds6c88be12005-06-20 20:26:03 -0700391 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700392 struct commit_graft *graft;
brian m. carlson2770ccb2018-07-16 01:27:56 +0000393 const int tree_entry_len = the_hash_algo->hexsz + 5;
394 const int parent_entry_len = the_hash_algo->hexsz + 7;
Jeff King12736d22019-10-18 00:43:29 -0400395 struct tree *tree;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400396
Daniel Barkalow175785e2005-04-18 11:39:48 -0700397 if (item->object.parsed)
398 return 0;
Jeff King228c78f2019-10-25 17:20:20 -0400399
400 if (item->parents) {
401 /*
402 * Presumably this is leftover from an earlier failed parse;
403 * clear it out in preparation for us re-parsing (we'll hit the
404 * same error, but that's good, since it lets our caller know
405 * the result cannot be trusted.
406 */
407 free_commit_list(item->parents);
408 item->parents = NULL;
409 }
410
Junio C Hamano3b44f152006-06-28 03:51:00 -0700411 tail += size;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000412 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
413 bufptr[tree_entry_len] != '\n')
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000414 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
brian m. carlson26ea3e72018-05-02 00:25:47 +0000415 if (get_oid_hex(bufptr + 5, &parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800416 return error("bad tree pointer in commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000417 oid_to_hex(&item->object.oid));
Jeff King12736d22019-10-18 00:43:29 -0400418 tree = lookup_tree(r, &parent);
419 if (!tree)
420 return error("bad tree pointer %s in commit %s",
421 oid_to_hex(&parent),
422 oid_to_hex(&item->object.oid));
423 set_commit_tree(item, tree);
brian m. carlson7683e2e2015-03-13 23:39:34 +0000424 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700425 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700426
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700427 graft = lookup_commit_graft(r, &item->object.oid);
Taylor Blauce163642020-07-08 17:10:53 -0400428 if (graft)
429 r->parsed_objects->substituted_parent = 1;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000430 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700431 struct commit *new_parent;
432
brian m. carlson7683e2e2015-03-13 23:39:34 +0000433 if (tail <= bufptr + parent_entry_len + 1 ||
brian m. carlson26ea3e72018-05-02 00:25:47 +0000434 get_oid_hex(bufptr + 7, &parent) ||
brian m. carlson7683e2e2015-03-13 23:39:34 +0000435 bufptr[parent_entry_len] != '\n')
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000436 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
brian m. carlson7683e2e2015-03-13 23:39:34 +0000437 bufptr += parent_entry_len + 1;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +0200438 /*
439 * The clone is shallow if nr_parent < 0, and we must
440 * not traverse its real parents even when we unhide them.
441 */
442 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700443 continue;
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700444 new_parent = lookup_commit(r, &parent);
Jeff Kingc78fe002019-10-18 00:42:13 -0400445 if (!new_parent)
446 return error("bad parent %s in commit %s",
447 oid_to_hex(&parent),
448 oid_to_hex(&item->object.oid));
449 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700450 }
451 if (graft) {
452 int i;
453 struct commit *new_parent;
454 for (i = 0; i < graft->nr_parent; i++) {
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700455 new_parent = lookup_commit(r,
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700456 &graft->parent[i]);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700457 if (!new_parent)
Jeff Kingc78fe002019-10-18 00:42:13 -0400458 return error("bad graft parent %s in commit %s",
459 oid_to_hex(&graft->parent[i]),
460 oid_to_hex(&item->object.oid));
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700461 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700462 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700463 }
Martin Koegler0a617792008-01-19 18:35:23 +0100464 item->date = parse_commit_date(bufptr, tail);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800465
Derrick Stoleee2838d82018-05-01 12:47:13 +0000466 if (check_graph)
Derrick Stoleec7944052019-05-09 07:22:31 -0700467 load_commit_graph_info(r, item);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000468
Jeff King228c78f2019-10-25 17:20:20 -0400469 item->object.parsed = 1;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700470 return 0;
471}
472
Stefan Beller9e5252a2018-11-13 16:12:50 -0800473int repo_parse_commit_internal(struct repository *r,
474 struct commit *item,
475 int quiet_on_missing,
476 int use_commit_graph)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400477{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500478 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400479 void *buffer;
480 unsigned long size;
481 int ret;
482
Martin Koegler9786f682008-02-18 21:48:02 +0100483 if (!item)
484 return -1;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400485 if (item->object.parsed)
486 return 0;
Stefan Beller9e5252a2018-11-13 16:12:50 -0800487 if (use_commit_graph && parse_commit_in_graph(r, item))
Derrick Stolee177722b2018-04-10 08:56:05 -0400488 return 0;
Stefan Beller9e5252a2018-11-13 16:12:50 -0800489 buffer = repo_read_object_file(r, &item->object.oid, &type, &size);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400490 if (!buffer)
Jeff King9cc2b072015-06-01 05:56:26 -0400491 return quiet_on_missing ? -1 :
492 error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000493 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500494 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400495 free(buffer);
496 return error("Object %s not a commit",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000497 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400498 }
Derrick Stolee9b19ada2018-06-27 09:24:30 -0400499
Stefan Beller9e5252a2018-11-13 16:12:50 -0800500 ret = parse_commit_buffer(r, item, buffer, size, 0);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700501 if (save_commit_buffer && !ret) {
Stefan Beller9e5252a2018-11-13 16:12:50 -0800502 set_commit_buffer(r, item, buffer, size);
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700503 return 0;
504 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400505 free(buffer);
506 return ret;
507}
508
Stefan Beller9e5252a2018-11-13 16:12:50 -0800509int repo_parse_commit_gently(struct repository *r,
510 struct commit *item, int quiet_on_missing)
Derrick Stolee9b19ada2018-06-27 09:24:30 -0400511{
Stefan Beller9e5252a2018-11-13 16:12:50 -0800512 return repo_parse_commit_internal(r, item, quiet_on_missing, 1);
Derrick Stolee9b19ada2018-06-27 09:24:30 -0400513}
514
Jeff King7059dcc2013-10-24 04:52:36 -0400515void parse_commit_or_die(struct commit *item)
516{
517 if (parse_commit(item))
518 die("unable to parse commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000519 item ? oid_to_hex(&item->object.oid) : "(null)");
Jeff King7059dcc2013-10-24 04:52:36 -0400520}
521
Christian Couder11af2aa2010-07-22 15:18:30 +0200522int find_commit_subject(const char *commit_buffer, const char **subject)
523{
524 const char *eol;
525 const char *p = commit_buffer;
526
527 while (*p && (*p != '\n' || p[1] != '\n'))
528 p++;
529 if (*p) {
Johannes Schindelin4e1b06d2016-06-22 22:20:20 +0200530 p = skip_blank_lines(p + 2);
Junio C Hamano9c9b03b2017-01-27 18:01:41 -0800531 eol = strchrnul(p, '\n');
Christian Couder11af2aa2010-07-22 15:18:30 +0200532 } else
533 eol = p;
534
535 *subject = p;
536
537 return eol - p;
538}
539
Charvi Mendiratta6e0e2882021-03-15 13:24:31 +0530540size_t commit_subject_length(const char *body)
541{
542 const char *p = body;
543 while (*p) {
544 const char *next = skip_blank_lines(p);
545 if (next != p)
546 break;
547 p = strchrnul(p, '\n');
548 if (*p)
549 p++;
550 }
551 return p - body;
552}
553
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700554struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700555{
Christopher Li812666c2005-04-26 12:00:58 -0700556 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700557 new_list->item = item;
558 new_list->next = *list_p;
559 *list_p = new_list;
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700560 return new_list;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700561}
562
Derrick Stolee597b2c32020-12-08 17:04:13 -0500563int commit_list_contains(struct commit *item, struct commit_list *list)
564{
565 while (list) {
566 if (list->item == item)
567 return 1;
568 list = list->next;
569 }
570
571 return 0;
572}
573
Miklos Vajna65319472008-06-27 18:21:55 +0200574unsigned commit_list_count(const struct commit_list *l)
575{
576 unsigned c = 0;
577 for (; l; l = l->next )
578 c++;
579 return c;
580}
581
Thomas Rast53d00b32013-07-31 22:13:20 +0200582struct commit_list *copy_commit_list(struct commit_list *list)
583{
584 struct commit_list *head = NULL;
585 struct commit_list **pp = &head;
586 while (list) {
René Scharfecb979db2014-07-10 11:47:47 +0200587 pp = commit_list_append(list->item, pp);
Thomas Rast53d00b32013-07-31 22:13:20 +0200588 list = list->next;
589 }
590 return head;
591}
592
Elijah Newrenb0ca1202020-12-16 22:27:59 +0000593struct commit_list *reverse_commit_list(struct commit_list *list)
594{
595 struct commit_list *next = NULL, *current, *backup;
596 for (current = list; current; current = backup) {
597 backup = current->next;
598 current->next = next;
599 next = current;
600 }
601 return next;
602}
603
Daniel Barkalow175785e2005-04-18 11:39:48 -0700604void free_commit_list(struct commit_list *list)
605{
René Scharfee510ab82015-10-24 18:21:31 +0200606 while (list)
607 pop_commit(&list);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700608}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700609
Thiago Farina47e44ed2010-11-26 23:58:14 -0200610struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700611{
612 struct commit_list **pp = list;
613 struct commit_list *p;
614 while ((p = *pp) != NULL) {
615 if (p->item->date < item->date) {
616 break;
617 }
618 pp = &p->next;
619 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700620 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700621}
622
René Scharfe46905892012-04-01 00:10:39 +0200623static int commit_list_compare_by_date(const void *a, const void *b)
624{
Johannes Schindelindddbad72017-04-26 21:29:31 +0200625 timestamp_t a_date = ((const struct commit_list *)a)->item->date;
626 timestamp_t b_date = ((const struct commit_list *)b)->item->date;
René Scharfe46905892012-04-01 00:10:39 +0200627 if (a_date < b_date)
628 return 1;
629 if (a_date > b_date)
630 return -1;
631 return 0;
632}
633
634static void *commit_list_get_next(const void *a)
635{
636 return ((const struct commit_list *)a)->next;
637}
638
639static void commit_list_set_next(void *a, void *next)
640{
641 ((struct commit_list *)a)->next = next;
642}
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700643
Thiago Farina47e44ed2010-11-26 23:58:14 -0200644void commit_list_sort_by_date(struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700645{
Junio C Hamano7365c952012-04-17 11:07:01 -0700646 *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next,
647 commit_list_compare_by_date);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700648}
649
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700650struct commit *pop_most_recent_commit(struct commit_list **list,
651 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700652{
René Scharfee510ab82015-10-24 18:21:31 +0200653 struct commit *ret = pop_commit(list);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700654 struct commit_list *parents = ret->parents;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700655
656 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700657 struct commit *commit = parents->item;
Martin Koeglerdec38c82008-02-18 21:48:03 +0100658 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700659 commit->object.flags |= mark;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200660 commit_list_insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700661 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700662 parents = parents->next;
663 }
664 return ret;
665}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700666
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700667static void clear_commit_marks_1(struct commit_list **plist,
668 struct commit *commit, unsigned int mark)
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800669{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100670 while (commit) {
671 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800672
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100673 if (!(mark & commit->object.flags))
674 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700675
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100676 commit->object.flags &= ~mark;
677
678 parents = commit->parents;
679 if (!parents)
680 return;
681
682 while ((parents = parents->next))
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700683 commit_list_insert(parents->item, plist);
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100684
685 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800686 }
687}
688
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800689void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700690{
691 struct commit_list *list = NULL;
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800692
693 while (nr--) {
René Scharfe07f7d552017-12-25 18:43:37 +0100694 clear_commit_marks_1(&list, *commit, mark);
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800695 commit++;
696 }
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700697 while (list)
698 clear_commit_marks_1(&list, pop_commit(&list), mark);
699}
700
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800701void clear_commit_marks(struct commit *commit, unsigned int mark)
702{
703 clear_commit_marks_many(1, &commit, mark);
704}
705
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000706struct commit *pop_commit(struct commit_list **stack)
707{
708 struct commit_list *top = *stack;
709 struct commit *item = top ? top->item : NULL;
710
711 if (top) {
712 *stack = top->next;
713 free(top);
714 }
715 return item;
716}
717
Jon Seymourab580ac2005-07-07 02:39:34 +1000718/*
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700719 * Topological sort support
720 */
Junio C Hamano66eb3752013-04-12 23:25:49 -0700721
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700722/* count number of children that have not been emitted */
723define_commit_slab(indegree_slab, int);
Jeff King96c4f4a2013-04-09 02:52:56 -0400724
Derrick Stolee18207032018-08-21 20:54:12 +0000725define_commit_slab(author_date_slab, timestamp_t);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700726
Derrick Stolee5284fc52018-11-01 13:46:21 +0000727void record_author_date(struct author_date_slab *author_date,
728 struct commit *commit)
Junio C Hamano81c6b382013-06-07 10:35:54 -0700729{
Jeff King8597ea32014-06-10 17:44:13 -0400730 const char *buffer = get_commit_buffer(commit, NULL);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700731 struct ident_split ident;
Jeff Kingea5517f2014-08-27 03:56:55 -0400732 const char *ident_line;
733 size_t ident_len;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700734 char *date_end;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200735 timestamp_t date;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700736
Jeff Kingea5517f2014-08-27 03:56:55 -0400737 ident_line = find_commit_header(buffer, "author", &ident_len);
738 if (!ident_line)
739 goto fail_exit; /* no author line */
740 if (split_ident_line(&ident, ident_line, ident_len) ||
741 !ident.date_begin || !ident.date_end)
742 goto fail_exit; /* malformed "author" line */
Junio C Hamano81c6b382013-06-07 10:35:54 -0700743
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200744 date = parse_timestamp(ident.date_begin, &date_end, 10);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700745 if (date_end != ident.date_end)
746 goto fail_exit; /* malformed date */
747 *(author_date_slab_at(author_date, commit)) = date;
748
749fail_exit:
Jeff Kingba41c1c2014-06-10 17:41:02 -0400750 unuse_commit_buffer(commit, buffer);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700751}
752
Derrick Stolee5284fc52018-11-01 13:46:21 +0000753int compare_commits_by_author_date(const void *a_, const void *b_,
754 void *cb_data)
Junio C Hamano81c6b382013-06-07 10:35:54 -0700755{
756 const struct commit *a = a_, *b = b_;
757 struct author_date_slab *author_date = cb_data;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200758 timestamp_t a_date = *(author_date_slab_at(author_date, a));
759 timestamp_t b_date = *(author_date_slab_at(author_date, b));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700760
761 /* newer commits with larger date first */
762 if (a_date < b_date)
763 return 1;
764 else if (a_date > b_date)
765 return -1;
766 return 0;
767}
768
Derrick Stolee3afc6792018-05-01 12:47:11 +0000769int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused)
770{
771 const struct commit *a = a_, *b = b_;
Abhishek Kumard7f92782021-01-16 18:11:13 +0000772 const timestamp_t generation_a = commit_graph_generation(a),
773 generation_b = commit_graph_generation(b);
Derrick Stolee3afc6792018-05-01 12:47:11 +0000774
775 /* newer commits first */
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530776 if (generation_a < generation_b)
Derrick Stolee3afc6792018-05-01 12:47:11 +0000777 return 1;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530778 else if (generation_a > generation_b)
Derrick Stolee3afc6792018-05-01 12:47:11 +0000779 return -1;
780
781 /* use date as a heuristic when generations are equal */
782 if (a->date < b->date)
783 return 1;
784 else if (a->date > b->date)
785 return -1;
786 return 0;
787}
788
Jeff King727377f2013-07-02 02:21:48 -0400789int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700790{
791 const struct commit *a = a_, *b = b_;
792 /* newer commits with larger date first */
793 if (a->date < b->date)
794 return 1;
795 else if (a->date > b->date)
796 return -1;
797 return 0;
798}
799
Jon Seymourab580ac2005-07-07 02:39:34 +1000800/*
801 * Performs an in-place topological sort on the list supplied.
802 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700803void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
Jon Seymourab580ac2005-07-07 02:39:34 +1000804{
Linus Torvalds23c17d42007-11-02 13:32:58 -0700805 struct commit_list *next, *orig = *list;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700806 struct commit_list **pptr;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700807 struct indegree_slab indegree;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700808 struct prio_queue queue;
809 struct commit *commit;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700810 struct author_date_slab author_date;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100811
Linus Torvalds23c17d42007-11-02 13:32:58 -0700812 if (!orig)
Eric Wong7d6fb372005-12-24 04:12:43 -0800813 return;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700814 *list = NULL;
815
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700816 init_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700817 memset(&queue, '\0', sizeof(queue));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700818
Junio C Hamanoda24b102013-06-06 21:58:12 -0700819 switch (sort_order) {
820 default: /* REV_SORT_IN_GRAPH_ORDER */
821 queue.compare = NULL;
822 break;
823 case REV_SORT_BY_COMMIT_DATE:
824 queue.compare = compare_commits_by_commit_date;
825 break;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700826 case REV_SORT_BY_AUTHOR_DATE:
827 init_author_date_slab(&author_date);
828 queue.compare = compare_commits_by_author_date;
829 queue.cb_data = &author_date;
830 break;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700831 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400832
Linus Torvalds23c17d42007-11-02 13:32:58 -0700833 /* Mark them and clear the indegree */
834 for (next = orig; next; next = next->next) {
835 struct commit *commit = next->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700836 *(indegree_slab_at(&indegree, commit)) = 1;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700837 /* also record the author dates, if needed */
838 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
839 record_author_date(&author_date, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000840 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700841
Jon Seymourab580ac2005-07-07 02:39:34 +1000842 /* update the indegree */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700843 for (next = orig; next; next = next->next) {
Junio C Hamanoda24b102013-06-06 21:58:12 -0700844 struct commit_list *parents = next->item->parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000845 while (parents) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700846 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700847 int *pi = indegree_slab_at(&indegree, parent);
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100848
Jeff King96c4f4a2013-04-09 02:52:56 -0400849 if (*pi)
850 (*pi)++;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700851 parents = parents->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000852 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000853 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700854
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700855 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200856 * find the tips
857 *
858 * tips are nodes not reachable from any other node in the list
859 *
860 * the tips serve as a starting set for the work queue.
861 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700862 for (next = orig; next; next = next->next) {
863 struct commit *commit = next->item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000864
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700865 if (*(indegree_slab_at(&indegree, commit)) == 1)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700866 prio_queue_put(&queue, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000867 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800868
Junio C Hamanoda24b102013-06-06 21:58:12 -0700869 /*
870 * This is unfortunate; the initial tips need to be shown
871 * in the order given from the revision traversal machinery.
872 */
873 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
874 prio_queue_reverse(&queue);
875
876 /* We no longer need the commit list */
877 free_commit_list(orig);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700878
879 pptr = list;
880 *list = NULL;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700881 while ((commit = prio_queue_get(&queue)) != NULL) {
882 struct commit_list *parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000883
Linus Torvalds23c17d42007-11-02 13:32:58 -0700884 for (parents = commit->parents; parents ; parents = parents->next) {
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -0700885 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700886 int *pi = indegree_slab_at(&indegree, parent);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700887
Jeff King96c4f4a2013-04-09 02:52:56 -0400888 if (!*pi)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700889 continue;
890
891 /*
892 * parents are only enqueued for emission
893 * when all their children have been emitted thereby
894 * guaranteeing topological order.
895 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700896 if (--(*pi) == 1)
897 prio_queue_put(&queue, parent);
Jon Seymourab580ac2005-07-07 02:39:34 +1000898 }
899 /*
Junio C Hamanoda24b102013-06-06 21:58:12 -0700900 * all children of commit have already been
901 * emitted. we can emit it now.
Pierre Habouzit674d1722007-09-10 12:35:06 +0200902 */
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700903 *(indegree_slab_at(&indegree, commit)) = 0;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700904
905 pptr = &commit_list_insert(commit, pptr)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000906 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400907
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700908 clear_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700909 clear_prio_queue(&queue);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700910 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
911 clear_author_date_slab(&author_date);
Jon Seymourab580ac2005-07-07 02:39:34 +1000912}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200913
Pratik Karki103148a2018-09-04 15:00:08 -0700914struct rev_collect {
915 struct commit **commit;
916 int nr;
917 int alloc;
918 unsigned int initial : 1;
919};
920
921static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
922{
923 struct commit *commit;
924
925 if (is_null_oid(oid))
926 return;
927
928 commit = lookup_commit(the_repository, oid);
929 if (!commit ||
930 (commit->object.flags & TMP_MARK) ||
931 parse_commit(commit))
932 return;
933
934 ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc);
935 revs->commit[revs->nr++] = commit;
936 commit->object.flags |= TMP_MARK;
937}
938
939static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
940 const char *ident, timestamp_t timestamp,
941 int tz, const char *message, void *cbdata)
942{
943 struct rev_collect *revs = cbdata;
944
945 if (revs->initial) {
946 revs->initial = 0;
947 add_one_commit(ooid, revs);
948 }
949 add_one_commit(noid, revs);
950 return 0;
951}
952
953struct commit *get_fork_point(const char *refname, struct commit *commit)
954{
955 struct object_id oid;
956 struct rev_collect revs;
957 struct commit_list *bases;
958 int i;
959 struct commit *ret = NULL;
Junio C Hamanof08132f2019-12-09 10:51:47 -0800960 char *full_refname;
961
Jonathan Tanf24c30e2020-09-01 15:28:09 -0700962 switch (dwim_ref(refname, strlen(refname), &oid, &full_refname, 0)) {
Junio C Hamanof08132f2019-12-09 10:51:47 -0800963 case 0:
964 die("No such ref: '%s'", refname);
965 case 1:
966 break; /* good */
967 default:
968 die("Ambiguous refname: '%s'", refname);
969 }
Pratik Karki103148a2018-09-04 15:00:08 -0700970
971 memset(&revs, 0, sizeof(revs));
972 revs.initial = 1;
Junio C Hamanof08132f2019-12-09 10:51:47 -0800973 for_each_reflog_ent(full_refname, collect_one_reflog_ent, &revs);
Pratik Karki103148a2018-09-04 15:00:08 -0700974
Junio C Hamanof08132f2019-12-09 10:51:47 -0800975 if (!revs.nr)
Pratik Karki103148a2018-09-04 15:00:08 -0700976 add_one_commit(&oid, &revs);
977
978 for (i = 0; i < revs.nr; i++)
979 revs.commit[i]->object.flags &= ~TMP_MARK;
980
981 bases = get_merge_bases_many(commit, revs.nr, revs.commit);
982
983 /*
984 * There should be one and only one merge base, when we found
985 * a common ancestor among reflog entries.
986 */
987 if (!bases || bases->next)
988 goto cleanup_return;
989
990 /* And the found one must be one of the reflog entries */
991 for (i = 0; i < revs.nr; i++)
992 if (&bases->item->object == &revs.commit[i]->object)
993 break; /* found */
994 if (revs.nr <= i)
995 goto cleanup_return;
996
997 ret = bases->item;
998
999cleanup_return:
1000 free_commit_list(bases);
Junio C Hamanof08132f2019-12-09 10:51:47 -08001001 free(full_refname);
Pratik Karki103148a2018-09-04 15:00:08 -07001002 return ret;
1003}
1004
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001005/*
1006 * Indexed by hash algorithm identifier.
1007 */
1008static const char *gpg_sig_headers[] = {
1009 NULL,
1010 "gpgsig",
1011 "gpgsig-sha256",
1012};
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001013
brian m. carlson937032e2021-02-11 02:08:04 +00001014int sign_with_header(struct strbuf *buf, const char *keyid)
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001015{
1016 struct strbuf sig = STRBUF_INIT;
1017 int inspos, copypos;
Johannes Schindelin3324dd82016-06-29 16:14:54 +02001018 const char *eoh;
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001019 const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(the_hash_algo)];
1020 int gpg_sig_header_len = strlen(gpg_sig_header);
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001021
1022 /* find the end of the header */
Johannes Schindelin3324dd82016-06-29 16:14:54 +02001023 eoh = strstr(buf->buf, "\n\n");
1024 if (!eoh)
1025 inspos = buf->len;
1026 else
1027 inspos = eoh - buf->buf + 1;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001028
1029 if (!keyid || !*keyid)
1030 keyid = get_signing_key();
1031 if (sign_buffer(buf, &sig, keyid)) {
1032 strbuf_release(&sig);
1033 return -1;
1034 }
1035
1036 for (copypos = 0; sig.buf[copypos]; ) {
1037 const char *bol = sig.buf + copypos;
1038 const char *eol = strchrnul(bol, '\n');
1039 int len = (eol - bol) + !!*eol;
1040
1041 if (!copypos) {
1042 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1043 inspos += gpg_sig_header_len;
1044 }
René Scharfea91cc7f2020-02-09 14:44:23 +01001045 strbuf_insertstr(buf, inspos++, " ");
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001046 strbuf_insert(buf, inspos, bol, len);
1047 inspos += len;
1048 copypos += len;
1049 }
1050 strbuf_release(&sig);
1051 return 0;
1052}
1053
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001054
brian m. carlson937032e2021-02-11 02:08:04 +00001055
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001056int parse_signed_commit(const struct commit *commit,
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001057 struct strbuf *payload, struct strbuf *signature,
1058 const struct git_hash_algo *algop)
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001059{
Jeff King218aa3a2014-06-13 02:32:11 -04001060 unsigned long size;
1061 const char *buffer = get_commit_buffer(commit, &size);
brian m. carlson937032e2021-02-11 02:08:04 +00001062 int ret = parse_buffer_signed_by_header(buffer, size, payload, signature, algop);
1063
1064 unuse_commit_buffer(commit, buffer);
1065 return ret;
1066}
1067
1068int parse_buffer_signed_by_header(const char *buffer,
1069 unsigned long size,
1070 struct strbuf *payload,
1071 struct strbuf *signature,
1072 const struct git_hash_algo *algop)
1073{
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001074 int in_signature = 0, saw_signature = 0, other_signature = 0;
1075 const char *line, *tail, *p;
1076 const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(algop)];
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001077
1078 line = buffer;
1079 tail = buffer + size;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001080 while (line < tail) {
1081 const char *sig = NULL;
Jeff King218aa3a2014-06-13 02:32:11 -04001082 const char *next = memchr(line, '\n', tail - line);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001083
1084 next = next ? next + 1 : tail;
1085 if (in_signature && line[0] == ' ')
1086 sig = line + 1;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001087 else if (skip_prefix(line, gpg_sig_header, &p) &&
1088 *p == ' ') {
1089 sig = line + strlen(gpg_sig_header) + 1;
1090 other_signature = 0;
1091 }
1092 else if (starts_with(line, "gpgsig"))
1093 other_signature = 1;
1094 else if (other_signature && line[0] != ' ')
1095 other_signature = 0;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001096 if (sig) {
1097 strbuf_add(signature, sig, next - sig);
1098 saw_signature = 1;
1099 in_signature = 1;
1100 } else {
1101 if (*line == '\n')
1102 /* dump the whole remainder of the buffer */
1103 next = tail;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001104 if (!other_signature)
1105 strbuf_add(payload, line, next - line);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001106 in_signature = 0;
1107 }
1108 line = next;
1109 }
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001110 return saw_signature;
1111}
1112
Christian Couder0b05ab62014-07-19 17:01:12 +02001113int remove_signature(struct strbuf *buf)
1114{
1115 const char *line = buf->buf;
1116 const char *tail = buf->buf + buf->len;
1117 int in_signature = 0;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001118 struct sigbuf {
1119 const char *start;
1120 const char *end;
1121 } sigs[2], *sigp = &sigs[0];
1122 int i;
1123 const char *orig_buf = buf->buf;
1124
1125 memset(sigs, 0, sizeof(sigs));
Christian Couder0b05ab62014-07-19 17:01:12 +02001126
1127 while (line < tail) {
1128 const char *next = memchr(line, '\n', tail - line);
1129 next = next ? next + 1 : tail;
1130
1131 if (in_signature && line[0] == ' ')
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001132 sigp->end = next;
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001133 else if (starts_with(line, "gpgsig")) {
1134 int i;
1135 for (i = 1; i < GIT_HASH_NALGOS; i++) {
1136 const char *p;
1137 if (skip_prefix(line, gpg_sig_headers[i], &p) &&
1138 *p == ' ') {
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001139 sigp->start = line;
1140 sigp->end = next;
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001141 in_signature = 1;
1142 }
1143 }
Christian Couder0b05ab62014-07-19 17:01:12 +02001144 } else {
1145 if (*line == '\n')
1146 /* dump the whole remainder of the buffer */
1147 next = tail;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001148 if (in_signature && sigp - sigs != ARRAY_SIZE(sigs))
1149 sigp++;
Christian Couder0b05ab62014-07-19 17:01:12 +02001150 in_signature = 0;
1151 }
1152 line = next;
1153 }
1154
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001155 for (i = ARRAY_SIZE(sigs) - 1; i >= 0; i--)
1156 if (sigs[i].start)
1157 strbuf_remove(buf, sigs[i].start - orig_buf, sigs[i].end - sigs[i].start);
Christian Couder0b05ab62014-07-19 17:01:12 +02001158
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001159 return sigs[0].start != NULL;
Christian Couder0b05ab62014-07-19 17:01:12 +02001160}
1161
Junio C Hamano5231c632011-11-07 16:21:32 -08001162static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1163{
1164 struct merge_remote_desc *desc;
1165 struct commit_extra_header *mergetag;
1166 char *buf;
brian m. carlson482c1192021-02-11 02:08:03 +00001167 unsigned long size;
Junio C Hamano5231c632011-11-07 16:21:32 -08001168 enum object_type type;
brian m. carlson482c1192021-02-11 02:08:03 +00001169 struct strbuf payload = STRBUF_INIT;
1170 struct strbuf signature = STRBUF_INIT;
Junio C Hamano5231c632011-11-07 16:21:32 -08001171
1172 desc = merge_remote_util(parent);
1173 if (!desc || !desc->obj)
1174 return;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001175 buf = read_object_file(&desc->obj->oid, &type, &size);
Junio C Hamano5231c632011-11-07 16:21:32 -08001176 if (!buf || type != OBJ_TAG)
1177 goto free_return;
brian m. carlson482c1192021-02-11 02:08:03 +00001178 if (!parse_signature(buf, size, &payload, &signature))
Junio C Hamano5231c632011-11-07 16:21:32 -08001179 goto free_return;
1180 /*
1181 * We could verify this signature and either omit the tag when
1182 * it does not validate, but the integrator may not have the
Felipe Contreras0e20b222021-06-15 14:11:10 +00001183 * public key of the signer of the tag being merged, while a
Junio C Hamano5231c632011-11-07 16:21:32 -08001184 * later auditor may have it while auditing, so let's not run
1185 * verify-signed-buffer here for now...
1186 *
1187 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1188 * warn("warning: signed tag unverified.");
1189 */
René Scharfeca56dad2021-03-13 17:17:22 +01001190 CALLOC_ARRAY(mergetag, 1);
Junio C Hamano5231c632011-11-07 16:21:32 -08001191 mergetag->key = xstrdup("mergetag");
1192 mergetag->value = buf;
1193 mergetag->len = size;
1194
1195 **tail = mergetag;
1196 *tail = &mergetag->next;
brian m. carlson482c1192021-02-11 02:08:03 +00001197 strbuf_release(&payload);
1198 strbuf_release(&signature);
Junio C Hamano5231c632011-11-07 16:21:32 -08001199 return;
1200
1201free_return:
1202 free(buf);
1203}
1204
brian m. carlson434060e2015-06-21 23:14:40 +00001205int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001206{
1207 struct strbuf payload = STRBUF_INIT;
1208 struct strbuf signature = STRBUF_INIT;
brian m. carlson434060e2015-06-21 23:14:40 +00001209 int ret = 1;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001210
1211 sigc->result = 'N';
1212
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001213 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001214 goto out;
brian m. carlson434060e2015-06-21 23:14:40 +00001215 ret = check_signature(payload.buf, payload.len, signature.buf,
1216 signature.len, sigc);
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001217
1218 out:
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001219 strbuf_release(&payload);
1220 strbuf_release(&signature);
brian m. carlson434060e2015-06-21 23:14:40 +00001221
1222 return ret;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001223}
1224
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001225void verify_merge_signature(struct commit *commit, int verbosity,
1226 int check_trust)
Jeff Kingedc4d472018-11-06 02:50:17 -05001227{
1228 char hex[GIT_MAX_HEXSZ + 1];
1229 struct signature_check signature_check;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001230 int ret;
Jeff Kingedc4d472018-11-06 02:50:17 -05001231 memset(&signature_check, 0, sizeof(signature_check));
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001232
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001233 ret = check_commit_signature(commit, &signature_check);
Jeff Kingedc4d472018-11-06 02:50:17 -05001234
1235 find_unique_abbrev_r(hex, &commit->object.oid, DEFAULT_ABBREV);
1236 switch (signature_check.result) {
1237 case 'G':
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001238 if (ret || (check_trust && signature_check.trust_level < TRUST_MARGINAL))
1239 die(_("Commit %s has an untrusted GPG signature, "
1240 "allegedly by %s."), hex, signature_check.signer);
Jeff Kingedc4d472018-11-06 02:50:17 -05001241 break;
Jeff Kingedc4d472018-11-06 02:50:17 -05001242 case 'B':
1243 die(_("Commit %s has a bad GPG signature "
1244 "allegedly by %s."), hex, signature_check.signer);
1245 default: /* 'N' */
1246 die(_("Commit %s does not have a GPG signature."), hex);
1247 }
1248 if (verbosity >= 0 && signature_check.result == 'G')
1249 printf(_("Commit %s has a good GPG signature by %s\n"),
1250 hex, signature_check.signer);
1251
1252 signature_check_clear(&signature_check);
1253}
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001254
Junio C Hamano5231c632011-11-07 16:21:32 -08001255void append_merge_tag_headers(struct commit_list *parents,
1256 struct commit_extra_header ***tail)
1257{
1258 while (parents) {
1259 struct commit *parent = parents->item;
1260 handle_signed_tag(parent, tail);
1261 parents = parents->next;
1262 }
1263}
1264
1265static void add_extra_header(struct strbuf *buffer,
1266 struct commit_extra_header *extra)
1267{
1268 strbuf_addstr(buffer, extra->key);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001269 if (extra->len)
1270 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1271 else
1272 strbuf_addch(buffer, '\n');
1273}
1274
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001275struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1276 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001277{
1278 struct commit_extra_header *extra = NULL;
1279 unsigned long size;
Jeff King218aa3a2014-06-13 02:32:11 -04001280 const char *buffer = get_commit_buffer(commit, &size);
1281 extra = read_commit_extra_header_lines(buffer, size, exclude);
1282 unuse_commit_buffer(commit, buffer);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001283 return extra;
1284}
1285
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001286int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
Christian Couder063da622014-07-07 08:35:37 +02001287{
1288 struct commit_extra_header *extra, *to_free;
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001289 int res = 0;
Christian Couder063da622014-07-07 08:35:37 +02001290
1291 to_free = read_commit_extra_headers(commit, NULL);
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001292 for (extra = to_free; !res && extra; extra = extra->next) {
Christian Couder063da622014-07-07 08:35:37 +02001293 if (strcmp(extra->key, "mergetag"))
1294 continue; /* not a merge tag */
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001295 res = fn(commit, extra, data);
Christian Couder063da622014-07-07 08:35:37 +02001296 }
1297 free_commit_extra_headers(to_free);
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001298 return res;
Christian Couder063da622014-07-07 08:35:37 +02001299}
1300
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001301static inline int standard_header_field(const char *field, size_t len)
1302{
René Scharfeb0725042017-02-25 20:27:40 +01001303 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1304 (len == 6 && !memcmp(field, "parent", 6)) ||
1305 (len == 6 && !memcmp(field, "author", 6)) ||
1306 (len == 9 && !memcmp(field, "committer", 9)) ||
1307 (len == 8 && !memcmp(field, "encoding", 8)));
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001308}
1309
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001310static int excluded_header_field(const char *field, size_t len, const char **exclude)
1311{
1312 if (!exclude)
1313 return 0;
1314
1315 while (*exclude) {
1316 size_t xlen = strlen(*exclude);
René Scharfeb0725042017-02-25 20:27:40 +01001317 if (len == xlen && !memcmp(field, *exclude, xlen))
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001318 return 1;
1319 exclude++;
1320 }
1321 return 0;
1322}
1323
Junio C Hamano82a75292012-09-15 13:58:15 -07001324static struct commit_extra_header *read_commit_extra_header_lines(
1325 const char *buffer, size_t size,
1326 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001327{
1328 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1329 const char *line, *next, *eof, *eob;
1330 struct strbuf buf = STRBUF_INIT;
1331
1332 for (line = buffer, eob = line + size;
1333 line < eob && *line != '\n';
1334 line = next) {
1335 next = memchr(line, '\n', eob - line);
1336 next = next ? next + 1 : eob;
1337 if (*line == ' ') {
1338 /* continuation */
1339 if (it)
1340 strbuf_add(&buf, line + 1, next - (line + 1));
1341 continue;
1342 }
1343 if (it)
1344 it->value = strbuf_detach(&buf, &it->len);
1345 strbuf_reset(&buf);
1346 it = NULL;
1347
René Scharfe50a01cc2017-02-25 20:21:52 +01001348 eof = memchr(line, ' ', next - line);
1349 if (!eof)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001350 eof = next;
René Scharfeb0725042017-02-25 20:27:40 +01001351 else if (standard_header_field(line, eof - line) ||
1352 excluded_header_field(line, eof - line, exclude))
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001353 continue;
1354
René Scharfeca56dad2021-03-13 17:17:22 +01001355 CALLOC_ARRAY(it, 1);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001356 it->key = xmemdupz(line, eof-line);
1357 *tail = it;
1358 tail = &it->next;
1359 if (eof + 1 < next)
1360 strbuf_add(&buf, eof + 1, next - (eof + 1));
1361 }
1362 if (it)
1363 it->value = strbuf_detach(&buf, &it->len);
1364 return extra;
Junio C Hamano5231c632011-11-07 16:21:32 -08001365}
1366
1367void free_commit_extra_headers(struct commit_extra_header *extra)
1368{
1369 while (extra) {
1370 struct commit_extra_header *next = extra->next;
1371 free(extra->key);
1372 free(extra->value);
1373 free(extra);
1374 extra = next;
1375 }
1376}
1377
Patryk Obara5078f342018-01-28 01:13:16 +01001378int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1379 struct commit_list *parents, struct object_id *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001380 const char *author, const char *sign_commit)
Junio C Hamano5231c632011-11-07 16:21:32 -08001381{
1382 struct commit_extra_header *extra = NULL, **tail = &extra;
1383 int result;
1384
1385 append_merge_tag_headers(parents, &tail);
Phillip Woode8cbe212020-08-17 18:40:01 +01001386 result = commit_tree_extended(msg, msg_len, tree, parents, ret, author,
1387 NULL, sign_commit, extra);
Junio C Hamano5231c632011-11-07 16:21:32 -08001388 free_commit_extra_headers(extra);
1389 return result;
1390}
1391
Linus Torvalds08a94a12012-06-28 11:24:14 -07001392static int find_invalid_utf8(const char *buf, int len)
1393{
1394 int offset = 0;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001395 static const unsigned int max_codepoint[] = {
1396 0x7f, 0x7ff, 0xffff, 0x10ffff
1397 };
Linus Torvalds08a94a12012-06-28 11:24:14 -07001398
1399 while (len) {
1400 unsigned char c = *buf++;
1401 int bytes, bad_offset;
brian m. carlson28110d42013-07-04 17:19:43 +00001402 unsigned int codepoint;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001403 unsigned int min_val, max_val;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001404
1405 len--;
1406 offset++;
1407
1408 /* Simple US-ASCII? No worries. */
1409 if (c < 0x80)
1410 continue;
1411
1412 bad_offset = offset-1;
1413
1414 /*
1415 * Count how many more high bits set: that's how
1416 * many more bytes this sequence should have.
1417 */
1418 bytes = 0;
1419 while (c & 0x40) {
1420 c <<= 1;
1421 bytes++;
1422 }
1423
brian m. carlson28110d42013-07-04 17:19:43 +00001424 /*
1425 * Must be between 1 and 3 more bytes. Longer sequences result in
1426 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1427 */
1428 if (bytes < 1 || 3 < bytes)
Linus Torvalds08a94a12012-06-28 11:24:14 -07001429 return bad_offset;
1430
1431 /* Do we *have* that many bytes? */
1432 if (len < bytes)
1433 return bad_offset;
1434
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001435 /*
1436 * Place the encoded bits at the bottom of the value and compute the
1437 * valid range.
1438 */
brian m. carlson28110d42013-07-04 17:19:43 +00001439 codepoint = (c & 0x7f) >> bytes;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001440 min_val = max_codepoint[bytes-1] + 1;
1441 max_val = max_codepoint[bytes];
brian m. carlson28110d42013-07-04 17:19:43 +00001442
Linus Torvalds08a94a12012-06-28 11:24:14 -07001443 offset += bytes;
1444 len -= bytes;
1445
1446 /* And verify that they are good continuation bytes */
1447 do {
brian m. carlson28110d42013-07-04 17:19:43 +00001448 codepoint <<= 6;
1449 codepoint |= *buf & 0x3f;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001450 if ((*buf++ & 0xc0) != 0x80)
1451 return bad_offset;
1452 } while (--bytes);
1453
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001454 /* Reject codepoints that are out of range for the sequence length. */
1455 if (codepoint < min_val || codepoint > max_val)
brian m. carlson28110d42013-07-04 17:19:43 +00001456 return bad_offset;
1457 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1458 if ((codepoint & 0x1ff800) == 0xd800)
1459 return bad_offset;
Peter Krefting81050ac2013-07-09 12:16:33 +01001460 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
Junio C Hamanodc773a62013-08-05 09:52:28 -07001461 if ((codepoint & 0xfffe) == 0xfffe)
Peter Krefting81050ac2013-07-09 12:16:33 +01001462 return bad_offset;
1463 /* So are anything in the range U+FDD0..U+FDEF. */
1464 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
brian m. carlson28110d42013-07-04 17:19:43 +00001465 return bad_offset;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001466 }
1467 return -1;
1468}
1469
1470/*
1471 * This verifies that the buffer is in proper utf8 format.
1472 *
1473 * If it isn't, it assumes any non-utf8 characters are Latin1,
1474 * and does the conversion.
Linus Torvalds08a94a12012-06-28 11:24:14 -07001475 */
1476static int verify_utf8(struct strbuf *buf)
1477{
1478 int ok = 1;
1479 long pos = 0;
1480
1481 for (;;) {
1482 int bad;
1483 unsigned char c;
1484 unsigned char replace[2];
1485
1486 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1487 if (bad < 0)
1488 return ok;
1489 pos += bad;
1490 ok = 0;
1491 c = buf->buf[pos];
1492 strbuf_remove(buf, pos, 1);
1493
1494 /* We know 'c' must be in the range 128-255 */
1495 replace[0] = 0xc0 + (c >> 6);
1496 replace[1] = 0x80 + (c & 0x3f);
1497 strbuf_insert(buf, pos, replace, 2);
1498 pos += 2;
1499 }
1500}
1501
Jeff King40d52ff2010-04-01 20:05:23 -04001502static const char commit_utf8_warn[] =
Vasco Almeida4fa4b312016-09-19 13:08:16 +00001503N_("Warning: commit message did not conform to UTF-8.\n"
1504 "You may want to amend it after fixing the message, or set the config\n"
1505 "variable i18n.commitencoding to the encoding your project uses.\n");
Jeff King40d52ff2010-04-01 20:05:23 -04001506
Jeff King3ffefb52014-06-10 17:36:52 -04001507int commit_tree_extended(const char *msg, size_t msg_len,
Patryk Obara5078f342018-01-28 01:13:16 +01001508 const struct object_id *tree,
1509 struct commit_list *parents, struct object_id *ret,
Phillip Woode8cbe212020-08-17 18:40:01 +01001510 const char *author, const char *committer,
1511 const char *sign_commit,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001512 struct commit_extra_header *extra)
Jeff King40d52ff2010-04-01 20:05:23 -04001513{
1514 int result;
1515 int encoding_is_utf8;
1516 struct strbuf buffer;
1517
brian m. carlsone816caa2018-03-12 02:27:42 +00001518 assert_oid_type(tree, OBJ_TREE);
Jeff King40d52ff2010-04-01 20:05:23 -04001519
Jeff King3ffefb52014-06-10 17:36:52 -04001520 if (memchr(msg, '\0', msg_len))
Nguyễn Thái Ngọc Duy37576c12011-12-15 20:47:23 +07001521 return error("a NUL byte in commit log message not allowed.");
1522
Jeff King40d52ff2010-04-01 20:05:23 -04001523 /* Not having i18n.commitencoding is the same as having utf-8 */
1524 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1525
1526 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
Patryk Obara5078f342018-01-28 01:13:16 +01001527 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
Jeff King40d52ff2010-04-01 20:05:23 -04001528
1529 /*
1530 * NOTE! This ordering means that the same exact tree merged with a
1531 * different order of parents will be a _different_ changeset even
1532 * if everything else stays the same.
1533 */
1534 while (parents) {
René Scharfee510ab82015-10-24 18:21:31 +02001535 struct commit *parent = pop_commit(&parents);
Jeff King40d52ff2010-04-01 20:05:23 -04001536 strbuf_addf(&buffer, "parent %s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001537 oid_to_hex(&parent->object.oid));
Jeff King40d52ff2010-04-01 20:05:23 -04001538 }
1539
1540 /* Person/date information */
1541 if (!author)
Jeff Kingf9bc5732012-05-24 19:28:40 -04001542 author = git_author_info(IDENT_STRICT);
Jeff King40d52ff2010-04-01 20:05:23 -04001543 strbuf_addf(&buffer, "author %s\n", author);
Phillip Woode8cbe212020-08-17 18:40:01 +01001544 if (!committer)
1545 committer = git_committer_info(IDENT_STRICT);
1546 strbuf_addf(&buffer, "committer %s\n", committer);
Jeff King40d52ff2010-04-01 20:05:23 -04001547 if (!encoding_is_utf8)
1548 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
Junio C Hamano5231c632011-11-07 16:21:32 -08001549
1550 while (extra) {
1551 add_extra_header(&buffer, extra);
1552 extra = extra->next;
1553 }
Jeff King40d52ff2010-04-01 20:05:23 -04001554 strbuf_addch(&buffer, '\n');
1555
1556 /* And add the comment */
Jeff King3ffefb52014-06-10 17:36:52 -04001557 strbuf_add(&buffer, msg, msg_len);
Jeff King40d52ff2010-04-01 20:05:23 -04001558
1559 /* And check the encoding */
Linus Torvalds08a94a12012-06-28 11:24:14 -07001560 if (encoding_is_utf8 && !verify_utf8(&buffer))
Vasco Almeida4fa4b312016-09-19 13:08:16 +00001561 fprintf(stderr, _(commit_utf8_warn));
Jeff King40d52ff2010-04-01 20:05:23 -04001562
brian m. carlson937032e2021-02-11 02:08:04 +00001563 if (sign_commit && sign_with_header(&buffer, sign_commit)) {
Rene Scharfee5051462017-08-30 19:49:38 +02001564 result = -1;
1565 goto out;
1566 }
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001567
Patryk Obaraa09c9852018-01-28 01:13:19 +01001568 result = write_object_file(buffer.buf, buffer.len, commit_type, ret);
Rene Scharfee5051462017-08-30 19:49:38 +02001569out:
Jeff King40d52ff2010-04-01 20:05:23 -04001570 strbuf_release(&buffer);
1571 return result;
1572}
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001573
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001574define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1575static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1576
1577struct merge_remote_desc *merge_remote_util(struct commit *commit)
1578{
1579 return *merge_desc_slab_at(&merge_desc_slab, commit);
1580}
1581
René Scharfebeb518c2016-08-13 14:11:27 +02001582void set_merge_remote_desc(struct commit *commit,
1583 const char *name, struct object *obj)
1584{
1585 struct merge_remote_desc *desc;
René Scharfe5447a762016-08-13 14:21:27 +02001586 FLEX_ALLOC_STR(desc, name, name);
René Scharfebeb518c2016-08-13 14:11:27 +02001587 desc->obj = obj;
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001588 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
René Scharfebeb518c2016-08-13 14:11:27 +02001589}
1590
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001591struct commit *get_merge_parent(const char *name)
1592{
1593 struct object *obj;
1594 struct commit *commit;
brian m. carlson7683e2e2015-03-13 23:39:34 +00001595 struct object_id oid;
brian m. carlsone82caf32017-07-13 23:49:28 +00001596 if (get_oid(name, &oid))
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001597 return NULL;
Stefan Beller109cd762018-06-28 18:21:51 -07001598 obj = parse_object(the_repository, &oid);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001599 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001600 if (commit && !merge_remote_util(commit))
René Scharfebeb518c2016-08-13 14:11:27 +02001601 set_merge_remote_desc(commit, name, obj);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001602 return commit;
1603}
René Scharfe89b5f1d2012-04-25 22:35:27 +02001604
1605/*
1606 * Append a commit to the end of the commit_list.
1607 *
1608 * next starts by pointing to the variable that holds the head of an
1609 * empty commit_list, and is updated to point to the "next" field of
1610 * the last item on the list as new commits are appended.
1611 *
1612 * Usage example:
1613 *
1614 * struct commit_list *list;
1615 * struct commit_list **next = &list;
1616 *
1617 * next = commit_list_append(c1, next);
1618 * next = commit_list_append(c2, next);
1619 * assert(commit_list_count(list) == 2);
1620 * return list;
1621 */
1622struct commit_list **commit_list_append(struct commit *commit,
1623 struct commit_list **next)
1624{
Brandon Williams258c03e2018-02-14 10:59:37 -08001625 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1626 new_commit->item = commit;
1627 *next = new_commit;
1628 new_commit->next = NULL;
1629 return &new_commit->next;
René Scharfe89b5f1d2012-04-25 22:35:27 +02001630}
Nguyễn Thái Ngọc Duyefc7df42012-10-26 22:53:51 +07001631
Jeff Kingfe6eb7f2014-08-27 03:56:01 -04001632const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1633{
1634 int key_len = strlen(key);
1635 const char *line = msg;
1636
1637 while (line) {
1638 const char *eol = strchrnul(line, '\n');
1639
1640 if (line == eol)
1641 return NULL;
1642
1643 if (eol - line > key_len &&
1644 !strncmp(line, key, key_len) &&
1645 line[key_len] == ' ') {
1646 *out_len = eol - line - key_len - 1;
1647 return line + key_len + 1;
1648 }
1649 line = *eol ? eol + 1 : NULL;
1650 }
1651 return NULL;
1652}
Junio C Hamano0ed8a4e2014-12-22 12:26:23 -08001653
Christian Couder8c384582014-11-09 10:23:41 +01001654/*
Jonathan Tan710714a2016-11-02 10:29:17 -07001655 * Inspect the given string and determine the true "end" of the log message, in
Bradley M. Kuhn3abd4a62020-10-19 18:03:55 -07001656 * order to find where to put a new Signed-off-by trailer. Ignored are
Brian Malehornd76650b2017-05-15 23:06:49 -07001657 * trailing comment lines and blank lines. To support "git commit -s
1658 * --amend" on an existing commit, we also ignore "Conflicts:". To
1659 * support "git commit -v", we truncate at cut lines.
Christian Couder8c384582014-11-09 10:23:41 +01001660 *
1661 * Returns the number of bytes from the tail to ignore, to be fed as
1662 * the second parameter to append_signoff().
1663 */
Jeff King66e83d92018-08-22 20:50:51 -04001664size_t ignore_non_trailer(const char *buf, size_t len)
Christian Couder8c384582014-11-09 10:23:41 +01001665{
Jeff King66e83d92018-08-22 20:50:51 -04001666 size_t boc = 0;
1667 size_t bol = 0;
Christian Couder8c384582014-11-09 10:23:41 +01001668 int in_old_conflicts_block = 0;
Brian Malehornd76650b2017-05-15 23:06:49 -07001669 size_t cutoff = wt_status_locate_end(buf, len);
Christian Couder8c384582014-11-09 10:23:41 +01001670
Brian Malehornd76650b2017-05-15 23:06:49 -07001671 while (bol < cutoff) {
Jonathan Tan710714a2016-11-02 10:29:17 -07001672 const char *next_line = memchr(buf + bol, '\n', len - bol);
Christian Couder8c384582014-11-09 10:23:41 +01001673
Jonathan Tan710714a2016-11-02 10:29:17 -07001674 if (!next_line)
1675 next_line = buf + len;
Christian Couder8c384582014-11-09 10:23:41 +01001676 else
1677 next_line++;
1678
Jonathan Tan710714a2016-11-02 10:29:17 -07001679 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
Christian Couder8c384582014-11-09 10:23:41 +01001680 /* is this the first of the run of comments? */
1681 if (!boc)
1682 boc = bol;
1683 /* otherwise, it is just continuing */
Jonathan Tan710714a2016-11-02 10:29:17 -07001684 } else if (starts_with(buf + bol, "Conflicts:\n")) {
Christian Couder8c384582014-11-09 10:23:41 +01001685 in_old_conflicts_block = 1;
1686 if (!boc)
1687 boc = bol;
Jonathan Tan710714a2016-11-02 10:29:17 -07001688 } else if (in_old_conflicts_block && buf[bol] == '\t') {
Christian Couder8c384582014-11-09 10:23:41 +01001689 ; /* a pathname in the conflicts block */
1690 } else if (boc) {
1691 /* the previous was not trailing comment */
1692 boc = 0;
1693 in_old_conflicts_block = 0;
1694 }
Jonathan Tan710714a2016-11-02 10:29:17 -07001695 bol = next_line - buf;
Christian Couder8c384582014-11-09 10:23:41 +01001696 }
Brian Malehornd76650b2017-05-15 23:06:49 -07001697 return boc ? len - boc : len - cutoff;
Christian Couder8c384582014-11-09 10:23:41 +01001698}
Phillip Wood49697cb2019-10-15 10:25:31 +00001699
1700int run_commit_hook(int editor_is_used, const char *index_file,
1701 const char *name, ...)
1702{
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001703 struct strvec hook_env = STRVEC_INIT;
Phillip Wood49697cb2019-10-15 10:25:31 +00001704 va_list args;
1705 int ret;
1706
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001707 strvec_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
Phillip Wood49697cb2019-10-15 10:25:31 +00001708
1709 /*
1710 * Let the hook know that no editor will be launched.
1711 */
1712 if (!editor_is_used)
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001713 strvec_push(&hook_env, "GIT_EDITOR=:");
Phillip Wood49697cb2019-10-15 10:25:31 +00001714
1715 va_start(args, name);
Jeff Kingd70a9eb2020-07-28 20:37:20 -04001716 ret = run_hook_ve(hook_env.v, name, args);
Phillip Wood49697cb2019-10-15 10:25:31 +00001717 va_end(args);
Jeff Kingef8d7ac2020-07-28 16:24:53 -04001718 strvec_clear(&hook_env);
Phillip Wood49697cb2019-10-15 10:25:31 +00001719
1720 return ret;
1721}