Coverage Report

Created: 2024-09-16 06:12

/src/git/commit.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "tag.h"
5
#include "commit.h"
6
#include "commit-graph.h"
7
#include "environment.h"
8
#include "gettext.h"
9
#include "hex.h"
10
#include "repository.h"
11
#include "object-name.h"
12
#include "object-store-ll.h"
13
#include "utf8.h"
14
#include "diff.h"
15
#include "revision.h"
16
#include "notes.h"
17
#include "alloc.h"
18
#include "gpg-interface.h"
19
#include "mergesort.h"
20
#include "commit-slab.h"
21
#include "prio-queue.h"
22
#include "hash-lookup.h"
23
#include "wt-status.h"
24
#include "advice.h"
25
#include "refs.h"
26
#include "commit-reach.h"
27
#include "setup.h"
28
#include "shallow.h"
29
#include "tree.h"
30
#include "hook.h"
31
#include "parse.h"
32
#include "object-file-convert.h"
33
34
static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
35
36
int save_commit_buffer = 1;
37
int no_graft_file_deprecated_advice;
38
39
const char *commit_type = "commit";
40
41
struct commit *lookup_commit_reference_gently(struct repository *r,
42
    const struct object_id *oid, int quiet)
43
10.6k
{
44
10.6k
  struct object *obj = deref_tag(r,
45
10.6k
               parse_object(r, oid),
46
10.6k
               NULL, 0);
47
48
10.6k
  if (!obj)
49
0
    return NULL;
50
10.6k
  return object_as_type(obj, OBJ_COMMIT, quiet);
51
10.6k
}
52
53
struct commit *lookup_commit_reference(struct repository *r, const struct object_id *oid)
54
10.6k
{
55
10.6k
  return lookup_commit_reference_gently(r, oid, 0);
56
10.6k
}
57
58
struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name)
59
7.91k
{
60
7.91k
  struct commit *c = lookup_commit_reference(the_repository, oid);
61
7.91k
  if (!c)
62
0
    die(_("could not parse %s"), ref_name);
63
7.91k
  if (!oideq(oid, &c->object.oid)) {
64
0
    warning(_("%s %s is not a commit!"),
65
0
      ref_name, oid_to_hex(oid));
66
0
  }
67
7.91k
  return c;
68
7.91k
}
69
70
struct commit *lookup_commit_object(struct repository *r,
71
            const struct object_id *oid)
72
0
{
73
0
  struct object *obj = parse_object(r, oid);
74
0
  return obj ? object_as_type(obj, OBJ_COMMIT, 0) : NULL;
75
76
0
}
77
78
struct commit *lookup_commit(struct repository *r, const struct object_id *oid)
79
26.4k
{
80
26.4k
  struct object *obj = lookup_object(r, oid);
81
26.4k
  if (!obj)
82
9.30k
    return create_object(r, oid, alloc_commit_node(r));
83
17.1k
  return object_as_type(obj, OBJ_COMMIT, 0);
84
26.4k
}
85
86
struct commit *lookup_commit_reference_by_name(const char *name)
87
0
{
88
0
  return lookup_commit_reference_by_name_gently(name, 0);
89
0
}
90
91
struct commit *lookup_commit_reference_by_name_gently(const char *name,
92
                  int quiet)
93
0
{
94
0
  struct object_id oid;
95
0
  struct commit *commit;
96
97
0
  if (repo_get_oid_committish(the_repository, name, &oid))
98
0
    return NULL;
99
0
  commit = lookup_commit_reference_gently(the_repository, &oid, quiet);
100
0
  if (repo_parse_commit(the_repository, commit))
101
0
    return NULL;
102
0
  return commit;
103
0
}
104
105
static timestamp_t parse_commit_date(const char *buf, const char *tail)
106
9.30k
{
107
9.30k
  const char *dateptr;
108
9.30k
  const char *eol;
109
110
9.30k
  if (buf + 6 >= tail)
111
0
    return 0;
112
9.30k
  if (memcmp(buf, "author", 6))
113
0
    return 0;
114
427k
  while (buf < tail && *buf++ != '\n')
115
418k
    /* nada */;
116
9.30k
  if (buf + 9 >= tail)
117
0
    return 0;
118
9.30k
  if (memcmp(buf, "committer", 9))
119
0
    return 0;
120
121
  /*
122
   * Jump to end-of-line so that we can walk backwards to find the
123
   * end-of-email ">". This is more forgiving of malformed cases
124
   * because unexpected characters tend to be in the name and email
125
   * fields.
126
   */
127
9.30k
  eol = memchr(buf, '\n', tail - buf);
128
9.30k
  if (!eol)
129
0
    return 0;
130
9.30k
  dateptr = eol;
131
167k
  while (dateptr > buf && dateptr[-1] != '>')
132
158k
    dateptr--;
133
9.30k
  if (dateptr == buf)
134
0
    return 0;
135
136
  /*
137
   * Trim leading whitespace, but make sure we have at least one
138
   * non-whitespace character, as parse_timestamp() will otherwise walk
139
   * right past the newline we found in "eol" when skipping whitespace
140
   * itself.
141
   *
142
   * In theory it would be sufficient to allow any character not matched
143
   * by isspace(), but there's a catch: our isspace() does not
144
   * necessarily match the behavior of parse_timestamp(), as the latter
145
   * is implemented by system routines which match more exotic control
146
   * codes, or even locale-dependent sequences.
147
   *
148
   * Since we expect the timestamp to be a number, we can check for that.
149
   * Anything else (e.g., a non-numeric token like "foo") would just
150
   * cause parse_timestamp() to return 0 anyway.
151
   */
152
18.6k
  while (dateptr < eol && isspace(*dateptr))
153
9.30k
    dateptr++;
154
9.30k
  if (!isdigit(*dateptr) && *dateptr != '-')
155
0
    return 0;
156
157
  /*
158
   * We know there is at least one digit (or dash), so we'll begin
159
   * parsing there and stop at worst case at eol.
160
   *
161
   * Note that we may feed parse_timestamp() extra characters here if the
162
   * commit is malformed, and it will parse as far as it can. For
163
   * example, "123foo456" would return "123". That might be questionable
164
   * (versus returning "0"), but it would help in a hypothetical case
165
   * like "123456+0100", where the whitespace from the timezone is
166
   * missing. Since such syntactic errors may be baked into history and
167
   * hard to correct now, let's err on trying to make our best guess
168
   * here, rather than insist on perfect syntax.
169
   */
170
9.30k
  return parse_timestamp(dateptr, NULL, 10);
171
9.30k
}
172
173
static const struct object_id *commit_graft_oid_access(size_t index, const void *table)
174
0
{
175
0
  const struct commit_graft * const *commit_graft_table = table;
176
0
  return &commit_graft_table[index]->oid;
177
0
}
178
179
int commit_graft_pos(struct repository *r, const struct object_id *oid)
180
9.30k
{
181
9.30k
  return oid_pos(oid, r->parsed_objects->grafts,
182
9.30k
           r->parsed_objects->grafts_nr,
183
9.30k
           commit_graft_oid_access);
184
9.30k
}
185
186
void unparse_commit(struct repository *r, const struct object_id *oid)
187
0
{
188
0
  struct commit *c = lookup_commit(r, oid);
189
190
0
  if (!c->object.parsed)
191
0
    return;
192
0
  free_commit_list(c->parents);
193
0
  c->parents = NULL;
194
0
  c->object.parsed = 0;
195
0
}
196
197
int register_commit_graft(struct repository *r, struct commit_graft *graft,
198
        int ignore_dups)
199
0
{
200
0
  int pos = commit_graft_pos(r, &graft->oid);
201
202
0
  if (0 <= pos) {
203
0
    if (ignore_dups)
204
0
      free(graft);
205
0
    else {
206
0
      free(r->parsed_objects->grafts[pos]);
207
0
      r->parsed_objects->grafts[pos] = graft;
208
0
    }
209
0
    return 1;
210
0
  }
211
0
  pos = -pos - 1;
212
0
  ALLOC_GROW(r->parsed_objects->grafts,
213
0
       r->parsed_objects->grafts_nr + 1,
214
0
       r->parsed_objects->grafts_alloc);
215
0
  r->parsed_objects->grafts_nr++;
216
0
  if (pos < r->parsed_objects->grafts_nr)
217
0
    memmove(r->parsed_objects->grafts + pos + 1,
218
0
      r->parsed_objects->grafts + pos,
219
0
      (r->parsed_objects->grafts_nr - pos - 1) *
220
0
      sizeof(*r->parsed_objects->grafts));
221
0
  r->parsed_objects->grafts[pos] = graft;
222
0
  unparse_commit(r, &graft->oid);
223
0
  return 0;
224
0
}
225
226
struct commit_graft *read_graft_line(struct strbuf *line)
227
0
{
228
  /* The format is just "Commit Parent1 Parent2 ...\n" */
229
0
  int i, phase;
230
0
  const char *tail = NULL;
231
0
  struct commit_graft *graft = NULL;
232
0
  struct object_id dummy_oid, *oid;
233
234
0
  strbuf_rtrim(line);
235
0
  if (!line->len || line->buf[0] == '#')
236
0
    return NULL;
237
  /*
238
   * phase 0 verifies line, counts hashes in line and allocates graft
239
   * phase 1 fills graft
240
   */
241
0
  for (phase = 0; phase < 2; phase++) {
242
0
    oid = graft ? &graft->oid : &dummy_oid;
243
0
    if (parse_oid_hex(line->buf, oid, &tail))
244
0
      goto bad_graft_data;
245
0
    for (i = 0; *tail != '\0'; i++) {
246
0
      oid = graft ? &graft->parent[i] : &dummy_oid;
247
0
      if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail))
248
0
        goto bad_graft_data;
249
0
    }
250
0
    if (!graft) {
251
0
      graft = xmalloc(st_add(sizeof(*graft),
252
0
                 st_mult(sizeof(struct object_id), i)));
253
0
      graft->nr_parent = i;
254
0
    }
255
0
  }
256
0
  return graft;
257
258
0
bad_graft_data:
259
0
  error("bad graft data: %s", line->buf);
260
0
  assert(!graft);
261
0
  return NULL;
262
0
}
263
264
static int read_graft_file(struct repository *r, const char *graft_file)
265
1.46k
{
266
1.46k
  FILE *fp = fopen_or_warn(graft_file, "r");
267
1.46k
  struct strbuf buf = STRBUF_INIT;
268
1.46k
  if (!fp)
269
1.46k
    return -1;
270
0
  if (!no_graft_file_deprecated_advice &&
271
0
      advice_enabled(ADVICE_GRAFT_FILE_DEPRECATED))
272
0
    advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n"
273
0
       "and will be removed in a future Git version.\n"
274
0
       "\n"
275
0
       "Please use \"git replace --convert-graft-file\"\n"
276
0
       "to convert the grafts into replace refs.\n"
277
0
       "\n"
278
0
       "Turn this message off by running\n"
279
0
       "\"git config advice.graftFileDeprecated false\""));
280
0
  while (!strbuf_getwholeline(&buf, fp, '\n')) {
281
    /* The format is just "Commit Parent1 Parent2 ...\n" */
282
0
    struct commit_graft *graft = read_graft_line(&buf);
283
0
    if (!graft)
284
0
      continue;
285
0
    if (register_commit_graft(r, graft, 1))
286
0
      error("duplicate graft data: %s", buf.buf);
287
0
  }
288
0
  fclose(fp);
289
0
  strbuf_release(&buf);
290
0
  return 0;
291
1.46k
}
292
293
void prepare_commit_graft(struct repository *r)
294
10.7k
{
295
10.7k
  char *graft_file;
296
297
10.7k
  if (r->parsed_objects->commit_graft_prepared)
298
9.30k
    return;
299
1.46k
  if (!startup_info->have_repository)
300
0
    return;
301
302
1.46k
  graft_file = get_graft_file(r);
303
1.46k
  read_graft_file(r, graft_file);
304
  /* make sure shallows are read */
305
1.46k
  is_repository_shallow(r);
306
1.46k
  r->parsed_objects->commit_graft_prepared = 1;
307
1.46k
}
308
309
struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid)
310
9.30k
{
311
9.30k
  int pos;
312
9.30k
  prepare_commit_graft(r);
313
9.30k
  pos = commit_graft_pos(r, oid);
314
9.30k
  if (pos < 0)
315
9.30k
    return NULL;
316
0
  return r->parsed_objects->grafts[pos];
317
9.30k
}
318
319
int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
320
0
{
321
0
  int i, ret;
322
0
  for (i = ret = 0; i < the_repository->parsed_objects->grafts_nr && !ret; i++)
323
0
    ret = fn(the_repository->parsed_objects->grafts[i], cb_data);
324
0
  return ret;
325
0
}
326
327
struct commit_buffer {
328
  void *buffer;
329
  unsigned long size;
330
};
331
define_commit_slab(buffer_slab, struct commit_buffer);
332
333
struct buffer_slab *allocate_commit_buffer_slab(void)
334
1.46k
{
335
1.46k
  struct buffer_slab *bs = xmalloc(sizeof(*bs));
336
1.46k
  init_buffer_slab(bs);
337
1.46k
  return bs;
338
1.46k
}
339
340
void free_commit_buffer_slab(struct buffer_slab *bs)
341
1.46k
{
342
1.46k
  clear_buffer_slab(bs);
343
1.46k
  free(bs);
344
1.46k
}
345
346
void set_commit_buffer(struct repository *r, struct commit *commit, void *buffer, unsigned long size)
347
9.30k
{
348
9.30k
  struct commit_buffer *v = buffer_slab_at(
349
9.30k
    r->parsed_objects->buffer_slab, commit);
350
9.30k
  v->buffer = buffer;
351
9.30k
  v->size = size;
352
9.30k
}
353
354
const void *get_cached_commit_buffer(struct repository *r, const struct commit *commit, unsigned long *sizep)
355
37.2k
{
356
37.2k
  struct commit_buffer *v = buffer_slab_peek(
357
37.2k
    r->parsed_objects->buffer_slab, commit);
358
37.2k
  if (!v) {
359
1.46k
    if (sizep)
360
0
      *sizep = 0;
361
1.46k
    return NULL;
362
1.46k
  }
363
35.7k
  if (sizep)
364
0
    *sizep = v->size;
365
35.7k
  return v->buffer;
366
37.2k
}
367
368
const void *repo_get_commit_buffer(struct repository *r,
369
           const struct commit *commit,
370
           unsigned long *sizep)
371
27.9k
{
372
27.9k
  const void *ret = get_cached_commit_buffer(r, commit, sizep);
373
27.9k
  if (!ret) {
374
0
    enum object_type type;
375
0
    unsigned long size;
376
0
    ret = repo_read_object_file(r, &commit->object.oid, &type, &size);
377
0
    if (!ret)
378
0
      die("cannot read commit object %s",
379
0
          oid_to_hex(&commit->object.oid));
380
0
    if (type != OBJ_COMMIT)
381
0
      die("expected commit for %s, got %s",
382
0
          oid_to_hex(&commit->object.oid), type_name(type));
383
0
    if (sizep)
384
0
      *sizep = size;
385
0
  }
386
27.9k
  return ret;
387
27.9k
}
388
389
void repo_unuse_commit_buffer(struct repository *r,
390
            const struct commit *commit,
391
            const void *buffer)
392
27.9k
{
393
27.9k
  struct commit_buffer *v = buffer_slab_peek(
394
27.9k
    r->parsed_objects->buffer_slab, commit);
395
27.9k
  if (!(v && v->buffer == buffer))
396
0
    free((void *)buffer);
397
27.9k
}
398
399
void free_commit_buffer(struct parsed_object_pool *pool, struct commit *commit)
400
9.30k
{
401
9.30k
  struct commit_buffer *v = buffer_slab_peek(
402
9.30k
    pool->buffer_slab, commit);
403
9.30k
  if (v) {
404
9.30k
    FREE_AND_NULL(v->buffer);
405
9.30k
    v->size = 0;
406
9.30k
  }
407
9.30k
}
408
409
static inline void set_commit_tree(struct commit *c, struct tree *t)
410
18.6k
{
411
18.6k
  c->maybe_tree = t;
412
18.6k
}
413
414
struct tree *repo_get_commit_tree(struct repository *r,
415
          const struct commit *commit)
416
48.6k
{
417
48.6k
  if (commit->maybe_tree || !commit->object.parsed)
418
48.6k
    return commit->maybe_tree;
419
420
0
  if (commit_graph_position(commit) != COMMIT_NOT_FROM_GRAPH)
421
0
    return get_commit_tree_in_graph(r, commit);
422
423
0
  return NULL;
424
0
}
425
426
struct object_id *get_commit_tree_oid(const struct commit *commit)
427
19.8k
{
428
19.8k
  struct tree *tree = repo_get_commit_tree(the_repository, commit);
429
19.8k
  return tree ? &tree->object.oid : NULL;
430
19.8k
}
431
432
void release_commit_memory(struct parsed_object_pool *pool, struct commit *c)
433
9.30k
{
434
9.30k
  set_commit_tree(c, NULL);
435
9.30k
  free_commit_buffer(pool, c);
436
9.30k
  c->index = 0;
437
9.30k
  free_commit_list(c->parents);
438
439
9.30k
  c->object.parsed = 0;
440
9.30k
}
441
442
const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep)
443
0
{
444
0
  struct commit_buffer *v = buffer_slab_peek(
445
0
    the_repository->parsed_objects->buffer_slab, commit);
446
0
  void *ret;
447
448
0
  if (!v) {
449
0
    if (sizep)
450
0
      *sizep = 0;
451
0
    return NULL;
452
0
  }
453
0
  ret = v->buffer;
454
0
  if (sizep)
455
0
    *sizep = v->size;
456
457
0
  v->buffer = NULL;
458
0
  v->size = 0;
459
0
  return ret;
460
0
}
461
462
int parse_commit_buffer(struct repository *r, struct commit *item, const void *buffer, unsigned long size, int check_graph)
463
9.30k
{
464
9.30k
  const char *tail = buffer;
465
9.30k
  const char *bufptr = buffer;
466
9.30k
  struct object_id parent;
467
9.30k
  struct commit_list **pptr;
468
9.30k
  struct commit_graft *graft;
469
9.30k
  const int tree_entry_len = the_hash_algo->hexsz + 5;
470
9.30k
  const int parent_entry_len = the_hash_algo->hexsz + 7;
471
9.30k
  struct tree *tree;
472
473
9.30k
  if (item->object.parsed)
474
0
    return 0;
475
  /*
476
   * Presumably this is leftover from an earlier failed parse;
477
   * clear it out in preparation for us re-parsing (we'll hit the
478
   * same error, but that's good, since it lets our caller know
479
   * the result cannot be trusted.
480
   */
481
9.30k
  free_commit_list(item->parents);
482
9.30k
  item->parents = NULL;
483
484
9.30k
  tail += size;
485
9.30k
  if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) ||
486
9.30k
      bufptr[tree_entry_len] != '\n')
487
0
    return error("bogus commit object %s", oid_to_hex(&item->object.oid));
488
9.30k
  if (get_oid_hex(bufptr + 5, &parent) < 0)
489
0
    return error("bad tree pointer in commit %s",
490
9.30k
           oid_to_hex(&item->object.oid));
491
9.30k
  tree = lookup_tree(r, &parent);
492
9.30k
  if (!tree)
493
0
    return error("bad tree pointer %s in commit %s",
494
9.30k
           oid_to_hex(&parent),
495
9.30k
           oid_to_hex(&item->object.oid));
496
9.30k
  set_commit_tree(item, tree);
497
9.30k
  bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */
498
9.30k
  pptr = &item->parents;
499
500
9.30k
  graft = lookup_commit_graft(r, &item->object.oid);
501
9.30k
  if (graft)
502
0
    r->parsed_objects->substituted_parent = 1;
503
17.1k
  while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) {
504
7.83k
    struct commit *new_parent;
505
506
7.83k
    if (tail <= bufptr + parent_entry_len + 1 ||
507
7.83k
        get_oid_hex(bufptr + 7, &parent) ||
508
7.83k
        bufptr[parent_entry_len] != '\n')
509
0
      return error("bad parents in commit %s", oid_to_hex(&item->object.oid));
510
7.83k
    bufptr += parent_entry_len + 1;
511
    /*
512
     * The clone is shallow if nr_parent < 0, and we must
513
     * not traverse its real parents even when we unhide them.
514
     */
515
7.83k
    if (graft && (graft->nr_parent < 0 || !grafts_keep_true_parents))
516
0
      continue;
517
7.83k
    new_parent = lookup_commit(r, &parent);
518
7.83k
    if (!new_parent)
519
0
      return error("bad parent %s in commit %s",
520
7.83k
             oid_to_hex(&parent),
521
7.83k
             oid_to_hex(&item->object.oid));
522
7.83k
    pptr = &commit_list_insert(new_parent, pptr)->next;
523
7.83k
  }
524
9.30k
  if (graft) {
525
0
    int i;
526
0
    struct commit *new_parent;
527
0
    for (i = 0; i < graft->nr_parent; i++) {
528
0
      new_parent = lookup_commit(r,
529
0
               &graft->parent[i]);
530
0
      if (!new_parent)
531
0
        return error("bad graft parent %s in commit %s",
532
0
               oid_to_hex(&graft->parent[i]),
533
0
               oid_to_hex(&item->object.oid));
534
0
      pptr = &commit_list_insert(new_parent, pptr)->next;
535
0
    }
536
0
  }
537
9.30k
  item->date = parse_commit_date(bufptr, tail);
538
539
9.30k
  if (check_graph)
540
9.30k
    load_commit_graph_info(r, item);
541
542
9.30k
  item->object.parsed = 1;
543
9.30k
  return 0;
544
9.30k
}
545
546
int repo_parse_commit_internal(struct repository *r,
547
             struct commit *item,
548
             int quiet_on_missing,
549
             int use_commit_graph)
550
37.1k
{
551
37.1k
  enum object_type type;
552
37.1k
  void *buffer;
553
37.1k
  unsigned long size;
554
37.1k
  struct object_info oi = {
555
37.1k
    .typep = &type,
556
37.1k
    .sizep = &size,
557
37.1k
    .contentp = &buffer,
558
37.1k
  };
559
  /*
560
   * Git does not support partial clones that exclude commits, so set
561
   * OBJECT_INFO_SKIP_FETCH_OBJECT to fail fast when an object is missing.
562
   */
563
37.1k
  int flags = OBJECT_INFO_LOOKUP_REPLACE | OBJECT_INFO_SKIP_FETCH_OBJECT |
564
37.1k
    OBJECT_INFO_DIE_IF_CORRUPT;
565
37.1k
  int ret;
566
567
37.1k
  if (!item)
568
0
    return -1;
569
37.1k
  if (item->object.parsed)
570
37.1k
    return 0;
571
0
  if (use_commit_graph && parse_commit_in_graph(r, item)) {
572
0
    static int commit_graph_paranoia = -1;
573
574
0
    if (commit_graph_paranoia == -1)
575
0
      commit_graph_paranoia = git_env_bool(GIT_COMMIT_GRAPH_PARANOIA, 0);
576
577
0
    if (commit_graph_paranoia && !has_object(r, &item->object.oid, 0)) {
578
0
      unparse_commit(r, &item->object.oid);
579
0
      return quiet_on_missing ? -1 :
580
0
        error(_("commit %s exists in commit-graph but not in the object database"),
581
0
              oid_to_hex(&item->object.oid));
582
0
    }
583
584
0
    return 0;
585
0
  }
586
587
0
  if (oid_object_info_extended(r, &item->object.oid, &oi, flags) < 0)
588
0
    return quiet_on_missing ? -1 :
589
0
      error("Could not read %s",
590
0
           oid_to_hex(&item->object.oid));
591
0
  if (type != OBJ_COMMIT) {
592
0
    free(buffer);
593
0
    return error("Object %s not a commit",
594
0
           oid_to_hex(&item->object.oid));
595
0
  }
596
597
0
  ret = parse_commit_buffer(r, item, buffer, size, 0);
598
0
  if (save_commit_buffer && !ret) {
599
0
    set_commit_buffer(r, item, buffer, size);
600
0
    return 0;
601
0
  }
602
0
  free(buffer);
603
0
  return ret;
604
0
}
605
606
int repo_parse_commit_gently(struct repository *r,
607
           struct commit *item, int quiet_on_missing)
608
37.1k
{
609
37.1k
  return repo_parse_commit_internal(r, item, quiet_on_missing, 1);
610
37.1k
}
611
612
void parse_commit_or_die(struct commit *item)
613
19.8k
{
614
19.8k
  if (repo_parse_commit(the_repository, item))
615
0
    die("unable to parse commit %s",
616
0
        item ? oid_to_hex(&item->object.oid) : "(null)");
617
19.8k
}
618
619
int find_commit_subject(const char *commit_buffer, const char **subject)
620
0
{
621
0
  const char *eol;
622
0
  const char *p = commit_buffer;
623
624
0
  while (*p && (*p != '\n' || p[1] != '\n'))
625
0
    p++;
626
0
  if (*p) {
627
0
    p = skip_blank_lines(p + 2);
628
0
    eol = strchrnul(p, '\n');
629
0
  } else
630
0
    eol = p;
631
632
0
  *subject = p;
633
634
0
  return eol - p;
635
0
}
636
637
size_t commit_subject_length(const char *body)
638
0
{
639
0
  const char *p = body;
640
0
  while (*p) {
641
0
    const char *next = skip_blank_lines(p);
642
0
    if (next != p)
643
0
      break;
644
0
    p = strchrnul(p, '\n');
645
0
    if (*p)
646
0
      p++;
647
0
  }
648
0
  return p - body;
649
0
}
650
651
struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
652
15.6k
{
653
15.6k
  struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
654
15.6k
  new_list->item = item;
655
15.6k
  new_list->next = *list_p;
656
15.6k
  *list_p = new_list;
657
15.6k
  return new_list;
658
15.6k
}
659
660
int commit_list_contains(struct commit *item, struct commit_list *list)
661
0
{
662
0
  while (list) {
663
0
    if (list->item == item)
664
0
      return 1;
665
0
    list = list->next;
666
0
  }
667
668
0
  return 0;
669
0
}
670
671
unsigned commit_list_count(const struct commit_list *l)
672
9.30k
{
673
9.30k
  unsigned c = 0;
674
17.1k
  for (; l; l = l->next )
675
7.83k
    c++;
676
9.30k
  return c;
677
9.30k
}
678
679
struct commit_list *copy_commit_list(const struct commit_list *list)
680
0
{
681
0
  struct commit_list *head = NULL;
682
0
  struct commit_list **pp = &head;
683
0
  while (list) {
684
0
    pp = commit_list_append(list->item, pp);
685
0
    list = list->next;
686
0
  }
687
0
  return head;
688
0
}
689
690
struct commit_list *reverse_commit_list(struct commit_list *list)
691
0
{
692
0
  struct commit_list *next = NULL, *current, *backup;
693
0
  for (current = list; current; current = backup) {
694
0
    backup = current->next;
695
0
    current->next = next;
696
0
    next = current;
697
0
  }
698
0
  return next;
699
0
}
700
701
void free_commit_list(struct commit_list *list)
702
123k
{
703
138k
  while (list)
704
15.6k
    pop_commit(&list);
705
123k
}
706
707
struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list)
708
0
{
709
0
  struct commit_list **pp = list;
710
0
  struct commit_list *p;
711
0
  while ((p = *pp) != NULL) {
712
0
    if (p->item->date < item->date) {
713
0
      break;
714
0
    }
715
0
    pp = &p->next;
716
0
  }
717
0
  return commit_list_insert(item, pp);
718
0
}
719
720
static int commit_list_compare_by_date(const struct commit_list *a,
721
               const struct commit_list *b)
722
0
{
723
0
  timestamp_t a_date = a->item->date;
724
0
  timestamp_t b_date = b->item->date;
725
0
  if (a_date < b_date)
726
0
    return 1;
727
0
  if (a_date > b_date)
728
0
    return -1;
729
0
  return 0;
730
0
}
731
732
DEFINE_LIST_SORT(static, commit_list_sort, struct commit_list, next);
733
734
void commit_list_sort_by_date(struct commit_list **list)
735
0
{
736
0
  commit_list_sort(list, commit_list_compare_by_date);
737
0
}
738
739
struct commit *pop_most_recent_commit(struct commit_list **list,
740
              unsigned int mark)
741
0
{
742
0
  struct commit *ret = pop_commit(list);
743
0
  struct commit_list *parents = ret->parents;
744
745
0
  while (parents) {
746
0
    struct commit *commit = parents->item;
747
0
    if (!repo_parse_commit(the_repository, commit) && !(commit->object.flags & mark)) {
748
0
      commit->object.flags |= mark;
749
0
      commit_list_insert_by_date(commit, list);
750
0
    }
751
0
    parents = parents->next;
752
0
  }
753
0
  return ret;
754
0
}
755
756
static void clear_commit_marks_1(struct commit_list **plist,
757
         struct commit *commit, unsigned int mark)
758
0
{
759
0
  while (commit) {
760
0
    struct commit_list *parents;
761
762
0
    if (!(mark & commit->object.flags))
763
0
      return;
764
765
0
    commit->object.flags &= ~mark;
766
767
0
    parents = commit->parents;
768
0
    if (!parents)
769
0
      return;
770
771
0
    while ((parents = parents->next)) {
772
0
      if (parents->item->object.flags & mark)
773
0
        commit_list_insert(parents->item, plist);
774
0
    }
775
776
0
    commit = commit->parents->item;
777
0
  }
778
0
}
779
780
void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark)
781
0
{
782
0
  struct commit_list *list = NULL;
783
784
0
  while (nr--) {
785
0
    clear_commit_marks_1(&list, *commit, mark);
786
0
    commit++;
787
0
  }
788
0
  while (list)
789
0
    clear_commit_marks_1(&list, pop_commit(&list), mark);
790
0
}
791
792
void clear_commit_marks(struct commit *commit, unsigned int mark)
793
0
{
794
0
  clear_commit_marks_many(1, &commit, mark);
795
0
}
796
797
struct commit *pop_commit(struct commit_list **stack)
798
15.6k
{
799
15.6k
  struct commit_list *top = *stack;
800
15.6k
  struct commit *item = top ? top->item : NULL;
801
802
15.6k
  if (top) {
803
15.6k
    *stack = top->next;
804
15.6k
    free(top);
805
15.6k
  }
806
15.6k
  return item;
807
15.6k
}
808
809
/*
810
 * Topological sort support
811
 */
812
813
/* count number of children that have not been emitted */
814
define_commit_slab(indegree_slab, int);
815
816
define_commit_slab(author_date_slab, timestamp_t);
817
818
void record_author_date(struct author_date_slab *author_date,
819
      struct commit *commit)
820
0
{
821
0
  const char *buffer = repo_get_commit_buffer(the_repository, commit,
822
0
                NULL);
823
0
  struct ident_split ident;
824
0
  const char *ident_line;
825
0
  size_t ident_len;
826
0
  char *date_end;
827
0
  timestamp_t date;
828
829
0
  ident_line = find_commit_header(buffer, "author", &ident_len);
830
0
  if (!ident_line)
831
0
    goto fail_exit; /* no author line */
832
0
  if (split_ident_line(&ident, ident_line, ident_len) ||
833
0
      !ident.date_begin || !ident.date_end)
834
0
    goto fail_exit; /* malformed "author" line */
835
836
0
  date = parse_timestamp(ident.date_begin, &date_end, 10);
837
0
  if (date_end != ident.date_end)
838
0
    goto fail_exit; /* malformed date */
839
0
  *(author_date_slab_at(author_date, commit)) = date;
840
841
0
fail_exit:
842
0
  repo_unuse_commit_buffer(the_repository, commit, buffer);
843
0
}
844
845
int compare_commits_by_author_date(const void *a_, const void *b_,
846
           void *cb_data)
847
0
{
848
0
  const struct commit *a = a_, *b = b_;
849
0
  struct author_date_slab *author_date = cb_data;
850
0
  timestamp_t a_date = *(author_date_slab_at(author_date, a));
851
0
  timestamp_t b_date = *(author_date_slab_at(author_date, b));
852
853
  /* newer commits with larger date first */
854
0
  if (a_date < b_date)
855
0
    return 1;
856
0
  else if (a_date > b_date)
857
0
    return -1;
858
0
  return 0;
859
0
}
860
861
int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_,
862
              void *unused UNUSED)
863
0
{
864
0
  const struct commit *a = a_, *b = b_;
865
0
  const timestamp_t generation_a = commit_graph_generation(a),
866
0
        generation_b = commit_graph_generation(b);
867
868
  /* newer commits first */
869
0
  if (generation_a < generation_b)
870
0
    return 1;
871
0
  else if (generation_a > generation_b)
872
0
    return -1;
873
874
  /* use date as a heuristic when generations are equal */
875
0
  if (a->date < b->date)
876
0
    return 1;
877
0
  else if (a->date > b->date)
878
0
    return -1;
879
0
  return 0;
880
0
}
881
882
int compare_commits_by_commit_date(const void *a_, const void *b_,
883
           void *unused UNUSED)
884
0
{
885
0
  const struct commit *a = a_, *b = b_;
886
  /* newer commits with larger date first */
887
0
  if (a->date < b->date)
888
0
    return 1;
889
0
  else if (a->date > b->date)
890
0
    return -1;
891
0
  return 0;
892
0
}
893
894
/*
895
 * Performs an in-place topological sort on the list supplied.
896
 */
897
void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order)
898
0
{
899
0
  struct commit_list *next, *orig = *list;
900
0
  struct commit_list **pptr;
901
0
  struct indegree_slab indegree;
902
0
  struct prio_queue queue;
903
0
  struct commit *commit;
904
0
  struct author_date_slab author_date;
905
906
0
  if (!orig)
907
0
    return;
908
0
  *list = NULL;
909
910
0
  init_indegree_slab(&indegree);
911
0
  memset(&queue, '\0', sizeof(queue));
912
913
0
  switch (sort_order) {
914
0
  default: /* REV_SORT_IN_GRAPH_ORDER */
915
0
    queue.compare = NULL;
916
0
    break;
917
0
  case REV_SORT_BY_COMMIT_DATE:
918
0
    queue.compare = compare_commits_by_commit_date;
919
0
    break;
920
0
  case REV_SORT_BY_AUTHOR_DATE:
921
0
    init_author_date_slab(&author_date);
922
0
    queue.compare = compare_commits_by_author_date;
923
0
    queue.cb_data = &author_date;
924
0
    break;
925
0
  }
926
927
  /* Mark them and clear the indegree */
928
0
  for (next = orig; next; next = next->next) {
929
0
    struct commit *commit = next->item;
930
0
    *(indegree_slab_at(&indegree, commit)) = 1;
931
    /* also record the author dates, if needed */
932
0
    if (sort_order == REV_SORT_BY_AUTHOR_DATE)
933
0
      record_author_date(&author_date, commit);
934
0
  }
935
936
  /* update the indegree */
937
0
  for (next = orig; next; next = next->next) {
938
0
    struct commit_list *parents = next->item->parents;
939
0
    while (parents) {
940
0
      struct commit *parent = parents->item;
941
0
      int *pi = indegree_slab_at(&indegree, parent);
942
943
0
      if (*pi)
944
0
        (*pi)++;
945
0
      parents = parents->next;
946
0
    }
947
0
  }
948
949
  /*
950
   * find the tips
951
   *
952
   * tips are nodes not reachable from any other node in the list
953
   *
954
   * the tips serve as a starting set for the work queue.
955
   */
956
0
  for (next = orig; next; next = next->next) {
957
0
    struct commit *commit = next->item;
958
959
0
    if (*(indegree_slab_at(&indegree, commit)) == 1)
960
0
      prio_queue_put(&queue, commit);
961
0
  }
962
963
  /*
964
   * This is unfortunate; the initial tips need to be shown
965
   * in the order given from the revision traversal machinery.
966
   */
967
0
  if (sort_order == REV_SORT_IN_GRAPH_ORDER)
968
0
    prio_queue_reverse(&queue);
969
970
  /* We no longer need the commit list */
971
0
  free_commit_list(orig);
972
973
0
  pptr = list;
974
0
  *list = NULL;
975
0
  while ((commit = prio_queue_get(&queue)) != NULL) {
976
0
    struct commit_list *parents;
977
978
0
    for (parents = commit->parents; parents ; parents = parents->next) {
979
0
      struct commit *parent = parents->item;
980
0
      int *pi = indegree_slab_at(&indegree, parent);
981
982
0
      if (!*pi)
983
0
        continue;
984
985
      /*
986
       * parents are only enqueued for emission
987
       * when all their children have been emitted thereby
988
       * guaranteeing topological order.
989
       */
990
0
      if (--(*pi) == 1)
991
0
        prio_queue_put(&queue, parent);
992
0
    }
993
    /*
994
     * all children of commit have already been
995
     * emitted. we can emit it now.
996
     */
997
0
    *(indegree_slab_at(&indegree, commit)) = 0;
998
999
0
    pptr = &commit_list_insert(commit, pptr)->next;
1000
0
  }
1001
1002
0
  clear_indegree_slab(&indegree);
1003
0
  clear_prio_queue(&queue);
1004
0
  if (sort_order == REV_SORT_BY_AUTHOR_DATE)
1005
0
    clear_author_date_slab(&author_date);
1006
0
}
1007
1008
struct rev_collect {
1009
  struct commit **commit;
1010
  int nr;
1011
  int alloc;
1012
  unsigned int initial : 1;
1013
};
1014
1015
static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
1016
0
{
1017
0
  struct commit *commit;
1018
1019
0
  if (is_null_oid(oid))
1020
0
    return;
1021
1022
0
  commit = lookup_commit(the_repository, oid);
1023
0
  if (!commit ||
1024
0
      (commit->object.flags & TMP_MARK) ||
1025
0
      repo_parse_commit(the_repository, commit))
1026
0
    return;
1027
1028
0
  ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc);
1029
0
  revs->commit[revs->nr++] = commit;
1030
0
  commit->object.flags |= TMP_MARK;
1031
0
}
1032
1033
static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
1034
          const char *ident UNUSED,
1035
          timestamp_t timestamp UNUSED, int tz UNUSED,
1036
          const char *message UNUSED, void *cbdata)
1037
0
{
1038
0
  struct rev_collect *revs = cbdata;
1039
1040
0
  if (revs->initial) {
1041
0
    revs->initial = 0;
1042
0
    add_one_commit(ooid, revs);
1043
0
  }
1044
0
  add_one_commit(noid, revs);
1045
0
  return 0;
1046
0
}
1047
1048
struct commit *get_fork_point(const char *refname, struct commit *commit)
1049
0
{
1050
0
  struct object_id oid;
1051
0
  struct rev_collect revs;
1052
0
  struct commit_list *bases = NULL;
1053
0
  int i;
1054
0
  struct commit *ret = NULL;
1055
0
  char *full_refname;
1056
1057
0
  switch (repo_dwim_ref(the_repository, refname, strlen(refname), &oid,
1058
0
            &full_refname, 0)) {
1059
0
  case 0:
1060
0
    die("No such ref: '%s'", refname);
1061
0
  case 1:
1062
0
    break; /* good */
1063
0
  default:
1064
0
    die("Ambiguous refname: '%s'", refname);
1065
0
  }
1066
1067
0
  memset(&revs, 0, sizeof(revs));
1068
0
  revs.initial = 1;
1069
0
  refs_for_each_reflog_ent(get_main_ref_store(the_repository),
1070
0
         full_refname, collect_one_reflog_ent, &revs);
1071
1072
0
  if (!revs.nr)
1073
0
    add_one_commit(&oid, &revs);
1074
1075
0
  for (i = 0; i < revs.nr; i++)
1076
0
    revs.commit[i]->object.flags &= ~TMP_MARK;
1077
1078
0
  if (repo_get_merge_bases_many(the_repository, commit, revs.nr,
1079
0
              revs.commit, &bases) < 0)
1080
0
    exit(128);
1081
1082
  /*
1083
   * There should be one and only one merge base, when we found
1084
   * a common ancestor among reflog entries.
1085
   */
1086
0
  if (!bases || bases->next)
1087
0
    goto cleanup_return;
1088
1089
  /* And the found one must be one of the reflog entries */
1090
0
  for (i = 0; i < revs.nr; i++)
1091
0
    if (&bases->item->object == &revs.commit[i]->object)
1092
0
      break; /* found */
1093
0
  if (revs.nr <= i)
1094
0
    goto cleanup_return;
1095
1096
0
  ret = bases->item;
1097
1098
0
cleanup_return:
1099
0
  free(revs.commit);
1100
0
  free_commit_list(bases);
1101
0
  free(full_refname);
1102
0
  return ret;
1103
0
}
1104
1105
/*
1106
 * Indexed by hash algorithm identifier.
1107
 */
1108
static const char *gpg_sig_headers[] = {
1109
  NULL,
1110
  "gpgsig",
1111
  "gpgsig-sha256",
1112
};
1113
1114
int add_header_signature(struct strbuf *buf, struct strbuf *sig, const struct git_hash_algo *algo)
1115
0
{
1116
0
  int inspos, copypos;
1117
0
  const char *eoh;
1118
0
  const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(algo)];
1119
0
  int gpg_sig_header_len = strlen(gpg_sig_header);
1120
1121
  /* find the end of the header */
1122
0
  eoh = strstr(buf->buf, "\n\n");
1123
0
  if (!eoh)
1124
0
    inspos = buf->len;
1125
0
  else
1126
0
    inspos = eoh - buf->buf + 1;
1127
1128
0
  for (copypos = 0; sig->buf[copypos]; ) {
1129
0
    const char *bol = sig->buf + copypos;
1130
0
    const char *eol = strchrnul(bol, '\n');
1131
0
    int len = (eol - bol) + !!*eol;
1132
1133
0
    if (!copypos) {
1134
0
      strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
1135
0
      inspos += gpg_sig_header_len;
1136
0
    }
1137
0
    strbuf_insertstr(buf, inspos++, " ");
1138
0
    strbuf_insert(buf, inspos, bol, len);
1139
0
    inspos += len;
1140
0
    copypos += len;
1141
0
  }
1142
0
  return 0;
1143
0
}
1144
1145
static int sign_commit_to_strbuf(struct strbuf *sig, struct strbuf *buf, const char *keyid)
1146
0
{
1147
0
  char *keyid_to_free = NULL;
1148
0
  int ret = 0;
1149
0
  if (!keyid || !*keyid)
1150
0
    keyid = keyid_to_free = get_signing_key();
1151
0
  if (sign_buffer(buf, sig, keyid))
1152
0
    ret = -1;
1153
0
  free(keyid_to_free);
1154
0
  return ret;
1155
0
}
1156
1157
int parse_signed_commit(const struct commit *commit,
1158
      struct strbuf *payload, struct strbuf *signature,
1159
      const struct git_hash_algo *algop)
1160
0
{
1161
0
  unsigned long size;
1162
0
  const char *buffer = repo_get_commit_buffer(the_repository, commit,
1163
0
                &size);
1164
0
  int ret = parse_buffer_signed_by_header(buffer, size, payload, signature, algop);
1165
1166
0
  repo_unuse_commit_buffer(the_repository, commit, buffer);
1167
0
  return ret;
1168
0
}
1169
1170
int parse_buffer_signed_by_header(const char *buffer,
1171
          unsigned long size,
1172
          struct strbuf *payload,
1173
          struct strbuf *signature,
1174
          const struct git_hash_algo *algop)
1175
0
{
1176
0
  int in_signature = 0, saw_signature = 0, other_signature = 0;
1177
0
  const char *line, *tail, *p;
1178
0
  const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(algop)];
1179
1180
0
  line = buffer;
1181
0
  tail = buffer + size;
1182
0
  while (line < tail) {
1183
0
    const char *sig = NULL;
1184
0
    const char *next = memchr(line, '\n', tail - line);
1185
1186
0
    next = next ? next + 1 : tail;
1187
0
    if (in_signature && line[0] == ' ')
1188
0
      sig = line + 1;
1189
0
    else if (skip_prefix(line, gpg_sig_header, &p) &&
1190
0
       *p == ' ') {
1191
0
      sig = line + strlen(gpg_sig_header) + 1;
1192
0
      other_signature = 0;
1193
0
    }
1194
0
    else if (starts_with(line, "gpgsig"))
1195
0
      other_signature = 1;
1196
0
    else if (other_signature && line[0] != ' ')
1197
0
      other_signature = 0;
1198
0
    if (sig) {
1199
0
      strbuf_add(signature, sig, next - sig);
1200
0
      saw_signature = 1;
1201
0
      in_signature = 1;
1202
0
    } else {
1203
0
      if (*line == '\n')
1204
        /* dump the whole remainder of the buffer */
1205
0
        next = tail;
1206
0
      if (!other_signature)
1207
0
        strbuf_add(payload, line, next - line);
1208
0
      in_signature = 0;
1209
0
    }
1210
0
    line = next;
1211
0
  }
1212
0
  return saw_signature;
1213
0
}
1214
1215
int remove_signature(struct strbuf *buf)
1216
0
{
1217
0
  const char *line = buf->buf;
1218
0
  const char *tail = buf->buf + buf->len;
1219
0
  int in_signature = 0;
1220
0
  struct sigbuf {
1221
0
    const char *start;
1222
0
    const char *end;
1223
0
  } sigs[2], *sigp = &sigs[0];
1224
0
  int i;
1225
0
  const char *orig_buf = buf->buf;
1226
1227
0
  memset(sigs, 0, sizeof(sigs));
1228
1229
0
  while (line < tail) {
1230
0
    const char *next = memchr(line, '\n', tail - line);
1231
0
    next = next ? next + 1 : tail;
1232
1233
0
    if (in_signature && line[0] == ' ')
1234
0
      sigp->end = next;
1235
0
    else if (starts_with(line, "gpgsig")) {
1236
0
      int i;
1237
0
      for (i = 1; i < GIT_HASH_NALGOS; i++) {
1238
0
        const char *p;
1239
0
        if (skip_prefix(line, gpg_sig_headers[i], &p) &&
1240
0
            *p == ' ') {
1241
0
          sigp->start = line;
1242
0
          sigp->end = next;
1243
0
          in_signature = 1;
1244
0
        }
1245
0
      }
1246
0
    } else {
1247
0
      if (*line == '\n')
1248
        /* dump the whole remainder of the buffer */
1249
0
        next = tail;
1250
0
      if (in_signature && sigp - sigs != ARRAY_SIZE(sigs))
1251
0
        sigp++;
1252
0
      in_signature = 0;
1253
0
    }
1254
0
    line = next;
1255
0
  }
1256
1257
0
  for (i = ARRAY_SIZE(sigs) - 1; i >= 0; i--)
1258
0
    if (sigs[i].start)
1259
0
      strbuf_remove(buf, sigs[i].start - orig_buf, sigs[i].end - sigs[i].start);
1260
1261
0
  return sigs[0].start != NULL;
1262
0
}
1263
1264
static void handle_signed_tag(const struct commit *parent, struct commit_extra_header ***tail)
1265
7.83k
{
1266
7.83k
  struct merge_remote_desc *desc;
1267
7.83k
  struct commit_extra_header *mergetag;
1268
7.83k
  char *buf;
1269
7.83k
  unsigned long size;
1270
7.83k
  enum object_type type;
1271
7.83k
  struct strbuf payload = STRBUF_INIT;
1272
7.83k
  struct strbuf signature = STRBUF_INIT;
1273
1274
7.83k
  desc = merge_remote_util(parent);
1275
7.83k
  if (!desc || !desc->obj)
1276
7.83k
    return;
1277
0
  buf = repo_read_object_file(the_repository, &desc->obj->oid, &type,
1278
0
            &size);
1279
0
  if (!buf || type != OBJ_TAG)
1280
0
    goto free_return;
1281
0
  if (!parse_signature(buf, size, &payload, &signature))
1282
0
    goto free_return;
1283
  /*
1284
   * We could verify this signature and either omit the tag when
1285
   * it does not validate, but the integrator may not have the
1286
   * public key of the signer of the tag being merged, while a
1287
   * later auditor may have it while auditing, so let's not run
1288
   * verify-signed-buffer here for now...
1289
   *
1290
   * if (verify_signed_buffer(buf, len, buf + len, size - len, ...))
1291
   *  warn("warning: signed tag unverified.");
1292
   */
1293
0
  CALLOC_ARRAY(mergetag, 1);
1294
0
  mergetag->key = xstrdup("mergetag");
1295
0
  mergetag->value = buf;
1296
0
  mergetag->len = size;
1297
1298
0
  **tail = mergetag;
1299
0
  *tail = &mergetag->next;
1300
0
  strbuf_release(&payload);
1301
0
  strbuf_release(&signature);
1302
0
  return;
1303
1304
0
free_return:
1305
0
  free(buf);
1306
0
}
1307
1308
int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1309
0
{
1310
0
  struct strbuf payload = STRBUF_INIT;
1311
0
  struct strbuf signature = STRBUF_INIT;
1312
0
  int ret = 1;
1313
1314
0
  sigc->result = 'N';
1315
1316
0
  if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
1317
0
    goto out;
1318
1319
0
  sigc->payload_type = SIGNATURE_PAYLOAD_COMMIT;
1320
0
  sigc->payload = strbuf_detach(&payload, &sigc->payload_len);
1321
0
  ret = check_signature(sigc, signature.buf, signature.len);
1322
1323
0
 out:
1324
0
  strbuf_release(&payload);
1325
0
  strbuf_release(&signature);
1326
1327
0
  return ret;
1328
0
}
1329
1330
void verify_merge_signature(struct commit *commit, int verbosity,
1331
          int check_trust)
1332
0
{
1333
0
  char hex[GIT_MAX_HEXSZ + 1];
1334
0
  struct signature_check signature_check;
1335
0
  int ret;
1336
0
  memset(&signature_check, 0, sizeof(signature_check));
1337
1338
0
  ret = check_commit_signature(commit, &signature_check);
1339
1340
0
  repo_find_unique_abbrev_r(the_repository, hex, &commit->object.oid,
1341
0
          DEFAULT_ABBREV);
1342
0
  switch (signature_check.result) {
1343
0
  case 'G':
1344
0
    if (ret || (check_trust && signature_check.trust_level < TRUST_MARGINAL))
1345
0
      die(_("Commit %s has an untrusted GPG signature, "
1346
0
            "allegedly by %s."), hex, signature_check.signer);
1347
0
    break;
1348
0
  case 'B':
1349
0
    die(_("Commit %s has a bad GPG signature "
1350
0
          "allegedly by %s."), hex, signature_check.signer);
1351
0
  default: /* 'N' */
1352
0
    die(_("Commit %s does not have a GPG signature."), hex);
1353
0
  }
1354
0
  if (verbosity >= 0 && signature_check.result == 'G')
1355
0
    printf(_("Commit %s has a good GPG signature by %s\n"),
1356
0
           hex, signature_check.signer);
1357
1358
0
  signature_check_clear(&signature_check);
1359
0
}
1360
1361
void append_merge_tag_headers(const struct commit_list *parents,
1362
            struct commit_extra_header ***tail)
1363
9.30k
{
1364
17.1k
  while (parents) {
1365
7.83k
    const struct commit *parent = parents->item;
1366
7.83k
    handle_signed_tag(parent, tail);
1367
7.83k
    parents = parents->next;
1368
7.83k
  }
1369
9.30k
}
1370
1371
static int convert_commit_extra_headers(const struct commit_extra_header *orig,
1372
          struct commit_extra_header **result)
1373
0
{
1374
0
  const struct git_hash_algo *compat = the_repository->compat_hash_algo;
1375
0
  const struct git_hash_algo *algo = the_repository->hash_algo;
1376
0
  struct commit_extra_header *extra = NULL, **tail = &extra;
1377
0
  struct strbuf out = STRBUF_INIT;
1378
0
  while (orig) {
1379
0
    struct commit_extra_header *new;
1380
0
    CALLOC_ARRAY(new, 1);
1381
0
    if (!strcmp(orig->key, "mergetag")) {
1382
0
      if (convert_object_file(&out, algo, compat,
1383
0
            orig->value, orig->len,
1384
0
            OBJ_TAG, 1)) {
1385
0
        free(new);
1386
0
        free_commit_extra_headers(extra);
1387
0
        return -1;
1388
0
      }
1389
0
      new->key = xstrdup("mergetag");
1390
0
      new->value = strbuf_detach(&out, &new->len);
1391
0
    } else {
1392
0
      new->key = xstrdup(orig->key);
1393
0
      new->len = orig->len;
1394
0
      new->value = xmemdupz(orig->value, orig->len);
1395
0
    }
1396
0
    *tail = new;
1397
0
    tail = &new->next;
1398
0
    orig = orig->next;
1399
0
  }
1400
0
  *result = extra;
1401
0
  return 0;
1402
0
}
1403
1404
static void add_extra_header(struct strbuf *buffer,
1405
           const struct commit_extra_header *extra)
1406
0
{
1407
0
  strbuf_addstr(buffer, extra->key);
1408
0
  if (extra->len)
1409
0
    strbuf_add_lines(buffer, " ", extra->value, extra->len);
1410
0
  else
1411
0
    strbuf_addch(buffer, '\n');
1412
0
}
1413
1414
struct commit_extra_header *read_commit_extra_headers(struct commit *commit,
1415
                  const char **exclude)
1416
0
{
1417
0
  struct commit_extra_header *extra = NULL;
1418
0
  unsigned long size;
1419
0
  const char *buffer = repo_get_commit_buffer(the_repository, commit,
1420
0
                &size);
1421
0
  extra = read_commit_extra_header_lines(buffer, size, exclude);
1422
0
  repo_unuse_commit_buffer(the_repository, commit, buffer);
1423
0
  return extra;
1424
0
}
1425
1426
int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
1427
0
{
1428
0
  struct commit_extra_header *extra, *to_free;
1429
0
  int res = 0;
1430
1431
0
  to_free = read_commit_extra_headers(commit, NULL);
1432
0
  for (extra = to_free; !res && extra; extra = extra->next) {
1433
0
    if (strcmp(extra->key, "mergetag"))
1434
0
      continue; /* not a merge tag */
1435
0
    res = fn(commit, extra, data);
1436
0
  }
1437
0
  free_commit_extra_headers(to_free);
1438
0
  return res;
1439
0
}
1440
1441
static inline int standard_header_field(const char *field, size_t len)
1442
0
{
1443
0
  return ((len == 4 && !memcmp(field, "tree", 4)) ||
1444
0
    (len == 6 && !memcmp(field, "parent", 6)) ||
1445
0
    (len == 6 && !memcmp(field, "author", 6)) ||
1446
0
    (len == 9 && !memcmp(field, "committer", 9)) ||
1447
0
    (len == 8 && !memcmp(field, "encoding", 8)));
1448
0
}
1449
1450
static int excluded_header_field(const char *field, size_t len, const char **exclude)
1451
0
{
1452
0
  if (!exclude)
1453
0
    return 0;
1454
1455
0
  while (*exclude) {
1456
0
    size_t xlen = strlen(*exclude);
1457
0
    if (len == xlen && !memcmp(field, *exclude, xlen))
1458
0
      return 1;
1459
0
    exclude++;
1460
0
  }
1461
0
  return 0;
1462
0
}
1463
1464
static struct commit_extra_header *read_commit_extra_header_lines(
1465
  const char *buffer, size_t size,
1466
  const char **exclude)
1467
0
{
1468
0
  struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL;
1469
0
  const char *line, *next, *eof, *eob;
1470
0
  struct strbuf buf = STRBUF_INIT;
1471
1472
0
  for (line = buffer, eob = line + size;
1473
0
       line < eob && *line != '\n';
1474
0
       line = next) {
1475
0
    next = memchr(line, '\n', eob - line);
1476
0
    next = next ? next + 1 : eob;
1477
0
    if (*line == ' ') {
1478
      /* continuation */
1479
0
      if (it)
1480
0
        strbuf_add(&buf, line + 1, next - (line + 1));
1481
0
      continue;
1482
0
    }
1483
0
    if (it)
1484
0
      it->value = strbuf_detach(&buf, &it->len);
1485
0
    strbuf_reset(&buf);
1486
0
    it = NULL;
1487
1488
0
    eof = memchr(line, ' ', next - line);
1489
0
    if (!eof)
1490
0
      eof = next;
1491
0
    else if (standard_header_field(line, eof - line) ||
1492
0
       excluded_header_field(line, eof - line, exclude))
1493
0
      continue;
1494
1495
0
    CALLOC_ARRAY(it, 1);
1496
0
    it->key = xmemdupz(line, eof-line);
1497
0
    *tail = it;
1498
0
    tail = &it->next;
1499
0
    if (eof + 1 < next)
1500
0
      strbuf_add(&buf, eof + 1, next - (eof + 1));
1501
0
  }
1502
0
  if (it)
1503
0
    it->value = strbuf_detach(&buf, &it->len);
1504
0
  return extra;
1505
0
}
1506
1507
void free_commit_extra_headers(struct commit_extra_header *extra)
1508
9.38k
{
1509
9.38k
  while (extra) {
1510
0
    struct commit_extra_header *next = extra->next;
1511
0
    free(extra->key);
1512
0
    free(extra->value);
1513
0
    free(extra);
1514
0
    extra = next;
1515
0
  }
1516
9.38k
}
1517
1518
int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree,
1519
    const struct commit_list *parents, struct object_id *ret,
1520
    const char *author, const char *sign_commit)
1521
0
{
1522
0
  struct commit_extra_header *extra = NULL, **tail = &extra;
1523
0
  int result;
1524
1525
0
  append_merge_tag_headers(parents, &tail);
1526
0
  result = commit_tree_extended(msg, msg_len, tree, parents, ret, author,
1527
0
              NULL, sign_commit, extra);
1528
0
  free_commit_extra_headers(extra);
1529
0
  return result;
1530
0
}
1531
1532
static int find_invalid_utf8(const char *buf, int len)
1533
18.6k
{
1534
18.6k
  int offset = 0;
1535
18.6k
  static const unsigned int max_codepoint[] = {
1536
18.6k
    0x7f, 0x7ff, 0xffff, 0x10ffff
1537
18.6k
  };
1538
1539
631M
  while (len) {
1540
631M
    unsigned char c = *buf++;
1541
631M
    int bytes, bad_offset;
1542
631M
    unsigned int codepoint;
1543
631M
    unsigned int min_val, max_val;
1544
1545
631M
    len--;
1546
631M
    offset++;
1547
1548
    /* Simple US-ASCII? No worries. */
1549
631M
    if (c < 0x80)
1550
631M
      continue;
1551
1552
0
    bad_offset = offset-1;
1553
1554
    /*
1555
     * Count how many more high bits set: that's how
1556
     * many more bytes this sequence should have.
1557
     */
1558
0
    bytes = 0;
1559
0
    while (c & 0x40) {
1560
0
      c <<= 1;
1561
0
      bytes++;
1562
0
    }
1563
1564
    /*
1565
     * Must be between 1 and 3 more bytes.  Longer sequences result in
1566
     * codepoints beyond U+10FFFF, which are guaranteed never to exist.
1567
     */
1568
0
    if (bytes < 1 || 3 < bytes)
1569
0
      return bad_offset;
1570
1571
    /* Do we *have* that many bytes? */
1572
0
    if (len < bytes)
1573
0
      return bad_offset;
1574
1575
    /*
1576
     * Place the encoded bits at the bottom of the value and compute the
1577
     * valid range.
1578
     */
1579
0
    codepoint = (c & 0x7f) >> bytes;
1580
0
    min_val = max_codepoint[bytes-1] + 1;
1581
0
    max_val = max_codepoint[bytes];
1582
1583
0
    offset += bytes;
1584
0
    len -= bytes;
1585
1586
    /* And verify that they are good continuation bytes */
1587
0
    do {
1588
0
      codepoint <<= 6;
1589
0
      codepoint |= *buf & 0x3f;
1590
0
      if ((*buf++ & 0xc0) != 0x80)
1591
0
        return bad_offset;
1592
0
    } while (--bytes);
1593
1594
    /* Reject codepoints that are out of range for the sequence length. */
1595
0
    if (codepoint < min_val || codepoint > max_val)
1596
0
      return bad_offset;
1597
    /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */
1598
0
    if ((codepoint & 0x1ff800) == 0xd800)
1599
0
      return bad_offset;
1600
    /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */
1601
0
    if ((codepoint & 0xfffe) == 0xfffe)
1602
0
      return bad_offset;
1603
    /* So are anything in the range U+FDD0..U+FDEF. */
1604
0
    if (codepoint >= 0xfdd0 && codepoint <= 0xfdef)
1605
0
      return bad_offset;
1606
0
  }
1607
18.6k
  return -1;
1608
18.6k
}
1609
1610
/*
1611
 * This verifies that the buffer is in proper utf8 format.
1612
 *
1613
 * If it isn't, it assumes any non-utf8 characters are Latin1,
1614
 * and does the conversion.
1615
 */
1616
static int verify_utf8(struct strbuf *buf)
1617
18.6k
{
1618
18.6k
  int ok = 1;
1619
18.6k
  long pos = 0;
1620
1621
18.6k
  for (;;) {
1622
18.6k
    int bad;
1623
18.6k
    unsigned char c;
1624
18.6k
    unsigned char replace[2];
1625
1626
18.6k
    bad = find_invalid_utf8(buf->buf + pos, buf->len - pos);
1627
18.6k
    if (bad < 0)
1628
18.6k
      return ok;
1629
0
    pos += bad;
1630
0
    ok = 0;
1631
0
    c = buf->buf[pos];
1632
0
    strbuf_remove(buf, pos, 1);
1633
1634
    /* We know 'c' must be in the range 128-255 */
1635
0
    replace[0] = 0xc0 + (c >> 6);
1636
0
    replace[1] = 0x80 + (c & 0x3f);
1637
0
    strbuf_insert(buf, pos, replace, 2);
1638
0
    pos += 2;
1639
0
  }
1640
18.6k
}
1641
1642
static const char commit_utf8_warn[] =
1643
N_("Warning: commit message did not conform to UTF-8.\n"
1644
   "You may want to amend it after fixing the message, or set the config\n"
1645
   "variable i18n.commitEncoding to the encoding your project uses.\n");
1646
1647
static void write_commit_tree(struct strbuf *buffer, const char *msg, size_t msg_len,
1648
            const struct object_id *tree,
1649
            const struct object_id *parents, size_t parents_len,
1650
            const char *author, const char *committer,
1651
            const struct commit_extra_header *extra)
1652
9.30k
{
1653
9.30k
  int encoding_is_utf8;
1654
9.30k
  size_t i;
1655
1656
  /* Not having i18n.commitencoding is the same as having utf-8 */
1657
9.30k
  encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1658
1659
9.30k
  strbuf_grow(buffer, 8192); /* should avoid reallocs for the headers */
1660
9.30k
  strbuf_addf(buffer, "tree %s\n", oid_to_hex(tree));
1661
1662
  /*
1663
   * NOTE! This ordering means that the same exact tree merged with a
1664
   * different order of parents will be a _different_ changeset even
1665
   * if everything else stays the same.
1666
   */
1667
17.1k
  for (i = 0; i < parents_len; i++)
1668
7.83k
    strbuf_addf(buffer, "parent %s\n", oid_to_hex(&parents[i]));
1669
1670
  /* Person/date information */
1671
9.30k
  if (!author)
1672
0
    author = git_author_info(IDENT_STRICT);
1673
9.30k
  strbuf_addf(buffer, "author %s\n", author);
1674
9.30k
  if (!committer)
1675
9.30k
    committer = git_committer_info(IDENT_STRICT);
1676
9.30k
  strbuf_addf(buffer, "committer %s\n", committer);
1677
9.30k
  if (!encoding_is_utf8)
1678
0
    strbuf_addf(buffer, "encoding %s\n", git_commit_encoding);
1679
1680
9.30k
  while (extra) {
1681
0
    add_extra_header(buffer, extra);
1682
0
    extra = extra->next;
1683
0
  }
1684
9.30k
  strbuf_addch(buffer, '\n');
1685
1686
  /* And add the comment */
1687
9.30k
  strbuf_add(buffer, msg, msg_len);
1688
9.30k
}
1689
1690
int commit_tree_extended(const char *msg, size_t msg_len,
1691
       const struct object_id *tree,
1692
       const struct commit_list *parents, struct object_id *ret,
1693
       const char *author, const char *committer,
1694
       const char *sign_commit,
1695
       const struct commit_extra_header *extra)
1696
9.30k
{
1697
9.30k
  struct repository *r = the_repository;
1698
9.30k
  int result = 0;
1699
9.30k
  int encoding_is_utf8;
1700
9.30k
  struct strbuf buffer = STRBUF_INIT, compat_buffer = STRBUF_INIT;
1701
9.30k
  struct strbuf sig = STRBUF_INIT, compat_sig = STRBUF_INIT;
1702
9.30k
  struct object_id *parent_buf = NULL, *compat_oid = NULL;
1703
9.30k
  struct object_id compat_oid_buf;
1704
9.30k
  size_t i, nparents;
1705
1706
  /* Not having i18n.commitencoding is the same as having utf-8 */
1707
9.30k
  encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
1708
1709
9.30k
  assert_oid_type(tree, OBJ_TREE);
1710
1711
9.30k
  if (memchr(msg, '\0', msg_len))
1712
0
    return error("a NUL byte in commit log message not allowed.");
1713
1714
9.30k
  nparents = commit_list_count(parents);
1715
9.30k
  CALLOC_ARRAY(parent_buf, nparents);
1716
9.30k
  i = 0;
1717
17.1k
  for (const struct commit_list *p = parents; p; p = p->next)
1718
7.83k
    oidcpy(&parent_buf[i++], &p->item->object.oid);
1719
1720
9.30k
  write_commit_tree(&buffer, msg, msg_len, tree, parent_buf, nparents, author, committer, extra);
1721
9.30k
  if (sign_commit && sign_commit_to_strbuf(&sig, &buffer, sign_commit)) {
1722
0
    result = -1;
1723
0
    goto out;
1724
0
  }
1725
9.30k
  if (r->compat_hash_algo) {
1726
0
    struct commit_extra_header *compat_extra = NULL;
1727
0
    struct object_id mapped_tree;
1728
0
    struct object_id *mapped_parents;
1729
1730
0
    CALLOC_ARRAY(mapped_parents, nparents);
1731
1732
0
    if (repo_oid_to_algop(r, tree, r->compat_hash_algo, &mapped_tree)) {
1733
0
      result = -1;
1734
0
      free(mapped_parents);
1735
0
      goto out;
1736
0
    }
1737
0
    for (i = 0; i < nparents; i++)
1738
0
      if (repo_oid_to_algop(r, &parent_buf[i], r->compat_hash_algo, &mapped_parents[i])) {
1739
0
        result = -1;
1740
0
        free(mapped_parents);
1741
0
        goto out;
1742
0
      }
1743
0
    if (convert_commit_extra_headers(extra, &compat_extra)) {
1744
0
      result = -1;
1745
0
      free(mapped_parents);
1746
0
      goto out;
1747
0
    }
1748
0
    write_commit_tree(&compat_buffer, msg, msg_len, &mapped_tree,
1749
0
          mapped_parents, nparents, author, committer, compat_extra);
1750
0
    free_commit_extra_headers(compat_extra);
1751
0
    free(mapped_parents);
1752
1753
0
    if (sign_commit && sign_commit_to_strbuf(&compat_sig, &compat_buffer, sign_commit)) {
1754
0
      result = -1;
1755
0
      goto out;
1756
0
    }
1757
0
  }
1758
1759
9.30k
  if (sign_commit) {
1760
0
    struct sig_pairs {
1761
0
      struct strbuf *sig;
1762
0
      const struct git_hash_algo *algo;
1763
0
    } bufs [2] = {
1764
0
      { &compat_sig, r->compat_hash_algo },
1765
0
      { &sig, r->hash_algo },
1766
0
    };
1767
0
    int i;
1768
1769
    /*
1770
     * We write algorithms in the order they were implemented in
1771
     * Git to produce a stable hash when multiple algorithms are
1772
     * used.
1773
     */
1774
0
    if (r->compat_hash_algo && hash_algo_by_ptr(bufs[0].algo) > hash_algo_by_ptr(bufs[1].algo))
1775
0
      SWAP(bufs[0], bufs[1]);
1776
1777
    /*
1778
     * We traverse each algorithm in order, and apply the signature
1779
     * to each buffer.
1780
     */
1781
0
    for (i = 0; i < ARRAY_SIZE(bufs); i++) {
1782
0
      if (!bufs[i].algo)
1783
0
        continue;
1784
0
      add_header_signature(&buffer, bufs[i].sig, bufs[i].algo);
1785
0
      if (r->compat_hash_algo)
1786
0
        add_header_signature(&compat_buffer, bufs[i].sig, bufs[i].algo);
1787
0
    }
1788
0
  }
1789
1790
  /* And check the encoding. */
1791
9.30k
  if (encoding_is_utf8 && (!verify_utf8(&buffer) || !verify_utf8(&compat_buffer)))
1792
0
    fprintf(stderr, _(commit_utf8_warn));
1793
1794
9.30k
  if (r->compat_hash_algo) {
1795
0
    hash_object_file(r->compat_hash_algo, compat_buffer.buf, compat_buffer.len,
1796
0
      OBJ_COMMIT, &compat_oid_buf);
1797
0
    compat_oid = &compat_oid_buf;
1798
0
  }
1799
1800
9.30k
  result = write_object_file_flags(buffer.buf, buffer.len, OBJ_COMMIT,
1801
9.30k
           ret, compat_oid, 0);
1802
9.30k
out:
1803
9.30k
  free(parent_buf);
1804
9.30k
  strbuf_release(&buffer);
1805
9.30k
  strbuf_release(&compat_buffer);
1806
9.30k
  strbuf_release(&sig);
1807
9.30k
  strbuf_release(&compat_sig);
1808
9.30k
  return result;
1809
9.30k
}
1810
1811
define_commit_slab(merge_desc_slab, struct merge_remote_desc *);
1812
static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab);
1813
1814
struct merge_remote_desc *merge_remote_util(const struct commit *commit)
1815
7.83k
{
1816
7.83k
  return *merge_desc_slab_at(&merge_desc_slab, commit);
1817
7.83k
}
1818
1819
void set_merge_remote_desc(struct commit *commit,
1820
         const char *name, struct object *obj)
1821
0
{
1822
0
  struct merge_remote_desc *desc;
1823
0
  FLEX_ALLOC_STR(desc, name, name);
1824
0
  desc->obj = obj;
1825
0
  *merge_desc_slab_at(&merge_desc_slab, commit) = desc;
1826
0
}
1827
1828
struct commit *get_merge_parent(const char *name)
1829
0
{
1830
0
  struct object *obj;
1831
0
  struct commit *commit;
1832
0
  struct object_id oid;
1833
0
  if (repo_get_oid(the_repository, name, &oid))
1834
0
    return NULL;
1835
0
  obj = parse_object(the_repository, &oid);
1836
0
  commit = (struct commit *)repo_peel_to_type(the_repository, name, 0,
1837
0
                obj, OBJ_COMMIT);
1838
0
  if (commit && !merge_remote_util(commit))
1839
0
    set_merge_remote_desc(commit, name, obj);
1840
0
  return commit;
1841
0
}
1842
1843
/*
1844
 * Append a commit to the end of the commit_list.
1845
 *
1846
 * next starts by pointing to the variable that holds the head of an
1847
 * empty commit_list, and is updated to point to the "next" field of
1848
 * the last item on the list as new commits are appended.
1849
 *
1850
 * Usage example:
1851
 *
1852
 *     struct commit_list *list;
1853
 *     struct commit_list **next = &list;
1854
 *
1855
 *     next = commit_list_append(c1, next);
1856
 *     next = commit_list_append(c2, next);
1857
 *     assert(commit_list_count(list) == 2);
1858
 *     return list;
1859
 */
1860
struct commit_list **commit_list_append(struct commit *commit,
1861
          struct commit_list **next)
1862
0
{
1863
0
  struct commit_list *new_commit = xmalloc(sizeof(struct commit_list));
1864
0
  new_commit->item = commit;
1865
0
  *next = new_commit;
1866
0
  new_commit->next = NULL;
1867
0
  return &new_commit->next;
1868
0
}
1869
1870
const char *find_commit_header(const char *msg, const char *key, size_t *out_len)
1871
27.9k
{
1872
27.9k
  int key_len = strlen(key);
1873
27.9k
  const char *line = msg;
1874
1875
135k
  while (line) {
1876
135k
    const char *eol = strchrnul(line, '\n');
1877
1878
135k
    if (line == eol)
1879
27.9k
      return NULL;
1880
1881
107k
    if (eol - line > key_len &&
1882
107k
        !strncmp(line, key, key_len) &&
1883
107k
        line[key_len] == ' ') {
1884
0
      *out_len = eol - line - key_len - 1;
1885
0
      return line + key_len + 1;
1886
0
    }
1887
107k
    line = *eol ? eol + 1 : NULL;
1888
107k
  }
1889
0
  return NULL;
1890
27.9k
}
1891
1892
/*
1893
 * Inspect the given string and determine the true "end" of the log message, in
1894
 * order to find where to put a new Signed-off-by trailer.  Ignored are
1895
 * trailing comment lines and blank lines.  To support "git commit -s
1896
 * --amend" on an existing commit, we also ignore "Conflicts:".  To
1897
 * support "git commit -v", we truncate at cut lines.
1898
 *
1899
 * Returns the number of bytes from the tail to ignore, to be fed as
1900
 * the second parameter to append_signoff().
1901
 */
1902
size_t ignored_log_message_bytes(const char *buf, size_t len)
1903
0
{
1904
0
  size_t boc = 0;
1905
0
  size_t bol = 0;
1906
0
  int in_old_conflicts_block = 0;
1907
0
  size_t cutoff = wt_status_locate_end(buf, len);
1908
1909
0
  while (bol < cutoff) {
1910
0
    const char *next_line = memchr(buf + bol, '\n', len - bol);
1911
1912
0
    if (!next_line)
1913
0
      next_line = buf + len;
1914
0
    else
1915
0
      next_line++;
1916
1917
0
    if (starts_with_mem(buf + bol, cutoff - bol, comment_line_str) ||
1918
0
        buf[bol] == '\n') {
1919
      /* is this the first of the run of comments? */
1920
0
      if (!boc)
1921
0
        boc = bol;
1922
      /* otherwise, it is just continuing */
1923
0
    } else if (starts_with(buf + bol, "Conflicts:\n")) {
1924
0
      in_old_conflicts_block = 1;
1925
0
      if (!boc)
1926
0
        boc = bol;
1927
0
    } else if (in_old_conflicts_block && buf[bol] == '\t') {
1928
0
      ; /* a pathname in the conflicts block */
1929
0
    } else if (boc) {
1930
      /* the previous was not trailing comment */
1931
0
      boc = 0;
1932
0
      in_old_conflicts_block = 0;
1933
0
    }
1934
0
    bol = next_line - buf;
1935
0
  }
1936
0
  return boc ? len - boc : len - cutoff;
1937
0
}
1938
1939
int run_commit_hook(int editor_is_used, const char *index_file,
1940
        int *invoked_hook, const char *name, ...)
1941
37.2k
{
1942
37.2k
  struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
1943
37.2k
  va_list args;
1944
37.2k
  const char *arg;
1945
1946
37.2k
  strvec_pushf(&opt.env, "GIT_INDEX_FILE=%s", index_file);
1947
1948
  /*
1949
   * Let the hook know that no editor will be launched.
1950
   */
1951
37.2k
  if (!editor_is_used)
1952
37.2k
    strvec_push(&opt.env, "GIT_EDITOR=:");
1953
1954
37.2k
  va_start(args, name);
1955
65.1k
  while ((arg = va_arg(args, const char *)))
1956
27.9k
    strvec_push(&opt.args, arg);
1957
37.2k
  va_end(args);
1958
1959
37.2k
  opt.invoked_hook = invoked_hook;
1960
37.2k
  return run_hooks_opt(the_repository, name, &opt);
1961
37.2k
}