blob: 7b63d3b0e1c0f2004e463b3d5fc4ca9ff28d9684 [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"
Elijah Newren41771fa2023-02-24 00:09:27 +00005#include "hex.h"
Jonathan Nieder6a1a79f2018-05-15 16:42:16 -07006#include "repository.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07007#include "object-store.h"
Johannes Schindelined09aef2006-10-30 20:09:06 +01008#include "pkt-line.h"
Junio C Hamano52883fb2006-12-25 11:48:35 -08009#include "utf8.h"
Junio C Hamano199c45b2007-04-09 02:34:05 -070010#include "diff.h"
11#include "revision.h"
Johannes Schindelina97a7462009-10-09 12:21:57 +020012#include "notes.h"
Stefan Beller14ba97f2018-05-15 14:48:42 -070013#include "alloc.h"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070014#include "gpg-interface.h"
René Scharfe46905892012-04-01 00:10:39 +020015#include "mergesort.h"
Junio C Hamanoa84b7942013-04-13 11:56:41 -070016#include "commit-slab.h"
Junio C Hamanoda24b102013-06-06 21:58:12 -070017#include "prio-queue.h"
Martin Ågrenbc626922020-12-31 12:56:23 +010018#include "hash-lookup.h"
Brian Malehornd76650b2017-05-15 23:06:49 -070019#include "wt-status.h"
Johannes Schindelinf9f99b32018-04-29 00:44:44 +020020#include "advice.h"
Pratik Karki103148a2018-09-04 15:00:08 -070021#include "refs.h"
Junio C Hamano02931212018-11-02 11:04:55 +090022#include "commit-reach.h"
Phillip Wood49697cb2019-10-15 10:25:31 +000023#include "run-command.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060024#include "shallow.h"
Emily Shafferf4432462021-12-22 04:59:40 +010025#include "hook.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -070026
Junio C Hamano82a75292012-09-15 13:58:15 -070027static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
28
Linus Torvalds60ab26d2005-09-15 14:43:17 -070029int save_commit_buffer = 1;
Ævar Arnfjörð Bjarmasonab628582021-08-23 12:44:02 +020030int no_graft_file_deprecated_advice;
Linus Torvalds60ab26d2005-09-15 14:43:17 -070031
Daniel Barkalow175785e2005-04-18 11:39:48 -070032const char *commit_type = "commit";
33
Stefan Bellerd9a05e72018-06-28 18:22:21 -070034struct commit *lookup_commit_reference_gently(struct repository *r,
Stefan Beller21e1ee82018-06-28 18:21:57 -070035 const struct object_id *oid, int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070036{
Stefan Bellerd9a05e72018-06-28 18:22:21 -070037 struct object *obj = deref_tag(r,
38 parse_object(r, oid),
Stefan Beller109cd762018-06-28 18:21:51 -070039 NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070040
41 if (!obj)
42 return NULL;
Abhishek Kumar6da43d92020-06-17 14:44:08 +053043 return object_as_type(obj, OBJ_COMMIT, quiet);
Junio C Hamanof76412e2005-08-21 02:51:10 -070044}
45
Stefan Beller1f6c72f2018-06-28 18:22:22 -070046struct commit *lookup_commit_reference(struct repository *r, const struct object_id *oid)
Junio C Hamanof76412e2005-08-21 02:51:10 -070047{
Stefan Beller1f6c72f2018-06-28 18:22:22 -070048 return lookup_commit_reference_gently(r, oid, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070049}
50
brian m. carlsonbc832662017-05-06 22:10:10 +000051struct 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 +100052{
Stefan Beller2122f672018-06-28 18:21:58 -070053 struct commit *c = lookup_commit_reference(the_repository, oid);
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100054 if (!c)
55 die(_("could not parse %s"), ref_name);
Jeff King9001dc22018-08-28 17:22:48 -040056 if (!oideq(oid, &c->object.oid)) {
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100057 warning(_("%s %s is not a commit!"),
brian m. carlsonbc832662017-05-06 22:10:10 +000058 ref_name, oid_to_hex(oid));
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100059 }
60 return c;
61}
62
Phillip Woodb8dbfd02022-10-17 13:17:40 +000063struct commit *lookup_commit_object(struct repository *r,
64 const struct object_id *oid)
65{
66 struct object *obj = parse_object(r, oid);
67 return obj ? object_as_type(obj, OBJ_COMMIT, 0) : NULL;
68
69}
70
Stefan Bellerbacf1682018-06-28 18:22:10 -070071struct commit *lookup_commit(struct repository *r, const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 11:39:48 -070072{
Jeff Kingd0229ab2019-06-20 03:41:14 -040073 struct object *obj = lookup_object(r, oid);
Jeff Kingd36f51c2014-07-13 02:41:55 -040074 if (!obj)
Jeff Kinga3785092019-06-20 03:41:21 -040075 return create_object(r, oid, alloc_commit_node(r));
Abhishek Kumar6da43d92020-06-17 14:44:08 +053076 return object_as_type(obj, OBJ_COMMIT, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070077}
78
Pat Notza6fa5992010-11-02 13:59:07 -060079struct commit *lookup_commit_reference_by_name(const char *name)
80{
brian m. carlson7683e2e2015-03-13 23:39:34 +000081 struct object_id oid;
Pat Notza6fa5992010-11-02 13:59:07 -060082 struct commit *commit;
83
brian m. carlsone82caf32017-07-13 23:49:28 +000084 if (get_oid_committish(name, &oid))
Pat Notza6fa5992010-11-02 13:59:07 -060085 return NULL;
Stefan Beller2122f672018-06-28 18:21:58 -070086 commit = lookup_commit_reference(the_repository, &oid);
Jeff King5e7d4d32013-10-24 04:53:19 -040087 if (parse_commit(commit))
Pat Notza6fa5992010-11-02 13:59:07 -060088 return NULL;
89 return commit;
90}
91
Johannes Schindelindddbad72017-04-26 21:29:31 +020092static timestamp_t parse_commit_date(const char *buf, const char *tail)
Daniel Barkalow175785e2005-04-18 11:39:48 -070093{
Martin Koegler0a617792008-01-19 18:35:23 +010094 const char *dateptr;
Daniel Barkalow175785e2005-04-18 11:39:48 -070095
Martin Koegler0a617792008-01-19 18:35:23 +010096 if (buf + 6 >= tail)
97 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -070098 if (memcmp(buf, "author", 6))
99 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +0100100 while (buf < tail && *buf++ != '\n')
Daniel Barkalow175785e2005-04-18 11:39:48 -0700101 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +0100102 if (buf + 9 >= tail)
103 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700104 if (memcmp(buf, "committer", 9))
105 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +0100106 while (buf < tail && *buf++ != '>')
Daniel Barkalow175785e2005-04-18 11:39:48 -0700107 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +0100108 if (buf >= tail)
109 return 0;
110 dateptr = buf;
111 while (buf < tail && *buf++ != '\n')
112 /* nada */;
113 if (buf >= tail)
114 return 0;
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200115 /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */
116 return parse_timestamp(dateptr, NULL, 10);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700117}
118
Jeff King8380dcd2021-01-28 01:20:23 -0500119static const struct object_id *commit_graft_oid_access(size_t index, const void *table)
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400120{
Jeff King8380dcd2021-01-28 01:20:23 -0500121 const struct commit_graft * const *commit_graft_table = table;
Jeff King45ee13b2021-01-28 01:19:42 -0500122 return &commit_graft_table[index]->oid;
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400123}
124
Jeff King98c431b2021-01-28 01:12:35 -0500125int commit_graft_pos(struct repository *r, const struct object_id *oid)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700126{
Jeff King45ee13b2021-01-28 01:19:42 -0500127 return oid_pos(oid, r->parsed_objects->grafts,
128 r->parsed_objects->grafts_nr,
129 commit_graft_oid_access);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700130}
131
Jonathan Tan4d4e49f2022-06-06 10:54:37 -0700132static void unparse_commit(struct repository *r, const struct object_id *oid)
133{
134 struct commit *c = lookup_commit(r, oid);
135
136 if (!c->object.parsed)
137 return;
138 free_commit_list(c->parents);
139 c->parents = NULL;
140 c->object.parsed = 0;
141}
142
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700143int register_commit_graft(struct repository *r, struct commit_graft *graft,
144 int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700145{
Jeff King98c431b2021-01-28 01:12:35 -0500146 int pos = commit_graft_pos(r, &graft->oid);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700147
Junio C Hamano5040f172006-04-06 23:58:51 -0700148 if (0 <= pos) {
149 if (ignore_dups)
150 free(graft);
151 else {
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700152 free(r->parsed_objects->grafts[pos]);
153 r->parsed_objects->grafts[pos] = graft;
Junio C Hamano5040f172006-04-06 23:58:51 -0700154 }
155 return 1;
156 }
157 pos = -pos - 1;
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700158 ALLOC_GROW(r->parsed_objects->grafts,
159 r->parsed_objects->grafts_nr + 1,
160 r->parsed_objects->grafts_alloc);
161 r->parsed_objects->grafts_nr++;
162 if (pos < r->parsed_objects->grafts_nr)
163 memmove(r->parsed_objects->grafts + pos + 1,
164 r->parsed_objects->grafts + pos,
165 (r->parsed_objects->grafts_nr - pos - 1) *
166 sizeof(*r->parsed_objects->grafts));
167 r->parsed_objects->grafts[pos] = graft;
Jonathan Tan4d4e49f2022-06-06 10:54:37 -0700168 unparse_commit(r, &graft->oid);
Junio C Hamano5040f172006-04-06 23:58:51 -0700169 return 0;
170}
171
Patryk Obara9a934032017-08-18 20:33:12 +0200172struct commit_graft *read_graft_line(struct strbuf *line)
Junio C Hamano5040f172006-04-06 23:58:51 -0700173{
174 /* The format is just "Commit Parent1 Parent2 ...\n" */
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200175 int i, phase;
176 const char *tail = NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700177 struct commit_graft *graft = NULL;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200178 struct object_id dummy_oid, *oid;
Junio C Hamano5040f172006-04-06 23:58:51 -0700179
Patryk Obara9a934032017-08-18 20:33:12 +0200180 strbuf_rtrim(line);
181 if (!line->len || line->buf[0] == '#')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700182 return NULL;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200183 /*
184 * phase 0 verifies line, counts hashes in line and allocates graft
185 * phase 1 fills graft
186 */
187 for (phase = 0; phase < 2; phase++) {
188 oid = graft ? &graft->oid : &dummy_oid;
189 if (parse_oid_hex(line->buf, oid, &tail))
Junio C Hamano5040f172006-04-06 23:58:51 -0700190 goto bad_graft_data;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200191 for (i = 0; *tail != '\0'; i++) {
192 oid = graft ? &graft->parent[i] : &dummy_oid;
193 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
194 goto bad_graft_data;
195 }
196 if (!graft) {
197 graft = xmalloc(st_add(sizeof(*graft),
198 st_mult(sizeof(struct object_id), i)));
199 graft->nr_parent = i;
200 }
Junio C Hamano5040f172006-04-06 23:58:51 -0700201 }
202 return graft;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100203
204bad_graft_data:
Patryk Obara9a934032017-08-18 20:33:12 +0200205 error("bad graft data: %s", line->buf);
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200206 assert(!graft);
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100207 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700208}
209
Brandon Williamsd0e5dd02018-05-17 15:51:49 -0700210static int read_graft_file(struct repository *r, const char *graft_file)
Junio C Hamano5040f172006-04-06 23:58:51 -0700211{
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700212 FILE *fp = fopen_or_warn(graft_file, "r");
Johannes Schindeline228c172013-12-27 21:49:57 +0100213 struct strbuf buf = STRBUF_INIT;
Junio C Hamano5040f172006-04-06 23:58:51 -0700214 if (!fp)
215 return -1;
Ævar Arnfjörð Bjarmasonab628582021-08-23 12:44:02 +0200216 if (!no_graft_file_deprecated_advice &&
217 advice_enabled(ADVICE_GRAFT_FILE_DEPRECATED))
Johannes Schindelinf9f99b32018-04-29 00:44:44 +0200218 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
219 "and will be removed in a future Git version.\n"
220 "\n"
221 "Please use \"git replace --convert-graft-file\"\n"
222 "to convert the grafts into replace refs.\n"
223 "\n"
224 "Turn this message off by running\n"
225 "\"git config advice.graftFileDeprecated false\""));
Johannes Schindeline228c172013-12-27 21:49:57 +0100226 while (!strbuf_getwholeline(&buf, fp, '\n')) {
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700227 /* The format is just "Commit Parent1 Parent2 ...\n" */
Patryk Obara9a934032017-08-18 20:33:12 +0200228 struct commit_graft *graft = read_graft_line(&buf);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700229 if (!graft)
230 continue;
Brandon Williamsd0e5dd02018-05-17 15:51:49 -0700231 if (register_commit_graft(r, graft, 1))
Johannes Schindeline228c172013-12-27 21:49:57 +0100232 error("duplicate graft data: %s", buf.buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700233 }
234 fclose(fp);
Johannes Schindeline228c172013-12-27 21:49:57 +0100235 strbuf_release(&buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700236 return 0;
237}
238
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000239void prepare_commit_graft(struct repository *r)
Junio C Hamano5040f172006-04-06 23:58:51 -0700240{
Junio C Hamano5040f172006-04-06 23:58:51 -0700241 char *graft_file;
242
Stefan Beller2f6c7672018-05-17 15:51:53 -0700243 if (r->parsed_objects->commit_graft_prepared)
Junio C Hamano5040f172006-04-06 23:58:51 -0700244 return;
Jeff King14a9bd22018-05-31 18:42:53 -0400245 if (!startup_info->have_repository)
246 return;
247
Stefan Beller2f6c7672018-05-17 15:51:53 -0700248 graft_file = get_graft_file(r);
249 read_graft_file(r, graft_file);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100250 /* make sure shallows are read */
Stefan Beller2f6c7672018-05-17 15:51:53 -0700251 is_repository_shallow(r);
252 r->parsed_objects->commit_graft_prepared = 1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700253}
254
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700255struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700256{
257 int pos;
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700258 prepare_commit_graft(r);
Jeff King98c431b2021-01-28 01:12:35 -0500259 pos = commit_graft_pos(r, oid);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700260 if (pos < 0)
261 return NULL;
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700262 return r->parsed_objects->grafts[pos];
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700263}
264
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700265int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100266{
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700267 int i, ret;
Jonathan Nieder6a1a79f2018-05-15 16:42:16 -0700268 for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++)
269 ret = fn(the_repository->parsed_objects->grafts[i], cb_data);
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700270 return ret;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100271}
272
Jonathan Tan2a69ff02022-03-17 11:24:47 -0700273void reset_commit_grafts(struct repository *r)
274{
275 int i;
276
Jonathan Tan4d4e49f2022-06-06 10:54:37 -0700277 for (i = 0; i < r->parsed_objects->grafts_nr; i++) {
278 unparse_commit(r, &r->parsed_objects->grafts[i]->oid);
Jonathan Tan2a69ff02022-03-17 11:24:47 -0700279 free(r->parsed_objects->grafts[i]);
Jonathan Tan4d4e49f2022-06-06 10:54:37 -0700280 }
Jonathan Tan2a69ff02022-03-17 11:24:47 -0700281 r->parsed_objects->grafts_nr = 0;
282 r->parsed_objects->commit_graft_prepared = 0;
283}
284
Jeff King8597ea32014-06-10 17:44:13 -0400285struct commit_buffer {
286 void *buffer;
287 unsigned long size;
288};
289define_commit_slab(buffer_slab, struct commit_buffer);
Jeff Kingc1b3c712014-06-10 17:43:02 -0400290
Stefan Beller65ea9d42018-06-28 18:22:15 -0700291struct buffer_slab *allocate_commit_buffer_slab(void)
Jeff King66c28272014-06-10 17:40:14 -0400292{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700293 struct buffer_slab *bs = xmalloc(sizeof(*bs));
294 init_buffer_slab(bs);
295 return bs;
296}
297
298void free_commit_buffer_slab(struct buffer_slab *bs)
299{
300 clear_buffer_slab(bs);
301 free(bs);
302}
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100303
Stefan Beller1a40fc42018-06-28 18:22:16 -0700304void set_commit_buffer(struct repository *r, struct commit *commit, void *buffer, unsigned long size)
Jeff King66c28272014-06-10 17:40:14 -0400305{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700306 struct commit_buffer *v = buffer_slab_at(
Stefan Beller1a40fc42018-06-28 18:22:16 -0700307 r->parsed_objects->buffer_slab, commit);
Jeff King8597ea32014-06-10 17:44:13 -0400308 v->buffer = buffer;
309 v->size = size;
Jeff King66c28272014-06-10 17:40:14 -0400310}
311
Stefan Beller4ff7e5c2018-06-28 18:22:17 -0700312const void *get_cached_commit_buffer(struct repository *r, const struct commit *commit, unsigned long *sizep)
Jeff King152ff1c2014-06-10 17:40:39 -0400313{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700314 struct commit_buffer *v = buffer_slab_peek(
Stefan Beller4ff7e5c2018-06-28 18:22:17 -0700315 r->parsed_objects->buffer_slab, commit);
Junio C Hamano862e7302015-05-14 15:25:52 -0700316 if (!v) {
317 if (sizep)
318 *sizep = 0;
319 return NULL;
320 }
Jeff King8597ea32014-06-10 17:44:13 -0400321 if (sizep)
322 *sizep = v->size;
323 return v->buffer;
Jeff King152ff1c2014-06-10 17:40:39 -0400324}
325
Stefan Beller07de3fd2018-11-13 16:12:57 -0800326const void *repo_get_commit_buffer(struct repository *r,
327 const struct commit *commit,
328 unsigned long *sizep)
Jeff King152ff1c2014-06-10 17:40:39 -0400329{
Stefan Beller07de3fd2018-11-13 16:12:57 -0800330 const void *ret = get_cached_commit_buffer(r, commit, sizep);
Jeff King152ff1c2014-06-10 17:40:39 -0400331 if (!ret) {
332 enum object_type type;
333 unsigned long size;
Stefan Beller07de3fd2018-11-13 16:12:57 -0800334 ret = repo_read_object_file(r, &commit->object.oid, &type, &size);
Jeff King152ff1c2014-06-10 17:40:39 -0400335 if (!ret)
336 die("cannot read commit object %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000337 oid_to_hex(&commit->object.oid));
Jeff King152ff1c2014-06-10 17:40:39 -0400338 if (type != OBJ_COMMIT)
339 die("expected commit for %s, got %s",
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800340 oid_to_hex(&commit->object.oid), type_name(type));
Jeff King8597ea32014-06-10 17:44:13 -0400341 if (sizep)
342 *sizep = size;
Jeff King152ff1c2014-06-10 17:40:39 -0400343 }
344 return ret;
345}
346
Stefan Beller70315372018-11-13 16:12:58 -0800347void repo_unuse_commit_buffer(struct repository *r,
348 const struct commit *commit,
349 const void *buffer)
Jeff King152ff1c2014-06-10 17:40:39 -0400350{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700351 struct commit_buffer *v = buffer_slab_peek(
Stefan Beller70315372018-11-13 16:12:58 -0800352 r->parsed_objects->buffer_slab, commit);
Junio C Hamano862e7302015-05-14 15:25:52 -0700353 if (!(v && v->buffer == buffer))
Jeff King152ff1c2014-06-10 17:40:39 -0400354 free((void *)buffer);
355}
356
Stefan Beller6a7895f2018-12-14 16:09:40 -0800357void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit)
Jeff King0fb370d2014-06-12 18:05:37 -0400358{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700359 struct commit_buffer *v = buffer_slab_peek(
Stefan Beller6a7895f2018-12-14 16:09:40 -0800360 pool->buffer_slab, commit);
Junio C Hamano862e7302015-05-14 15:25:52 -0700361 if (v) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000362 FREE_AND_NULL(v->buffer);
Junio C Hamano862e7302015-05-14 15:25:52 -0700363 v->size = 0;
364 }
Jeff King0fb370d2014-06-12 18:05:37 -0400365}
366
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700367static inline void set_commit_tree(struct commit *c, struct tree *t)
368{
369 c->maybe_tree = t;
370}
371
Nguyễn Thái Ngọc Duy301b8c72019-04-16 16:33:19 +0700372struct tree *repo_get_commit_tree(struct repository *r,
373 const struct commit *commit)
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000374{
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000375 if (commit->maybe_tree || !commit->object.parsed)
376 return commit->maybe_tree;
377
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530378 if (commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
Junio C Hamanoea2dab12019-05-09 00:37:25 +0900379 return get_commit_tree_in_graph(r, commit);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000380
Jeff King83487662019-04-09 19:13:20 -0700381 return NULL;
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000382}
383
384struct object_id *get_commit_tree_oid(const struct commit *commit)
385{
Taylor Blau806278d2019-09-05 18:04:57 -0400386 struct tree *tree = get_commit_tree(commit);
387 return tree ? &tree->object.oid : NULL;
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000388}
389
Stefan Beller6a7895f2018-12-14 16:09:40 -0800390void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)
Stefan Beller14ba97f2018-05-15 14:48:42 -0700391{
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700392 set_commit_tree(c, NULL);
Stefan Beller6a7895f2018-12-14 16:09:40 -0800393 free_commit_buffer(pool, c);
Mike Hommey9784f972019-08-26 11:01:37 +0900394 c->index = 0;
Stefan Beller14ba97f2018-05-15 14:48:42 -0700395 free_commit_list(c->parents);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700396
397 c->object.parsed = 0;
398}
399
Jeff King8597ea32014-06-10 17:44:13 -0400400const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
Jeff King0fb370d2014-06-12 18:05:37 -0400401{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700402 struct commit_buffer *v = buffer_slab_peek(
403 the_repository->parsed_objects->buffer_slab, commit);
Jeff King8597ea32014-06-10 17:44:13 -0400404 void *ret;
405
Junio C Hamano862e7302015-05-14 15:25:52 -0700406 if (!v) {
407 if (sizep)
408 *sizep = 0;
409 return NULL;
410 }
Jeff King8597ea32014-06-10 17:44:13 -0400411 ret = v->buffer;
412 if (sizep)
413 *sizep = v->size;
414
415 v->buffer = NULL;
416 v->size = 0;
Jeff King0fb370d2014-06-12 18:05:37 -0400417 return ret;
418}
419
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700420int 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 -0700421{
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700422 const char *tail = buffer;
423 const char *bufptr = buffer;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000424 struct object_id parent;
Linus Torvalds6c88be12005-06-20 20:26:03 -0700425 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700426 struct commit_graft *graft;
brian m. carlson2770ccb2018-07-16 01:27:56 +0000427 const int tree_entry_len = the_hash_algo->hexsz + 5;
428 const int parent_entry_len = the_hash_algo->hexsz + 7;
Jeff King12736d22019-10-18 00:43:29 -0400429 struct tree *tree;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400430
Daniel Barkalow175785e2005-04-18 11:39:48 -0700431 if (item->object.parsed)
432 return 0;
Ævar Arnfjörð Bjarmasonbf20fe42022-04-13 22:01:34 +0200433 /*
434 * Presumably this is leftover from an earlier failed parse;
435 * clear it out in preparation for us re-parsing (we'll hit the
436 * same error, but that's good, since it lets our caller know
437 * the result cannot be trusted.
438 */
439 free_commit_list(item->parents);
440 item->parents = NULL;
Jeff King228c78f2019-10-25 17:20:20 -0400441
Junio C Hamano3b44f152006-06-28 03:51:00 -0700442 tail += size;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000443 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
444 bufptr[tree_entry_len] != '\n')
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000445 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
brian m. carlson26ea3e72018-05-02 00:25:47 +0000446 if (get_oid_hex(bufptr + 5, &parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800447 return error("bad tree pointer in commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000448 oid_to_hex(&item->object.oid));
Jeff King12736d22019-10-18 00:43:29 -0400449 tree = lookup_tree(r, &parent);
450 if (!tree)
451 return error("bad tree pointer %s in commit %s",
452 oid_to_hex(&parent),
453 oid_to_hex(&item->object.oid));
454 set_commit_tree(item, tree);
brian m. carlson7683e2e2015-03-13 23:39:34 +0000455 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700456 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700457
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700458 graft = lookup_commit_graft(r, &item->object.oid);
Taylor Blauce163642020-07-08 17:10:53 -0400459 if (graft)
460 r->parsed_objects->substituted_parent = 1;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000461 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700462 struct commit *new_parent;
463
brian m. carlson7683e2e2015-03-13 23:39:34 +0000464 if (tail <= bufptr + parent_entry_len + 1 ||
brian m. carlson26ea3e72018-05-02 00:25:47 +0000465 get_oid_hex(bufptr + 7, &parent) ||
brian m. carlson7683e2e2015-03-13 23:39:34 +0000466 bufptr[parent_entry_len] != '\n')
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000467 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
brian m. carlson7683e2e2015-03-13 23:39:34 +0000468 bufptr += parent_entry_len + 1;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +0200469 /*
470 * The clone is shallow if nr_parent < 0, and we must
471 * not traverse its real parents even when we unhide them.
472 */
473 if (graft && (graft->nr_parent < 0 || grafts_replace_parents))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700474 continue;
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700475 new_parent = lookup_commit(r, &parent);
Jeff Kingc78fe002019-10-18 00:42:13 -0400476 if (!new_parent)
477 return error("bad parent %s in commit %s",
478 oid_to_hex(&parent),
479 oid_to_hex(&item->object.oid));
480 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700481 }
482 if (graft) {
483 int i;
484 struct commit *new_parent;
485 for (i = 0; i < graft->nr_parent; i++) {
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700486 new_parent = lookup_commit(r,
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700487 &graft->parent[i]);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700488 if (!new_parent)
Jeff Kingc78fe002019-10-18 00:42:13 -0400489 return error("bad graft parent %s in commit %s",
490 oid_to_hex(&graft->parent[i]),
491 oid_to_hex(&item->object.oid));
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700492 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700493 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700494 }
Martin Koegler0a617792008-01-19 18:35:23 +0100495 item->date = parse_commit_date(bufptr, tail);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800496
Derrick Stoleee2838d82018-05-01 12:47:13 +0000497 if (check_graph)
Derrick Stoleec7944052019-05-09 07:22:31 -0700498 load_commit_graph_info(r, item);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000499
Jeff King228c78f2019-10-25 17:20:20 -0400500 item->object.parsed = 1;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700501 return 0;
502}
503
Stefan Beller9e5252a2018-11-13 16:12:50 -0800504int repo_parse_commit_internal(struct repository *r,
505 struct commit *item,
506 int quiet_on_missing,
507 int use_commit_graph)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400508{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500509 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400510 void *buffer;
511 unsigned long size;
Jonathan Tan7e2ad1c2022-12-14 11:17:43 -0800512 struct object_info oi = {
513 .typep = &type,
514 .sizep = &size,
515 .contentp = &buffer,
516 };
517 /*
518 * Git does not support partial clones that exclude commits, so set
519 * OBJECT_INFO_SKIP_FETCH_OBJECT to fail fast when an object is missing.
520 */
521 int flags = OBJECT_INFO_LOOKUP_REPLACE | OBJECT_INFO_SKIP_FETCH_OBJECT |
522 OBJECT_INFO_DIE_IF_CORRUPT;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400523 int ret;
524
Martin Koegler9786f682008-02-18 21:48:02 +0100525 if (!item)
526 return -1;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400527 if (item->object.parsed)
528 return 0;
Stefan Beller9e5252a2018-11-13 16:12:50 -0800529 if (use_commit_graph && parse_commit_in_graph(r, item))
Derrick Stolee177722b2018-04-10 08:56:05 -0400530 return 0;
Jonathan Tan7e2ad1c2022-12-14 11:17:43 -0800531
532 if (oid_object_info_extended(r, &item->object.oid, &oi, flags) < 0)
Jeff King9cc2b072015-06-01 05:56:26 -0400533 return quiet_on_missing ? -1 :
534 error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000535 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500536 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400537 free(buffer);
538 return error("Object %s not a commit",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000539 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400540 }
Derrick Stolee9b19ada2018-06-27 09:24:30 -0400541
Stefan Beller9e5252a2018-11-13 16:12:50 -0800542 ret = parse_commit_buffer(r, item, buffer, size, 0);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700543 if (save_commit_buffer && !ret) {
Stefan Beller9e5252a2018-11-13 16:12:50 -0800544 set_commit_buffer(r, item, buffer, size);
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700545 return 0;
546 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400547 free(buffer);
548 return ret;
549}
550
Stefan Beller9e5252a2018-11-13 16:12:50 -0800551int repo_parse_commit_gently(struct repository *r,
552 struct commit *item, int quiet_on_missing)
Derrick Stolee9b19ada2018-06-27 09:24:30 -0400553{
Stefan Beller9e5252a2018-11-13 16:12:50 -0800554 return repo_parse_commit_internal(r, item, quiet_on_missing, 1);
Derrick Stolee9b19ada2018-06-27 09:24:30 -0400555}
556
Jeff King7059dcc2013-10-24 04:52:36 -0400557void parse_commit_or_die(struct commit *item)
558{
559 if (parse_commit(item))
560 die("unable to parse commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000561 item ? oid_to_hex(&item->object.oid) : "(null)");
Jeff King7059dcc2013-10-24 04:52:36 -0400562}
563
Christian Couder11af2aa2010-07-22 15:18:30 +0200564int find_commit_subject(const char *commit_buffer, const char **subject)
565{
566 const char *eol;
567 const char *p = commit_buffer;
568
569 while (*p && (*p != '\n' || p[1] != '\n'))
570 p++;
571 if (*p) {
Johannes Schindelin4e1b06d2016-06-22 22:20:20 +0200572 p = skip_blank_lines(p + 2);
Junio C Hamano9c9b03b2017-01-27 18:01:41 -0800573 eol = strchrnul(p, '\n');
Christian Couder11af2aa2010-07-22 15:18:30 +0200574 } else
575 eol = p;
576
577 *subject = p;
578
579 return eol - p;
580}
581
Charvi Mendiratta6e0e2882021-03-15 13:24:31 +0530582size_t commit_subject_length(const char *body)
583{
584 const char *p = body;
585 while (*p) {
586 const char *next = skip_blank_lines(p);
587 if (next != p)
588 break;
589 p = strchrnul(p, '\n');
590 if (*p)
591 p++;
592 }
593 return p - body;
594}
595
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700596struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700597{
Christopher Li812666c2005-04-26 12:00:58 -0700598 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700599 new_list->item = item;
600 new_list->next = *list_p;
601 *list_p = new_list;
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700602 return new_list;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700603}
604
Derrick Stolee597b2c32020-12-08 17:04:13 -0500605int commit_list_contains(struct commit *item, struct commit_list *list)
606{
607 while (list) {
608 if (list->item == item)
609 return 1;
610 list = list->next;
611 }
612
613 return 0;
614}
615
Miklos Vajna65319472008-06-27 18:21:55 +0200616unsigned commit_list_count(const struct commit_list *l)
617{
618 unsigned c = 0;
619 for (; l; l = l->next )
620 c++;
621 return c;
622}
623
Thomas Rast53d00b32013-07-31 22:13:20 +0200624struct commit_list *copy_commit_list(struct commit_list *list)
625{
626 struct commit_list *head = NULL;
627 struct commit_list **pp = &head;
628 while (list) {
René Scharfecb979db2014-07-10 11:47:47 +0200629 pp = commit_list_append(list->item, pp);
Thomas Rast53d00b32013-07-31 22:13:20 +0200630 list = list->next;
631 }
632 return head;
633}
634
Elijah Newrenb0ca1202020-12-16 22:27:59 +0000635struct commit_list *reverse_commit_list(struct commit_list *list)
636{
637 struct commit_list *next = NULL, *current, *backup;
638 for (current = list; current; current = backup) {
639 backup = current->next;
640 current->next = next;
641 next = current;
642 }
643 return next;
644}
645
Daniel Barkalow175785e2005-04-18 11:39:48 -0700646void free_commit_list(struct commit_list *list)
647{
René Scharfee510ab82015-10-24 18:21:31 +0200648 while (list)
649 pop_commit(&list);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700650}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700651
Thiago Farina47e44ed2010-11-26 23:58:14 -0200652struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700653{
654 struct commit_list **pp = list;
655 struct commit_list *p;
656 while ((p = *pp) != NULL) {
657 if (p->item->date < item->date) {
658 break;
659 }
660 pp = &p->next;
661 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700662 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700663}
664
René Scharfec0fb5772022-07-16 18:59:05 +0200665static int commit_list_compare_by_date(const struct commit_list *a,
666 const struct commit_list *b)
René Scharfe46905892012-04-01 00:10:39 +0200667{
René Scharfec0fb5772022-07-16 18:59:05 +0200668 timestamp_t a_date = a->item->date;
669 timestamp_t b_date = b->item->date;
René Scharfe46905892012-04-01 00:10:39 +0200670 if (a_date < b_date)
671 return 1;
672 if (a_date > b_date)
673 return -1;
674 return 0;
675}
676
René Scharfec0fb5772022-07-16 18:59:05 +0200677DEFINE_LIST_SORT(static, commit_list_sort, struct commit_list, next);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700678
Thiago Farina47e44ed2010-11-26 23:58:14 -0200679void commit_list_sort_by_date(struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700680{
René Scharfec0fb5772022-07-16 18:59:05 +0200681 commit_list_sort(list, commit_list_compare_by_date);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700682}
683
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700684struct commit *pop_most_recent_commit(struct commit_list **list,
685 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700686{
René Scharfee510ab82015-10-24 18:21:31 +0200687 struct commit *ret = pop_commit(list);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700688 struct commit_list *parents = ret->parents;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700689
690 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700691 struct commit *commit = parents->item;
Martin Koeglerdec38c82008-02-18 21:48:03 +0100692 if (!parse_commit(commit) && !(commit->object.flags & mark)) {
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700693 commit->object.flags |= mark;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200694 commit_list_insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700695 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700696 parents = parents->next;
697 }
698 return ret;
699}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700700
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700701static void clear_commit_marks_1(struct commit_list **plist,
702 struct commit *commit, unsigned int mark)
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800703{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100704 while (commit) {
705 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800706
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100707 if (!(mark & commit->object.flags))
708 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700709
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100710 commit->object.flags &= ~mark;
711
712 parents = commit->parents;
713 if (!parents)
714 return;
715
René Scharfe4cb39fc2022-12-13 07:27:10 +0100716 while ((parents = parents->next)) {
717 if (parents->item->object.flags & mark)
718 commit_list_insert(parents->item, plist);
719 }
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100720
721 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800722 }
723}
724
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800725void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700726{
727 struct commit_list *list = NULL;
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800728
729 while (nr--) {
René Scharfe07f7d552017-12-25 18:43:37 +0100730 clear_commit_marks_1(&list, *commit, mark);
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800731 commit++;
732 }
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700733 while (list)
734 clear_commit_marks_1(&list, pop_commit(&list), mark);
735}
736
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800737void clear_commit_marks(struct commit *commit, unsigned int mark)
738{
739 clear_commit_marks_many(1, &commit, mark);
740}
741
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000742struct commit *pop_commit(struct commit_list **stack)
743{
744 struct commit_list *top = *stack;
745 struct commit *item = top ? top->item : NULL;
746
747 if (top) {
748 *stack = top->next;
749 free(top);
750 }
751 return item;
752}
753
Jon Seymourab580ac2005-07-07 02:39:34 +1000754/*
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700755 * Topological sort support
756 */
Junio C Hamano66eb3752013-04-12 23:25:49 -0700757
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700758/* count number of children that have not been emitted */
759define_commit_slab(indegree_slab, int);
Jeff King96c4f4a2013-04-09 02:52:56 -0400760
Derrick Stolee18207032018-08-21 20:54:12 +0000761define_commit_slab(author_date_slab, timestamp_t);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700762
Derrick Stolee5284fc52018-11-01 13:46:21 +0000763void record_author_date(struct author_date_slab *author_date,
764 struct commit *commit)
Junio C Hamano81c6b382013-06-07 10:35:54 -0700765{
Jeff King8597ea32014-06-10 17:44:13 -0400766 const char *buffer = get_commit_buffer(commit, NULL);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700767 struct ident_split ident;
Jeff Kingea5517f2014-08-27 03:56:55 -0400768 const char *ident_line;
769 size_t ident_len;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700770 char *date_end;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200771 timestamp_t date;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700772
Jeff Kingea5517f2014-08-27 03:56:55 -0400773 ident_line = find_commit_header(buffer, "author", &ident_len);
774 if (!ident_line)
775 goto fail_exit; /* no author line */
776 if (split_ident_line(&ident, ident_line, ident_len) ||
777 !ident.date_begin || !ident.date_end)
778 goto fail_exit; /* malformed "author" line */
Junio C Hamano81c6b382013-06-07 10:35:54 -0700779
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200780 date = parse_timestamp(ident.date_begin, &date_end, 10);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700781 if (date_end != ident.date_end)
782 goto fail_exit; /* malformed date */
783 *(author_date_slab_at(author_date, commit)) = date;
784
785fail_exit:
Jeff Kingba41c1c2014-06-10 17:41:02 -0400786 unuse_commit_buffer(commit, buffer);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700787}
788
Derrick Stolee5284fc52018-11-01 13:46:21 +0000789int compare_commits_by_author_date(const void *a_, const void *b_,
790 void *cb_data)
Junio C Hamano81c6b382013-06-07 10:35:54 -0700791{
792 const struct commit *a = a_, *b = b_;
793 struct author_date_slab *author_date = cb_data;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200794 timestamp_t a_date = *(author_date_slab_at(author_date, a));
795 timestamp_t b_date = *(author_date_slab_at(author_date, b));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700796
797 /* newer commits with larger date first */
798 if (a_date < b_date)
799 return 1;
800 else if (a_date > b_date)
801 return -1;
802 return 0;
803}
804
Jeff King17587122023-02-24 01:39:27 -0500805int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_,
806 void *unused UNUSED)
Derrick Stolee3afc6792018-05-01 12:47:11 +0000807{
808 const struct commit *a = a_, *b = b_;
Abhishek Kumard7f92782021-01-16 18:11:13 +0000809 const timestamp_t generation_a = commit_graph_generation(a),
810 generation_b = commit_graph_generation(b);
Derrick Stolee3afc6792018-05-01 12:47:11 +0000811
812 /* newer commits first */
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530813 if (generation_a < generation_b)
Derrick Stolee3afc6792018-05-01 12:47:11 +0000814 return 1;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530815 else if (generation_a > generation_b)
Derrick Stolee3afc6792018-05-01 12:47:11 +0000816 return -1;
817
818 /* use date as a heuristic when generations are equal */
819 if (a->date < b->date)
820 return 1;
821 else if (a->date > b->date)
822 return -1;
823 return 0;
824}
825
Jeff King17587122023-02-24 01:39:27 -0500826int compare_commits_by_commit_date(const void *a_, const void *b_,
827 void *unused UNUSED)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700828{
829 const struct commit *a = a_, *b = b_;
830 /* newer commits with larger date first */
831 if (a->date < b->date)
832 return 1;
833 else if (a->date > b->date)
834 return -1;
835 return 0;
836}
837
Jon Seymourab580ac2005-07-07 02:39:34 +1000838/*
839 * Performs an in-place topological sort on the list supplied.
840 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700841void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
Jon Seymourab580ac2005-07-07 02:39:34 +1000842{
Linus Torvalds23c17d42007-11-02 13:32:58 -0700843 struct commit_list *next, *orig = *list;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700844 struct commit_list **pptr;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700845 struct indegree_slab indegree;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700846 struct prio_queue queue;
847 struct commit *commit;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700848 struct author_date_slab author_date;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100849
Linus Torvalds23c17d42007-11-02 13:32:58 -0700850 if (!orig)
Eric Wong7d6fb372005-12-24 04:12:43 -0800851 return;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700852 *list = NULL;
853
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700854 init_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700855 memset(&queue, '\0', sizeof(queue));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700856
Junio C Hamanoda24b102013-06-06 21:58:12 -0700857 switch (sort_order) {
858 default: /* REV_SORT_IN_GRAPH_ORDER */
859 queue.compare = NULL;
860 break;
861 case REV_SORT_BY_COMMIT_DATE:
862 queue.compare = compare_commits_by_commit_date;
863 break;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700864 case REV_SORT_BY_AUTHOR_DATE:
865 init_author_date_slab(&author_date);
866 queue.compare = compare_commits_by_author_date;
867 queue.cb_data = &author_date;
868 break;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700869 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400870
Linus Torvalds23c17d42007-11-02 13:32:58 -0700871 /* Mark them and clear the indegree */
872 for (next = orig; next; next = next->next) {
873 struct commit *commit = next->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700874 *(indegree_slab_at(&indegree, commit)) = 1;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700875 /* also record the author dates, if needed */
876 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
877 record_author_date(&author_date, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000878 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700879
Jon Seymourab580ac2005-07-07 02:39:34 +1000880 /* update the indegree */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700881 for (next = orig; next; next = next->next) {
Junio C Hamanoda24b102013-06-06 21:58:12 -0700882 struct commit_list *parents = next->item->parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000883 while (parents) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700884 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700885 int *pi = indegree_slab_at(&indegree, parent);
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100886
Jeff King96c4f4a2013-04-09 02:52:56 -0400887 if (*pi)
888 (*pi)++;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700889 parents = parents->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000890 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000891 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700892
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700893 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200894 * find the tips
895 *
896 * tips are nodes not reachable from any other node in the list
897 *
898 * the tips serve as a starting set for the work queue.
899 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700900 for (next = orig; next; next = next->next) {
901 struct commit *commit = next->item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000902
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700903 if (*(indegree_slab_at(&indegree, commit)) == 1)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700904 prio_queue_put(&queue, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000905 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800906
Junio C Hamanoda24b102013-06-06 21:58:12 -0700907 /*
908 * This is unfortunate; the initial tips need to be shown
909 * in the order given from the revision traversal machinery.
910 */
911 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
912 prio_queue_reverse(&queue);
913
914 /* We no longer need the commit list */
915 free_commit_list(orig);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700916
917 pptr = list;
918 *list = NULL;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700919 while ((commit = prio_queue_get(&queue)) != NULL) {
920 struct commit_list *parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000921
Linus Torvalds23c17d42007-11-02 13:32:58 -0700922 for (parents = commit->parents; parents ; parents = parents->next) {
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -0700923 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700924 int *pi = indegree_slab_at(&indegree, parent);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700925
Jeff King96c4f4a2013-04-09 02:52:56 -0400926 if (!*pi)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700927 continue;
928
929 /*
930 * parents are only enqueued for emission
931 * when all their children have been emitted thereby
932 * guaranteeing topological order.
933 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700934 if (--(*pi) == 1)
935 prio_queue_put(&queue, parent);
Jon Seymourab580ac2005-07-07 02:39:34 +1000936 }
937 /*
Junio C Hamanoda24b102013-06-06 21:58:12 -0700938 * all children of commit have already been
939 * emitted. we can emit it now.
Pierre Habouzit674d1722007-09-10 12:35:06 +0200940 */
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700941 *(indegree_slab_at(&indegree, commit)) = 0;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700942
943 pptr = &commit_list_insert(commit, pptr)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000944 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400945
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700946 clear_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700947 clear_prio_queue(&queue);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700948 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
949 clear_author_date_slab(&author_date);
Jon Seymourab580ac2005-07-07 02:39:34 +1000950}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +0200951
Pratik Karki103148a2018-09-04 15:00:08 -0700952struct rev_collect {
953 struct commit **commit;
954 int nr;
955 int alloc;
956 unsigned int initial : 1;
957};
958
959static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
960{
961 struct commit *commit;
962
963 if (is_null_oid(oid))
964 return;
965
966 commit = lookup_commit(the_repository, oid);
967 if (!commit ||
968 (commit->object.flags & TMP_MARK) ||
969 parse_commit(commit))
970 return;
971
972 ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc);
973 revs->commit[revs->nr++] = commit;
974 commit->object.flags |= TMP_MARK;
975}
976
977static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200978 const char *ident UNUSED,
979 timestamp_t timestamp UNUSED, int tz UNUSED,
980 const char *message UNUSED, void *cbdata)
Pratik Karki103148a2018-09-04 15:00:08 -0700981{
982 struct rev_collect *revs = cbdata;
983
984 if (revs->initial) {
985 revs->initial = 0;
986 add_one_commit(ooid, revs);
987 }
988 add_one_commit(noid, revs);
989 return 0;
990}
991
992struct commit *get_fork_point(const char *refname, struct commit *commit)
993{
994 struct object_id oid;
995 struct rev_collect revs;
996 struct commit_list *bases;
997 int i;
998 struct commit *ret = NULL;
Junio C Hamanof08132f2019-12-09 10:51:47 -0800999 char *full_refname;
1000
Jonathan Tanf24c30e2020-09-01 15:28:09 -07001001 switch (dwim_ref(refname, strlen(refname), &oid, &full_refname, 0)) {
Junio C Hamanof08132f2019-12-09 10:51:47 -08001002 case 0:
1003 die("No such ref: '%s'", refname);
1004 case 1:
1005 break; /* good */
1006 default:
1007 die("Ambiguous refname: '%s'", refname);
1008 }
Pratik Karki103148a2018-09-04 15:00:08 -07001009
1010 memset(&revs, 0, sizeof(revs));
1011 revs.initial = 1;
Junio C Hamanof08132f2019-12-09 10:51:47 -08001012 for_each_reflog_ent(full_refname, collect_one_reflog_ent, &revs);
Pratik Karki103148a2018-09-04 15:00:08 -07001013
Junio C Hamanof08132f2019-12-09 10:51:47 -08001014 if (!revs.nr)
Pratik Karki103148a2018-09-04 15:00:08 -07001015 add_one_commit(&oid, &revs);
1016
1017 for (i = 0; i < revs.nr; i++)
1018 revs.commit[i]->object.flags &= ~TMP_MARK;
1019
1020 bases = get_merge_bases_many(commit, revs.nr, revs.commit);
1021
1022 /*
1023 * There should be one and only one merge base, when we found
1024 * a common ancestor among reflog entries.
1025 */
1026 if (!bases || bases->next)
1027 goto cleanup_return;
1028
1029 /* And the found one must be one of the reflog entries */
1030 for (i = 0; i < revs.nr; i++)
1031 if (&bases->item->object == &revs.commit[i]->object)
1032 break; /* found */
1033 if (revs.nr <= i)
1034 goto cleanup_return;
1035
1036 ret = bases->item;
1037
1038cleanup_return:
Ævar Arnfjörð Bjarmason0c10ed12023-02-06 20:08:13 +01001039 free(revs.commit);
Pratik Karki103148a2018-09-04 15:00:08 -07001040 free_commit_list(bases);
Junio C Hamanof08132f2019-12-09 10:51:47 -08001041 free(full_refname);
Pratik Karki103148a2018-09-04 15:00:08 -07001042 return ret;
1043}
1044
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001045/*
1046 * Indexed by hash algorithm identifier.
1047 */
1048static const char *gpg_sig_headers[] = {
1049 NULL,
1050 "gpgsig",
1051 "gpgsig-sha256",
1052};
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001053
brian m. carlson937032e2021-02-11 02:08:04 +00001054int sign_with_header(struct strbuf *buf, const char *keyid)
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001055{
1056 struct strbuf sig = STRBUF_INIT;
1057 int inspos, copypos;
Johannes Schindelin3324dd82016-06-29 16:14:54 +02001058 const char *eoh;
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001059 const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(the_hash_algo)];
1060 int gpg_sig_header_len = strlen(gpg_sig_header);
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001061
1062 /* find the end of the header */
Johannes Schindelin3324dd82016-06-29 16:14:54 +02001063 eoh = strstr(buf->buf, "\n\n");
1064 if (!eoh)
1065 inspos = buf->len;
1066 else
1067 inspos = eoh - buf->buf + 1;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001068
1069 if (!keyid || !*keyid)
1070 keyid = get_signing_key();
1071 if (sign_buffer(buf, &sig, keyid)) {
1072 strbuf_release(&sig);
1073 return -1;
1074 }
1075
1076 for (copypos = 0; sig.buf[copypos]; ) {
1077 const char *bol = sig.buf + copypos;
1078 const char *eol = strchrnul(bol, '\n');
1079 int len = (eol - bol) + !!*eol;
1080
1081 if (!copypos) {
1082 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1083 inspos += gpg_sig_header_len;
1084 }
René Scharfea91cc7f2020-02-09 14:44:23 +01001085 strbuf_insertstr(buf, inspos++, " ");
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001086 strbuf_insert(buf, inspos, bol, len);
1087 inspos += len;
1088 copypos += len;
1089 }
1090 strbuf_release(&sig);
1091 return 0;
1092}
1093
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001094
brian m. carlson937032e2021-02-11 02:08:04 +00001095
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001096int parse_signed_commit(const struct commit *commit,
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001097 struct strbuf *payload, struct strbuf *signature,
1098 const struct git_hash_algo *algop)
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001099{
Jeff King218aa3a2014-06-13 02:32:11 -04001100 unsigned long size;
1101 const char *buffer = get_commit_buffer(commit, &size);
brian m. carlson937032e2021-02-11 02:08:04 +00001102 int ret = parse_buffer_signed_by_header(buffer, size, payload, signature, algop);
1103
1104 unuse_commit_buffer(commit, buffer);
1105 return ret;
1106}
1107
1108int parse_buffer_signed_by_header(const char *buffer,
1109 unsigned long size,
1110 struct strbuf *payload,
1111 struct strbuf *signature,
1112 const struct git_hash_algo *algop)
1113{
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001114 int in_signature = 0, saw_signature = 0, other_signature = 0;
1115 const char *line, *tail, *p;
1116 const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(algop)];
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001117
1118 line = buffer;
1119 tail = buffer + size;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001120 while (line < tail) {
1121 const char *sig = NULL;
Jeff King218aa3a2014-06-13 02:32:11 -04001122 const char *next = memchr(line, '\n', tail - line);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001123
1124 next = next ? next + 1 : tail;
1125 if (in_signature && line[0] == ' ')
1126 sig = line + 1;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001127 else if (skip_prefix(line, gpg_sig_header, &p) &&
1128 *p == ' ') {
1129 sig = line + strlen(gpg_sig_header) + 1;
1130 other_signature = 0;
1131 }
1132 else if (starts_with(line, "gpgsig"))
1133 other_signature = 1;
1134 else if (other_signature && line[0] != ' ')
1135 other_signature = 0;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001136 if (sig) {
1137 strbuf_add(signature, sig, next - sig);
1138 saw_signature = 1;
1139 in_signature = 1;
1140 } else {
1141 if (*line == '\n')
1142 /* dump the whole remainder of the buffer */
1143 next = tail;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001144 if (!other_signature)
1145 strbuf_add(payload, line, next - line);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001146 in_signature = 0;
1147 }
1148 line = next;
1149 }
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001150 return saw_signature;
1151}
1152
Christian Couder0b05ab62014-07-19 17:01:12 +02001153int remove_signature(struct strbuf *buf)
1154{
1155 const char *line = buf->buf;
1156 const char *tail = buf->buf + buf->len;
1157 int in_signature = 0;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001158 struct sigbuf {
1159 const char *start;
1160 const char *end;
1161 } sigs[2], *sigp = &sigs[0];
1162 int i;
1163 const char *orig_buf = buf->buf;
1164
1165 memset(sigs, 0, sizeof(sigs));
Christian Couder0b05ab62014-07-19 17:01:12 +02001166
1167 while (line < tail) {
1168 const char *next = memchr(line, '\n', tail - line);
1169 next = next ? next + 1 : tail;
1170
1171 if (in_signature && line[0] == ' ')
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001172 sigp->end = next;
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001173 else if (starts_with(line, "gpgsig")) {
1174 int i;
1175 for (i = 1; i < GIT_HASH_NALGOS; i++) {
1176 const char *p;
1177 if (skip_prefix(line, gpg_sig_headers[i], &p) &&
1178 *p == ' ') {
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001179 sigp->start = line;
1180 sigp->end = next;
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001181 in_signature = 1;
1182 }
1183 }
Christian Couder0b05ab62014-07-19 17:01:12 +02001184 } else {
1185 if (*line == '\n')
1186 /* dump the whole remainder of the buffer */
1187 next = tail;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001188 if (in_signature && sigp - sigs != ARRAY_SIZE(sigs))
1189 sigp++;
Christian Couder0b05ab62014-07-19 17:01:12 +02001190 in_signature = 0;
1191 }
1192 line = next;
1193 }
1194
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001195 for (i = ARRAY_SIZE(sigs) - 1; i >= 0; i--)
1196 if (sigs[i].start)
1197 strbuf_remove(buf, sigs[i].start - orig_buf, sigs[i].end - sigs[i].start);
Christian Couder0b05ab62014-07-19 17:01:12 +02001198
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001199 return sigs[0].start != NULL;
Christian Couder0b05ab62014-07-19 17:01:12 +02001200}
1201
Junio C Hamano5231c632011-11-07 16:21:32 -08001202static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1203{
1204 struct merge_remote_desc *desc;
1205 struct commit_extra_header *mergetag;
1206 char *buf;
brian m. carlson482c1192021-02-11 02:08:03 +00001207 unsigned long size;
Junio C Hamano5231c632011-11-07 16:21:32 -08001208 enum object_type type;
brian m. carlson482c1192021-02-11 02:08:03 +00001209 struct strbuf payload = STRBUF_INIT;
1210 struct strbuf signature = STRBUF_INIT;
Junio C Hamano5231c632011-11-07 16:21:32 -08001211
1212 desc = merge_remote_util(parent);
1213 if (!desc || !desc->obj)
1214 return;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00001215 buf = read_object_file(&desc->obj->oid, &type, &size);
Junio C Hamano5231c632011-11-07 16:21:32 -08001216 if (!buf || type != OBJ_TAG)
1217 goto free_return;
brian m. carlson482c1192021-02-11 02:08:03 +00001218 if (!parse_signature(buf, size, &payload, &signature))
Junio C Hamano5231c632011-11-07 16:21:32 -08001219 goto free_return;
1220 /*
1221 * We could verify this signature and either omit the tag when
1222 * it does not validate, but the integrator may not have the
Felipe Contreras0e20b222021-06-15 14:11:10 +00001223 * public key of the signer of the tag being merged, while a
Junio C Hamano5231c632011-11-07 16:21:32 -08001224 * later auditor may have it while auditing, so let's not run
1225 * verify-signed-buffer here for now...
1226 *
1227 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1228 * warn("warning: signed tag unverified.");
1229 */
René Scharfeca56dad2021-03-13 17:17:22 +01001230 CALLOC_ARRAY(mergetag, 1);
Junio C Hamano5231c632011-11-07 16:21:32 -08001231 mergetag->key = xstrdup("mergetag");
1232 mergetag->value = buf;
1233 mergetag->len = size;
1234
1235 **tail = mergetag;
1236 *tail = &mergetag->next;
brian m. carlson482c1192021-02-11 02:08:03 +00001237 strbuf_release(&payload);
1238 strbuf_release(&signature);
Junio C Hamano5231c632011-11-07 16:21:32 -08001239 return;
1240
1241free_return:
1242 free(buf);
1243}
1244
brian m. carlson434060e2015-06-21 23:14:40 +00001245int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001246{
1247 struct strbuf payload = STRBUF_INIT;
1248 struct strbuf signature = STRBUF_INIT;
brian m. carlson434060e2015-06-21 23:14:40 +00001249 int ret = 1;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001250
1251 sigc->result = 'N';
1252
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001253 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001254 goto out;
Fabian Stelzer02769432021-12-09 09:52:43 +01001255
Fabian Stelzer6393c952021-12-09 09:52:45 +01001256 sigc->payload_type = SIGNATURE_PAYLOAD_COMMIT;
Fabian Stelzer02769432021-12-09 09:52:43 +01001257 sigc->payload = strbuf_detach(&payload, &sigc->payload_len);
1258 ret = check_signature(sigc, signature.buf, signature.len);
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001259
1260 out:
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001261 strbuf_release(&payload);
1262 strbuf_release(&signature);
brian m. carlson434060e2015-06-21 23:14:40 +00001263
1264 return ret;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001265}
1266
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001267void verify_merge_signature(struct commit *commit, int verbosity,
1268 int check_trust)
Jeff Kingedc4d472018-11-06 02:50:17 -05001269{
1270 char hex[GIT_MAX_HEXSZ + 1];
1271 struct signature_check signature_check;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001272 int ret;
Jeff Kingedc4d472018-11-06 02:50:17 -05001273 memset(&signature_check, 0, sizeof(signature_check));
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001274
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001275 ret = check_commit_signature(commit, &signature_check);
Jeff Kingedc4d472018-11-06 02:50:17 -05001276
1277 find_unique_abbrev_r(hex, &commit->object.oid, DEFAULT_ABBREV);
1278 switch (signature_check.result) {
1279 case 'G':
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001280 if (ret || (check_trust && signature_check.trust_level < TRUST_MARGINAL))
1281 die(_("Commit %s has an untrusted GPG signature, "
1282 "allegedly by %s."), hex, signature_check.signer);
Jeff Kingedc4d472018-11-06 02:50:17 -05001283 break;
Jeff Kingedc4d472018-11-06 02:50:17 -05001284 case 'B':
1285 die(_("Commit %s has a bad GPG signature "
1286 "allegedly by %s."), hex, signature_check.signer);
1287 default: /* 'N' */
1288 die(_("Commit %s does not have a GPG signature."), hex);
1289 }
1290 if (verbosity >= 0 && signature_check.result == 'G')
1291 printf(_("Commit %s has a good GPG signature by %s\n"),
1292 hex, signature_check.signer);
1293
1294 signature_check_clear(&signature_check);
1295}
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001296
Junio C Hamano5231c632011-11-07 16:21:32 -08001297void append_merge_tag_headers(struct commit_list *parents,
1298 struct commit_extra_header ***tail)
1299{
1300 while (parents) {
1301 struct commit *parent = parents->item;
1302 handle_signed_tag(parent, tail);
1303 parents = parents->next;
1304 }
1305}
1306
1307static void add_extra_header(struct strbuf *buffer,
1308 struct commit_extra_header *extra)
1309{
1310 strbuf_addstr(buffer, extra->key);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001311 if (extra->len)
1312 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1313 else
1314 strbuf_addch(buffer, '\n');
1315}
1316
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001317struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1318 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001319{
1320 struct commit_extra_header *extra = NULL;
1321 unsigned long size;
Jeff King218aa3a2014-06-13 02:32:11 -04001322 const char *buffer = get_commit_buffer(commit, &size);
1323 extra = read_commit_extra_header_lines(buffer, size, exclude);
1324 unuse_commit_buffer(commit, buffer);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001325 return extra;
1326}
1327
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001328int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
Christian Couder063da622014-07-07 08:35:37 +02001329{
1330 struct commit_extra_header *extra, *to_free;
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001331 int res = 0;
Christian Couder063da622014-07-07 08:35:37 +02001332
1333 to_free = read_commit_extra_headers(commit, NULL);
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001334 for (extra = to_free; !res && extra; extra = extra->next) {
Christian Couder063da622014-07-07 08:35:37 +02001335 if (strcmp(extra->key, "mergetag"))
1336 continue; /* not a merge tag */
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001337 res = fn(commit, extra, data);
Christian Couder063da622014-07-07 08:35:37 +02001338 }
1339 free_commit_extra_headers(to_free);
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001340 return res;
Christian Couder063da622014-07-07 08:35:37 +02001341}
1342
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001343static inline int standard_header_field(const char *field, size_t len)
1344{
René Scharfeb0725042017-02-25 20:27:40 +01001345 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1346 (len == 6 && !memcmp(field, "parent", 6)) ||
1347 (len == 6 && !memcmp(field, "author", 6)) ||
1348 (len == 9 && !memcmp(field, "committer", 9)) ||
1349 (len == 8 && !memcmp(field, "encoding", 8)));
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001350}
1351
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001352static int excluded_header_field(const char *field, size_t len, const char **exclude)
1353{
1354 if (!exclude)
1355 return 0;
1356
1357 while (*exclude) {
1358 size_t xlen = strlen(*exclude);
René Scharfeb0725042017-02-25 20:27:40 +01001359 if (len == xlen && !memcmp(field, *exclude, xlen))
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001360 return 1;
1361 exclude++;
1362 }
1363 return 0;
1364}
1365
Junio C Hamano82a75292012-09-15 13:58:15 -07001366static struct commit_extra_header *read_commit_extra_header_lines(
1367 const char *buffer, size_t size,
1368 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001369{
1370 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1371 const char *line, *next, *eof, *eob;
1372 struct strbuf buf = STRBUF_INIT;
1373
1374 for (line = buffer, eob = line + size;
1375 line < eob && *line != '\n';
1376 line = next) {
1377 next = memchr(line, '\n', eob - line);
1378 next = next ? next + 1 : eob;
1379 if (*line == ' ') {
1380 /* continuation */
1381 if (it)
1382 strbuf_add(&buf, line + 1, next - (line + 1));
1383 continue;
1384 }
1385 if (it)
1386 it->value = strbuf_detach(&buf, &it->len);
1387 strbuf_reset(&buf);
1388 it = NULL;
1389
René Scharfe50a01cc2017-02-25 20:21:52 +01001390 eof = memchr(line, ' ', next - line);
1391 if (!eof)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001392 eof = next;
René Scharfeb0725042017-02-25 20:27:40 +01001393 else if (standard_header_field(line, eof - line) ||
1394 excluded_header_field(line, eof - line, exclude))
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001395 continue;
1396
René Scharfeca56dad2021-03-13 17:17:22 +01001397 CALLOC_ARRAY(it, 1);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001398 it->key = xmemdupz(line, eof-line);
1399 *tail = it;
1400 tail = &it->next;
1401 if (eof + 1 < next)
1402 strbuf_add(&buf, eof + 1, next - (eof + 1));
1403 }
1404 if (it)
1405 it->value = strbuf_detach(&buf, &it->len);
1406 return extra;
Junio C Hamano5231c632011-11-07 16:21:32 -08001407}
1408
1409void free_commit_extra_headers(struct commit_extra_header *extra)
1410{
1411 while (extra) {
1412 struct commit_extra_header *next = extra->next;
1413 free(extra->key);
1414 free(extra->value);
1415 free(extra);
1416 extra = next;
1417 }
1418}
1419
Patryk Obara5078f342018-01-28 01:13:16 +01001420int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1421 struct commit_list *parents, struct object_id *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001422 const char *author, const char *sign_commit)
Junio C Hamano5231c632011-11-07 16:21:32 -08001423{
1424 struct commit_extra_header *extra = NULL, **tail = &extra;
1425 int result;
1426
1427 append_merge_tag_headers(parents, &tail);
Phillip Woode8cbe212020-08-17 18:40:01 +01001428 result = commit_tree_extended(msg, msg_len, tree, parents, ret, author,
1429 NULL, sign_commit, extra);
Junio C Hamano5231c632011-11-07 16:21:32 -08001430 free_commit_extra_headers(extra);
1431 return result;
1432}
1433
Linus Torvalds08a94a12012-06-28 11:24:14 -07001434static int find_invalid_utf8(const char *buf, int len)
1435{
1436 int offset = 0;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001437 static const unsigned int max_codepoint[] = {
1438 0x7f, 0x7ff, 0xffff, 0x10ffff
1439 };
Linus Torvalds08a94a12012-06-28 11:24:14 -07001440
1441 while (len) {
1442 unsigned char c = *buf++;
1443 int bytes, bad_offset;
brian m. carlson28110d42013-07-04 17:19:43 +00001444 unsigned int codepoint;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001445 unsigned int min_val, max_val;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001446
1447 len--;
1448 offset++;
1449
1450 /* Simple US-ASCII? No worries. */
1451 if (c < 0x80)
1452 continue;
1453
1454 bad_offset = offset-1;
1455
1456 /*
1457 * Count how many more high bits set: that's how
1458 * many more bytes this sequence should have.
1459 */
1460 bytes = 0;
1461 while (c & 0x40) {
1462 c <<= 1;
1463 bytes++;
1464 }
1465
brian m. carlson28110d42013-07-04 17:19:43 +00001466 /*
1467 * Must be between 1 and 3 more bytes. Longer sequences result in
1468 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1469 */
1470 if (bytes < 1 || 3 < bytes)
Linus Torvalds08a94a12012-06-28 11:24:14 -07001471 return bad_offset;
1472
1473 /* Do we *have* that many bytes? */
1474 if (len < bytes)
1475 return bad_offset;
1476
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001477 /*
1478 * Place the encoded bits at the bottom of the value and compute the
1479 * valid range.
1480 */
brian m. carlson28110d42013-07-04 17:19:43 +00001481 codepoint = (c & 0x7f) >> bytes;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001482 min_val = max_codepoint[bytes-1] + 1;
1483 max_val = max_codepoint[bytes];
brian m. carlson28110d42013-07-04 17:19:43 +00001484
Linus Torvalds08a94a12012-06-28 11:24:14 -07001485 offset += bytes;
1486 len -= bytes;
1487
1488 /* And verify that they are good continuation bytes */
1489 do {
brian m. carlson28110d42013-07-04 17:19:43 +00001490 codepoint <<= 6;
1491 codepoint |= *buf & 0x3f;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001492 if ((*buf++ & 0xc0) != 0x80)
1493 return bad_offset;
1494 } while (--bytes);
1495
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001496 /* Reject codepoints that are out of range for the sequence length. */
1497 if (codepoint < min_val || codepoint > max_val)
brian m. carlson28110d42013-07-04 17:19:43 +00001498 return bad_offset;
1499 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1500 if ((codepoint & 0x1ff800) == 0xd800)
1501 return bad_offset;
Peter Krefting81050ac2013-07-09 12:16:33 +01001502 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
Junio C Hamanodc773a62013-08-05 09:52:28 -07001503 if ((codepoint & 0xfffe) == 0xfffe)
Peter Krefting81050ac2013-07-09 12:16:33 +01001504 return bad_offset;
1505 /* So are anything in the range U+FDD0..U+FDEF. */
1506 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
brian m. carlson28110d42013-07-04 17:19:43 +00001507 return bad_offset;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001508 }
1509 return -1;
1510}
1511
1512/*
1513 * This verifies that the buffer is in proper utf8 format.
1514 *
1515 * If it isn't, it assumes any non-utf8 characters are Latin1,
1516 * and does the conversion.
Linus Torvalds08a94a12012-06-28 11:24:14 -07001517 */
1518static int verify_utf8(struct strbuf *buf)
1519{
1520 int ok = 1;
1521 long pos = 0;
1522
1523 for (;;) {
1524 int bad;
1525 unsigned char c;
1526 unsigned char replace[2];
1527
1528 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1529 if (bad < 0)
1530 return ok;
1531 pos += bad;
1532 ok = 0;
1533 c = buf->buf[pos];
1534 strbuf_remove(buf, pos, 1);
1535
1536 /* We know 'c' must be in the range 128-255 */
1537 replace[0] = 0xc0 + (c >> 6);
1538 replace[1] = 0x80 + (c & 0x3f);
1539 strbuf_insert(buf, pos, replace, 2);
1540 pos += 2;
1541 }
1542}
1543
Jeff King40d52ff2010-04-01 20:05:23 -04001544static const char commit_utf8_warn[] =
Vasco Almeida4fa4b312016-09-19 13:08:16 +00001545N_("Warning: commit message did not conform to UTF-8.\n"
1546 "You may want to amend it after fixing the message, or set the config\n"
Jiang Xinb4eda052022-06-17 18:03:09 +08001547 "variable i18n.commitEncoding to the encoding your project uses.\n");
Jeff King40d52ff2010-04-01 20:05:23 -04001548
Jeff King3ffefb52014-06-10 17:36:52 -04001549int commit_tree_extended(const char *msg, size_t msg_len,
Patryk Obara5078f342018-01-28 01:13:16 +01001550 const struct object_id *tree,
1551 struct commit_list *parents, struct object_id *ret,
Phillip Woode8cbe212020-08-17 18:40:01 +01001552 const char *author, const char *committer,
1553 const char *sign_commit,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001554 struct commit_extra_header *extra)
Jeff King40d52ff2010-04-01 20:05:23 -04001555{
1556 int result;
1557 int encoding_is_utf8;
1558 struct strbuf buffer;
1559
brian m. carlsone816caa2018-03-12 02:27:42 +00001560 assert_oid_type(tree, OBJ_TREE);
Jeff King40d52ff2010-04-01 20:05:23 -04001561
Jeff King3ffefb52014-06-10 17:36:52 -04001562 if (memchr(msg, '\0', msg_len))
Nguyễn Thái Ngọc Duy37576c12011-12-15 20:47:23 +07001563 return error("a NUL byte in commit log message not allowed.");
1564
Jeff King40d52ff2010-04-01 20:05:23 -04001565 /* Not having i18n.commitencoding is the same as having utf-8 */
1566 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1567
1568 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
Patryk Obara5078f342018-01-28 01:13:16 +01001569 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
Jeff King40d52ff2010-04-01 20:05:23 -04001570
1571 /*
1572 * NOTE! This ordering means that the same exact tree merged with a
1573 * different order of parents will be a _different_ changeset even
1574 * if everything else stays the same.
1575 */
1576 while (parents) {
René Scharfee510ab82015-10-24 18:21:31 +02001577 struct commit *parent = pop_commit(&parents);
Jeff King40d52ff2010-04-01 20:05:23 -04001578 strbuf_addf(&buffer, "parent %s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001579 oid_to_hex(&parent->object.oid));
Jeff King40d52ff2010-04-01 20:05:23 -04001580 }
1581
1582 /* Person/date information */
1583 if (!author)
Jeff Kingf9bc5732012-05-24 19:28:40 -04001584 author = git_author_info(IDENT_STRICT);
Jeff King40d52ff2010-04-01 20:05:23 -04001585 strbuf_addf(&buffer, "author %s\n", author);
Phillip Woode8cbe212020-08-17 18:40:01 +01001586 if (!committer)
1587 committer = git_committer_info(IDENT_STRICT);
1588 strbuf_addf(&buffer, "committer %s\n", committer);
Jeff King40d52ff2010-04-01 20:05:23 -04001589 if (!encoding_is_utf8)
1590 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
Junio C Hamano5231c632011-11-07 16:21:32 -08001591
1592 while (extra) {
1593 add_extra_header(&buffer, extra);
1594 extra = extra->next;
1595 }
Jeff King40d52ff2010-04-01 20:05:23 -04001596 strbuf_addch(&buffer, '\n');
1597
1598 /* And add the comment */
Jeff King3ffefb52014-06-10 17:36:52 -04001599 strbuf_add(&buffer, msg, msg_len);
Jeff King40d52ff2010-04-01 20:05:23 -04001600
1601 /* And check the encoding */
Linus Torvalds08a94a12012-06-28 11:24:14 -07001602 if (encoding_is_utf8 && !verify_utf8(&buffer))
Vasco Almeida4fa4b312016-09-19 13:08:16 +00001603 fprintf(stderr, _(commit_utf8_warn));
Jeff King40d52ff2010-04-01 20:05:23 -04001604
brian m. carlson937032e2021-02-11 02:08:04 +00001605 if (sign_commit && sign_with_header(&buffer, sign_commit)) {
Rene Scharfee5051462017-08-30 19:49:38 +02001606 result = -1;
1607 goto out;
1608 }
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001609
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01001610 result = write_object_file(buffer.buf, buffer.len, OBJ_COMMIT, ret);
Rene Scharfee5051462017-08-30 19:49:38 +02001611out:
Jeff King40d52ff2010-04-01 20:05:23 -04001612 strbuf_release(&buffer);
1613 return result;
1614}
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001615
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001616define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1617static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1618
1619struct merge_remote_desc *merge_remote_util(struct commit *commit)
1620{
1621 return *merge_desc_slab_at(&merge_desc_slab, commit);
1622}
1623
René Scharfebeb518c2016-08-13 14:11:27 +02001624void set_merge_remote_desc(struct commit *commit,
1625 const char *name, struct object *obj)
1626{
1627 struct merge_remote_desc *desc;
René Scharfe5447a762016-08-13 14:21:27 +02001628 FLEX_ALLOC_STR(desc, name, name);
René Scharfebeb518c2016-08-13 14:11:27 +02001629 desc->obj = obj;
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001630 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
René Scharfebeb518c2016-08-13 14:11:27 +02001631}
1632
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001633struct commit *get_merge_parent(const char *name)
1634{
1635 struct object *obj;
1636 struct commit *commit;
brian m. carlson7683e2e2015-03-13 23:39:34 +00001637 struct object_id oid;
brian m. carlsone82caf32017-07-13 23:49:28 +00001638 if (get_oid(name, &oid))
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001639 return NULL;
Stefan Beller109cd762018-06-28 18:21:51 -07001640 obj = parse_object(the_repository, &oid);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001641 commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001642 if (commit && !merge_remote_util(commit))
René Scharfebeb518c2016-08-13 14:11:27 +02001643 set_merge_remote_desc(commit, name, obj);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001644 return commit;
1645}
René Scharfe89b5f1d2012-04-25 22:35:27 +02001646
1647/*
1648 * Append a commit to the end of the commit_list.
1649 *
1650 * next starts by pointing to the variable that holds the head of an
1651 * empty commit_list, and is updated to point to the "next" field of
1652 * the last item on the list as new commits are appended.
1653 *
1654 * Usage example:
1655 *
1656 * struct commit_list *list;
1657 * struct commit_list **next = &list;
1658 *
1659 * next = commit_list_append(c1, next);
1660 * next = commit_list_append(c2, next);
1661 * assert(commit_list_count(list) == 2);
1662 * return list;
1663 */
1664struct commit_list **commit_list_append(struct commit *commit,
1665 struct commit_list **next)
1666{
Brandon Williams258c03e2018-02-14 10:59:37 -08001667 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1668 new_commit->item = commit;
1669 *next = new_commit;
1670 new_commit->next = NULL;
1671 return &new_commit->next;
René Scharfe89b5f1d2012-04-25 22:35:27 +02001672}
Nguyễn Thái Ngọc Duyefc7df42012-10-26 22:53:51 +07001673
John Caicfc5cf42022-01-06 20:07:35 +00001674const char *find_header_mem(const char *msg, size_t len,
1675 const char *key, size_t *out_len)
Jeff Kingfe6eb7f2014-08-27 03:56:01 -04001676{
1677 int key_len = strlen(key);
1678 const char *line = msg;
1679
John Caicfc5cf42022-01-06 20:07:35 +00001680 /*
1681 * NEEDSWORK: It's possible for strchrnul() to scan beyond the range
1682 * given by len. However, current callers are safe because they compute
1683 * len by scanning a NUL-terminated block of memory starting at msg.
1684 * Nonetheless, it would be better to ensure the function does not look
1685 * at msg beyond the len provided by the caller.
1686 */
1687 while (line && line < msg + len) {
Jeff Kingfe6eb7f2014-08-27 03:56:01 -04001688 const char *eol = strchrnul(line, '\n');
1689
1690 if (line == eol)
1691 return NULL;
1692
1693 if (eol - line > key_len &&
1694 !strncmp(line, key, key_len) &&
1695 line[key_len] == ' ') {
1696 *out_len = eol - line - key_len - 1;
1697 return line + key_len + 1;
1698 }
1699 line = *eol ? eol + 1 : NULL;
1700 }
1701 return NULL;
1702}
Junio C Hamano0ed8a4e2014-12-22 12:26:23 -08001703
John Caicfc5cf42022-01-06 20:07:35 +00001704const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1705{
1706 return find_header_mem(msg, strlen(msg), key, out_len);
1707}
Christian Couder8c384582014-11-09 10:23:41 +01001708/*
Jonathan Tan710714a2016-11-02 10:29:17 -07001709 * Inspect the given string and determine the true "end" of the log message, in
Bradley M. Kuhn3abd4a62020-10-19 18:03:55 -07001710 * order to find where to put a new Signed-off-by trailer. Ignored are
Brian Malehornd76650b2017-05-15 23:06:49 -07001711 * trailing comment lines and blank lines. To support "git commit -s
1712 * --amend" on an existing commit, we also ignore "Conflicts:". To
1713 * support "git commit -v", we truncate at cut lines.
Christian Couder8c384582014-11-09 10:23:41 +01001714 *
1715 * Returns the number of bytes from the tail to ignore, to be fed as
1716 * the second parameter to append_signoff().
1717 */
Jeff King66e83d92018-08-22 20:50:51 -04001718size_t ignore_non_trailer(const char *buf, size_t len)
Christian Couder8c384582014-11-09 10:23:41 +01001719{
Jeff King66e83d92018-08-22 20:50:51 -04001720 size_t boc = 0;
1721 size_t bol = 0;
Christian Couder8c384582014-11-09 10:23:41 +01001722 int in_old_conflicts_block = 0;
Brian Malehornd76650b2017-05-15 23:06:49 -07001723 size_t cutoff = wt_status_locate_end(buf, len);
Christian Couder8c384582014-11-09 10:23:41 +01001724
Brian Malehornd76650b2017-05-15 23:06:49 -07001725 while (bol < cutoff) {
Jonathan Tan710714a2016-11-02 10:29:17 -07001726 const char *next_line = memchr(buf + bol, '\n', len - bol);
Christian Couder8c384582014-11-09 10:23:41 +01001727
Jonathan Tan710714a2016-11-02 10:29:17 -07001728 if (!next_line)
1729 next_line = buf + len;
Christian Couder8c384582014-11-09 10:23:41 +01001730 else
1731 next_line++;
1732
Jonathan Tan710714a2016-11-02 10:29:17 -07001733 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
Christian Couder8c384582014-11-09 10:23:41 +01001734 /* is this the first of the run of comments? */
1735 if (!boc)
1736 boc = bol;
1737 /* otherwise, it is just continuing */
Jonathan Tan710714a2016-11-02 10:29:17 -07001738 } else if (starts_with(buf + bol, "Conflicts:\n")) {
Christian Couder8c384582014-11-09 10:23:41 +01001739 in_old_conflicts_block = 1;
1740 if (!boc)
1741 boc = bol;
Jonathan Tan710714a2016-11-02 10:29:17 -07001742 } else if (in_old_conflicts_block && buf[bol] == '\t') {
Christian Couder8c384582014-11-09 10:23:41 +01001743 ; /* a pathname in the conflicts block */
1744 } else if (boc) {
1745 /* the previous was not trailing comment */
1746 boc = 0;
1747 in_old_conflicts_block = 0;
1748 }
Jonathan Tan710714a2016-11-02 10:29:17 -07001749 bol = next_line - buf;
Christian Couder8c384582014-11-09 10:23:41 +01001750 }
Brian Malehornd76650b2017-05-15 23:06:49 -07001751 return boc ? len - boc : len - cutoff;
Christian Couder8c384582014-11-09 10:23:41 +01001752}
Phillip Wood49697cb2019-10-15 10:25:31 +00001753
1754int run_commit_hook(int editor_is_used, const char *index_file,
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001755 int *invoked_hook, const char *name, ...)
Phillip Wood49697cb2019-10-15 10:25:31 +00001756{
Emily Shafferf4432462021-12-22 04:59:40 +01001757 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
Phillip Wood49697cb2019-10-15 10:25:31 +00001758 va_list args;
Emily Shafferf4432462021-12-22 04:59:40 +01001759 const char *arg;
Phillip Wood49697cb2019-10-15 10:25:31 +00001760
Emily Shafferf4432462021-12-22 04:59:40 +01001761 strvec_pushf(&opt.env, "GIT_INDEX_FILE=%s", index_file);
Phillip Wood49697cb2019-10-15 10:25:31 +00001762
1763 /*
1764 * Let the hook know that no editor will be launched.
1765 */
1766 if (!editor_is_used)
Emily Shafferf4432462021-12-22 04:59:40 +01001767 strvec_push(&opt.env, "GIT_EDITOR=:");
Phillip Wood49697cb2019-10-15 10:25:31 +00001768
1769 va_start(args, name);
Emily Shafferf4432462021-12-22 04:59:40 +01001770 while ((arg = va_arg(args, const char *)))
1771 strvec_push(&opt.args, arg);
Phillip Wood49697cb2019-10-15 10:25:31 +00001772 va_end(args);
Phillip Wood49697cb2019-10-15 10:25:31 +00001773
Ævar Arnfjörð Bjarmason4369e3a2022-03-22 00:15:13 +01001774 opt.invoked_hook = invoked_hook;
Emily Shafferf4432462021-12-22 04:59:40 +01001775 return run_hooks_opt(name, &opt);
Phillip Wood49697cb2019-10-15 10:25:31 +00001776}