blob: 8405d7c3fceab23c6eafd16e36d76c7172c44923 [file] [log] [blame]
Elijah Newrend812c3b2023-04-11 00:41:56 -07001#include "git-compat-util.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 Newren7ee24e12023-03-21 06:25:57 +00005#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00006#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00007#include "hex.h"
Jonathan Nieder6a1a79f2018-05-15 16:42:16 -07008#include "repository.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -07009#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:06 +000010#include "object-store-ll.h"
Johannes Schindelined09aef2006-10-30 20:09:06 +010011#include "pkt-line.h"
Junio C Hamano52883fb2006-12-25 11:48:35 -080012#include "utf8.h"
Junio C Hamano199c45b2007-04-09 02:34:05 -070013#include "diff.h"
14#include "revision.h"
Johannes Schindelina97a7462009-10-09 12:21:57 +020015#include "notes.h"
Stefan Beller14ba97f2018-05-15 14:48:42 -070016#include "alloc.h"
Junio C Hamanoba3c69a2011-10-05 17:23:20 -070017#include "gpg-interface.h"
René Scharfe46905892012-04-01 00:10:39 +020018#include "mergesort.h"
Junio C Hamanoa84b7942013-04-13 11:56:41 -070019#include "commit-slab.h"
Junio C Hamanoda24b102013-06-06 21:58:12 -070020#include "prio-queue.h"
Martin Ågrenbc626922020-12-31 12:56:23 +010021#include "hash-lookup.h"
Brian Malehornd76650b2017-05-15 23:06:49 -070022#include "wt-status.h"
Johannes Schindelinf9f99b32018-04-29 00:44:44 +020023#include "advice.h"
Pratik Karki103148a2018-09-04 15:00:08 -070024#include "refs.h"
Junio C Hamano02931212018-11-02 11:04:55 +090025#include "commit-reach.h"
Phillip Wood49697cb2019-10-15 10:25:31 +000026#include "run-command.h"
Elijah Newrene38da482023-03-21 06:26:05 +000027#include "setup.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060028#include "shallow.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +000029#include "tree.h"
Emily Shafferf4432462021-12-22 04:59:40 +010030#include "hook.h"
Patrick Steinhardt7a5d6042023-10-31 08:16:18 +010031#include "parse.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -070032
Junio C Hamano82a75292012-09-15 13:58:15 -070033static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
34
Linus Torvalds60ab26d2005-09-15 14:43:17 -070035int save_commit_buffer = 1;
Ævar Arnfjörð Bjarmasonab628582021-08-23 12:44:02 +020036int no_graft_file_deprecated_advice;
Linus Torvalds60ab26d2005-09-15 14:43:17 -070037
Daniel Barkalow175785e2005-04-18 11:39:48 -070038const char *commit_type = "commit";
39
Stefan Bellerd9a05e72018-06-28 18:22:21 -070040struct commit *lookup_commit_reference_gently(struct repository *r,
Stefan Beller21e1ee82018-06-28 18:21:57 -070041 const struct object_id *oid, int quiet)
Linus Torvalds961784e2005-05-18 16:14:22 -070042{
Stefan Bellerd9a05e72018-06-28 18:22:21 -070043 struct object *obj = deref_tag(r,
44 parse_object(r, oid),
Stefan Beller109cd762018-06-28 18:21:51 -070045 NULL, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070046
47 if (!obj)
48 return NULL;
Abhishek Kumar6da43d92020-06-17 14:44:08 +053049 return object_as_type(obj, OBJ_COMMIT, quiet);
Junio C Hamanof76412e2005-08-21 02:51:10 -070050}
51
Stefan Beller1f6c72f2018-06-28 18:22:22 -070052struct commit *lookup_commit_reference(struct repository *r, const struct object_id *oid)
Junio C Hamanof76412e2005-08-21 02:51:10 -070053{
Stefan Beller1f6c72f2018-06-28 18:22:22 -070054 return lookup_commit_reference_gently(r, oid, 0);
Linus Torvalds961784e2005-05-18 16:14:22 -070055}
56
brian m. carlsonbc832662017-05-06 22:10:10 +000057struct 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 +100058{
Stefan Beller2122f672018-06-28 18:21:58 -070059 struct commit *c = lookup_commit_reference(the_repository, oid);
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100060 if (!c)
61 die(_("could not parse %s"), ref_name);
Jeff King9001dc22018-08-28 17:22:48 -040062 if (!oideq(oid, &c->object.oid)) {
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100063 warning(_("%s %s is not a commit!"),
brian m. carlsonbc832662017-05-06 22:10:10 +000064 ref_name, oid_to_hex(oid));
Nguyễn Thái Ngọc Duybaf18fc2011-09-17 21:57:45 +100065 }
66 return c;
67}
68
Phillip Woodb8dbfd02022-10-17 13:17:40 +000069struct commit *lookup_commit_object(struct repository *r,
70 const struct object_id *oid)
71{
72 struct object *obj = parse_object(r, oid);
73 return obj ? object_as_type(obj, OBJ_COMMIT, 0) : NULL;
74
75}
76
Stefan Bellerbacf1682018-06-28 18:22:10 -070077struct commit *lookup_commit(struct repository *r, const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 11:39:48 -070078{
Jeff Kingd0229ab2019-06-20 03:41:14 -040079 struct object *obj = lookup_object(r, oid);
Jeff Kingd36f51c2014-07-13 02:41:55 -040080 if (!obj)
Jeff Kinga3785092019-06-20 03:41:21 -040081 return create_object(r, oid, alloc_commit_node(r));
Abhishek Kumar6da43d92020-06-17 14:44:08 +053082 return object_as_type(obj, OBJ_COMMIT, 0);
Daniel Barkalow175785e2005-04-18 11:39:48 -070083}
84
Pat Notza6fa5992010-11-02 13:59:07 -060085struct commit *lookup_commit_reference_by_name(const char *name)
86{
brian m. carlson7683e2e2015-03-13 23:39:34 +000087 struct object_id oid;
Pat Notza6fa5992010-11-02 13:59:07 -060088 struct commit *commit;
89
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +020090 if (repo_get_oid_committish(the_repository, name, &oid))
Pat Notza6fa5992010-11-02 13:59:07 -060091 return NULL;
Stefan Beller2122f672018-06-28 18:21:58 -070092 commit = lookup_commit_reference(the_repository, &oid);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +020093 if (repo_parse_commit(the_repository, commit))
Pat Notza6fa5992010-11-02 13:59:07 -060094 return NULL;
95 return commit;
96}
97
Johannes Schindelindddbad72017-04-26 21:29:31 +020098static timestamp_t parse_commit_date(const char *buf, const char *tail)
Daniel Barkalow175785e2005-04-18 11:39:48 -070099{
Martin Koegler0a617792008-01-19 18:35:23 +0100100 const char *dateptr;
Jeff Kingea1615d2023-04-27 04:14:09 -0400101 const char *eol;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700102
Martin Koegler0a617792008-01-19 18:35:23 +0100103 if (buf + 6 >= tail)
104 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700105 if (memcmp(buf, "author", 6))
106 return 0;
Martin Koegler0a617792008-01-19 18:35:23 +0100107 while (buf < tail && *buf++ != '\n')
Daniel Barkalow175785e2005-04-18 11:39:48 -0700108 /* nada */;
Martin Koegler0a617792008-01-19 18:35:23 +0100109 if (buf + 9 >= tail)
110 return 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700111 if (memcmp(buf, "committer", 9))
112 return 0;
Jeff Kingea1615d2023-04-27 04:14:09 -0400113
114 /*
115 * Jump to end-of-line so that we can walk backwards to find the
116 * end-of-email ">". This is more forgiving of malformed cases
117 * because unexpected characters tend to be in the name and email
118 * fields.
119 */
120 eol = memchr(buf, '\n', tail - buf);
121 if (!eol)
Martin Koegler0a617792008-01-19 18:35:23 +0100122 return 0;
Jeff Kingea1615d2023-04-27 04:14:09 -0400123 dateptr = eol;
124 while (dateptr > buf && dateptr[-1] != '>')
125 dateptr--;
Jeff King089d9ad2023-04-27 04:17:15 -0400126 if (dateptr == buf)
Martin Koegler0a617792008-01-19 18:35:23 +0100127 return 0;
Jeff Kingea1615d2023-04-27 04:14:09 -0400128
Jeff King089d9ad2023-04-27 04:17:15 -0400129 /*
130 * Trim leading whitespace, but make sure we have at least one
131 * non-whitespace character, as parse_timestamp() will otherwise walk
132 * right past the newline we found in "eol" when skipping whitespace
133 * itself.
134 *
135 * In theory it would be sufficient to allow any character not matched
136 * by isspace(), but there's a catch: our isspace() does not
137 * necessarily match the behavior of parse_timestamp(), as the latter
138 * is implemented by system routines which match more exotic control
139 * codes, or even locale-dependent sequences.
140 *
141 * Since we expect the timestamp to be a number, we can check for that.
142 * Anything else (e.g., a non-numeric token like "foo") would just
143 * cause parse_timestamp() to return 0 anyway.
144 */
145 while (dateptr < eol && isspace(*dateptr))
146 dateptr++;
147 if (!isdigit(*dateptr) && *dateptr != '-')
148 return 0;
149
150 /*
151 * We know there is at least one digit (or dash), so we'll begin
152 * parsing there and stop at worst case at eol.
Jeff King90ef0f12023-04-27 04:17:24 -0400153 *
154 * Note that we may feed parse_timestamp() extra characters here if the
155 * commit is malformed, and it will parse as far as it can. For
156 * example, "123foo456" would return "123". That might be questionable
157 * (versus returning "0"), but it would help in a hypothetical case
158 * like "123456+0100", where the whitespace from the timezone is
159 * missing. Since such syntactic errors may be baked into history and
160 * hard to correct now, let's err on trying to make our best guess
161 * here, rather than insist on perfect syntax.
Jeff King089d9ad2023-04-27 04:17:15 -0400162 */
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200163 return parse_timestamp(dateptr, NULL, 10);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700164}
165
Jeff King8380dcd2021-01-28 01:20:23 -0500166static const struct object_id *commit_graft_oid_access(size_t index, const void *table)
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400167{
Jeff King8380dcd2021-01-28 01:20:23 -0500168 const struct commit_graft * const *commit_graft_table = table;
Jeff King45ee13b2021-01-28 01:19:42 -0500169 return &commit_graft_table[index]->oid;
Dmitry S. Dolzhenko0a9136f2014-02-26 22:49:22 +0400170}
171
Jeff King98c431b2021-01-28 01:12:35 -0500172int commit_graft_pos(struct repository *r, const struct object_id *oid)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700173{
Jeff King45ee13b2021-01-28 01:19:42 -0500174 return oid_pos(oid, r->parsed_objects->grafts,
175 r->parsed_objects->grafts_nr,
176 commit_graft_oid_access);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700177}
178
Jonathan Tan4d4e49f2022-06-06 10:54:37 -0700179static void unparse_commit(struct repository *r, const struct object_id *oid)
180{
181 struct commit *c = lookup_commit(r, oid);
182
183 if (!c->object.parsed)
184 return;
185 free_commit_list(c->parents);
186 c->parents = NULL;
187 c->object.parsed = 0;
188}
189
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700190int register_commit_graft(struct repository *r, struct commit_graft *graft,
191 int ignore_dups)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700192{
Jeff King98c431b2021-01-28 01:12:35 -0500193 int pos = commit_graft_pos(r, &graft->oid);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700194
Junio C Hamano5040f172006-04-06 23:58:51 -0700195 if (0 <= pos) {
196 if (ignore_dups)
197 free(graft);
198 else {
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700199 free(r->parsed_objects->grafts[pos]);
200 r->parsed_objects->grafts[pos] = graft;
Junio C Hamano5040f172006-04-06 23:58:51 -0700201 }
202 return 1;
203 }
204 pos = -pos - 1;
Brandon Williamsa3b78e82018-05-17 15:51:48 -0700205 ALLOC_GROW(r->parsed_objects->grafts,
206 r->parsed_objects->grafts_nr + 1,
207 r->parsed_objects->grafts_alloc);
208 r->parsed_objects->grafts_nr++;
209 if (pos < r->parsed_objects->grafts_nr)
210 memmove(r->parsed_objects->grafts + pos + 1,
211 r->parsed_objects->grafts + pos,
212 (r->parsed_objects->grafts_nr - pos - 1) *
213 sizeof(*r->parsed_objects->grafts));
214 r->parsed_objects->grafts[pos] = graft;
Jonathan Tan4d4e49f2022-06-06 10:54:37 -0700215 unparse_commit(r, &graft->oid);
Junio C Hamano5040f172006-04-06 23:58:51 -0700216 return 0;
217}
218
Patryk Obara9a934032017-08-18 20:33:12 +0200219struct commit_graft *read_graft_line(struct strbuf *line)
Junio C Hamano5040f172006-04-06 23:58:51 -0700220{
221 /* The format is just "Commit Parent1 Parent2 ...\n" */
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200222 int i, phase;
223 const char *tail = NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700224 struct commit_graft *graft = NULL;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200225 struct object_id dummy_oid, *oid;
Junio C Hamano5040f172006-04-06 23:58:51 -0700226
Patryk Obara9a934032017-08-18 20:33:12 +0200227 strbuf_rtrim(line);
228 if (!line->len || line->buf[0] == '#')
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700229 return NULL;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200230 /*
231 * phase 0 verifies line, counts hashes in line and allocates graft
232 * phase 1 fills graft
233 */
234 for (phase = 0; phase < 2; phase++) {
235 oid = graft ? &graft->oid : &dummy_oid;
236 if (parse_oid_hex(line->buf, oid, &tail))
Junio C Hamano5040f172006-04-06 23:58:51 -0700237 goto bad_graft_data;
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200238 for (i = 0; *tail != '\0'; i++) {
239 oid = graft ? &graft->parent[i] : &dummy_oid;
240 if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
241 goto bad_graft_data;
242 }
243 if (!graft) {
244 graft = xmalloc(st_add(sizeof(*graft),
245 st_mult(sizeof(struct object_id), i)));
246 graft->nr_parent = i;
247 }
Junio C Hamano5040f172006-04-06 23:58:51 -0700248 }
249 return graft;
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100250
251bad_graft_data:
Patryk Obara9a934032017-08-18 20:33:12 +0200252 error("bad graft data: %s", line->buf);
Patryk Obaracfa5bf12017-08-18 20:33:14 +0200253 assert(!graft);
Ralf Thielowdf5d43b2010-12-01 20:15:59 +0100254 return NULL;
Junio C Hamano5040f172006-04-06 23:58:51 -0700255}
256
Brandon Williamsd0e5dd02018-05-17 15:51:49 -0700257static int read_graft_file(struct repository *r, const char *graft_file)
Junio C Hamano5040f172006-04-06 23:58:51 -0700258{
Nguyễn Thái Ngọc Duye9d983f2017-05-03 17:16:50 +0700259 FILE *fp = fopen_or_warn(graft_file, "r");
Johannes Schindeline228c172013-12-27 21:49:57 +0100260 struct strbuf buf = STRBUF_INIT;
Junio C Hamano5040f172006-04-06 23:58:51 -0700261 if (!fp)
262 return -1;
Ævar Arnfjörð Bjarmasonab628582021-08-23 12:44:02 +0200263 if (!no_graft_file_deprecated_advice &&
264 advice_enabled(ADVICE_GRAFT_FILE_DEPRECATED))
Johannes Schindelinf9f99b32018-04-29 00:44:44 +0200265 advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
266 "and will be removed in a future Git version.\n"
267 "\n"
268 "Please use \"git replace --convert-graft-file\"\n"
269 "to convert the grafts into replace refs.\n"
270 "\n"
271 "Turn this message off by running\n"
272 "\"git config advice.graftFileDeprecated false\""));
Johannes Schindeline228c172013-12-27 21:49:57 +0100273 while (!strbuf_getwholeline(&buf, fp, '\n')) {
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700274 /* The format is just "Commit Parent1 Parent2 ...\n" */
Patryk Obara9a934032017-08-18 20:33:12 +0200275 struct commit_graft *graft = read_graft_line(&buf);
Junio C Hamano5bc4ce52006-04-16 14:24:56 -0700276 if (!graft)
277 continue;
Brandon Williamsd0e5dd02018-05-17 15:51:49 -0700278 if (register_commit_graft(r, graft, 1))
Johannes Schindeline228c172013-12-27 21:49:57 +0100279 error("duplicate graft data: %s", buf.buf);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700280 }
281 fclose(fp);
Johannes Schindeline228c172013-12-27 21:49:57 +0100282 strbuf_release(&buf);
Junio C Hamano5040f172006-04-06 23:58:51 -0700283 return 0;
284}
285
Derrick Stolee20fd6d52018-08-20 18:24:30 +0000286void prepare_commit_graft(struct repository *r)
Junio C Hamano5040f172006-04-06 23:58:51 -0700287{
Junio C Hamano5040f172006-04-06 23:58:51 -0700288 char *graft_file;
289
Stefan Beller2f6c7672018-05-17 15:51:53 -0700290 if (r->parsed_objects->commit_graft_prepared)
Junio C Hamano5040f172006-04-06 23:58:51 -0700291 return;
Jeff King14a9bd22018-05-31 18:42:53 -0400292 if (!startup_info->have_repository)
293 return;
294
Stefan Beller2f6c7672018-05-17 15:51:53 -0700295 graft_file = get_graft_file(r);
296 read_graft_file(r, graft_file);
Johannes Schindelined09aef2006-10-30 20:09:06 +0100297 /* make sure shallows are read */
Stefan Beller2f6c7672018-05-17 15:51:53 -0700298 is_repository_shallow(r);
299 r->parsed_objects->commit_graft_prepared = 1;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700300}
301
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700302struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid)
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700303{
304 int pos;
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700305 prepare_commit_graft(r);
Jeff King98c431b2021-01-28 01:12:35 -0500306 pos = commit_graft_pos(r, oid);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700307 if (pos < 0)
308 return NULL;
Stefan Bellerb9dbddf2018-05-17 15:51:54 -0700309 return r->parsed_objects->grafts[pos];
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700310}
311
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700312int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
Johannes Schindelined09aef2006-10-30 20:09:06 +0100313{
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700314 int i, ret;
Jonathan Nieder6a1a79f2018-05-15 16:42:16 -0700315 for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++)
316 ret = fn(the_repository->parsed_objects->grafts[i], cb_data);
Nguyễn Thái Ngọc Duy09d46642011-08-18 19:29:35 +0700317 return ret;
Johannes Schindelined09aef2006-10-30 20:09:06 +0100318}
319
Jonathan Tan2a69ff02022-03-17 11:24:47 -0700320void reset_commit_grafts(struct repository *r)
321{
322 int i;
323
Jonathan Tan4d4e49f2022-06-06 10:54:37 -0700324 for (i = 0; i < r->parsed_objects->grafts_nr; i++) {
325 unparse_commit(r, &r->parsed_objects->grafts[i]->oid);
Jonathan Tan2a69ff02022-03-17 11:24:47 -0700326 free(r->parsed_objects->grafts[i]);
Jonathan Tan4d4e49f2022-06-06 10:54:37 -0700327 }
Jonathan Tan2a69ff02022-03-17 11:24:47 -0700328 r->parsed_objects->grafts_nr = 0;
329 r->parsed_objects->commit_graft_prepared = 0;
330}
331
Jeff King8597ea32014-06-10 17:44:13 -0400332struct commit_buffer {
333 void *buffer;
334 unsigned long size;
335};
336define_commit_slab(buffer_slab, struct commit_buffer);
Jeff Kingc1b3c712014-06-10 17:43:02 -0400337
Stefan Beller65ea9d42018-06-28 18:22:15 -0700338struct buffer_slab *allocate_commit_buffer_slab(void)
Jeff King66c28272014-06-10 17:40:14 -0400339{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700340 struct buffer_slab *bs = xmalloc(sizeof(*bs));
341 init_buffer_slab(bs);
342 return bs;
343}
344
345void free_commit_buffer_slab(struct buffer_slab *bs)
346{
347 clear_buffer_slab(bs);
348 free(bs);
349}
Johannes Schindelinf53514b2006-10-30 20:09:53 +0100350
Stefan Beller1a40fc42018-06-28 18:22:16 -0700351void set_commit_buffer(struct repository *r, struct commit *commit, void *buffer, unsigned long size)
Jeff King66c28272014-06-10 17:40:14 -0400352{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700353 struct commit_buffer *v = buffer_slab_at(
Stefan Beller1a40fc42018-06-28 18:22:16 -0700354 r->parsed_objects->buffer_slab, commit);
Jeff King8597ea32014-06-10 17:44:13 -0400355 v->buffer = buffer;
356 v->size = size;
Jeff King66c28272014-06-10 17:40:14 -0400357}
358
Stefan Beller4ff7e5c2018-06-28 18:22:17 -0700359const void *get_cached_commit_buffer(struct repository *r, const struct commit *commit, unsigned long *sizep)
Jeff King152ff1c2014-06-10 17:40:39 -0400360{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700361 struct commit_buffer *v = buffer_slab_peek(
Stefan Beller4ff7e5c2018-06-28 18:22:17 -0700362 r->parsed_objects->buffer_slab, commit);
Junio C Hamano862e7302015-05-14 15:25:52 -0700363 if (!v) {
364 if (sizep)
365 *sizep = 0;
366 return NULL;
367 }
Jeff King8597ea32014-06-10 17:44:13 -0400368 if (sizep)
369 *sizep = v->size;
370 return v->buffer;
Jeff King152ff1c2014-06-10 17:40:39 -0400371}
372
Stefan Beller07de3fd2018-11-13 16:12:57 -0800373const void *repo_get_commit_buffer(struct repository *r,
374 const struct commit *commit,
375 unsigned long *sizep)
Jeff King152ff1c2014-06-10 17:40:39 -0400376{
Stefan Beller07de3fd2018-11-13 16:12:57 -0800377 const void *ret = get_cached_commit_buffer(r, commit, sizep);
Jeff King152ff1c2014-06-10 17:40:39 -0400378 if (!ret) {
379 enum object_type type;
380 unsigned long size;
Stefan Beller07de3fd2018-11-13 16:12:57 -0800381 ret = repo_read_object_file(r, &commit->object.oid, &type, &size);
Jeff King152ff1c2014-06-10 17:40:39 -0400382 if (!ret)
383 die("cannot read commit object %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000384 oid_to_hex(&commit->object.oid));
Jeff King152ff1c2014-06-10 17:40:39 -0400385 if (type != OBJ_COMMIT)
386 die("expected commit for %s, got %s",
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800387 oid_to_hex(&commit->object.oid), type_name(type));
Jeff King8597ea32014-06-10 17:44:13 -0400388 if (sizep)
389 *sizep = size;
Jeff King152ff1c2014-06-10 17:40:39 -0400390 }
391 return ret;
392}
393
Stefan Beller70315372018-11-13 16:12:58 -0800394void repo_unuse_commit_buffer(struct repository *r,
395 const struct commit *commit,
396 const void *buffer)
Jeff King152ff1c2014-06-10 17:40:39 -0400397{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700398 struct commit_buffer *v = buffer_slab_peek(
Stefan Beller70315372018-11-13 16:12:58 -0800399 r->parsed_objects->buffer_slab, commit);
Junio C Hamano862e7302015-05-14 15:25:52 -0700400 if (!(v && v->buffer == buffer))
Jeff King152ff1c2014-06-10 17:40:39 -0400401 free((void *)buffer);
402}
403
Stefan Beller6a7895f2018-12-14 16:09:40 -0800404void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit)
Jeff King0fb370d2014-06-12 18:05:37 -0400405{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700406 struct commit_buffer *v = buffer_slab_peek(
Stefan Beller6a7895f2018-12-14 16:09:40 -0800407 pool->buffer_slab, commit);
Junio C Hamano862e7302015-05-14 15:25:52 -0700408 if (v) {
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000409 FREE_AND_NULL(v->buffer);
Junio C Hamano862e7302015-05-14 15:25:52 -0700410 v->size = 0;
411 }
Jeff King0fb370d2014-06-12 18:05:37 -0400412}
413
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700414static inline void set_commit_tree(struct commit *c, struct tree *t)
415{
416 c->maybe_tree = t;
417}
418
Nguyễn Thái Ngọc Duy301b8c72019-04-16 16:33:19 +0700419struct tree *repo_get_commit_tree(struct repository *r,
420 const struct commit *commit)
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000421{
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000422 if (commit->maybe_tree || !commit->object.parsed)
423 return commit->maybe_tree;
424
Abhishek Kumarc49c82a2020-06-17 14:44:10 +0530425 if (commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
Junio C Hamanoea2dab12019-05-09 00:37:25 +0900426 return get_commit_tree_in_graph(r, commit);
Derrick Stolee7b8a21d2018-04-06 19:09:46 +0000427
Jeff King83487662019-04-09 19:13:20 -0700428 return NULL;
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000429}
430
431struct object_id *get_commit_tree_oid(const struct commit *commit)
432{
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200433 struct tree *tree = repo_get_commit_tree(the_repository, commit);
Taylor Blau806278d2019-09-05 18:04:57 -0400434 return tree ? &tree->object.oid : NULL;
Derrick Stolee5bb03de2018-04-06 19:09:34 +0000435}
436
Stefan Beller6a7895f2018-12-14 16:09:40 -0800437void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)
Stefan Beller14ba97f2018-05-15 14:48:42 -0700438{
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700439 set_commit_tree(c, NULL);
Stefan Beller6a7895f2018-12-14 16:09:40 -0800440 free_commit_buffer(pool, c);
Mike Hommey9784f972019-08-26 11:01:37 +0900441 c->index = 0;
Stefan Beller14ba97f2018-05-15 14:48:42 -0700442 free_commit_list(c->parents);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700443
444 c->object.parsed = 0;
445}
446
Jeff King8597ea32014-06-10 17:44:13 -0400447const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
Jeff King0fb370d2014-06-12 18:05:37 -0400448{
Stefan Beller65ea9d42018-06-28 18:22:15 -0700449 struct commit_buffer *v = buffer_slab_peek(
450 the_repository->parsed_objects->buffer_slab, commit);
Jeff King8597ea32014-06-10 17:44:13 -0400451 void *ret;
452
Junio C Hamano862e7302015-05-14 15:25:52 -0700453 if (!v) {
454 if (sizep)
455 *sizep = 0;
456 return NULL;
457 }
Jeff King8597ea32014-06-10 17:44:13 -0400458 ret = v->buffer;
459 if (sizep)
460 *sizep = v->size;
461
462 v->buffer = NULL;
463 v->size = 0;
Jeff King0fb370d2014-06-12 18:05:37 -0400464 return ret;
465}
466
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700467int 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 -0700468{
Nguyễn Thái Ngọc Duycf7b1ca2011-02-05 17:52:20 +0700469 const char *tail = buffer;
470 const char *bufptr = buffer;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000471 struct object_id parent;
Linus Torvalds6c88be12005-06-20 20:26:03 -0700472 struct commit_list **pptr;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700473 struct commit_graft *graft;
brian m. carlson2770ccb2018-07-16 01:27:56 +0000474 const int tree_entry_len = the_hash_algo->hexsz + 5;
475 const int parent_entry_len = the_hash_algo->hexsz + 7;
Jeff King12736d22019-10-18 00:43:29 -0400476 struct tree *tree;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400477
Daniel Barkalow175785e2005-04-18 11:39:48 -0700478 if (item->object.parsed)
479 return 0;
Ævar Arnfjörð Bjarmasonbf20fe42022-04-13 22:01:34 +0200480 /*
481 * Presumably this is leftover from an earlier failed parse;
482 * clear it out in preparation for us re-parsing (we'll hit the
483 * same error, but that's good, since it lets our caller know
484 * the result cannot be trusted.
485 */
486 free_commit_list(item->parents);
487 item->parents = NULL;
Jeff King228c78f2019-10-25 17:20:20 -0400488
Junio C Hamano3b44f152006-06-28 03:51:00 -0700489 tail += size;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000490 if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
491 bufptr[tree_entry_len] != '\n')
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000492 return error("bogus commit object %s", oid_to_hex(&item->object.oid));
brian m. carlson26ea3e72018-05-02 00:25:47 +0000493 if (get_oid_hex(bufptr + 5, &parent) < 0)
Junio C Hamanobd2afde2006-02-22 17:47:10 -0800494 return error("bad tree pointer in commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000495 oid_to_hex(&item->object.oid));
Jeff King12736d22019-10-18 00:43:29 -0400496 tree = lookup_tree(r, &parent);
497 if (!tree)
498 return error("bad tree pointer %s in commit %s",
499 oid_to_hex(&parent),
500 oid_to_hex(&item->object.oid));
501 set_commit_tree(item, tree);
brian m. carlson7683e2e2015-03-13 23:39:34 +0000502 bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
Linus Torvalds6c88be12005-06-20 20:26:03 -0700503 pptr = &item->parents;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700504
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700505 graft = lookup_commit_graft(r, &item->object.oid);
Taylor Blauce163642020-07-08 17:10:53 -0400506 if (graft)
507 r->parsed_objects->substituted_parent = 1;
brian m. carlson7683e2e2015-03-13 23:39:34 +0000508 while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
Linus Torvaldsf7cc77d2005-07-27 15:12:48 -0700509 struct commit *new_parent;
510
brian m. carlson7683e2e2015-03-13 23:39:34 +0000511 if (tail <= bufptr + parent_entry_len + 1 ||
brian m. carlson26ea3e72018-05-02 00:25:47 +0000512 get_oid_hex(bufptr + 7, &parent) ||
brian m. carlson7683e2e2015-03-13 23:39:34 +0000513 bufptr[parent_entry_len] != '\n')
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000514 return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
brian m. carlson7683e2e2015-03-13 23:39:34 +0000515 bufptr += parent_entry_len + 1;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +0200516 /*
517 * The clone is shallow if nr_parent < 0, and we must
518 * not traverse its real parents even when we unhide them.
519 */
René Scharfe3a5f3082023-07-21 14:41:56 +0200520 if (graft && (graft->nr_parent < 0 || !grafts_keep_true_parents))
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700521 continue;
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700522 new_parent = lookup_commit(r, &parent);
Jeff Kingc78fe002019-10-18 00:42:13 -0400523 if (!new_parent)
524 return error("bad parent %s in commit %s",
525 oid_to_hex(&parent),
526 oid_to_hex(&item->object.oid));
527 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700528 }
529 if (graft) {
530 int i;
531 struct commit *new_parent;
532 for (i = 0; i < graft->nr_parent; i++) {
Stefan Bellerfd8030c2018-06-28 18:22:13 -0700533 new_parent = lookup_commit(r,
Stefan Bellerc1f5eb42018-06-28 18:21:59 -0700534 &graft->parent[i]);
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700535 if (!new_parent)
Jeff Kingc78fe002019-10-18 00:42:13 -0400536 return error("bad graft parent %s in commit %s",
537 oid_to_hex(&graft->parent[i]),
538 oid_to_hex(&item->object.oid));
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700539 pptr = &commit_list_insert(new_parent, pptr)->next;
Junio C Hamano5da5c8f2005-07-30 00:58:28 -0700540 }
Daniel Barkalow175785e2005-04-18 11:39:48 -0700541 }
Martin Koegler0a617792008-01-19 18:35:23 +0100542 item->date = parse_commit_date(bufptr, tail);
Junio C Hamano27dedf02005-11-16 21:32:44 -0800543
Derrick Stoleee2838d82018-05-01 12:47:13 +0000544 if (check_graph)
Derrick Stoleec7944052019-05-09 07:22:31 -0700545 load_commit_graph_info(r, item);
Derrick Stoleee2838d82018-05-01 12:47:13 +0000546
Jeff King228c78f2019-10-25 17:20:20 -0400547 item->object.parsed = 1;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700548 return 0;
549}
550
Stefan Beller9e5252a2018-11-13 16:12:50 -0800551int repo_parse_commit_internal(struct repository *r,
552 struct commit *item,
553 int quiet_on_missing,
554 int use_commit_graph)
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400555{
Nicolas Pitre21666f12007-02-26 14:55:59 -0500556 enum object_type type;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400557 void *buffer;
558 unsigned long size;
Jonathan Tan7e2ad1c2022-12-14 11:17:43 -0800559 struct object_info oi = {
560 .typep = &type,
561 .sizep = &size,
562 .contentp = &buffer,
563 };
564 /*
565 * Git does not support partial clones that exclude commits, so set
566 * OBJECT_INFO_SKIP_FETCH_OBJECT to fail fast when an object is missing.
567 */
568 int flags = OBJECT_INFO_LOOKUP_REPLACE | OBJECT_INFO_SKIP_FETCH_OBJECT |
569 OBJECT_INFO_DIE_IF_CORRUPT;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400570 int ret;
571
Martin Koegler9786f682008-02-18 21:48:02 +0100572 if (!item)
573 return -1;
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400574 if (item->object.parsed)
575 return 0;
Patrick Steinhardt7a5d6042023-10-31 08:16:18 +0100576 if (use_commit_graph && parse_commit_in_graph(r, item)) {
577 static int commit_graph_paranoia = -1;
578
579 if (commit_graph_paranoia == -1)
580 commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 1);
581
582 if (commit_graph_paranoia && !has_object(r, &item->object.oid, 0)) {
583 unparse_commit(r, &item->object.oid);
584 return quiet_on_missing ? -1 :
585 error(_("commit %s exists in commit-graph but not in the object database"),
586 oid_to_hex(&item->object.oid));
587 }
588
Derrick Stolee177722b2018-04-10 08:56:05 -0400589 return 0;
Patrick Steinhardt7a5d6042023-10-31 08:16:18 +0100590 }
Jonathan Tan7e2ad1c2022-12-14 11:17:43 -0800591
592 if (oid_object_info_extended(r, &item->object.oid, &oi, flags) < 0)
Jeff King9cc2b072015-06-01 05:56:26 -0400593 return quiet_on_missing ? -1 :
594 error("Could not read %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000595 oid_to_hex(&item->object.oid));
Nicolas Pitre21666f12007-02-26 14:55:59 -0500596 if (type != OBJ_COMMIT) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400597 free(buffer);
598 return error("Object %s not a commit",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000599 oid_to_hex(&item->object.oid));
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400600 }
Derrick Stolee9b19ada2018-06-27 09:24:30 -0400601
Stefan Beller9e5252a2018-11-13 16:12:50 -0800602 ret = parse_commit_buffer(r, item, buffer, size, 0);
Linus Torvalds60ab26d2005-09-15 14:43:17 -0700603 if (save_commit_buffer && !ret) {
Stefan Beller9e5252a2018-11-13 16:12:50 -0800604 set_commit_buffer(r, item, buffer, size);
Linus Torvalds3ff1fbb2005-05-25 18:27:14 -0700605 return 0;
606 }
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400607 free(buffer);
608 return ret;
609}
610
Stefan Beller9e5252a2018-11-13 16:12:50 -0800611int repo_parse_commit_gently(struct repository *r,
612 struct commit *item, int quiet_on_missing)
Derrick Stolee9b19ada2018-06-27 09:24:30 -0400613{
Stefan Beller9e5252a2018-11-13 16:12:50 -0800614 return repo_parse_commit_internal(r, item, quiet_on_missing, 1);
Derrick Stolee9b19ada2018-06-27 09:24:30 -0400615}
616
Jeff King7059dcc2013-10-24 04:52:36 -0400617void parse_commit_or_die(struct commit *item)
618{
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200619 if (repo_parse_commit(the_repository, item))
Jeff King7059dcc2013-10-24 04:52:36 -0400620 die("unable to parse commit %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000621 item ? oid_to_hex(&item->object.oid) : "(null)");
Jeff King7059dcc2013-10-24 04:52:36 -0400622}
623
Christian Couder11af2aa2010-07-22 15:18:30 +0200624int find_commit_subject(const char *commit_buffer, const char **subject)
625{
626 const char *eol;
627 const char *p = commit_buffer;
628
629 while (*p && (*p != '\n' || p[1] != '\n'))
630 p++;
631 if (*p) {
Johannes Schindelin4e1b06d2016-06-22 22:20:20 +0200632 p = skip_blank_lines(p + 2);
Junio C Hamano9c9b03b2017-01-27 18:01:41 -0800633 eol = strchrnul(p, '\n');
Christian Couder11af2aa2010-07-22 15:18:30 +0200634 } else
635 eol = p;
636
637 *subject = p;
638
639 return eol - p;
640}
641
Charvi Mendiratta6e0e2882021-03-15 13:24:31 +0530642size_t commit_subject_length(const char *body)
643{
644 const char *p = body;
645 while (*p) {
646 const char *next = skip_blank_lines(p);
647 if (next != p)
648 break;
649 p = strchrnul(p, '\n');
650 if (*p)
651 p++;
652 }
653 return p - body;
654}
655
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700656struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700657{
Christopher Li812666c2005-04-26 12:00:58 -0700658 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700659 new_list->item = item;
660 new_list->next = *list_p;
661 *list_p = new_list;
Linus Torvaldsac5155e2005-05-30 18:44:02 -0700662 return new_list;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700663}
664
Derrick Stolee597b2c32020-12-08 17:04:13 -0500665int commit_list_contains(struct commit *item, struct commit_list *list)
666{
667 while (list) {
668 if (list->item == item)
669 return 1;
670 list = list->next;
671 }
672
673 return 0;
674}
675
Miklos Vajna65319472008-06-27 18:21:55 +0200676unsigned commit_list_count(const struct commit_list *l)
677{
678 unsigned c = 0;
679 for (; l; l = l->next )
680 c++;
681 return c;
682}
683
Thomas Rast53d00b32013-07-31 22:13:20 +0200684struct commit_list *copy_commit_list(struct commit_list *list)
685{
686 struct commit_list *head = NULL;
687 struct commit_list **pp = &head;
688 while (list) {
René Scharfecb979db2014-07-10 11:47:47 +0200689 pp = commit_list_append(list->item, pp);
Thomas Rast53d00b32013-07-31 22:13:20 +0200690 list = list->next;
691 }
692 return head;
693}
694
Elijah Newrenb0ca1202020-12-16 22:27:59 +0000695struct commit_list *reverse_commit_list(struct commit_list *list)
696{
697 struct commit_list *next = NULL, *current, *backup;
698 for (current = list; current; current = backup) {
699 backup = current->next;
700 current->next = next;
701 next = current;
702 }
703 return next;
704}
705
Daniel Barkalow175785e2005-04-18 11:39:48 -0700706void free_commit_list(struct commit_list *list)
707{
René Scharfee510ab82015-10-24 18:21:31 +0200708 while (list)
709 pop_commit(&list);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700710}
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700711
Thiago Farina47e44ed2010-11-26 23:58:14 -0200712struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700713{
714 struct commit_list **pp = list;
715 struct commit_list *p;
716 while ((p = *pp) != NULL) {
717 if (p->item->date < item->date) {
718 break;
719 }
720 pp = &p->next;
721 }
Linus Torvaldsf7554942005-07-06 09:31:17 -0700722 return commit_list_insert(item, pp);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700723}
724
René Scharfec0fb5772022-07-16 18:59:05 +0200725static int commit_list_compare_by_date(const struct commit_list *a,
726 const struct commit_list *b)
René Scharfe46905892012-04-01 00:10:39 +0200727{
René Scharfec0fb5772022-07-16 18:59:05 +0200728 timestamp_t a_date = a->item->date;
729 timestamp_t b_date = b->item->date;
René Scharfe46905892012-04-01 00:10:39 +0200730 if (a_date < b_date)
731 return 1;
732 if (a_date > b_date)
733 return -1;
734 return 0;
735}
736
René Scharfec0fb5772022-07-16 18:59:05 +0200737DEFINE_LIST_SORT(static, commit_list_sort, struct commit_list, next);
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700738
Thiago Farina47e44ed2010-11-26 23:58:14 -0200739void commit_list_sort_by_date(struct commit_list **list)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700740{
René Scharfec0fb5772022-07-16 18:59:05 +0200741 commit_list_sort(list, commit_list_compare_by_date);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700742}
743
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700744struct commit *pop_most_recent_commit(struct commit_list **list,
745 unsigned int mark)
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700746{
René Scharfee510ab82015-10-24 18:21:31 +0200747 struct commit *ret = pop_commit(list);
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700748 struct commit_list *parents = ret->parents;
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700749
750 while (parents) {
Linus Torvalds4056c092005-04-23 19:21:28 -0700751 struct commit *commit = parents->item;
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200752 if (!repo_parse_commit(the_repository, commit) && !(commit->object.flags & mark)) {
Daniel Barkalow58e28af2005-04-23 20:29:22 -0700753 commit->object.flags |= mark;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200754 commit_list_insert_by_date(commit, list);
Linus Torvalds4056c092005-04-23 19:21:28 -0700755 }
Daniel Barkalowdd97f852005-04-23 18:47:23 -0700756 parents = parents->next;
757 }
758 return ret;
759}
Linus Torvaldse3bc7a32005-06-01 08:34:23 -0700760
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700761static void clear_commit_marks_1(struct commit_list **plist,
762 struct commit *commit, unsigned int mark)
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800763{
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100764 while (commit) {
765 struct commit_list *parents;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800766
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100767 if (!(mark & commit->object.flags))
768 return;
Junio C Hamano58ecf5c2006-07-04 17:45:22 -0700769
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100770 commit->object.flags &= ~mark;
771
772 parents = commit->parents;
773 if (!parents)
774 return;
775
René Scharfe4cb39fc2022-12-13 07:27:10 +0100776 while ((parents = parents->next)) {
777 if (parents->item->object.flags & mark)
778 commit_list_insert(parents->item, plist);
779 }
Johannes Schindelin60fcc2e2007-10-10 23:14:35 +0100780
781 commit = commit->parents->item;
Junio C Hamanof8f9c732006-01-07 18:52:42 -0800782 }
783}
784
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800785void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700786{
787 struct commit_list *list = NULL;
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800788
789 while (nr--) {
René Scharfe07f7d552017-12-25 18:43:37 +0100790 clear_commit_marks_1(&list, *commit, mark);
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800791 commit++;
792 }
Nguyễn Thái Ngọc Duy941ba8d2012-01-14 19:19:53 +0700793 while (list)
794 clear_commit_marks_1(&list, pop_commit(&list), mark);
795}
796
Junio C Hamanoe895cb52013-03-05 11:42:20 -0800797void clear_commit_marks(struct commit *commit, unsigned int mark)
798{
799 clear_commit_marks_many(1, &commit, mark);
800}
801
jon@blackcubes.dyndns.orga3437b82005-06-06 15:39:40 +0000802struct commit *pop_commit(struct commit_list **stack)
803{
804 struct commit_list *top = *stack;
805 struct commit *item = top ? top->item : NULL;
806
807 if (top) {
808 *stack = top->next;
809 free(top);
810 }
811 return item;
812}
813
Jon Seymourab580ac2005-07-07 02:39:34 +1000814/*
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700815 * Topological sort support
816 */
Junio C Hamano66eb3752013-04-12 23:25:49 -0700817
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700818/* count number of children that have not been emitted */
819define_commit_slab(indegree_slab, int);
Jeff King96c4f4a2013-04-09 02:52:56 -0400820
Derrick Stolee18207032018-08-21 20:54:12 +0000821define_commit_slab(author_date_slab, timestamp_t);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700822
Derrick Stolee5284fc52018-11-01 13:46:21 +0000823void record_author_date(struct author_date_slab *author_date,
824 struct commit *commit)
Junio C Hamano81c6b382013-06-07 10:35:54 -0700825{
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200826 const char *buffer = repo_get_commit_buffer(the_repository, commit,
827 NULL);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700828 struct ident_split ident;
Jeff Kingea5517f2014-08-27 03:56:55 -0400829 const char *ident_line;
830 size_t ident_len;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700831 char *date_end;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200832 timestamp_t date;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700833
Jeff Kingea5517f2014-08-27 03:56:55 -0400834 ident_line = find_commit_header(buffer, "author", &ident_len);
835 if (!ident_line)
836 goto fail_exit; /* no author line */
837 if (split_ident_line(&ident, ident_line, ident_len) ||
838 !ident.date_begin || !ident.date_end)
839 goto fail_exit; /* malformed "author" line */
Junio C Hamano81c6b382013-06-07 10:35:54 -0700840
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +0200841 date = parse_timestamp(ident.date_begin, &date_end, 10);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700842 if (date_end != ident.date_end)
843 goto fail_exit; /* malformed date */
844 *(author_date_slab_at(author_date, commit)) = date;
845
846fail_exit:
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +0200847 repo_unuse_commit_buffer(the_repository, commit, buffer);
Junio C Hamano81c6b382013-06-07 10:35:54 -0700848}
849
Derrick Stolee5284fc52018-11-01 13:46:21 +0000850int compare_commits_by_author_date(const void *a_, const void *b_,
851 void *cb_data)
Junio C Hamano81c6b382013-06-07 10:35:54 -0700852{
853 const struct commit *a = a_, *b = b_;
854 struct author_date_slab *author_date = cb_data;
Johannes Schindelindddbad72017-04-26 21:29:31 +0200855 timestamp_t a_date = *(author_date_slab_at(author_date, a));
856 timestamp_t b_date = *(author_date_slab_at(author_date, b));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700857
858 /* newer commits with larger date first */
859 if (a_date < b_date)
860 return 1;
861 else if (a_date > b_date)
862 return -1;
863 return 0;
864}
865
Jeff King17587122023-02-24 01:39:27 -0500866int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_,
867 void *unused UNUSED)
Derrick Stolee3afc6792018-05-01 12:47:11 +0000868{
869 const struct commit *a = a_, *b = b_;
Abhishek Kumard7f92782021-01-16 18:11:13 +0000870 const timestamp_t generation_a = commit_graph_generation(a),
871 generation_b = commit_graph_generation(b);
Derrick Stolee3afc6792018-05-01 12:47:11 +0000872
873 /* newer commits first */
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530874 if (generation_a < generation_b)
Derrick Stolee3afc6792018-05-01 12:47:11 +0000875 return 1;
Abhishek Kumarc752ad02020-06-17 14:44:11 +0530876 else if (generation_a > generation_b)
Derrick Stolee3afc6792018-05-01 12:47:11 +0000877 return -1;
878
879 /* use date as a heuristic when generations are equal */
880 if (a->date < b->date)
881 return 1;
882 else if (a->date > b->date)
883 return -1;
884 return 0;
885}
886
Jeff King17587122023-02-24 01:39:27 -0500887int compare_commits_by_commit_date(const void *a_, const void *b_,
888 void *unused UNUSED)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700889{
890 const struct commit *a = a_, *b = b_;
891 /* newer commits with larger date first */
892 if (a->date < b->date)
893 return 1;
894 else if (a->date > b->date)
895 return -1;
896 return 0;
897}
898
Jon Seymourab580ac2005-07-07 02:39:34 +1000899/*
900 * Performs an in-place topological sort on the list supplied.
901 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700902void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
Jon Seymourab580ac2005-07-07 02:39:34 +1000903{
Linus Torvalds23c17d42007-11-02 13:32:58 -0700904 struct commit_list *next, *orig = *list;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700905 struct commit_list **pptr;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700906 struct indegree_slab indegree;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700907 struct prio_queue queue;
908 struct commit *commit;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700909 struct author_date_slab author_date;
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100910
Linus Torvalds23c17d42007-11-02 13:32:58 -0700911 if (!orig)
Eric Wong7d6fb372005-12-24 04:12:43 -0800912 return;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700913 *list = NULL;
914
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700915 init_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -0700916 memset(&queue, '\0', sizeof(queue));
Junio C Hamano81c6b382013-06-07 10:35:54 -0700917
Junio C Hamanoda24b102013-06-06 21:58:12 -0700918 switch (sort_order) {
919 default: /* REV_SORT_IN_GRAPH_ORDER */
920 queue.compare = NULL;
921 break;
922 case REV_SORT_BY_COMMIT_DATE:
923 queue.compare = compare_commits_by_commit_date;
924 break;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700925 case REV_SORT_BY_AUTHOR_DATE:
926 init_author_date_slab(&author_date);
927 queue.compare = compare_commits_by_author_date;
928 queue.cb_data = &author_date;
929 break;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700930 }
Jeff King96c4f4a2013-04-09 02:52:56 -0400931
Linus Torvalds23c17d42007-11-02 13:32:58 -0700932 /* Mark them and clear the indegree */
933 for (next = orig; next; next = next->next) {
934 struct commit *commit = next->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700935 *(indegree_slab_at(&indegree, commit)) = 1;
Junio C Hamano81c6b382013-06-07 10:35:54 -0700936 /* also record the author dates, if needed */
937 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
938 record_author_date(&author_date, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000939 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700940
Jon Seymourab580ac2005-07-07 02:39:34 +1000941 /* update the indegree */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700942 for (next = orig; next; next = next->next) {
Junio C Hamanoda24b102013-06-06 21:58:12 -0700943 struct commit_list *parents = next->item->parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000944 while (parents) {
Linus Torvalds23c17d42007-11-02 13:32:58 -0700945 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700946 int *pi = indegree_slab_at(&indegree, parent);
Fredrik Kuivinen6b6dcfc2006-03-10 10:21:37 +0100947
Jeff King96c4f4a2013-04-09 02:52:56 -0400948 if (*pi)
949 (*pi)++;
Linus Torvalds23c17d42007-11-02 13:32:58 -0700950 parents = parents->next;
Jon Seymourab580ac2005-07-07 02:39:34 +1000951 }
Jon Seymourab580ac2005-07-07 02:39:34 +1000952 }
Linus Torvalds23c17d42007-11-02 13:32:58 -0700953
Junio C Hamanoa6080a02007-06-07 00:04:01 -0700954 /*
Pierre Habouzit674d1722007-09-10 12:35:06 +0200955 * find the tips
956 *
957 * tips are nodes not reachable from any other node in the list
958 *
959 * the tips serve as a starting set for the work queue.
960 */
Linus Torvalds23c17d42007-11-02 13:32:58 -0700961 for (next = orig; next; next = next->next) {
962 struct commit *commit = next->item;
Jon Seymourab580ac2005-07-07 02:39:34 +1000963
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700964 if (*(indegree_slab_at(&indegree, commit)) == 1)
Junio C Hamanoda24b102013-06-06 21:58:12 -0700965 prio_queue_put(&queue, commit);
Jon Seymourab580ac2005-07-07 02:39:34 +1000966 }
Junio C Hamano4c8725f2006-02-15 22:05:33 -0800967
Junio C Hamanoda24b102013-06-06 21:58:12 -0700968 /*
969 * This is unfortunate; the initial tips need to be shown
970 * in the order given from the revision traversal machinery.
971 */
972 if (sort_order == REV_SORT_IN_GRAPH_ORDER)
973 prio_queue_reverse(&queue);
974
975 /* We no longer need the commit list */
976 free_commit_list(orig);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700977
978 pptr = list;
979 *list = NULL;
Junio C Hamanoda24b102013-06-06 21:58:12 -0700980 while ((commit = prio_queue_get(&queue)) != NULL) {
981 struct commit_list *parents;
Jon Seymourab580ac2005-07-07 02:39:34 +1000982
Linus Torvalds23c17d42007-11-02 13:32:58 -0700983 for (parents = commit->parents; parents ; parents = parents->next) {
Junio C Hamanocd2b8ae2011-08-25 14:46:52 -0700984 struct commit *parent = parents->item;
Junio C Hamanoa84b7942013-04-13 11:56:41 -0700985 int *pi = indegree_slab_at(&indegree, parent);
Linus Torvalds23c17d42007-11-02 13:32:58 -0700986
Jeff King96c4f4a2013-04-09 02:52:56 -0400987 if (!*pi)
Linus Torvalds23c17d42007-11-02 13:32:58 -0700988 continue;
989
990 /*
991 * parents are only enqueued for emission
992 * when all their children have been emitted thereby
993 * guaranteeing topological order.
994 */
Junio C Hamanoda24b102013-06-06 21:58:12 -0700995 if (--(*pi) == 1)
996 prio_queue_put(&queue, parent);
Jon Seymourab580ac2005-07-07 02:39:34 +1000997 }
998 /*
Junio C Hamanoda24b102013-06-06 21:58:12 -0700999 * all children of commit have already been
1000 * emitted. we can emit it now.
Pierre Habouzit674d1722007-09-10 12:35:06 +02001001 */
Junio C Hamanoa84b7942013-04-13 11:56:41 -07001002 *(indegree_slab_at(&indegree, commit)) = 0;
Junio C Hamanoda24b102013-06-06 21:58:12 -07001003
1004 pptr = &commit_list_insert(commit, pptr)->next;
Jon Seymourab580ac2005-07-07 02:39:34 +10001005 }
Jeff King96c4f4a2013-04-09 02:52:56 -04001006
Junio C Hamanoa84b7942013-04-13 11:56:41 -07001007 clear_indegree_slab(&indegree);
Junio C Hamanoda24b102013-06-06 21:58:12 -07001008 clear_prio_queue(&queue);
Junio C Hamano81c6b382013-06-07 10:35:54 -07001009 if (sort_order == REV_SORT_BY_AUTHOR_DATE)
1010 clear_author_date_slab(&author_date);
Jon Seymourab580ac2005-07-07 02:39:34 +10001011}
Johannes Schindelin7c6f8aa2006-06-29 15:17:32 +02001012
Pratik Karki103148a2018-09-04 15:00:08 -07001013struct rev_collect {
1014 struct commit **commit;
1015 int nr;
1016 int alloc;
1017 unsigned int initial : 1;
1018};
1019
1020static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
1021{
1022 struct commit *commit;
1023
1024 if (is_null_oid(oid))
1025 return;
1026
1027 commit = lookup_commit(the_repository, oid);
1028 if (!commit ||
1029 (commit->object.flags & TMP_MARK) ||
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001030 repo_parse_commit(the_repository, commit))
Pratik Karki103148a2018-09-04 15:00:08 -07001031 return;
1032
1033 ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc);
1034 revs->commit[revs->nr++] = commit;
1035 commit->object.flags |= TMP_MARK;
1036}
1037
1038static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +02001039 const char *ident UNUSED,
1040 timestamp_t timestamp UNUSED, int tz UNUSED,
1041 const char *message UNUSED, void *cbdata)
Pratik Karki103148a2018-09-04 15:00:08 -07001042{
1043 struct rev_collect *revs = cbdata;
1044
1045 if (revs->initial) {
1046 revs->initial = 0;
1047 add_one_commit(ooid, revs);
1048 }
1049 add_one_commit(noid, revs);
1050 return 0;
1051}
1052
1053struct commit *get_fork_point(const char *refname, struct commit *commit)
1054{
1055 struct object_id oid;
1056 struct rev_collect revs;
1057 struct commit_list *bases;
1058 int i;
1059 struct commit *ret = NULL;
Junio C Hamanof08132f2019-12-09 10:51:47 -08001060 char *full_refname;
1061
Ævar Arnfjörð Bjarmason12cb1c12023-03-28 15:58:54 +02001062 switch (repo_dwim_ref(the_repository, refname, strlen(refname), &oid,
1063 &full_refname, 0)) {
Junio C Hamanof08132f2019-12-09 10:51:47 -08001064 case 0:
1065 die("No such ref: '%s'", refname);
1066 case 1:
1067 break; /* good */
1068 default:
1069 die("Ambiguous refname: '%s'", refname);
1070 }
Pratik Karki103148a2018-09-04 15:00:08 -07001071
1072 memset(&revs, 0, sizeof(revs));
1073 revs.initial = 1;
Junio C Hamanof08132f2019-12-09 10:51:47 -08001074 for_each_reflog_ent(full_refname, collect_one_reflog_ent, &revs);
Pratik Karki103148a2018-09-04 15:00:08 -07001075
Junio C Hamanof08132f2019-12-09 10:51:47 -08001076 if (!revs.nr)
Pratik Karki103148a2018-09-04 15:00:08 -07001077 add_one_commit(&oid, &revs);
1078
1079 for (i = 0; i < revs.nr; i++)
1080 revs.commit[i]->object.flags &= ~TMP_MARK;
1081
Ævar Arnfjörð Bjarmasoncb338c22023-03-28 15:58:47 +02001082 bases = repo_get_merge_bases_many(the_repository, commit, revs.nr,
1083 revs.commit);
Pratik Karki103148a2018-09-04 15:00:08 -07001084
1085 /*
1086 * There should be one and only one merge base, when we found
1087 * a common ancestor among reflog entries.
1088 */
1089 if (!bases || bases->next)
1090 goto cleanup_return;
1091
1092 /* And the found one must be one of the reflog entries */
1093 for (i = 0; i < revs.nr; i++)
1094 if (&bases->item->object == &revs.commit[i]->object)
1095 break; /* found */
1096 if (revs.nr <= i)
1097 goto cleanup_return;
1098
1099 ret = bases->item;
1100
1101cleanup_return:
Ævar Arnfjörð Bjarmason0c10ed12023-02-06 20:08:13 +01001102 free(revs.commit);
Pratik Karki103148a2018-09-04 15:00:08 -07001103 free_commit_list(bases);
Junio C Hamanof08132f2019-12-09 10:51:47 -08001104 free(full_refname);
Pratik Karki103148a2018-09-04 15:00:08 -07001105 return ret;
1106}
1107
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001108/*
1109 * Indexed by hash algorithm identifier.
1110 */
1111static const char *gpg_sig_headers[] = {
1112 NULL,
1113 "gpgsig",
1114 "gpgsig-sha256",
1115};
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001116
brian m. carlson937032e2021-02-11 02:08:04 +00001117int sign_with_header(struct strbuf *buf, const char *keyid)
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001118{
1119 struct strbuf sig = STRBUF_INIT;
1120 int inspos, copypos;
Johannes Schindelin3324dd82016-06-29 16:14:54 +02001121 const char *eoh;
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001122 const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(the_hash_algo)];
1123 int gpg_sig_header_len = strlen(gpg_sig_header);
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001124
1125 /* find the end of the header */
Johannes Schindelin3324dd82016-06-29 16:14:54 +02001126 eoh = strstr(buf->buf, "\n\n");
1127 if (!eoh)
1128 inspos = buf->len;
1129 else
1130 inspos = eoh - buf->buf + 1;
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001131
1132 if (!keyid || !*keyid)
1133 keyid = get_signing_key();
1134 if (sign_buffer(buf, &sig, keyid)) {
1135 strbuf_release(&sig);
1136 return -1;
1137 }
1138
1139 for (copypos = 0; sig.buf[copypos]; ) {
1140 const char *bol = sig.buf + copypos;
1141 const char *eol = strchrnul(bol, '\n');
1142 int len = (eol - bol) + !!*eol;
1143
1144 if (!copypos) {
1145 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1146 inspos += gpg_sig_header_len;
1147 }
René Scharfea91cc7f2020-02-09 14:44:23 +01001148 strbuf_insertstr(buf, inspos++, " ");
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001149 strbuf_insert(buf, inspos, bol, len);
1150 inspos += len;
1151 copypos += len;
1152 }
1153 strbuf_release(&sig);
1154 return 0;
1155}
1156
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001157
brian m. carlson937032e2021-02-11 02:08:04 +00001158
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001159int parse_signed_commit(const struct commit *commit,
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001160 struct strbuf *payload, struct strbuf *signature,
1161 const struct git_hash_algo *algop)
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001162{
Jeff King218aa3a2014-06-13 02:32:11 -04001163 unsigned long size;
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001164 const char *buffer = repo_get_commit_buffer(the_repository, commit,
1165 &size);
brian m. carlson937032e2021-02-11 02:08:04 +00001166 int ret = parse_buffer_signed_by_header(buffer, size, payload, signature, algop);
1167
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001168 repo_unuse_commit_buffer(the_repository, commit, buffer);
brian m. carlson937032e2021-02-11 02:08:04 +00001169 return ret;
1170}
1171
1172int parse_buffer_signed_by_header(const char *buffer,
1173 unsigned long size,
1174 struct strbuf *payload,
1175 struct strbuf *signature,
1176 const struct git_hash_algo *algop)
1177{
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001178 int in_signature = 0, saw_signature = 0, other_signature = 0;
1179 const char *line, *tail, *p;
1180 const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(algop)];
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001181
1182 line = buffer;
1183 tail = buffer + size;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001184 while (line < tail) {
1185 const char *sig = NULL;
Jeff King218aa3a2014-06-13 02:32:11 -04001186 const char *next = memchr(line, '\n', tail - line);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001187
1188 next = next ? next + 1 : tail;
1189 if (in_signature && line[0] == ' ')
1190 sig = line + 1;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001191 else if (skip_prefix(line, gpg_sig_header, &p) &&
1192 *p == ' ') {
1193 sig = line + strlen(gpg_sig_header) + 1;
1194 other_signature = 0;
1195 }
1196 else if (starts_with(line, "gpgsig"))
1197 other_signature = 1;
1198 else if (other_signature && line[0] != ' ')
1199 other_signature = 0;
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001200 if (sig) {
1201 strbuf_add(signature, sig, next - sig);
1202 saw_signature = 1;
1203 in_signature = 1;
1204 } else {
1205 if (*line == '\n')
1206 /* dump the whole remainder of the buffer */
1207 next = tail;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001208 if (!other_signature)
1209 strbuf_add(payload, line, next - line);
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001210 in_signature = 0;
1211 }
1212 line = next;
1213 }
Junio C Hamano0c37f1f2011-10-18 15:53:23 -07001214 return saw_signature;
1215}
1216
Christian Couder0b05ab62014-07-19 17:01:12 +02001217int remove_signature(struct strbuf *buf)
1218{
1219 const char *line = buf->buf;
1220 const char *tail = buf->buf + buf->len;
1221 int in_signature = 0;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001222 struct sigbuf {
1223 const char *start;
1224 const char *end;
1225 } sigs[2], *sigp = &sigs[0];
1226 int i;
1227 const char *orig_buf = buf->buf;
1228
1229 memset(sigs, 0, sizeof(sigs));
Christian Couder0b05ab62014-07-19 17:01:12 +02001230
1231 while (line < tail) {
1232 const char *next = memchr(line, '\n', tail - line);
1233 next = next ? next + 1 : tail;
1234
1235 if (in_signature && line[0] == ' ')
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001236 sigp->end = next;
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001237 else if (starts_with(line, "gpgsig")) {
1238 int i;
1239 for (i = 1; i < GIT_HASH_NALGOS; i++) {
1240 const char *p;
1241 if (skip_prefix(line, gpg_sig_headers[i], &p) &&
1242 *p == ' ') {
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001243 sigp->start = line;
1244 sigp->end = next;
brian m. carlson42d4e1d2020-02-22 20:17:42 +00001245 in_signature = 1;
1246 }
1247 }
Christian Couder0b05ab62014-07-19 17:01:12 +02001248 } else {
1249 if (*line == '\n')
1250 /* dump the whole remainder of the buffer */
1251 next = tail;
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001252 if (in_signature && sigp - sigs != ARRAY_SIZE(sigs))
1253 sigp++;
Christian Couder0b05ab62014-07-19 17:01:12 +02001254 in_signature = 0;
1255 }
1256 line = next;
1257 }
1258
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001259 for (i = ARRAY_SIZE(sigs) - 1; i >= 0; i--)
1260 if (sigs[i].start)
1261 strbuf_remove(buf, sigs[i].start - orig_buf, sigs[i].end - sigs[i].start);
Christian Couder0b05ab62014-07-19 17:01:12 +02001262
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001263 return sigs[0].start != NULL;
Christian Couder0b05ab62014-07-19 17:01:12 +02001264}
1265
Junio C Hamano5231c632011-11-07 16:21:32 -08001266static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail)
1267{
1268 struct merge_remote_desc *desc;
1269 struct commit_extra_header *mergetag;
1270 char *buf;
brian m. carlson482c1192021-02-11 02:08:03 +00001271 unsigned long size;
Junio C Hamano5231c632011-11-07 16:21:32 -08001272 enum object_type type;
brian m. carlson482c1192021-02-11 02:08:03 +00001273 struct strbuf payload = STRBUF_INIT;
1274 struct strbuf signature = STRBUF_INIT;
Junio C Hamano5231c632011-11-07 16:21:32 -08001275
1276 desc = merge_remote_util(parent);
1277 if (!desc || !desc->obj)
1278 return;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02001279 buf = repo_read_object_file(the_repository, &desc->obj->oid, &type,
1280 &size);
Junio C Hamano5231c632011-11-07 16:21:32 -08001281 if (!buf || type != OBJ_TAG)
1282 goto free_return;
brian m. carlson482c1192021-02-11 02:08:03 +00001283 if (!parse_signature(buf, size, &payload, &signature))
Junio C Hamano5231c632011-11-07 16:21:32 -08001284 goto free_return;
1285 /*
1286 * We could verify this signature and either omit the tag when
1287 * it does not validate, but the integrator may not have the
Felipe Contreras0e20b222021-06-15 14:11:10 +00001288 * public key of the signer of the tag being merged, while a
Junio C Hamano5231c632011-11-07 16:21:32 -08001289 * later auditor may have it while auditing, so let's not run
1290 * verify-signed-buffer here for now...
1291 *
1292 * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1293 * warn("warning: signed tag unverified.");
1294 */
René Scharfeca56dad2021-03-13 17:17:22 +01001295 CALLOC_ARRAY(mergetag, 1);
Junio C Hamano5231c632011-11-07 16:21:32 -08001296 mergetag->key = xstrdup("mergetag");
1297 mergetag->value = buf;
1298 mergetag->len = size;
1299
1300 **tail = mergetag;
1301 *tail = &mergetag->next;
brian m. carlson482c1192021-02-11 02:08:03 +00001302 strbuf_release(&payload);
1303 strbuf_release(&signature);
Junio C Hamano5231c632011-11-07 16:21:32 -08001304 return;
1305
1306free_return:
1307 free(buf);
1308}
1309
brian m. carlson434060e2015-06-21 23:14:40 +00001310int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001311{
1312 struct strbuf payload = STRBUF_INIT;
1313 struct strbuf signature = STRBUF_INIT;
brian m. carlson434060e2015-06-21 23:14:40 +00001314 int ret = 1;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001315
1316 sigc->result = 'N';
1317
brian m. carlson1fb5cf02021-01-18 23:49:11 +00001318 if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001319 goto out;
Fabian Stelzer02769432021-12-09 09:52:43 +01001320
Fabian Stelzer6393c952021-12-09 09:52:45 +01001321 sigc->payload_type = SIGNATURE_PAYLOAD_COMMIT;
Fabian Stelzer02769432021-12-09 09:52:43 +01001322 sigc->payload = strbuf_detach(&payload, &sigc->payload_len);
1323 ret = check_signature(sigc, signature.buf, signature.len);
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001324
1325 out:
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001326 strbuf_release(&payload);
1327 strbuf_release(&signature);
brian m. carlson434060e2015-06-21 23:14:40 +00001328
1329 return ret;
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001330}
1331
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001332void verify_merge_signature(struct commit *commit, int verbosity,
1333 int check_trust)
Jeff Kingedc4d472018-11-06 02:50:17 -05001334{
1335 char hex[GIT_MAX_HEXSZ + 1];
1336 struct signature_check signature_check;
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001337 int ret;
Jeff Kingedc4d472018-11-06 02:50:17 -05001338 memset(&signature_check, 0, sizeof(signature_check));
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001339
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001340 ret = check_commit_signature(commit, &signature_check);
Jeff Kingedc4d472018-11-06 02:50:17 -05001341
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001342 repo_find_unique_abbrev_r(the_repository, hex, &commit->object.oid,
1343 DEFAULT_ABBREV);
Jeff Kingedc4d472018-11-06 02:50:17 -05001344 switch (signature_check.result) {
1345 case 'G':
Hans Jerry Illikainen54887b42019-12-27 13:55:57 +00001346 if (ret || (check_trust && signature_check.trust_level < TRUST_MARGINAL))
1347 die(_("Commit %s has an untrusted GPG signature, "
1348 "allegedly by %s."), hex, signature_check.signer);
Jeff Kingedc4d472018-11-06 02:50:17 -05001349 break;
Jeff Kingedc4d472018-11-06 02:50:17 -05001350 case 'B':
1351 die(_("Commit %s has a bad GPG signature "
1352 "allegedly by %s."), hex, signature_check.signer);
1353 default: /* 'N' */
1354 die(_("Commit %s does not have a GPG signature."), hex);
1355 }
1356 if (verbosity >= 0 && signature_check.result == 'G')
1357 printf(_("Commit %s has a good GPG signature by %s\n"),
1358 hex, signature_check.signer);
1359
1360 signature_check_clear(&signature_check);
1361}
Sebastian Götteffb6d7d2013-03-31 18:00:14 +02001362
Junio C Hamano5231c632011-11-07 16:21:32 -08001363void append_merge_tag_headers(struct commit_list *parents,
1364 struct commit_extra_header ***tail)
1365{
1366 while (parents) {
1367 struct commit *parent = parents->item;
1368 handle_signed_tag(parent, tail);
1369 parents = parents->next;
1370 }
1371}
1372
1373static void add_extra_header(struct strbuf *buffer,
1374 struct commit_extra_header *extra)
1375{
1376 strbuf_addstr(buffer, extra->key);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001377 if (extra->len)
1378 strbuf_add_lines(buffer, " ", extra->value, extra->len);
1379 else
1380 strbuf_addch(buffer, '\n');
1381}
1382
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001383struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1384 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001385{
1386 struct commit_extra_header *extra = NULL;
1387 unsigned long size;
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001388 const char *buffer = repo_get_commit_buffer(the_repository, commit,
1389 &size);
Jeff King218aa3a2014-06-13 02:32:11 -04001390 extra = read_commit_extra_header_lines(buffer, size, exclude);
Ævar Arnfjörð Bjarmasonecb50912023-03-28 15:58:48 +02001391 repo_unuse_commit_buffer(the_repository, commit, buffer);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001392 return extra;
1393}
1394
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001395int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
Christian Couder063da622014-07-07 08:35:37 +02001396{
1397 struct commit_extra_header *extra, *to_free;
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001398 int res = 0;
Christian Couder063da622014-07-07 08:35:37 +02001399
1400 to_free = read_commit_extra_headers(commit, NULL);
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001401 for (extra = to_free; !res && extra; extra = extra->next) {
Christian Couder063da622014-07-07 08:35:37 +02001402 if (strcmp(extra->key, "mergetag"))
1403 continue; /* not a merge tag */
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001404 res = fn(commit, extra, data);
Christian Couder063da622014-07-07 08:35:37 +02001405 }
1406 free_commit_extra_headers(to_free);
Johannes Schindelinfef461e2018-04-25 11:54:04 +02001407 return res;
Christian Couder063da622014-07-07 08:35:37 +02001408}
1409
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001410static inline int standard_header_field(const char *field, size_t len)
1411{
René Scharfeb0725042017-02-25 20:27:40 +01001412 return ((len == 4 && !memcmp(field, "tree", 4)) ||
1413 (len == 6 && !memcmp(field, "parent", 6)) ||
1414 (len == 6 && !memcmp(field, "author", 6)) ||
1415 (len == 9 && !memcmp(field, "committer", 9)) ||
1416 (len == 8 && !memcmp(field, "encoding", 8)));
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001417}
1418
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001419static int excluded_header_field(const char *field, size_t len, const char **exclude)
1420{
1421 if (!exclude)
1422 return 0;
1423
1424 while (*exclude) {
1425 size_t xlen = strlen(*exclude);
René Scharfeb0725042017-02-25 20:27:40 +01001426 if (len == xlen && !memcmp(field, *exclude, xlen))
Junio C Hamanoc871a1d2012-01-05 10:54:14 -08001427 return 1;
1428 exclude++;
1429 }
1430 return 0;
1431}
1432
Junio C Hamano82a75292012-09-15 13:58:15 -07001433static struct commit_extra_header *read_commit_extra_header_lines(
1434 const char *buffer, size_t size,
1435 const char **exclude)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001436{
1437 struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1438 const char *line, *next, *eof, *eob;
1439 struct strbuf buf = STRBUF_INIT;
1440
1441 for (line = buffer, eob = line + size;
1442 line < eob && *line != '\n';
1443 line = next) {
1444 next = memchr(line, '\n', eob - line);
1445 next = next ? next + 1 : eob;
1446 if (*line == ' ') {
1447 /* continuation */
1448 if (it)
1449 strbuf_add(&buf, line + 1, next - (line + 1));
1450 continue;
1451 }
1452 if (it)
1453 it->value = strbuf_detach(&buf, &it->len);
1454 strbuf_reset(&buf);
1455 it = NULL;
1456
René Scharfe50a01cc2017-02-25 20:21:52 +01001457 eof = memchr(line, ' ', next - line);
1458 if (!eof)
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001459 eof = next;
René Scharfeb0725042017-02-25 20:27:40 +01001460 else if (standard_header_field(line, eof - line) ||
1461 excluded_header_field(line, eof - line, exclude))
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001462 continue;
1463
René Scharfeca56dad2021-03-13 17:17:22 +01001464 CALLOC_ARRAY(it, 1);
Junio C Hamanoed7a42a2011-11-08 15:38:07 -08001465 it->key = xmemdupz(line, eof-line);
1466 *tail = it;
1467 tail = &it->next;
1468 if (eof + 1 < next)
1469 strbuf_add(&buf, eof + 1, next - (eof + 1));
1470 }
1471 if (it)
1472 it->value = strbuf_detach(&buf, &it->len);
1473 return extra;
Junio C Hamano5231c632011-11-07 16:21:32 -08001474}
1475
1476void free_commit_extra_headers(struct commit_extra_header *extra)
1477{
1478 while (extra) {
1479 struct commit_extra_header *next = extra->next;
1480 free(extra->key);
1481 free(extra->value);
1482 free(extra);
1483 extra = next;
1484 }
1485}
1486
Patryk Obara5078f342018-01-28 01:13:16 +01001487int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1488 struct commit_list *parents, struct object_id *ret,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001489 const char *author, const char *sign_commit)
Junio C Hamano5231c632011-11-07 16:21:32 -08001490{
1491 struct commit_extra_header *extra = NULL, **tail = &extra;
1492 int result;
1493
1494 append_merge_tag_headers(parents, &tail);
Phillip Woode8cbe212020-08-17 18:40:01 +01001495 result = commit_tree_extended(msg, msg_len, tree, parents, ret, author,
1496 NULL, sign_commit, extra);
Junio C Hamano5231c632011-11-07 16:21:32 -08001497 free_commit_extra_headers(extra);
1498 return result;
1499}
1500
Linus Torvalds08a94a12012-06-28 11:24:14 -07001501static int find_invalid_utf8(const char *buf, int len)
1502{
1503 int offset = 0;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001504 static const unsigned int max_codepoint[] = {
1505 0x7f, 0x7ff, 0xffff, 0x10ffff
1506 };
Linus Torvalds08a94a12012-06-28 11:24:14 -07001507
1508 while (len) {
1509 unsigned char c = *buf++;
1510 int bytes, bad_offset;
brian m. carlson28110d42013-07-04 17:19:43 +00001511 unsigned int codepoint;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001512 unsigned int min_val, max_val;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001513
1514 len--;
1515 offset++;
1516
1517 /* Simple US-ASCII? No worries. */
1518 if (c < 0x80)
1519 continue;
1520
1521 bad_offset = offset-1;
1522
1523 /*
1524 * Count how many more high bits set: that's how
1525 * many more bytes this sequence should have.
1526 */
1527 bytes = 0;
1528 while (c & 0x40) {
1529 c <<= 1;
1530 bytes++;
1531 }
1532
brian m. carlson28110d42013-07-04 17:19:43 +00001533 /*
1534 * Must be between 1 and 3 more bytes. Longer sequences result in
1535 * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1536 */
1537 if (bytes < 1 || 3 < bytes)
Linus Torvalds08a94a12012-06-28 11:24:14 -07001538 return bad_offset;
1539
1540 /* Do we *have* that many bytes? */
1541 if (len < bytes)
1542 return bad_offset;
1543
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001544 /*
1545 * Place the encoded bits at the bottom of the value and compute the
1546 * valid range.
1547 */
brian m. carlson28110d42013-07-04 17:19:43 +00001548 codepoint = (c & 0x7f) >> bytes;
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001549 min_val = max_codepoint[bytes-1] + 1;
1550 max_val = max_codepoint[bytes];
brian m. carlson28110d42013-07-04 17:19:43 +00001551
Linus Torvalds08a94a12012-06-28 11:24:14 -07001552 offset += bytes;
1553 len -= bytes;
1554
1555 /* And verify that they are good continuation bytes */
1556 do {
brian m. carlson28110d42013-07-04 17:19:43 +00001557 codepoint <<= 6;
1558 codepoint |= *buf & 0x3f;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001559 if ((*buf++ & 0xc0) != 0x80)
1560 return bad_offset;
1561 } while (--bytes);
1562
brian m. carlsone82bd6c2013-07-04 17:20:34 +00001563 /* Reject codepoints that are out of range for the sequence length. */
1564 if (codepoint < min_val || codepoint > max_val)
brian m. carlson28110d42013-07-04 17:19:43 +00001565 return bad_offset;
1566 /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1567 if ((codepoint & 0x1ff800) == 0xd800)
1568 return bad_offset;
Peter Krefting81050ac2013-07-09 12:16:33 +01001569 /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
Junio C Hamanodc773a62013-08-05 09:52:28 -07001570 if ((codepoint & 0xfffe) == 0xfffe)
Peter Krefting81050ac2013-07-09 12:16:33 +01001571 return bad_offset;
1572 /* So are anything in the range U+FDD0..U+FDEF. */
1573 if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
brian m. carlson28110d42013-07-04 17:19:43 +00001574 return bad_offset;
Linus Torvalds08a94a12012-06-28 11:24:14 -07001575 }
1576 return -1;
1577}
1578
1579/*
1580 * This verifies that the buffer is in proper utf8 format.
1581 *
1582 * If it isn't, it assumes any non-utf8 characters are Latin1,
1583 * and does the conversion.
Linus Torvalds08a94a12012-06-28 11:24:14 -07001584 */
1585static int verify_utf8(struct strbuf *buf)
1586{
1587 int ok = 1;
1588 long pos = 0;
1589
1590 for (;;) {
1591 int bad;
1592 unsigned char c;
1593 unsigned char replace[2];
1594
1595 bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1596 if (bad < 0)
1597 return ok;
1598 pos += bad;
1599 ok = 0;
1600 c = buf->buf[pos];
1601 strbuf_remove(buf, pos, 1);
1602
1603 /* We know 'c' must be in the range 128-255 */
1604 replace[0] = 0xc0 + (c >> 6);
1605 replace[1] = 0x80 + (c & 0x3f);
1606 strbuf_insert(buf, pos, replace, 2);
1607 pos += 2;
1608 }
1609}
1610
Jeff King40d52ff2010-04-01 20:05:23 -04001611static const char commit_utf8_warn[] =
Vasco Almeida4fa4b312016-09-19 13:08:16 +00001612N_("Warning: commit message did not conform to UTF-8.\n"
1613 "You may want to amend it after fixing the message, or set the config\n"
Jiang Xinb4eda052022-06-17 18:03:09 +08001614 "variable i18n.commitEncoding to the encoding your project uses.\n");
Jeff King40d52ff2010-04-01 20:05:23 -04001615
Jeff King3ffefb52014-06-10 17:36:52 -04001616int commit_tree_extended(const char *msg, size_t msg_len,
Patryk Obara5078f342018-01-28 01:13:16 +01001617 const struct object_id *tree,
1618 struct commit_list *parents, struct object_id *ret,
Phillip Woode8cbe212020-08-17 18:40:01 +01001619 const char *author, const char *committer,
1620 const char *sign_commit,
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001621 struct commit_extra_header *extra)
Jeff King40d52ff2010-04-01 20:05:23 -04001622{
1623 int result;
1624 int encoding_is_utf8;
1625 struct strbuf buffer;
1626
brian m. carlsone816caa2018-03-12 02:27:42 +00001627 assert_oid_type(tree, OBJ_TREE);
Jeff King40d52ff2010-04-01 20:05:23 -04001628
Jeff King3ffefb52014-06-10 17:36:52 -04001629 if (memchr(msg, '\0', msg_len))
Nguyễn Thái Ngọc Duy37576c12011-12-15 20:47:23 +07001630 return error("a NUL byte in commit log message not allowed.");
1631
Jeff King40d52ff2010-04-01 20:05:23 -04001632 /* Not having i18n.commitencoding is the same as having utf-8 */
1633 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1634
1635 strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
Patryk Obara5078f342018-01-28 01:13:16 +01001636 strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree));
Jeff King40d52ff2010-04-01 20:05:23 -04001637
1638 /*
1639 * NOTE! This ordering means that the same exact tree merged with a
1640 * different order of parents will be a _different_ changeset even
1641 * if everything else stays the same.
1642 */
1643 while (parents) {
René Scharfee510ab82015-10-24 18:21:31 +02001644 struct commit *parent = pop_commit(&parents);
Jeff King40d52ff2010-04-01 20:05:23 -04001645 strbuf_addf(&buffer, "parent %s\n",
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001646 oid_to_hex(&parent->object.oid));
Jeff King40d52ff2010-04-01 20:05:23 -04001647 }
1648
1649 /* Person/date information */
1650 if (!author)
Jeff Kingf9bc5732012-05-24 19:28:40 -04001651 author = git_author_info(IDENT_STRICT);
Jeff King40d52ff2010-04-01 20:05:23 -04001652 strbuf_addf(&buffer, "author %s\n", author);
Phillip Woode8cbe212020-08-17 18:40:01 +01001653 if (!committer)
1654 committer = git_committer_info(IDENT_STRICT);
1655 strbuf_addf(&buffer, "committer %s\n", committer);
Jeff King40d52ff2010-04-01 20:05:23 -04001656 if (!encoding_is_utf8)
1657 strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
Junio C Hamano5231c632011-11-07 16:21:32 -08001658
1659 while (extra) {
1660 add_extra_header(&buffer, extra);
1661 extra = extra->next;
1662 }
Jeff King40d52ff2010-04-01 20:05:23 -04001663 strbuf_addch(&buffer, '\n');
1664
1665 /* And add the comment */
Jeff King3ffefb52014-06-10 17:36:52 -04001666 strbuf_add(&buffer, msg, msg_len);
Jeff King40d52ff2010-04-01 20:05:23 -04001667
1668 /* And check the encoding */
Linus Torvalds08a94a12012-06-28 11:24:14 -07001669 if (encoding_is_utf8 && !verify_utf8(&buffer))
Vasco Almeida4fa4b312016-09-19 13:08:16 +00001670 fprintf(stderr, _(commit_utf8_warn));
Jeff King40d52ff2010-04-01 20:05:23 -04001671
brian m. carlson937032e2021-02-11 02:08:04 +00001672 if (sign_commit && sign_with_header(&buffer, sign_commit)) {
Rene Scharfee5051462017-08-30 19:49:38 +02001673 result = -1;
1674 goto out;
1675 }
Junio C Hamanoba3c69a2011-10-05 17:23:20 -07001676
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01001677 result = write_object_file(buffer.buf, buffer.len, OBJ_COMMIT, ret);
Rene Scharfee5051462017-08-30 19:49:38 +02001678out:
Jeff King40d52ff2010-04-01 20:05:23 -04001679 strbuf_release(&buffer);
1680 return result;
1681}
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001682
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001683define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1684static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1685
1686struct merge_remote_desc *merge_remote_util(struct commit *commit)
1687{
1688 return *merge_desc_slab_at(&merge_desc_slab, commit);
1689}
1690
René Scharfebeb518c2016-08-13 14:11:27 +02001691void set_merge_remote_desc(struct commit *commit,
1692 const char *name, struct object *obj)
1693{
1694 struct merge_remote_desc *desc;
René Scharfe5447a762016-08-13 14:21:27 +02001695 FLEX_ALLOC_STR(desc, name, name);
René Scharfebeb518c2016-08-13 14:11:27 +02001696 desc->obj = obj;
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001697 *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
René Scharfebeb518c2016-08-13 14:11:27 +02001698}
1699
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001700struct commit *get_merge_parent(const char *name)
1701{
1702 struct object *obj;
1703 struct commit *commit;
brian m. carlson7683e2e2015-03-13 23:39:34 +00001704 struct object_id oid;
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001705 if (repo_get_oid(the_repository, name, &oid))
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001706 return NULL;
Stefan Beller109cd762018-06-28 18:21:51 -07001707 obj = parse_object(the_repository, &oid);
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001708 commit = (struct commit *)repo_peel_to_type(the_repository, name, 0,
1709 obj, OBJ_COMMIT);
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +02001710 if (commit && !merge_remote_util(commit))
René Scharfebeb518c2016-08-13 14:11:27 +02001711 set_merge_remote_desc(commit, name, obj);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -08001712 return commit;
1713}
René Scharfe89b5f1d2012-04-25 22:35:27 +02001714
1715/*
1716 * Append a commit to the end of the commit_list.
1717 *
1718 * next starts by pointing to the variable that holds the head of an
1719 * empty commit_list, and is updated to point to the "next" field of
1720 * the last item on the list as new commits are appended.
1721 *
1722 * Usage example:
1723 *
1724 * struct commit_list *list;
1725 * struct commit_list **next = &list;
1726 *
1727 * next = commit_list_append(c1, next);
1728 * next = commit_list_append(c2, next);
1729 * assert(commit_list_count(list) == 2);
1730 * return list;
1731 */
1732struct commit_list **commit_list_append(struct commit *commit,
1733 struct commit_list **next)
1734{
Brandon Williams258c03e2018-02-14 10:59:37 -08001735 struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1736 new_commit->item = commit;
1737 *next = new_commit;
1738 new_commit->next = NULL;
1739 return &new_commit->next;
René Scharfe89b5f1d2012-04-25 22:35:27 +02001740}
Nguyễn Thái Ngọc Duyefc7df42012-10-26 22:53:51 +07001741
John Caicfc5cf42022-01-06 20:07:35 +00001742const char *find_header_mem(const char *msg, size_t len,
1743 const char *key, size_t *out_len)
Jeff Kingfe6eb7f2014-08-27 03:56:01 -04001744{
1745 int key_len = strlen(key);
1746 const char *line = msg;
1747
John Caicfc5cf42022-01-06 20:07:35 +00001748 /*
1749 * NEEDSWORK: It's possible for strchrnul() to scan beyond the range
1750 * given by len. However, current callers are safe because they compute
1751 * len by scanning a NUL-terminated block of memory starting at msg.
1752 * Nonetheless, it would be better to ensure the function does not look
1753 * at msg beyond the len provided by the caller.
1754 */
1755 while (line && line < msg + len) {
Jeff Kingfe6eb7f2014-08-27 03:56:01 -04001756 const char *eol = strchrnul(line, '\n');
1757
1758 if (line == eol)
1759 return NULL;
1760
1761 if (eol - line > key_len &&
1762 !strncmp(line, key, key_len) &&
1763 line[key_len] == ' ') {
1764 *out_len = eol - line - key_len - 1;
1765 return line + key_len + 1;
1766 }
1767 line = *eol ? eol + 1 : NULL;
1768 }
1769 return NULL;
1770}
Junio C Hamano0ed8a4e2014-12-22 12:26:23 -08001771
John Caicfc5cf42022-01-06 20:07:35 +00001772const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1773{
1774 return find_header_mem(msg, strlen(msg), key, out_len);
1775}
Christian Couder8c384582014-11-09 10:23:41 +01001776/*
Jonathan Tan710714a2016-11-02 10:29:17 -07001777 * Inspect the given string and determine the true "end" of the log message, in
Bradley M. Kuhn3abd4a62020-10-19 18:03:55 -07001778 * order to find where to put a new Signed-off-by trailer. Ignored are
Brian Malehornd76650b2017-05-15 23:06:49 -07001779 * trailing comment lines and blank lines. To support "git commit -s
1780 * --amend" on an existing commit, we also ignore "Conflicts:". To
1781 * support "git commit -v", we truncate at cut lines.
Christian Couder8c384582014-11-09 10:23:41 +01001782 *
1783 * Returns the number of bytes from the tail to ignore, to be fed as
1784 * the second parameter to append_signoff().
1785 */
Jeff King66e83d92018-08-22 20:50:51 -04001786size_t ignore_non_trailer(const char *buf, size_t len)
Christian Couder8c384582014-11-09 10:23:41 +01001787{
Jeff King66e83d92018-08-22 20:50:51 -04001788 size_t boc = 0;
1789 size_t bol = 0;
Christian Couder8c384582014-11-09 10:23:41 +01001790 int in_old_conflicts_block = 0;
Brian Malehornd76650b2017-05-15 23:06:49 -07001791 size_t cutoff = wt_status_locate_end(buf, len);
Christian Couder8c384582014-11-09 10:23:41 +01001792
Brian Malehornd76650b2017-05-15 23:06:49 -07001793 while (bol < cutoff) {
Jonathan Tan710714a2016-11-02 10:29:17 -07001794 const char *next_line = memchr(buf + bol, '\n', len - bol);
Christian Couder8c384582014-11-09 10:23:41 +01001795
Jonathan Tan710714a2016-11-02 10:29:17 -07001796 if (!next_line)
1797 next_line = buf + len;
Christian Couder8c384582014-11-09 10:23:41 +01001798 else
1799 next_line++;
1800
Jonathan Tan710714a2016-11-02 10:29:17 -07001801 if (buf[bol] == comment_line_char || buf[bol] == '\n') {
Christian Couder8c384582014-11-09 10:23:41 +01001802 /* is this the first of the run of comments? */
1803 if (!boc)
1804 boc = bol;
1805 /* otherwise, it is just continuing */
Jonathan Tan710714a2016-11-02 10:29:17 -07001806 } else if (starts_with(buf + bol, "Conflicts:\n")) {
Christian Couder8c384582014-11-09 10:23:41 +01001807 in_old_conflicts_block = 1;
1808 if (!boc)
1809 boc = bol;
Jonathan Tan710714a2016-11-02 10:29:17 -07001810 } else if (in_old_conflicts_block && buf[bol] == '\t') {
Christian Couder8c384582014-11-09 10:23:41 +01001811 ; /* a pathname in the conflicts block */
1812 } else if (boc) {
1813 /* the previous was not trailing comment */
1814 boc = 0;
1815 in_old_conflicts_block = 0;
1816 }
Jonathan Tan710714a2016-11-02 10:29:17 -07001817 bol = next_line - buf;
Christian Couder8c384582014-11-09 10:23:41 +01001818 }
Brian Malehornd76650b2017-05-15 23:06:49 -07001819 return boc ? len - boc : len - cutoff;
Christian Couder8c384582014-11-09 10:23:41 +01001820}
Phillip Wood49697cb2019-10-15 10:25:31 +00001821
1822int run_commit_hook(int editor_is_used, const char *index_file,
Ævar Arnfjörð Bjarmasona8cc5942022-03-07 13:33:46 +01001823 int *invoked_hook, const char *name, ...)
Phillip Wood49697cb2019-10-15 10:25:31 +00001824{
Emily Shafferf4432462021-12-22 04:59:40 +01001825 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
Phillip Wood49697cb2019-10-15 10:25:31 +00001826 va_list args;
Emily Shafferf4432462021-12-22 04:59:40 +01001827 const char *arg;
Phillip Wood49697cb2019-10-15 10:25:31 +00001828
Emily Shafferf4432462021-12-22 04:59:40 +01001829 strvec_pushf(&opt.env, "GIT_INDEX_FILE=%s", index_file);
Phillip Wood49697cb2019-10-15 10:25:31 +00001830
1831 /*
1832 * Let the hook know that no editor will be launched.
1833 */
1834 if (!editor_is_used)
Emily Shafferf4432462021-12-22 04:59:40 +01001835 strvec_push(&opt.env, "GIT_EDITOR=:");
Phillip Wood49697cb2019-10-15 10:25:31 +00001836
1837 va_start(args, name);
Emily Shafferf4432462021-12-22 04:59:40 +01001838 while ((arg = va_arg(args, const char *)))
1839 strvec_push(&opt.args, arg);
Phillip Wood49697cb2019-10-15 10:25:31 +00001840 va_end(args);
Phillip Wood49697cb2019-10-15 10:25:31 +00001841
Ævar Arnfjörð Bjarmason4369e3a2022-03-22 00:15:13 +01001842 opt.invoked_hook = invoked_hook;
Emily Shafferf4432462021-12-22 04:59:40 +01001843 return run_hooks_opt(name, &opt);
Phillip Wood49697cb2019-10-15 10:25:31 +00001844}