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