Coverage Report

Created: 2025-12-31 07:01

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