]>
Commit | Line | Data |
---|---|---|
8f1d2e6f | 1 | #include "cache.h" |
961784ee | 2 | #include "tag.h" |
175785e5 | 3 | #include "commit.h" |
177722b3 | 4 | #include "commit-graph.h" |
ed09aef0 | 5 | #include "pkt-line.h" |
52883fbd | 6 | #include "utf8.h" |
199c45bf JH |
7 | #include "diff.h" |
8 | #include "revision.h" | |
a97a7468 | 9 | #include "notes.h" |
ba3c69a9 | 10 | #include "gpg-interface.h" |
46905893 | 11 | #include "mergesort.h" |
a84b794a | 12 | #include "commit-slab.h" |
da24b104 | 13 | #include "prio-queue.h" |
0a9136f3 | 14 | #include "sha1-lookup.h" |
d76650b8 | 15 | #include "wt-status.h" |
f9f99b3f | 16 | #include "advice.h" |
175785e5 | 17 | |
82a75299 JH |
18 | static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **); |
19 | ||
60ab26de LT |
20 | int save_commit_buffer = 1; |
21 | ||
175785e5 DB |
22 | const char *commit_type = "commit"; |
23 | ||
bc83266a | 24 | struct commit *lookup_commit_reference_gently(const struct object_id *oid, |
f76412ed | 25 | int quiet) |
961784ee | 26 | { |
c251c83d | 27 | struct object *obj = deref_tag(parse_object(oid), NULL, 0); |
961784ee LT |
28 | |
29 | if (!obj) | |
30 | return NULL; | |
8ff226a9 | 31 | return object_as_type(obj, OBJ_COMMIT, quiet); |
f76412ed JH |
32 | } |
33 | ||
bc83266a | 34 | struct commit *lookup_commit_reference(const struct object_id *oid) |
f76412ed | 35 | { |
bc83266a | 36 | return lookup_commit_reference_gently(oid, 0); |
961784ee LT |
37 | } |
38 | ||
bc83266a | 39 | struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name) |
baf18fc2 | 40 | { |
bc83266a | 41 | struct commit *c = lookup_commit_reference(oid); |
baf18fc2 NTND |
42 | if (!c) |
43 | die(_("could not parse %s"), ref_name); | |
bc83266a | 44 | if (oidcmp(oid, &c->object.oid)) { |
baf18fc2 | 45 | warning(_("%s %s is not a commit!"), |
bc83266a | 46 | ref_name, oid_to_hex(oid)); |
baf18fc2 NTND |
47 | } |
48 | return c; | |
49 | } | |
50 | ||
bc83266a | 51 | struct commit *lookup_commit(const struct object_id *oid) |
175785e5 | 52 | { |
bc83266a | 53 | struct object *obj = lookup_object(oid->hash); |
d36f51c1 | 54 | if (!obj) |
bc83266a | 55 | return create_object(oid->hash, alloc_commit_node()); |
8ff226a9 | 56 | return object_as_type(obj, OBJ_COMMIT, 0); |
175785e5 DB |
57 | } |
58 | ||
a6fa5992 PN |
59 | struct commit *lookup_commit_reference_by_name(const char *name) |
60 | { | |
7683e2e6 | 61 | struct object_id oid; |
a6fa5992 PN |
62 | struct commit *commit; |
63 | ||
e82caf38 | 64 | if (get_oid_committish(name, &oid)) |
a6fa5992 | 65 | return NULL; |
bc83266a | 66 | commit = lookup_commit_reference(&oid); |
5e7d4d3e | 67 | if (parse_commit(commit)) |
a6fa5992 PN |
68 | return NULL; |
69 | return commit; | |
70 | } | |
71 | ||
dddbad72 | 72 | static timestamp_t parse_commit_date(const char *buf, const char *tail) |
175785e5 | 73 | { |
0a617799 | 74 | const char *dateptr; |
175785e5 | 75 | |
0a617799 MK |
76 | if (buf + 6 >= tail) |
77 | return 0; | |
175785e5 DB |
78 | if (memcmp(buf, "author", 6)) |
79 | return 0; | |
0a617799 | 80 | while (buf < tail && *buf++ != '\n') |
175785e5 | 81 | /* nada */; |
0a617799 MK |
82 | if (buf + 9 >= tail) |
83 | return 0; | |
175785e5 DB |
84 | if (memcmp(buf, "committer", 9)) |
85 | return 0; | |
0a617799 | 86 | while (buf < tail && *buf++ != '>') |
175785e5 | 87 | /* nada */; |
0a617799 MK |
88 | if (buf >= tail) |
89 | return 0; | |
90 | dateptr = buf; | |
91 | while (buf < tail && *buf++ != '\n') | |
92 | /* nada */; | |
93 | if (buf >= tail) | |
94 | return 0; | |
1aeb7e75 JS |
95 | /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */ |
96 | return parse_timestamp(dateptr, NULL, 10); | |
175785e5 DB |
97 | } |
98 | ||
5040f17e | 99 | static struct commit_graft **commit_graft; |
5da5c8f4 JH |
100 | static int commit_graft_alloc, commit_graft_nr; |
101 | ||
0a9136f3 DD |
102 | static const unsigned char *commit_graft_sha1_access(size_t index, void *table) |
103 | { | |
104 | struct commit_graft **commit_graft_table = table; | |
7683e2e6 | 105 | return commit_graft_table[index]->oid.hash; |
0a9136f3 DD |
106 | } |
107 | ||
5da5c8f4 JH |
108 | static int commit_graft_pos(const unsigned char *sha1) |
109 | { | |
0a9136f3 DD |
110 | return sha1_pos(sha1, commit_graft, commit_graft_nr, |
111 | commit_graft_sha1_access); | |
5da5c8f4 JH |
112 | } |
113 | ||
5040f17e JH |
114 | int register_commit_graft(struct commit_graft *graft, int ignore_dups) |
115 | { | |
7683e2e6 | 116 | int pos = commit_graft_pos(graft->oid.hash); |
a6080a0a | 117 | |
5040f17e JH |
118 | if (0 <= pos) { |
119 | if (ignore_dups) | |
120 | free(graft); | |
121 | else { | |
122 | free(commit_graft[pos]); | |
123 | commit_graft[pos] = graft; | |
124 | } | |
125 | return 1; | |
126 | } | |
127 | pos = -pos - 1; | |
d6e82b57 DD |
128 | ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc); |
129 | commit_graft_nr++; | |
5040f17e | 130 | if (pos < commit_graft_nr) |
f919ffeb SG |
131 | MOVE_ARRAY(commit_graft + pos + 1, commit_graft + pos, |
132 | commit_graft_nr - pos - 1); | |
5040f17e JH |
133 | commit_graft[pos] = graft; |
134 | return 0; | |
135 | } | |
136 | ||
9a934032 | 137 | struct commit_graft *read_graft_line(struct strbuf *line) |
5040f17e JH |
138 | { |
139 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
cfa5bf16 PO |
140 | int i, phase; |
141 | const char *tail = NULL; | |
5040f17e | 142 | struct commit_graft *graft = NULL; |
cfa5bf16 | 143 | struct object_id dummy_oid, *oid; |
5040f17e | 144 | |
9a934032 PO |
145 | strbuf_rtrim(line); |
146 | if (!line->len || line->buf[0] == '#') | |
5bc4ce58 | 147 | return NULL; |
cfa5bf16 PO |
148 | /* |
149 | * phase 0 verifies line, counts hashes in line and allocates graft | |
150 | * phase 1 fills graft | |
151 | */ | |
152 | for (phase = 0; phase < 2; phase++) { | |
153 | oid = graft ? &graft->oid : &dummy_oid; | |
154 | if (parse_oid_hex(line->buf, oid, &tail)) | |
5040f17e | 155 | goto bad_graft_data; |
cfa5bf16 PO |
156 | for (i = 0; *tail != '\0'; i++) { |
157 | oid = graft ? &graft->parent[i] : &dummy_oid; | |
158 | if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail)) | |
159 | goto bad_graft_data; | |
160 | } | |
161 | if (!graft) { | |
162 | graft = xmalloc(st_add(sizeof(*graft), | |
163 | st_mult(sizeof(struct object_id), i))); | |
164 | graft->nr_parent = i; | |
165 | } | |
5040f17e JH |
166 | } |
167 | return graft; | |
df5d43be RT |
168 | |
169 | bad_graft_data: | |
9a934032 | 170 | error("bad graft data: %s", line->buf); |
cfa5bf16 | 171 | assert(!graft); |
df5d43be | 172 | return NULL; |
5040f17e JH |
173 | } |
174 | ||
c5ae6439 | 175 | static int read_graft_file(const char *graft_file) |
5da5c8f4 | 176 | { |
e9d983f1 | 177 | FILE *fp = fopen_or_warn(graft_file, "r"); |
e228c173 | 178 | struct strbuf buf = STRBUF_INIT; |
5040f17e JH |
179 | if (!fp) |
180 | return -1; | |
f9f99b3f JS |
181 | if (advice_graft_file_deprecated) |
182 | advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n" | |
183 | "and will be removed in a future Git version.\n" | |
184 | "\n" | |
185 | "Please use \"git replace --convert-graft-file\"\n" | |
186 | "to convert the grafts into replace refs.\n" | |
187 | "\n" | |
188 | "Turn this message off by running\n" | |
189 | "\"git config advice.graftFileDeprecated false\"")); | |
e228c173 | 190 | while (!strbuf_getwholeline(&buf, fp, '\n')) { |
5da5c8f4 | 191 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
9a934032 | 192 | struct commit_graft *graft = read_graft_line(&buf); |
5bc4ce58 JH |
193 | if (!graft) |
194 | continue; | |
5040f17e | 195 | if (register_commit_graft(graft, 1)) |
e228c173 | 196 | error("duplicate graft data: %s", buf.buf); |
5da5c8f4 JH |
197 | } |
198 | fclose(fp); | |
e228c173 | 199 | strbuf_release(&buf); |
5040f17e JH |
200 | return 0; |
201 | } | |
202 | ||
203 | static void prepare_commit_graft(void) | |
204 | { | |
205 | static int commit_graft_prepared; | |
206 | char *graft_file; | |
207 | ||
208 | if (commit_graft_prepared) | |
209 | return; | |
14a9bd28 JK |
210 | if (!startup_info->have_repository) |
211 | return; | |
212 | ||
5040f17e JH |
213 | graft_file = get_graft_file(); |
214 | read_graft_file(graft_file); | |
ed09aef0 JS |
215 | /* make sure shallows are read */ |
216 | is_repository_shallow(); | |
5040f17e | 217 | commit_graft_prepared = 1; |
5da5c8f4 JH |
218 | } |
219 | ||
8b65a34c | 220 | struct commit_graft *lookup_commit_graft(const struct object_id *oid) |
5da5c8f4 JH |
221 | { |
222 | int pos; | |
5040f17e | 223 | prepare_commit_graft(); |
8b65a34c | 224 | pos = commit_graft_pos(oid->hash); |
5da5c8f4 JH |
225 | if (pos < 0) |
226 | return NULL; | |
227 | return commit_graft[pos]; | |
228 | } | |
229 | ||
09d46644 | 230 | int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data) |
ed09aef0 | 231 | { |
09d46644 NTND |
232 | int i, ret; |
233 | for (i = ret = 0; i < commit_graft_nr && !ret; i++) | |
234 | ret = fn(commit_graft[i], cb_data); | |
235 | return ret; | |
ed09aef0 JS |
236 | } |
237 | ||
e92b848c | 238 | int unregister_shallow(const struct object_id *oid) |
f53514bc | 239 | { |
e92b848c | 240 | int pos = commit_graft_pos(oid->hash); |
f53514bc JS |
241 | if (pos < 0) |
242 | return -1; | |
243 | if (pos + 1 < commit_graft_nr) | |
f331ab9d RS |
244 | MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1, |
245 | commit_graft_nr - pos - 1); | |
f53514bc JS |
246 | commit_graft_nr--; |
247 | return 0; | |
248 | } | |
249 | ||
8597ea3a JK |
250 | struct commit_buffer { |
251 | void *buffer; | |
252 | unsigned long size; | |
253 | }; | |
254 | define_commit_slab(buffer_slab, struct commit_buffer); | |
c1b3c71f JK |
255 | static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab); |
256 | ||
8597ea3a | 257 | void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size) |
66c2827e | 258 | { |
8597ea3a JK |
259 | struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit); |
260 | v->buffer = buffer; | |
261 | v->size = size; | |
66c2827e JK |
262 | } |
263 | ||
8597ea3a | 264 | const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep) |
152ff1cc | 265 | { |
862e730e JH |
266 | struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit); |
267 | if (!v) { | |
268 | if (sizep) | |
269 | *sizep = 0; | |
270 | return NULL; | |
271 | } | |
8597ea3a JK |
272 | if (sizep) |
273 | *sizep = v->size; | |
274 | return v->buffer; | |
152ff1cc JK |
275 | } |
276 | ||
8597ea3a | 277 | const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep) |
152ff1cc | 278 | { |
8597ea3a | 279 | const void *ret = get_cached_commit_buffer(commit, sizep); |
152ff1cc JK |
280 | if (!ret) { |
281 | enum object_type type; | |
282 | unsigned long size; | |
b4f5aca4 | 283 | ret = read_object_file(&commit->object.oid, &type, &size); |
152ff1cc JK |
284 | if (!ret) |
285 | die("cannot read commit object %s", | |
f2fd0760 | 286 | oid_to_hex(&commit->object.oid)); |
152ff1cc JK |
287 | if (type != OBJ_COMMIT) |
288 | die("expected commit for %s, got %s", | |
debca9d2 | 289 | oid_to_hex(&commit->object.oid), type_name(type)); |
8597ea3a JK |
290 | if (sizep) |
291 | *sizep = size; | |
152ff1cc JK |
292 | } |
293 | return ret; | |
294 | } | |
295 | ||
296 | void unuse_commit_buffer(const struct commit *commit, const void *buffer) | |
297 | { | |
862e730e JH |
298 | struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit); |
299 | if (!(v && v->buffer == buffer)) | |
152ff1cc JK |
300 | free((void *)buffer); |
301 | } | |
302 | ||
0fb370da JK |
303 | void free_commit_buffer(struct commit *commit) |
304 | { | |
862e730e JH |
305 | struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit); |
306 | if (v) { | |
6a83d902 | 307 | FREE_AND_NULL(v->buffer); |
862e730e JH |
308 | v->size = 0; |
309 | } | |
0fb370da JK |
310 | } |
311 | ||
5bb03de1 DS |
312 | struct tree *get_commit_tree(const struct commit *commit) |
313 | { | |
7b8a21db DS |
314 | if (commit->maybe_tree || !commit->object.parsed) |
315 | return commit->maybe_tree; | |
316 | ||
317 | if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH) | |
318 | BUG("commit has NULL tree, but was not loaded from commit-graph"); | |
319 | ||
320 | return get_commit_tree_in_graph(commit); | |
5bb03de1 DS |
321 | } |
322 | ||
323 | struct object_id *get_commit_tree_oid(const struct commit *commit) | |
324 | { | |
325 | return &get_commit_tree(commit)->object.oid; | |
326 | } | |
327 | ||
8597ea3a | 328 | const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep) |
0fb370da | 329 | { |
862e730e | 330 | struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit); |
8597ea3a JK |
331 | void *ret; |
332 | ||
862e730e JH |
333 | if (!v) { |
334 | if (sizep) | |
335 | *sizep = 0; | |
336 | return NULL; | |
337 | } | |
8597ea3a JK |
338 | ret = v->buffer; |
339 | if (sizep) | |
340 | *sizep = v->size; | |
341 | ||
342 | v->buffer = NULL; | |
343 | v->size = 0; | |
0fb370da JK |
344 | return ret; |
345 | } | |
346 | ||
cf7b1cad | 347 | int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size) |
175785e5 | 348 | { |
cf7b1cad NTND |
349 | const char *tail = buffer; |
350 | const char *bufptr = buffer; | |
7683e2e6 | 351 | struct object_id parent; |
6c88be16 | 352 | struct commit_list **pptr; |
5da5c8f4 | 353 | struct commit_graft *graft; |
7683e2e6 | 354 | const int tree_entry_len = GIT_SHA1_HEXSZ + 5; |
355 | const int parent_entry_len = GIT_SHA1_HEXSZ + 7; | |
bd2c39f5 | 356 | |
175785e5 DB |
357 | if (item->object.parsed) |
358 | return 0; | |
359 | item->object.parsed = 1; | |
3b44f15a | 360 | tail += size; |
7683e2e6 | 361 | if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) || |
362 | bufptr[tree_entry_len] != '\n') | |
f2fd0760 | 363 | return error("bogus commit object %s", oid_to_hex(&item->object.oid)); |
26ea3e7d | 364 | if (get_oid_hex(bufptr + 5, &parent) < 0) |
bd2afde8 | 365 | return error("bad tree pointer in commit %s", |
f2fd0760 | 366 | oid_to_hex(&item->object.oid)); |
891435d5 | 367 | item->maybe_tree = lookup_tree(&parent); |
7683e2e6 | 368 | bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */ |
6c88be16 | 369 | pptr = &item->parents; |
5da5c8f4 | 370 | |
8b65a34c | 371 | graft = lookup_commit_graft(&item->object.oid); |
7683e2e6 | 372 | while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) { |
f7cc77d7 LT |
373 | struct commit *new_parent; |
374 | ||
7683e2e6 | 375 | if (tail <= bufptr + parent_entry_len + 1 || |
26ea3e7d | 376 | get_oid_hex(bufptr + 7, &parent) || |
7683e2e6 | 377 | bufptr[parent_entry_len] != '\n') |
f2fd0760 | 378 | return error("bad parents in commit %s", oid_to_hex(&item->object.oid)); |
7683e2e6 | 379 | bufptr += parent_entry_len + 1; |
7f3140cd JS |
380 | /* |
381 | * The clone is shallow if nr_parent < 0, and we must | |
382 | * not traverse its real parents even when we unhide them. | |
383 | */ | |
384 | if (graft && (graft->nr_parent < 0 || grafts_replace_parents)) | |
5da5c8f4 | 385 | continue; |
bc83266a | 386 | new_parent = lookup_commit(&parent); |
39468def | 387 | if (new_parent) |
6c88be16 | 388 | pptr = &commit_list_insert(new_parent, pptr)->next; |
5da5c8f4 JH |
389 | } |
390 | if (graft) { | |
391 | int i; | |
392 | struct commit *new_parent; | |
393 | for (i = 0; i < graft->nr_parent; i++) { | |
bc83266a | 394 | new_parent = lookup_commit(&graft->parent[i]); |
5da5c8f4 JH |
395 | if (!new_parent) |
396 | continue; | |
397 | pptr = &commit_list_insert(new_parent, pptr)->next; | |
5da5c8f4 | 398 | } |
175785e5 | 399 | } |
0a617799 | 400 | item->date = parse_commit_date(bufptr, tail); |
27dedf0c | 401 | |
175785e5 DB |
402 | return 0; |
403 | } | |
404 | ||
9cc2b07a | 405 | int parse_commit_gently(struct commit *item, int quiet_on_missing) |
bd2c39f5 | 406 | { |
21666f1a | 407 | enum object_type type; |
bd2c39f5 NP |
408 | void *buffer; |
409 | unsigned long size; | |
410 | int ret; | |
411 | ||
9786f68b MK |
412 | if (!item) |
413 | return -1; | |
bd2c39f5 NP |
414 | if (item->object.parsed) |
415 | return 0; | |
177722b3 DS |
416 | if (parse_commit_in_graph(item)) |
417 | return 0; | |
b4f5aca4 | 418 | buffer = read_object_file(&item->object.oid, &type, &size); |
bd2c39f5 | 419 | if (!buffer) |
9cc2b07a JK |
420 | return quiet_on_missing ? -1 : |
421 | error("Could not read %s", | |
f2fd0760 | 422 | oid_to_hex(&item->object.oid)); |
21666f1a | 423 | if (type != OBJ_COMMIT) { |
bd2c39f5 NP |
424 | free(buffer); |
425 | return error("Object %s not a commit", | |
f2fd0760 | 426 | oid_to_hex(&item->object.oid)); |
bd2c39f5 NP |
427 | } |
428 | ret = parse_commit_buffer(item, buffer, size); | |
60ab26de | 429 | if (save_commit_buffer && !ret) { |
8597ea3a | 430 | set_commit_buffer(item, buffer, size); |
3ff1fbbb LT |
431 | return 0; |
432 | } | |
bd2c39f5 NP |
433 | free(buffer); |
434 | return ret; | |
435 | } | |
436 | ||
7059dccc JK |
437 | void parse_commit_or_die(struct commit *item) |
438 | { | |
439 | if (parse_commit(item)) | |
440 | die("unable to parse commit %s", | |
f2fd0760 | 441 | item ? oid_to_hex(&item->object.oid) : "(null)"); |
7059dccc JK |
442 | } |
443 | ||
11af2aae CC |
444 | int find_commit_subject(const char *commit_buffer, const char **subject) |
445 | { | |
446 | const char *eol; | |
447 | const char *p = commit_buffer; | |
448 | ||
449 | while (*p && (*p != '\n' || p[1] != '\n')) | |
450 | p++; | |
451 | if (*p) { | |
4e1b06da | 452 | p = skip_blank_lines(p + 2); |
9c9b03b1 | 453 | eol = strchrnul(p, '\n'); |
11af2aae CC |
454 | } else |
455 | eol = p; | |
456 | ||
457 | *subject = p; | |
458 | ||
459 | return eol - p; | |
460 | } | |
461 | ||
ac5155ef | 462 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
dd97f850 | 463 | { |
812666c8 | 464 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
dd97f850 DB |
465 | new_list->item = item; |
466 | new_list->next = *list_p; | |
467 | *list_p = new_list; | |
ac5155ef | 468 | return new_list; |
dd97f850 DB |
469 | } |
470 | ||
65319475 MV |
471 | unsigned commit_list_count(const struct commit_list *l) |
472 | { | |
473 | unsigned c = 0; | |
474 | for (; l; l = l->next ) | |
475 | c++; | |
476 | return c; | |
477 | } | |
478 | ||
53d00b39 TR |
479 | struct commit_list *copy_commit_list(struct commit_list *list) |
480 | { | |
481 | struct commit_list *head = NULL; | |
482 | struct commit_list **pp = &head; | |
483 | while (list) { | |
cb979dbd | 484 | pp = commit_list_append(list->item, pp); |
53d00b39 TR |
485 | list = list->next; |
486 | } | |
487 | return head; | |
488 | } | |
489 | ||
175785e5 DB |
490 | void free_commit_list(struct commit_list *list) |
491 | { | |
e510ab89 RS |
492 | while (list) |
493 | pop_commit(&list); | |
175785e5 | 494 | } |
dd97f850 | 495 | |
47e44ed1 | 496 | struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list) |
dd97f850 DB |
497 | { |
498 | struct commit_list **pp = list; | |
499 | struct commit_list *p; | |
500 | while ((p = *pp) != NULL) { | |
501 | if (p->item->date < item->date) { | |
502 | break; | |
503 | } | |
504 | pp = &p->next; | |
505 | } | |
f755494c | 506 | return commit_list_insert(item, pp); |
dd97f850 DB |
507 | } |
508 | ||
46905893 RS |
509 | static int commit_list_compare_by_date(const void *a, const void *b) |
510 | { | |
dddbad72 JS |
511 | timestamp_t a_date = ((const struct commit_list *)a)->item->date; |
512 | timestamp_t b_date = ((const struct commit_list *)b)->item->date; | |
46905893 RS |
513 | if (a_date < b_date) |
514 | return 1; | |
515 | if (a_date > b_date) | |
516 | return -1; | |
517 | return 0; | |
518 | } | |
519 | ||
520 | static void *commit_list_get_next(const void *a) | |
521 | { | |
522 | return ((const struct commit_list *)a)->next; | |
523 | } | |
524 | ||
525 | static void commit_list_set_next(void *a, void *next) | |
526 | { | |
527 | ((struct commit_list *)a)->next = next; | |
528 | } | |
a6080a0a | 529 | |
47e44ed1 | 530 | void commit_list_sort_by_date(struct commit_list **list) |
dd97f850 | 531 | { |
7365c95d JH |
532 | *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next, |
533 | commit_list_compare_by_date); | |
dd97f850 DB |
534 | } |
535 | ||
58e28af6 DB |
536 | struct commit *pop_most_recent_commit(struct commit_list **list, |
537 | unsigned int mark) | |
dd97f850 | 538 | { |
e510ab89 | 539 | struct commit *ret = pop_commit(list); |
dd97f850 | 540 | struct commit_list *parents = ret->parents; |
dd97f850 DB |
541 | |
542 | while (parents) { | |
4056c091 | 543 | struct commit *commit = parents->item; |
dec38c81 | 544 | if (!parse_commit(commit) && !(commit->object.flags & mark)) { |
58e28af6 | 545 | commit->object.flags |= mark; |
47e44ed1 | 546 | commit_list_insert_by_date(commit, list); |
4056c091 | 547 | } |
dd97f850 DB |
548 | parents = parents->next; |
549 | } | |
550 | return ret; | |
551 | } | |
e3bc7a3b | 552 | |
941ba8db NTND |
553 | static void clear_commit_marks_1(struct commit_list **plist, |
554 | struct commit *commit, unsigned int mark) | |
f8f9c73c | 555 | { |
60fcc2e6 JS |
556 | while (commit) { |
557 | struct commit_list *parents; | |
f8f9c73c | 558 | |
60fcc2e6 JS |
559 | if (!(mark & commit->object.flags)) |
560 | return; | |
58ecf5c1 | 561 | |
60fcc2e6 JS |
562 | commit->object.flags &= ~mark; |
563 | ||
564 | parents = commit->parents; | |
565 | if (!parents) | |
566 | return; | |
567 | ||
568 | while ((parents = parents->next)) | |
941ba8db | 569 | commit_list_insert(parents->item, plist); |
60fcc2e6 JS |
570 | |
571 | commit = commit->parents->item; | |
f8f9c73c JH |
572 | } |
573 | } | |
574 | ||
e895cb51 | 575 | void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark) |
941ba8db NTND |
576 | { |
577 | struct commit_list *list = NULL; | |
e895cb51 JH |
578 | |
579 | while (nr--) { | |
07f7d55a | 580 | clear_commit_marks_1(&list, *commit, mark); |
e895cb51 JH |
581 | commit++; |
582 | } | |
941ba8db NTND |
583 | while (list) |
584 | clear_commit_marks_1(&list, pop_commit(&list), mark); | |
585 | } | |
586 | ||
e895cb51 JH |
587 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
588 | { | |
589 | clear_commit_marks_many(1, &commit, mark); | |
590 | } | |
591 | ||
a3437b8c JS |
592 | struct commit *pop_commit(struct commit_list **stack) |
593 | { | |
594 | struct commit_list *top = *stack; | |
595 | struct commit *item = top ? top->item : NULL; | |
596 | ||
597 | if (top) { | |
598 | *stack = top->next; | |
599 | free(top); | |
600 | } | |
601 | return item; | |
602 | } | |
603 | ||
a84b794a JH |
604 | /* |
605 | * Topological sort support | |
606 | */ | |
96c4f4a3 | 607 | |
a84b794a JH |
608 | /* count number of children that have not been emitted */ |
609 | define_commit_slab(indegree_slab, int); | |
96c4f4a3 | 610 | |
81c6b38b JH |
611 | /* record author-date for each commit object */ |
612 | define_commit_slab(author_date_slab, unsigned long); | |
613 | ||
614 | static void record_author_date(struct author_date_slab *author_date, | |
615 | struct commit *commit) | |
616 | { | |
8597ea3a | 617 | const char *buffer = get_commit_buffer(commit, NULL); |
81c6b38b | 618 | struct ident_split ident; |
ea5517f0 JK |
619 | const char *ident_line; |
620 | size_t ident_len; | |
81c6b38b | 621 | char *date_end; |
dddbad72 | 622 | timestamp_t date; |
81c6b38b | 623 | |
ea5517f0 JK |
624 | ident_line = find_commit_header(buffer, "author", &ident_len); |
625 | if (!ident_line) | |
626 | goto fail_exit; /* no author line */ | |
627 | if (split_ident_line(&ident, ident_line, ident_len) || | |
628 | !ident.date_begin || !ident.date_end) | |
629 | goto fail_exit; /* malformed "author" line */ | |
81c6b38b | 630 | |
1aeb7e75 | 631 | date = parse_timestamp(ident.date_begin, &date_end, 10); |
81c6b38b JH |
632 | if (date_end != ident.date_end) |
633 | goto fail_exit; /* malformed date */ | |
634 | *(author_date_slab_at(author_date, commit)) = date; | |
635 | ||
636 | fail_exit: | |
ba41c1c9 | 637 | unuse_commit_buffer(commit, buffer); |
81c6b38b JH |
638 | } |
639 | ||
640 | static int compare_commits_by_author_date(const void *a_, const void *b_, | |
641 | void *cb_data) | |
642 | { | |
643 | const struct commit *a = a_, *b = b_; | |
644 | struct author_date_slab *author_date = cb_data; | |
dddbad72 JS |
645 | timestamp_t a_date = *(author_date_slab_at(author_date, a)); |
646 | timestamp_t b_date = *(author_date_slab_at(author_date, b)); | |
81c6b38b JH |
647 | |
648 | /* newer commits with larger date first */ | |
649 | if (a_date < b_date) | |
650 | return 1; | |
651 | else if (a_date > b_date) | |
652 | return -1; | |
653 | return 0; | |
654 | } | |
655 | ||
727377ff | 656 | int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused) |
da24b104 JH |
657 | { |
658 | const struct commit *a = a_, *b = b_; | |
659 | /* newer commits with larger date first */ | |
660 | if (a->date < b->date) | |
661 | return 1; | |
662 | else if (a->date > b->date) | |
663 | return -1; | |
664 | return 0; | |
665 | } | |
666 | ||
ab580ace JS |
667 | /* |
668 | * Performs an in-place topological sort on the list supplied. | |
669 | */ | |
da24b104 | 670 | void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order) |
6b6dcfc2 | 671 | { |
23c17d4a | 672 | struct commit_list *next, *orig = *list; |
23c17d4a | 673 | struct commit_list **pptr; |
a84b794a | 674 | struct indegree_slab indegree; |
da24b104 JH |
675 | struct prio_queue queue; |
676 | struct commit *commit; | |
81c6b38b | 677 | struct author_date_slab author_date; |
a6080a0a | 678 | |
23c17d4a | 679 | if (!orig) |
7d6fb370 | 680 | return; |
23c17d4a LT |
681 | *list = NULL; |
682 | ||
a84b794a | 683 | init_indegree_slab(&indegree); |
da24b104 | 684 | memset(&queue, '\0', sizeof(queue)); |
81c6b38b | 685 | |
da24b104 JH |
686 | switch (sort_order) { |
687 | default: /* REV_SORT_IN_GRAPH_ORDER */ | |
688 | queue.compare = NULL; | |
689 | break; | |
690 | case REV_SORT_BY_COMMIT_DATE: | |
691 | queue.compare = compare_commits_by_commit_date; | |
692 | break; | |
81c6b38b JH |
693 | case REV_SORT_BY_AUTHOR_DATE: |
694 | init_author_date_slab(&author_date); | |
695 | queue.compare = compare_commits_by_author_date; | |
696 | queue.cb_data = &author_date; | |
697 | break; | |
da24b104 | 698 | } |
96c4f4a3 | 699 | |
23c17d4a LT |
700 | /* Mark them and clear the indegree */ |
701 | for (next = orig; next; next = next->next) { | |
702 | struct commit *commit = next->item; | |
a84b794a | 703 | *(indegree_slab_at(&indegree, commit)) = 1; |
81c6b38b JH |
704 | /* also record the author dates, if needed */ |
705 | if (sort_order == REV_SORT_BY_AUTHOR_DATE) | |
706 | record_author_date(&author_date, commit); | |
ab580ace | 707 | } |
23c17d4a | 708 | |
ab580ace | 709 | /* update the indegree */ |
23c17d4a | 710 | for (next = orig; next; next = next->next) { |
da24b104 | 711 | struct commit_list *parents = next->item->parents; |
ab580ace | 712 | while (parents) { |
23c17d4a | 713 | struct commit *parent = parents->item; |
a84b794a | 714 | int *pi = indegree_slab_at(&indegree, parent); |
6b6dcfc2 | 715 | |
96c4f4a3 JK |
716 | if (*pi) |
717 | (*pi)++; | |
23c17d4a | 718 | parents = parents->next; |
ab580ace | 719 | } |
ab580ace | 720 | } |
23c17d4a | 721 | |
a6080a0a | 722 | /* |
674d1727 PH |
723 | * find the tips |
724 | * | |
725 | * tips are nodes not reachable from any other node in the list | |
726 | * | |
727 | * the tips serve as a starting set for the work queue. | |
728 | */ | |
23c17d4a LT |
729 | for (next = orig; next; next = next->next) { |
730 | struct commit *commit = next->item; | |
ab580ace | 731 | |
a84b794a | 732 | if (*(indegree_slab_at(&indegree, commit)) == 1) |
da24b104 | 733 | prio_queue_put(&queue, commit); |
ab580ace | 734 | } |
4c8725f1 | 735 | |
da24b104 JH |
736 | /* |
737 | * This is unfortunate; the initial tips need to be shown | |
738 | * in the order given from the revision traversal machinery. | |
739 | */ | |
740 | if (sort_order == REV_SORT_IN_GRAPH_ORDER) | |
741 | prio_queue_reverse(&queue); | |
742 | ||
743 | /* We no longer need the commit list */ | |
744 | free_commit_list(orig); | |
23c17d4a LT |
745 | |
746 | pptr = list; | |
747 | *list = NULL; | |
da24b104 JH |
748 | while ((commit = prio_queue_get(&queue)) != NULL) { |
749 | struct commit_list *parents; | |
23c17d4a | 750 | |
23c17d4a | 751 | for (parents = commit->parents; parents ; parents = parents->next) { |
cd2b8ae9 | 752 | struct commit *parent = parents->item; |
a84b794a | 753 | int *pi = indegree_slab_at(&indegree, parent); |
23c17d4a | 754 | |
96c4f4a3 | 755 | if (!*pi) |
23c17d4a LT |
756 | continue; |
757 | ||
758 | /* | |
759 | * parents are only enqueued for emission | |
760 | * when all their children have been emitted thereby | |
761 | * guaranteeing topological order. | |
762 | */ | |
da24b104 JH |
763 | if (--(*pi) == 1) |
764 | prio_queue_put(&queue, parent); | |
ab580ace JS |
765 | } |
766 | /* | |
da24b104 JH |
767 | * all children of commit have already been |
768 | * emitted. we can emit it now. | |
674d1727 | 769 | */ |
a84b794a | 770 | *(indegree_slab_at(&indegree, commit)) = 0; |
da24b104 JH |
771 | |
772 | pptr = &commit_list_insert(commit, pptr)->next; | |
ab580ace | 773 | } |
96c4f4a3 | 774 | |
a84b794a | 775 | clear_indegree_slab(&indegree); |
da24b104 | 776 | clear_prio_queue(&queue); |
81c6b38b JH |
777 | if (sort_order == REV_SORT_BY_AUTHOR_DATE) |
778 | clear_author_date_slab(&author_date); | |
ab580ace | 779 | } |
7c6f8aaf | 780 | |
1295228d | 781 | /* merge-base stuff */ |
7c6f8aaf | 782 | |
208acbfb | 783 | /* Remember to update object flag allocation in object.h */ |
577ed5c2 JH |
784 | #define PARENT1 (1u<<16) |
785 | #define PARENT2 (1u<<17) | |
786 | #define STALE (1u<<18) | |
787 | #define RESULT (1u<<19) | |
7c6f8aaf | 788 | |
1295228d JH |
789 | static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT); |
790 | ||
73f43f22 | 791 | static int queue_has_nonstale(struct prio_queue *queue) |
7c6f8aaf | 792 | { |
73f43f22 JK |
793 | int i; |
794 | for (i = 0; i < queue->nr; i++) { | |
795 | struct commit *commit = queue->array[i].data; | |
796 | if (!(commit->object.flags & STALE)) | |
797 | return 1; | |
7c6f8aaf | 798 | } |
73f43f22 | 799 | return 0; |
7c6f8aaf JS |
800 | } |
801 | ||
d866924a | 802 | /* all input commits in one and twos[] must have been parsed! */ |
da1f5156 | 803 | static struct commit_list *paint_down_to_common(struct commit *one, int n, struct commit **twos) |
7c6f8aaf | 804 | { |
73f43f22 | 805 | struct prio_queue queue = { compare_commits_by_commit_date }; |
7c6f8aaf | 806 | struct commit_list *result = NULL; |
6a938648 | 807 | int i; |
7c6f8aaf | 808 | |
f3249438 | 809 | one->object.flags |= PARENT1; |
73f43f22 JK |
810 | if (!n) { |
811 | commit_list_append(one, &result); | |
812 | return result; | |
813 | } | |
814 | prio_queue_put(&queue, one); | |
815 | ||
6a938648 JH |
816 | for (i = 0; i < n; i++) { |
817 | twos[i]->object.flags |= PARENT2; | |
73f43f22 | 818 | prio_queue_put(&queue, twos[i]); |
6a938648 | 819 | } |
7c6f8aaf | 820 | |
73f43f22 JK |
821 | while (queue_has_nonstale(&queue)) { |
822 | struct commit *commit = prio_queue_get(&queue); | |
7c6f8aaf | 823 | struct commit_list *parents; |
f3249438 | 824 | int flags; |
7c6f8aaf | 825 | |
f3249438 JH |
826 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); |
827 | if (flags == (PARENT1 | PARENT2)) { | |
828 | if (!(commit->object.flags & RESULT)) { | |
829 | commit->object.flags |= RESULT; | |
47e44ed1 | 830 | commit_list_insert_by_date(commit, &result); |
f3249438 | 831 | } |
542ccefe JH |
832 | /* Mark parents of a found merge stale */ |
833 | flags |= STALE; | |
7c6f8aaf JS |
834 | } |
835 | parents = commit->parents; | |
836 | while (parents) { | |
837 | struct commit *p = parents->item; | |
838 | parents = parents->next; | |
839 | if ((p->object.flags & flags) == flags) | |
840 | continue; | |
172947e6 MK |
841 | if (parse_commit(p)) |
842 | return NULL; | |
7c6f8aaf | 843 | p->object.flags |= flags; |
73f43f22 | 844 | prio_queue_put(&queue, p); |
7c6f8aaf JS |
845 | } |
846 | } | |
847 | ||
73f43f22 | 848 | clear_prio_queue(&queue); |
da1f5156 JH |
849 | return result; |
850 | } | |
851 | ||
852 | static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos) | |
853 | { | |
854 | struct commit_list *list = NULL; | |
855 | struct commit_list *result = NULL; | |
856 | int i; | |
857 | ||
858 | for (i = 0; i < n; i++) { | |
859 | if (one == twos[i]) | |
860 | /* | |
861 | * We do not mark this even with RESULT so we do not | |
862 | * have to clean it up. | |
863 | */ | |
864 | return commit_list_insert(one, &result); | |
865 | } | |
866 | ||
867 | if (parse_commit(one)) | |
868 | return NULL; | |
869 | for (i = 0; i < n; i++) { | |
870 | if (parse_commit(twos[i])) | |
871 | return NULL; | |
872 | } | |
873 | ||
874 | list = paint_down_to_common(one, n, twos); | |
875 | ||
f3249438 | 876 | while (list) { |
e510ab89 RS |
877 | struct commit *commit = pop_commit(&list); |
878 | if (!(commit->object.flags & STALE)) | |
879 | commit_list_insert_by_date(commit, &result); | |
f3249438 JH |
880 | } |
881 | return result; | |
882 | } | |
7c6f8aaf | 883 | |
5240c9d7 MV |
884 | struct commit_list *get_octopus_merge_bases(struct commit_list *in) |
885 | { | |
886 | struct commit_list *i, *j, *k, *ret = NULL; | |
5240c9d7 | 887 | |
6bc76725 VM |
888 | if (!in) |
889 | return ret; | |
890 | ||
891 | commit_list_insert(in->item, &ret); | |
892 | ||
893 | for (i = in->next; i; i = i->next) { | |
258c03eb | 894 | struct commit_list *new_commits = NULL, *end = NULL; |
6bc76725 VM |
895 | |
896 | for (j = ret; j; j = j->next) { | |
897 | struct commit_list *bases; | |
2ce406cc | 898 | bases = get_merge_bases(i->item, j->item); |
258c03eb BW |
899 | if (!new_commits) |
900 | new_commits = bases; | |
6bc76725 VM |
901 | else |
902 | end->next = bases; | |
903 | for (k = bases; k; k = k->next) | |
904 | end = k; | |
5240c9d7 | 905 | } |
258c03eb | 906 | ret = new_commits; |
5240c9d7 MV |
907 | } |
908 | return ret; | |
909 | } | |
910 | ||
94f0ced0 JH |
911 | static int remove_redundant(struct commit **array, int cnt) |
912 | { | |
913 | /* | |
914 | * Some commit in the array may be an ancestor of | |
915 | * another commit. Move such commit to the end of | |
916 | * the array, and return the number of commits that | |
917 | * are independent from each other. | |
918 | */ | |
919 | struct commit **work; | |
920 | unsigned char *redundant; | |
921 | int *filled_index; | |
922 | int i, j, filled; | |
923 | ||
924 | work = xcalloc(cnt, sizeof(*work)); | |
925 | redundant = xcalloc(cnt, 1); | |
b32fa95f | 926 | ALLOC_ARRAY(filled_index, cnt - 1); |
94f0ced0 | 927 | |
d866924a JH |
928 | for (i = 0; i < cnt; i++) |
929 | parse_commit(array[i]); | |
94f0ced0 JH |
930 | for (i = 0; i < cnt; i++) { |
931 | struct commit_list *common; | |
932 | ||
933 | if (redundant[i]) | |
934 | continue; | |
935 | for (j = filled = 0; j < cnt; j++) { | |
936 | if (i == j || redundant[j]) | |
937 | continue; | |
938 | filled_index[filled] = j; | |
939 | work[filled++] = array[j]; | |
940 | } | |
941 | common = paint_down_to_common(array[i], filled, work); | |
942 | if (array[i]->object.flags & PARENT2) | |
943 | redundant[i] = 1; | |
944 | for (j = 0; j < filled; j++) | |
945 | if (work[j]->object.flags & PARENT1) | |
946 | redundant[filled_index[j]] = 1; | |
947 | clear_commit_marks(array[i], all_flags); | |
abc03512 | 948 | clear_commit_marks_many(filled, work, all_flags); |
94f0ced0 JH |
949 | free_commit_list(common); |
950 | } | |
951 | ||
952 | /* Now collect the result */ | |
45ccef87 | 953 | COPY_ARRAY(work, array, cnt); |
94f0ced0 JH |
954 | for (i = filled = 0; i < cnt; i++) |
955 | if (!redundant[i]) | |
956 | array[filled++] = work[i]; | |
957 | for (j = filled, i = 0; i < cnt; i++) | |
958 | if (redundant[i]) | |
959 | array[j++] = work[i]; | |
960 | free(work); | |
961 | free(redundant); | |
962 | free(filled_index); | |
963 | return filled; | |
964 | } | |
965 | ||
2ce406cc JH |
966 | static struct commit_list *get_merge_bases_many_0(struct commit *one, |
967 | int n, | |
968 | struct commit **twos, | |
969 | int cleanup) | |
f3249438 | 970 | { |
f3249438 JH |
971 | struct commit_list *list; |
972 | struct commit **rslt; | |
973 | struct commit_list *result; | |
94f0ced0 | 974 | int cnt, i; |
f3249438 | 975 | |
6a938648 JH |
976 | result = merge_bases_many(one, n, twos); |
977 | for (i = 0; i < n; i++) { | |
978 | if (one == twos[i]) | |
979 | return result; | |
980 | } | |
f3249438 JH |
981 | if (!result || !result->next) { |
982 | if (cleanup) { | |
983 | clear_commit_marks(one, all_flags); | |
e895cb51 | 984 | clear_commit_marks_many(n, twos, all_flags); |
7c6f8aaf | 985 | } |
f3249438 | 986 | return result; |
7c6f8aaf JS |
987 | } |
988 | ||
f3249438 | 989 | /* There are more than one */ |
4bbaa1eb | 990 | cnt = commit_list_count(result); |
f3249438 JH |
991 | rslt = xcalloc(cnt, sizeof(*rslt)); |
992 | for (list = result, i = 0; list; list = list->next) | |
993 | rslt[i++] = list->item; | |
994 | free_commit_list(result); | |
995 | ||
996 | clear_commit_marks(one, all_flags); | |
e895cb51 | 997 | clear_commit_marks_many(n, twos, all_flags); |
7c6f8aaf | 998 | |
94f0ced0 | 999 | cnt = remove_redundant(rslt, cnt); |
f3249438 | 1000 | result = NULL; |
94f0ced0 JH |
1001 | for (i = 0; i < cnt; i++) |
1002 | commit_list_insert_by_date(rslt[i], &result); | |
f3249438 | 1003 | free(rslt); |
7c6f8aaf JS |
1004 | return result; |
1005 | } | |
2ecd2bbc | 1006 | |
2ce406cc JH |
1007 | struct commit_list *get_merge_bases_many(struct commit *one, |
1008 | int n, | |
1009 | struct commit **twos) | |
1010 | { | |
1011 | return get_merge_bases_many_0(one, n, twos, 1); | |
1012 | } | |
1013 | ||
1014 | struct commit_list *get_merge_bases_many_dirty(struct commit *one, | |
1015 | int n, | |
1016 | struct commit **twos) | |
1017 | { | |
1018 | return get_merge_bases_many_0(one, n, twos, 0); | |
1019 | } | |
1020 | ||
1021 | struct commit_list *get_merge_bases(struct commit *one, struct commit *two) | |
6a938648 | 1022 | { |
2ce406cc | 1023 | return get_merge_bases_many_0(one, 1, &two, 1); |
6a938648 JH |
1024 | } |
1025 | ||
a20efee9 | 1026 | /* |
41ccfdd9 | 1027 | * Is "commit" a descendant of one of the elements on the "with_commit" list? |
a20efee9 | 1028 | */ |
7fcdb36e JG |
1029 | int is_descendant_of(struct commit *commit, struct commit_list *with_commit) |
1030 | { | |
1031 | if (!with_commit) | |
1032 | return 1; | |
1033 | while (with_commit) { | |
1034 | struct commit *other; | |
1035 | ||
1036 | other = with_commit->item; | |
1037 | with_commit = with_commit->next; | |
a20efee9 | 1038 | if (in_merge_bases(other, commit)) |
7fcdb36e JG |
1039 | return 1; |
1040 | } | |
1041 | return 0; | |
1042 | } | |
1043 | ||
a20efee9 | 1044 | /* |
4c4b27e8 | 1045 | * Is "commit" an ancestor of one of the "references"? |
a20efee9 | 1046 | */ |
4c4b27e8 | 1047 | int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference) |
2ecd2bbc | 1048 | { |
6440fdba | 1049 | struct commit_list *bases; |
4c4b27e8 | 1050 | int ret = 0, i; |
2ecd2bbc | 1051 | |
4c4b27e8 | 1052 | if (parse_commit(commit)) |
6440fdba | 1053 | return ret; |
4c4b27e8 JH |
1054 | for (i = 0; i < nr_reference; i++) |
1055 | if (parse_commit(reference[i])) | |
1056 | return ret; | |
2ecd2bbc | 1057 | |
4c4b27e8 | 1058 | bases = paint_down_to_common(commit, nr_reference, reference); |
6440fdba JH |
1059 | if (commit->object.flags & PARENT2) |
1060 | ret = 1; | |
b0f9e9ee | 1061 | clear_commit_marks(commit, all_flags); |
557899ff | 1062 | clear_commit_marks_many(nr_reference, reference, all_flags); |
2ecd2bbc JH |
1063 | free_commit_list(bases); |
1064 | return ret; | |
1065 | } | |
98cf9c3b | 1066 | |
4c4b27e8 JH |
1067 | /* |
1068 | * Is "commit" an ancestor of (i.e. reachable from) the "reference"? | |
1069 | */ | |
1070 | int in_merge_bases(struct commit *commit, struct commit *reference) | |
1071 | { | |
1072 | return in_merge_bases_many(commit, 1, &reference); | |
1073 | } | |
1074 | ||
98cf9c3b JH |
1075 | struct commit_list *reduce_heads(struct commit_list *heads) |
1076 | { | |
1077 | struct commit_list *p; | |
1078 | struct commit_list *result = NULL, **tail = &result; | |
f37d3c75 JH |
1079 | struct commit **array; |
1080 | int num_head, i; | |
98cf9c3b JH |
1081 | |
1082 | if (!heads) | |
1083 | return NULL; | |
1084 | ||
f37d3c75 JH |
1085 | /* Uniquify */ |
1086 | for (p = heads; p; p = p->next) | |
1087 | p->item->object.flags &= ~STALE; | |
1088 | for (p = heads, num_head = 0; p; p = p->next) { | |
1089 | if (p->item->object.flags & STALE) | |
711f6b29 | 1090 | continue; |
f37d3c75 JH |
1091 | p->item->object.flags |= STALE; |
1092 | num_head++; | |
1093 | } | |
c4a7b009 | 1094 | array = xcalloc(num_head, sizeof(*array)); |
f37d3c75 JH |
1095 | for (p = heads, i = 0; p; p = p->next) { |
1096 | if (p->item->object.flags & STALE) { | |
1097 | array[i++] = p->item; | |
1098 | p->item->object.flags &= ~STALE; | |
98cf9c3b | 1099 | } |
98cf9c3b | 1100 | } |
f37d3c75 JH |
1101 | num_head = remove_redundant(array, num_head); |
1102 | for (i = 0; i < num_head; i++) | |
1103 | tail = &commit_list_insert(array[i], tail)->next; | |
cb7b29eb | 1104 | free(array); |
98cf9c3b JH |
1105 | return result; |
1106 | } | |
40d52ff7 | 1107 | |
4da72644 MÅ |
1108 | void reduce_heads_replace(struct commit_list **heads) |
1109 | { | |
1110 | struct commit_list *result = reduce_heads(*heads); | |
1111 | free_commit_list(*heads); | |
1112 | *heads = result; | |
1113 | } | |
1114 | ||
ba3c69a9 JH |
1115 | static const char gpg_sig_header[] = "gpgsig"; |
1116 | static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1; | |
1117 | ||
1118 | static int do_sign_commit(struct strbuf *buf, const char *keyid) | |
1119 | { | |
1120 | struct strbuf sig = STRBUF_INIT; | |
1121 | int inspos, copypos; | |
3324dd8f | 1122 | const char *eoh; |
ba3c69a9 JH |
1123 | |
1124 | /* find the end of the header */ | |
3324dd8f JS |
1125 | eoh = strstr(buf->buf, "\n\n"); |
1126 | if (!eoh) | |
1127 | inspos = buf->len; | |
1128 | else | |
1129 | inspos = eoh - buf->buf + 1; | |
ba3c69a9 JH |
1130 | |
1131 | if (!keyid || !*keyid) | |
1132 | keyid = get_signing_key(); | |
1133 | if (sign_buffer(buf, &sig, keyid)) { | |
1134 | strbuf_release(&sig); | |
1135 | return -1; | |
1136 | } | |
1137 | ||
1138 | for (copypos = 0; sig.buf[copypos]; ) { | |
1139 | const char *bol = sig.buf + copypos; | |
1140 | const char *eol = strchrnul(bol, '\n'); | |
1141 | int len = (eol - bol) + !!*eol; | |
1142 | ||
1143 | if (!copypos) { | |
1144 | strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len); | |
1145 | inspos += gpg_sig_header_len; | |
1146 | } | |
1147 | strbuf_insert(buf, inspos++, " ", 1); | |
1148 | strbuf_insert(buf, inspos, bol, len); | |
1149 | inspos += len; | |
1150 | copypos += len; | |
1151 | } | |
1152 | strbuf_release(&sig); | |
1153 | return 0; | |
1154 | } | |
1155 | ||
218aa3a6 | 1156 | int parse_signed_commit(const struct commit *commit, |
0c37f1fc JH |
1157 | struct strbuf *payload, struct strbuf *signature) |
1158 | { | |
218aa3a6 | 1159 | |
0c37f1fc | 1160 | unsigned long size; |
218aa3a6 | 1161 | const char *buffer = get_commit_buffer(commit, &size); |
0c37f1fc | 1162 | int in_signature, saw_signature = -1; |
218aa3a6 | 1163 | const char *line, *tail; |
0c37f1fc JH |
1164 | |
1165 | line = buffer; | |
1166 | tail = buffer + size; | |
1167 | in_signature = 0; | |
1168 | saw_signature = 0; | |
1169 | while (line < tail) { | |
1170 | const char *sig = NULL; | |
218aa3a6 | 1171 | const char *next = memchr(line, '\n', tail - line); |
0c37f1fc JH |
1172 | |
1173 | next = next ? next + 1 : tail; | |
1174 | if (in_signature && line[0] == ' ') | |
1175 | sig = line + 1; | |
59556548 | 1176 | else if (starts_with(line, gpg_sig_header) && |
0c37f1fc JH |
1177 | line[gpg_sig_header_len] == ' ') |
1178 | sig = line + gpg_sig_header_len + 1; | |
1179 | if (sig) { | |
1180 | strbuf_add(signature, sig, next - sig); | |
1181 | saw_signature = 1; | |
1182 | in_signature = 1; | |
1183 | } else { | |
1184 | if (*line == '\n') | |
1185 | /* dump the whole remainder of the buffer */ | |
1186 | next = tail; | |
1187 | strbuf_add(payload, line, next - line); | |
1188 | in_signature = 0; | |
1189 | } | |
1190 | line = next; | |
1191 | } | |
218aa3a6 | 1192 | unuse_commit_buffer(commit, buffer); |
0c37f1fc JH |
1193 | return saw_signature; |
1194 | } | |
1195 | ||
0b05ab6f CC |
1196 | int remove_signature(struct strbuf *buf) |
1197 | { | |
1198 | const char *line = buf->buf; | |
1199 | const char *tail = buf->buf + buf->len; | |
1200 | int in_signature = 0; | |
1201 | const char *sig_start = NULL; | |
1202 | const char *sig_end = NULL; | |
1203 | ||
1204 | while (line < tail) { | |
1205 | const char *next = memchr(line, '\n', tail - line); | |
1206 | next = next ? next + 1 : tail; | |
1207 | ||
1208 | if (in_signature && line[0] == ' ') | |
1209 | sig_end = next; | |
1210 | else if (starts_with(line, gpg_sig_header) && | |
1211 | line[gpg_sig_header_len] == ' ') { | |
1212 | sig_start = line; | |
1213 | sig_end = next; | |
1214 | in_signature = 1; | |
1215 | } else { | |
1216 | if (*line == '\n') | |
1217 | /* dump the whole remainder of the buffer */ | |
1218 | next = tail; | |
1219 | in_signature = 0; | |
1220 | } | |
1221 | line = next; | |
1222 | } | |
1223 | ||
1224 | if (sig_start) | |
1225 | strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start); | |
1226 | ||
1227 | return sig_start != NULL; | |
1228 | } | |
1229 | ||
5231c633 JH |
1230 | static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail) |
1231 | { | |
1232 | struct merge_remote_desc *desc; | |
1233 | struct commit_extra_header *mergetag; | |
1234 | char *buf; | |
1235 | unsigned long size, len; | |
1236 | enum object_type type; | |
1237 | ||
1238 | desc = merge_remote_util(parent); | |
1239 | if (!desc || !desc->obj) | |
1240 | return; | |
b4f5aca4 | 1241 | buf = read_object_file(&desc->obj->oid, &type, &size); |
5231c633 JH |
1242 | if (!buf || type != OBJ_TAG) |
1243 | goto free_return; | |
1244 | len = parse_signature(buf, size); | |
1245 | if (size == len) | |
1246 | goto free_return; | |
1247 | /* | |
1248 | * We could verify this signature and either omit the tag when | |
1249 | * it does not validate, but the integrator may not have the | |
1250 | * public key of the signer of the tag he is merging, while a | |
1251 | * later auditor may have it while auditing, so let's not run | |
1252 | * verify-signed-buffer here for now... | |
1253 | * | |
1254 | * if (verify_signed_buffer(buf, len, buf + len, size - len, ...)) | |
1255 | * warn("warning: signed tag unverified."); | |
1256 | */ | |
1257 | mergetag = xcalloc(1, sizeof(*mergetag)); | |
1258 | mergetag->key = xstrdup("mergetag"); | |
1259 | mergetag->value = buf; | |
1260 | mergetag->len = size; | |
1261 | ||
1262 | **tail = mergetag; | |
1263 | *tail = &mergetag->next; | |
1264 | return; | |
1265 | ||
1266 | free_return: | |
1267 | free(buf); | |
1268 | } | |
1269 | ||
434060ec | 1270 | int check_commit_signature(const struct commit *commit, struct signature_check *sigc) |
ffb6d7d5 SG |
1271 | { |
1272 | struct strbuf payload = STRBUF_INIT; | |
1273 | struct strbuf signature = STRBUF_INIT; | |
434060ec | 1274 | int ret = 1; |
ffb6d7d5 SG |
1275 | |
1276 | sigc->result = 'N'; | |
1277 | ||
218aa3a6 | 1278 | if (parse_signed_commit(commit, &payload, &signature) <= 0) |
ffb6d7d5 | 1279 | goto out; |
434060ec | 1280 | ret = check_signature(payload.buf, payload.len, signature.buf, |
1281 | signature.len, sigc); | |
ffb6d7d5 SG |
1282 | |
1283 | out: | |
ffb6d7d5 SG |
1284 | strbuf_release(&payload); |
1285 | strbuf_release(&signature); | |
434060ec | 1286 | |
1287 | return ret; | |
ffb6d7d5 SG |
1288 | } |
1289 | ||
1290 | ||
1291 | ||
5231c633 JH |
1292 | void append_merge_tag_headers(struct commit_list *parents, |
1293 | struct commit_extra_header ***tail) | |
1294 | { | |
1295 | while (parents) { | |
1296 | struct commit *parent = parents->item; | |
1297 | handle_signed_tag(parent, tail); | |
1298 | parents = parents->next; | |
1299 | } | |
1300 | } | |
1301 | ||
1302 | static void add_extra_header(struct strbuf *buffer, | |
1303 | struct commit_extra_header *extra) | |
1304 | { | |
1305 | strbuf_addstr(buffer, extra->key); | |
ed7a42a0 JH |
1306 | if (extra->len) |
1307 | strbuf_add_lines(buffer, " ", extra->value, extra->len); | |
1308 | else | |
1309 | strbuf_addch(buffer, '\n'); | |
1310 | } | |
1311 | ||
c871a1d1 JH |
1312 | struct commit_extra_header *read_commit_extra_headers(struct commit *commit, |
1313 | const char **exclude) | |
ed7a42a0 JH |
1314 | { |
1315 | struct commit_extra_header *extra = NULL; | |
1316 | unsigned long size; | |
218aa3a6 JK |
1317 | const char *buffer = get_commit_buffer(commit, &size); |
1318 | extra = read_commit_extra_header_lines(buffer, size, exclude); | |
1319 | unuse_commit_buffer(commit, buffer); | |
ed7a42a0 JH |
1320 | return extra; |
1321 | } | |
1322 | ||
fef461ea | 1323 | int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data) |
063da62b CC |
1324 | { |
1325 | struct commit_extra_header *extra, *to_free; | |
fef461ea | 1326 | int res = 0; |
063da62b CC |
1327 | |
1328 | to_free = read_commit_extra_headers(commit, NULL); | |
fef461ea | 1329 | for (extra = to_free; !res && extra; extra = extra->next) { |
063da62b CC |
1330 | if (strcmp(extra->key, "mergetag")) |
1331 | continue; /* not a merge tag */ | |
fef461ea | 1332 | res = fn(commit, extra, data); |
063da62b CC |
1333 | } |
1334 | free_commit_extra_headers(to_free); | |
fef461ea | 1335 | return res; |
063da62b CC |
1336 | } |
1337 | ||
ed7a42a0 JH |
1338 | static inline int standard_header_field(const char *field, size_t len) |
1339 | { | |
b072504c RS |
1340 | return ((len == 4 && !memcmp(field, "tree", 4)) || |
1341 | (len == 6 && !memcmp(field, "parent", 6)) || | |
1342 | (len == 6 && !memcmp(field, "author", 6)) || | |
1343 | (len == 9 && !memcmp(field, "committer", 9)) || | |
1344 | (len == 8 && !memcmp(field, "encoding", 8))); | |
ed7a42a0 JH |
1345 | } |
1346 | ||
c871a1d1 JH |
1347 | static int excluded_header_field(const char *field, size_t len, const char **exclude) |
1348 | { | |
1349 | if (!exclude) | |
1350 | return 0; | |
1351 | ||
1352 | while (*exclude) { | |
1353 | size_t xlen = strlen(*exclude); | |
b072504c | 1354 | if (len == xlen && !memcmp(field, *exclude, xlen)) |
c871a1d1 JH |
1355 | return 1; |
1356 | exclude++; | |
1357 | } | |
1358 | return 0; | |
1359 | } | |
1360 | ||
82a75299 JH |
1361 | static struct commit_extra_header *read_commit_extra_header_lines( |
1362 | const char *buffer, size_t size, | |
1363 | const char **exclude) | |
ed7a42a0 JH |
1364 | { |
1365 | struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL; | |
1366 | const char *line, *next, *eof, *eob; | |
1367 | struct strbuf buf = STRBUF_INIT; | |
1368 | ||
1369 | for (line = buffer, eob = line + size; | |
1370 | line < eob && *line != '\n'; | |
1371 | line = next) { | |
1372 | next = memchr(line, '\n', eob - line); | |
1373 | next = next ? next + 1 : eob; | |
1374 | if (*line == ' ') { | |
1375 | /* continuation */ | |
1376 | if (it) | |
1377 | strbuf_add(&buf, line + 1, next - (line + 1)); | |
1378 | continue; | |
1379 | } | |
1380 | if (it) | |
1381 | it->value = strbuf_detach(&buf, &it->len); | |
1382 | strbuf_reset(&buf); | |
1383 | it = NULL; | |
1384 | ||
50a01cc4 RS |
1385 | eof = memchr(line, ' ', next - line); |
1386 | if (!eof) | |
ed7a42a0 | 1387 | eof = next; |
b072504c RS |
1388 | else if (standard_header_field(line, eof - line) || |
1389 | excluded_header_field(line, eof - line, exclude)) | |
ed7a42a0 JH |
1390 | continue; |
1391 | ||
1392 | it = xcalloc(1, sizeof(*it)); | |
1393 | it->key = xmemdupz(line, eof-line); | |
1394 | *tail = it; | |
1395 | tail = &it->next; | |
1396 | if (eof + 1 < next) | |
1397 | strbuf_add(&buf, eof + 1, next - (eof + 1)); | |
1398 | } | |
1399 | if (it) | |
1400 | it->value = strbuf_detach(&buf, &it->len); | |
1401 | return extra; | |
5231c633 JH |
1402 | } |
1403 | ||
1404 | void free_commit_extra_headers(struct commit_extra_header *extra) | |
1405 | { | |
1406 | while (extra) { | |
1407 | struct commit_extra_header *next = extra->next; | |
1408 | free(extra->key); | |
1409 | free(extra->value); | |
1410 | free(extra); | |
1411 | extra = next; | |
1412 | } | |
1413 | } | |
1414 | ||
5078f344 PO |
1415 | int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree, |
1416 | struct commit_list *parents, struct object_id *ret, | |
ba3c69a9 | 1417 | const char *author, const char *sign_commit) |
5231c633 JH |
1418 | { |
1419 | struct commit_extra_header *extra = NULL, **tail = &extra; | |
1420 | int result; | |
1421 | ||
1422 | append_merge_tag_headers(parents, &tail); | |
3ffefb54 | 1423 | result = commit_tree_extended(msg, msg_len, tree, parents, ret, |
ba3c69a9 | 1424 | author, sign_commit, extra); |
5231c633 JH |
1425 | free_commit_extra_headers(extra); |
1426 | return result; | |
1427 | } | |
1428 | ||
08a94a14 LT |
1429 | static int find_invalid_utf8(const char *buf, int len) |
1430 | { | |
1431 | int offset = 0; | |
e82bd6cc | 1432 | static const unsigned int max_codepoint[] = { |
1433 | 0x7f, 0x7ff, 0xffff, 0x10ffff | |
1434 | }; | |
08a94a14 LT |
1435 | |
1436 | while (len) { | |
1437 | unsigned char c = *buf++; | |
1438 | int bytes, bad_offset; | |
28110d4b | 1439 | unsigned int codepoint; |
e82bd6cc | 1440 | unsigned int min_val, max_val; |
08a94a14 LT |
1441 | |
1442 | len--; | |
1443 | offset++; | |
1444 | ||
1445 | /* Simple US-ASCII? No worries. */ | |
1446 | if (c < 0x80) | |
1447 | continue; | |
1448 | ||
1449 | bad_offset = offset-1; | |
1450 | ||
1451 | /* | |
1452 | * Count how many more high bits set: that's how | |
1453 | * many more bytes this sequence should have. | |
1454 | */ | |
1455 | bytes = 0; | |
1456 | while (c & 0x40) { | |
1457 | c <<= 1; | |
1458 | bytes++; | |
1459 | } | |
1460 | ||
28110d4b | 1461 | /* |
1462 | * Must be between 1 and 3 more bytes. Longer sequences result in | |
1463 | * codepoints beyond U+10FFFF, which are guaranteed never to exist. | |
1464 | */ | |
1465 | if (bytes < 1 || 3 < bytes) | |
08a94a14 LT |
1466 | return bad_offset; |
1467 | ||
1468 | /* Do we *have* that many bytes? */ | |
1469 | if (len < bytes) | |
1470 | return bad_offset; | |
1471 | ||
e82bd6cc | 1472 | /* |
1473 | * Place the encoded bits at the bottom of the value and compute the | |
1474 | * valid range. | |
1475 | */ | |
28110d4b | 1476 | codepoint = (c & 0x7f) >> bytes; |
e82bd6cc | 1477 | min_val = max_codepoint[bytes-1] + 1; |
1478 | max_val = max_codepoint[bytes]; | |
28110d4b | 1479 | |
08a94a14 LT |
1480 | offset += bytes; |
1481 | len -= bytes; | |
1482 | ||
1483 | /* And verify that they are good continuation bytes */ | |
1484 | do { | |
28110d4b | 1485 | codepoint <<= 6; |
1486 | codepoint |= *buf & 0x3f; | |
08a94a14 LT |
1487 | if ((*buf++ & 0xc0) != 0x80) |
1488 | return bad_offset; | |
1489 | } while (--bytes); | |
1490 | ||
e82bd6cc | 1491 | /* Reject codepoints that are out of range for the sequence length. */ |
1492 | if (codepoint < min_val || codepoint > max_val) | |
28110d4b | 1493 | return bad_offset; |
1494 | /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */ | |
1495 | if ((codepoint & 0x1ff800) == 0xd800) | |
1496 | return bad_offset; | |
81050ac6 | 1497 | /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */ |
dc773a67 | 1498 | if ((codepoint & 0xfffe) == 0xfffe) |
81050ac6 PK |
1499 | return bad_offset; |
1500 | /* So are anything in the range U+FDD0..U+FDEF. */ | |
1501 | if (codepoint >= 0xfdd0 && codepoint <= 0xfdef) | |
28110d4b | 1502 | return bad_offset; |
08a94a14 LT |
1503 | } |
1504 | return -1; | |
1505 | } | |
1506 | ||
1507 | /* | |
1508 | * This verifies that the buffer is in proper utf8 format. | |
1509 | * | |
1510 | * If it isn't, it assumes any non-utf8 characters are Latin1, | |
1511 | * and does the conversion. | |
08a94a14 LT |
1512 | */ |
1513 | static int verify_utf8(struct strbuf *buf) | |
1514 | { | |
1515 | int ok = 1; | |
1516 | long pos = 0; | |
1517 | ||
1518 | for (;;) { | |
1519 | int bad; | |
1520 | unsigned char c; | |
1521 | unsigned char replace[2]; | |
1522 | ||
1523 | bad = find_invalid_utf8(buf->buf + pos, buf->len - pos); | |
1524 | if (bad < 0) | |
1525 | return ok; | |
1526 | pos += bad; | |
1527 | ok = 0; | |
1528 | c = buf->buf[pos]; | |
1529 | strbuf_remove(buf, pos, 1); | |
1530 | ||
1531 | /* We know 'c' must be in the range 128-255 */ | |
1532 | replace[0] = 0xc0 + (c >> 6); | |
1533 | replace[1] = 0x80 + (c & 0x3f); | |
1534 | strbuf_insert(buf, pos, replace, 2); | |
1535 | pos += 2; | |
1536 | } | |
1537 | } | |
1538 | ||
40d52ff7 | 1539 | static const char commit_utf8_warn[] = |
4fa4b315 VA |
1540 | N_("Warning: commit message did not conform to UTF-8.\n" |
1541 | "You may want to amend it after fixing the message, or set the config\n" | |
1542 | "variable i18n.commitencoding to the encoding your project uses.\n"); | |
40d52ff7 | 1543 | |
3ffefb54 | 1544 | int commit_tree_extended(const char *msg, size_t msg_len, |
5078f344 PO |
1545 | const struct object_id *tree, |
1546 | struct commit_list *parents, struct object_id *ret, | |
ba3c69a9 JH |
1547 | const char *author, const char *sign_commit, |
1548 | struct commit_extra_header *extra) | |
40d52ff7 JK |
1549 | { |
1550 | int result; | |
1551 | int encoding_is_utf8; | |
1552 | struct strbuf buffer; | |
1553 | ||
e816caa0 | 1554 | assert_oid_type(tree, OBJ_TREE); |
40d52ff7 | 1555 | |
3ffefb54 | 1556 | if (memchr(msg, '\0', msg_len)) |
37576c14 NTND |
1557 | return error("a NUL byte in commit log message not allowed."); |
1558 | ||
40d52ff7 JK |
1559 | /* Not having i18n.commitencoding is the same as having utf-8 */ |
1560 | encoding_is_utf8 = is_encoding_utf8(git_commit_encoding); | |
1561 | ||
1562 | strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */ | |
5078f344 | 1563 | strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree)); |
40d52ff7 JK |
1564 | |
1565 | /* | |
1566 | * NOTE! This ordering means that the same exact tree merged with a | |
1567 | * different order of parents will be a _different_ changeset even | |
1568 | * if everything else stays the same. | |
1569 | */ | |
1570 | while (parents) { | |
e510ab89 | 1571 | struct commit *parent = pop_commit(&parents); |
40d52ff7 | 1572 | strbuf_addf(&buffer, "parent %s\n", |
f2fd0760 | 1573 | oid_to_hex(&parent->object.oid)); |
40d52ff7 JK |
1574 | } |
1575 | ||
1576 | /* Person/date information */ | |
1577 | if (!author) | |
f9bc573f | 1578 | author = git_author_info(IDENT_STRICT); |
40d52ff7 | 1579 | strbuf_addf(&buffer, "author %s\n", author); |
f9bc573f | 1580 | strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT)); |
40d52ff7 JK |
1581 | if (!encoding_is_utf8) |
1582 | strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding); | |
5231c633 JH |
1583 | |
1584 | while (extra) { | |
1585 | add_extra_header(&buffer, extra); | |
1586 | extra = extra->next; | |
1587 | } | |
40d52ff7 JK |
1588 | strbuf_addch(&buffer, '\n'); |
1589 | ||
1590 | /* And add the comment */ | |
3ffefb54 | 1591 | strbuf_add(&buffer, msg, msg_len); |
40d52ff7 JK |
1592 | |
1593 | /* And check the encoding */ | |
08a94a14 | 1594 | if (encoding_is_utf8 && !verify_utf8(&buffer)) |
4fa4b315 | 1595 | fprintf(stderr, _(commit_utf8_warn)); |
40d52ff7 | 1596 | |
e505146d RS |
1597 | if (sign_commit && do_sign_commit(&buffer, sign_commit)) { |
1598 | result = -1; | |
1599 | goto out; | |
1600 | } | |
ba3c69a9 | 1601 | |
a09c985e | 1602 | result = write_object_file(buffer.buf, buffer.len, commit_type, ret); |
e505146d | 1603 | out: |
40d52ff7 JK |
1604 | strbuf_release(&buffer); |
1605 | return result; | |
1606 | } | |
ae8e4c9c | 1607 | |
beb518c9 RS |
1608 | void set_merge_remote_desc(struct commit *commit, |
1609 | const char *name, struct object *obj) | |
1610 | { | |
1611 | struct merge_remote_desc *desc; | |
5447a76a | 1612 | FLEX_ALLOC_STR(desc, name, name); |
beb518c9 | 1613 | desc->obj = obj; |
beb518c9 RS |
1614 | commit->util = desc; |
1615 | } | |
1616 | ||
ae8e4c9c JH |
1617 | struct commit *get_merge_parent(const char *name) |
1618 | { | |
1619 | struct object *obj; | |
1620 | struct commit *commit; | |
7683e2e6 | 1621 | struct object_id oid; |
e82caf38 | 1622 | if (get_oid(name, &oid)) |
ae8e4c9c | 1623 | return NULL; |
c251c83d | 1624 | obj = parse_object(&oid); |
ae8e4c9c | 1625 | commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT); |
beb518c9 RS |
1626 | if (commit && !commit->util) |
1627 | set_merge_remote_desc(commit, name, obj); | |
ae8e4c9c JH |
1628 | return commit; |
1629 | } | |
89b5f1d9 RS |
1630 | |
1631 | /* | |
1632 | * Append a commit to the end of the commit_list. | |
1633 | * | |
1634 | * next starts by pointing to the variable that holds the head of an | |
1635 | * empty commit_list, and is updated to point to the "next" field of | |
1636 | * the last item on the list as new commits are appended. | |
1637 | * | |
1638 | * Usage example: | |
1639 | * | |
1640 | * struct commit_list *list; | |
1641 | * struct commit_list **next = &list; | |
1642 | * | |
1643 | * next = commit_list_append(c1, next); | |
1644 | * next = commit_list_append(c2, next); | |
1645 | * assert(commit_list_count(list) == 2); | |
1646 | * return list; | |
1647 | */ | |
1648 | struct commit_list **commit_list_append(struct commit *commit, | |
1649 | struct commit_list **next) | |
1650 | { | |
258c03eb BW |
1651 | struct commit_list *new_commit = xmalloc(sizeof(struct commit_list)); |
1652 | new_commit->item = commit; | |
1653 | *next = new_commit; | |
1654 | new_commit->next = NULL; | |
1655 | return &new_commit->next; | |
89b5f1d9 | 1656 | } |
efc7df45 | 1657 | |
fe6eb7f2 JK |
1658 | const char *find_commit_header(const char *msg, const char *key, size_t *out_len) |
1659 | { | |
1660 | int key_len = strlen(key); | |
1661 | const char *line = msg; | |
1662 | ||
1663 | while (line) { | |
1664 | const char *eol = strchrnul(line, '\n'); | |
1665 | ||
1666 | if (line == eol) | |
1667 | return NULL; | |
1668 | ||
1669 | if (eol - line > key_len && | |
1670 | !strncmp(line, key, key_len) && | |
1671 | line[key_len] == ' ') { | |
1672 | *out_len = eol - line - key_len - 1; | |
1673 | return line + key_len + 1; | |
1674 | } | |
1675 | line = *eol ? eol + 1 : NULL; | |
1676 | } | |
1677 | return NULL; | |
1678 | } | |
0ed8a4e1 | 1679 | |
8c384589 | 1680 | /* |
710714aa | 1681 | * Inspect the given string and determine the true "end" of the log message, in |
8c384589 | 1682 | * order to find where to put a new Signed-off-by: line. Ignored are |
d76650b8 BM |
1683 | * trailing comment lines and blank lines. To support "git commit -s |
1684 | * --amend" on an existing commit, we also ignore "Conflicts:". To | |
1685 | * support "git commit -v", we truncate at cut lines. | |
8c384589 CC |
1686 | * |
1687 | * Returns the number of bytes from the tail to ignore, to be fed as | |
1688 | * the second parameter to append_signoff(). | |
1689 | */ | |
710714aa | 1690 | int ignore_non_trailer(const char *buf, size_t len) |
8c384589 CC |
1691 | { |
1692 | int boc = 0; | |
1693 | int bol = 0; | |
1694 | int in_old_conflicts_block = 0; | |
d76650b8 | 1695 | size_t cutoff = wt_status_locate_end(buf, len); |
8c384589 | 1696 | |
d76650b8 | 1697 | while (bol < cutoff) { |
710714aa | 1698 | const char *next_line = memchr(buf + bol, '\n', len - bol); |
8c384589 | 1699 | |
710714aa JT |
1700 | if (!next_line) |
1701 | next_line = buf + len; | |
8c384589 CC |
1702 | else |
1703 | next_line++; | |
1704 | ||
710714aa | 1705 | if (buf[bol] == comment_line_char || buf[bol] == '\n') { |
8c384589 CC |
1706 | /* is this the first of the run of comments? */ |
1707 | if (!boc) | |
1708 | boc = bol; | |
1709 | /* otherwise, it is just continuing */ | |
710714aa | 1710 | } else if (starts_with(buf + bol, "Conflicts:\n")) { |
8c384589 CC |
1711 | in_old_conflicts_block = 1; |
1712 | if (!boc) | |
1713 | boc = bol; | |
710714aa | 1714 | } else if (in_old_conflicts_block && buf[bol] == '\t') { |
8c384589 CC |
1715 | ; /* a pathname in the conflicts block */ |
1716 | } else if (boc) { | |
1717 | /* the previous was not trailing comment */ | |
1718 | boc = 0; | |
1719 | in_old_conflicts_block = 0; | |
1720 | } | |
710714aa | 1721 | bol = next_line - buf; |
8c384589 | 1722 | } |
d76650b8 | 1723 | return boc ? len - boc : len - cutoff; |
8c384589 | 1724 | } |