Coverage Report

Created: 2024-09-16 06:10

/src/git/log-tree.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "commit-reach.h"
5
#include "config.h"
6
#include "diff.h"
7
#include "diffcore.h"
8
#include "environment.h"
9
#include "hex.h"
10
#include "object-name.h"
11
#include "object-store-ll.h"
12
#include "repository.h"
13
#include "tmp-objdir.h"
14
#include "commit.h"
15
#include "tag.h"
16
#include "graph.h"
17
#include "log-tree.h"
18
#include "merge-ort.h"
19
#include "reflog-walk.h"
20
#include "refs.h"
21
#include "replace-object.h"
22
#include "revision.h"
23
#include "string-list.h"
24
#include "color.h"
25
#include "gpg-interface.h"
26
#include "sequencer.h"
27
#include "line-log.h"
28
#include "help.h"
29
#include "range-diff.h"
30
#include "strmap.h"
31
#include "tree.h"
32
#include "wildmatch.h"
33
#include "write-or-die.h"
34
#include "pager.h"
35
36
static struct decoration name_decoration = { "object names" };
37
static int decoration_loaded;
38
static int decoration_flags;
39
40
static char decoration_colors[][COLOR_MAXLEN] = {
41
  GIT_COLOR_RESET,
42
  GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
43
  GIT_COLOR_BOLD_RED, /* REF_REMOTE */
44
  GIT_COLOR_BOLD_YELLOW,  /* REF_TAG */
45
  GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
46
  GIT_COLOR_BOLD_CYAN,  /* REF_HEAD */
47
  GIT_COLOR_BOLD_BLUE,  /* GRAFTED */
48
};
49
50
static const char *color_decorate_slots[] = {
51
  [DECORATION_REF_LOCAL]  = "branch",
52
  [DECORATION_REF_REMOTE] = "remoteBranch",
53
  [DECORATION_REF_TAG]  = "tag",
54
  [DECORATION_REF_STASH]  = "stash",
55
  [DECORATION_REF_HEAD] = "HEAD",
56
  [DECORATION_GRAFTED]  = "grafted",
57
};
58
59
static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
60
0
{
61
0
  if (want_color(decorate_use_color))
62
0
    return decoration_colors[ix];
63
0
  return "";
64
0
}
65
66
define_list_config_array(color_decorate_slots);
67
68
int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
69
0
{
70
0
  int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
71
0
  if (slot < 0)
72
0
    return 0;
73
0
  if (!value)
74
0
    return config_error_nonbool(var);
75
0
  return color_parse(value, decoration_colors[slot]);
76
0
}
77
78
/*
79
 * log-tree.c uses DIFF_OPT_TST for determining whether to use color
80
 * for showing the commit sha1, use the same check for --decorate
81
 */
82
#define decorate_get_color_opt(o, ix) \
83
  decorate_get_color((o)->use_color, ix)
84
85
void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
86
0
{
87
0
  struct name_decoration *res;
88
0
  FLEX_ALLOC_STR(res, name, name);
89
0
  res->type = type;
90
0
  res->next = add_decoration(&name_decoration, obj, res);
91
0
}
92
93
const struct name_decoration *get_name_decoration(const struct object *obj)
94
0
{
95
0
  load_ref_decorations(NULL, DECORATE_SHORT_REFS);
96
0
  return lookup_decoration(&name_decoration, obj);
97
0
}
98
99
static int match_ref_pattern(const char *refname,
100
           const struct string_list_item *item)
101
0
{
102
0
  int matched = 0;
103
0
  if (!item->util) {
104
0
    if (!wildmatch(item->string, refname, 0))
105
0
      matched = 1;
106
0
  } else {
107
0
    const char *rest;
108
0
    if (skip_prefix(refname, item->string, &rest) &&
109
0
        (!*rest || *rest == '/'))
110
0
      matched = 1;
111
0
  }
112
0
  return matched;
113
0
}
114
115
static int ref_filter_match(const char *refname,
116
          const struct decoration_filter *filter)
117
0
{
118
0
  struct string_list_item *item;
119
0
  const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
120
0
  const struct string_list *include_patterns = filter->include_ref_pattern;
121
0
  const struct string_list *exclude_patterns_config =
122
0
        filter->exclude_ref_config_pattern;
123
124
0
  if (exclude_patterns && exclude_patterns->nr) {
125
0
    for_each_string_list_item(item, exclude_patterns) {
126
0
      if (match_ref_pattern(refname, item))
127
0
        return 0;
128
0
    }
129
0
  }
130
131
0
  if (include_patterns && include_patterns->nr) {
132
0
    for_each_string_list_item(item, include_patterns) {
133
0
      if (match_ref_pattern(refname, item))
134
0
        return 1;
135
0
    }
136
0
    return 0;
137
0
  }
138
139
0
  if (exclude_patterns_config && exclude_patterns_config->nr) {
140
0
    for_each_string_list_item(item, exclude_patterns_config) {
141
0
      if (match_ref_pattern(refname, item))
142
0
        return 0;
143
0
    }
144
0
  }
145
146
0
  return 1;
147
0
}
148
149
static int add_ref_decoration(const char *refname, const char *referent UNUSED, const struct object_id *oid,
150
            int flags UNUSED,
151
            void *cb_data)
152
0
{
153
0
  int i;
154
0
  struct object *obj;
155
0
  enum object_type objtype;
156
0
  enum decoration_type deco_type = DECORATION_NONE;
157
0
  struct decoration_filter *filter = (struct decoration_filter *)cb_data;
158
0
  const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
159
160
0
  if (filter && !ref_filter_match(refname, filter))
161
0
    return 0;
162
163
0
  if (starts_with(refname, git_replace_ref_base)) {
164
0
    struct object_id original_oid;
165
0
    if (!replace_refs_enabled(the_repository))
166
0
      return 0;
167
0
    if (get_oid_hex(refname + strlen(git_replace_ref_base),
168
0
        &original_oid)) {
169
0
      warning("invalid replace ref %s", refname);
170
0
      return 0;
171
0
    }
172
0
    obj = parse_object(the_repository, &original_oid);
173
0
    if (obj)
174
0
      add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
175
0
    return 0;
176
0
  }
177
178
0
  objtype = oid_object_info(the_repository, oid, NULL);
179
0
  if (objtype < 0)
180
0
    return 0;
181
0
  obj = lookup_object_by_type(the_repository, oid, objtype);
182
183
0
  for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) {
184
0
    struct ref_namespace_info *info = &ref_namespace[i];
185
186
0
    if (!info->decoration)
187
0
      continue;
188
0
    if (info->exact) {
189
0
      if (!strcmp(refname, info->ref)) {
190
0
        deco_type = info->decoration;
191
0
        break;
192
0
      }
193
0
    } else if (starts_with(refname, info->ref)) {
194
0
      deco_type = info->decoration;
195
0
      break;
196
0
    }
197
0
  }
198
199
0
  add_name_decoration(deco_type, refname, obj);
200
0
  while (obj->type == OBJ_TAG) {
201
0
    if (!obj->parsed)
202
0
      parse_object(the_repository, &obj->oid);
203
0
    obj = ((struct tag *)obj)->tagged;
204
0
    if (!obj)
205
0
      break;
206
0
    add_name_decoration(DECORATION_REF_TAG, refname, obj);
207
0
  }
208
0
  return 0;
209
0
}
210
211
static int add_graft_decoration(const struct commit_graft *graft,
212
        void *cb_data UNUSED)
213
0
{
214
0
  struct commit *commit = lookup_commit(the_repository, &graft->oid);
215
0
  if (!commit)
216
0
    return 0;
217
0
  add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
218
0
  return 0;
219
0
}
220
221
void load_ref_decorations(struct decoration_filter *filter, int flags)
222
0
{
223
0
  if (!decoration_loaded) {
224
0
    if (filter) {
225
0
      struct string_list_item *item;
226
0
      for_each_string_list_item(item, filter->exclude_ref_pattern) {
227
0
        normalize_glob_ref(item, NULL, item->string);
228
0
      }
229
0
      for_each_string_list_item(item, filter->include_ref_pattern) {
230
0
        normalize_glob_ref(item, NULL, item->string);
231
0
      }
232
0
      for_each_string_list_item(item, filter->exclude_ref_config_pattern) {
233
0
        normalize_glob_ref(item, NULL, item->string);
234
0
      }
235
0
    }
236
0
    decoration_loaded = 1;
237
0
    decoration_flags = flags;
238
0
    refs_for_each_ref(get_main_ref_store(the_repository),
239
0
          add_ref_decoration, filter);
240
0
    refs_head_ref(get_main_ref_store(the_repository),
241
0
            add_ref_decoration, filter);
242
0
    for_each_commit_graft(add_graft_decoration, filter);
243
0
  }
244
0
}
245
246
static void show_parents(struct commit *commit, int abbrev, FILE *file)
247
0
{
248
0
  struct commit_list *p;
249
0
  for (p = commit->parents; p ; p = p->next) {
250
0
    struct commit *parent = p->item;
251
0
    fprintf(file, " %s",
252
0
      repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev));
253
0
  }
254
0
}
255
256
static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
257
0
{
258
0
  struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
259
0
  for ( ; p; p = p->next) {
260
0
    fprintf(opt->diffopt.file, " %s",
261
0
      repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev));
262
0
  }
263
0
}
264
265
/*
266
 * Do we have HEAD in the output, and also the branch it points at?
267
 * If so, find that decoration entry for that current branch.
268
 */
269
static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
270
0
{
271
0
  const struct name_decoration *list, *head = NULL;
272
0
  const char *branch_name = NULL;
273
0
  int rru_flags;
274
275
  /* First find HEAD */
276
0
  for (list = decoration; list; list = list->next)
277
0
    if (list->type == DECORATION_REF_HEAD) {
278
0
      head = list;
279
0
      break;
280
0
    }
281
0
  if (!head)
282
0
    return NULL;
283
284
  /* Now resolve and find the matching current branch */
285
0
  branch_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
286
0
                "HEAD", 0, NULL, &rru_flags);
287
0
  if (!branch_name || !(rru_flags & REF_ISSYMREF))
288
0
    return NULL;
289
290
0
  if (!starts_with(branch_name, "refs/"))
291
0
    return NULL;
292
293
  /* OK, do we have that ref in the list? */
294
0
  for (list = decoration; list; list = list->next)
295
0
    if ((list->type == DECORATION_REF_LOCAL) &&
296
0
        !strcmp(branch_name, list->name)) {
297
0
      return list;
298
0
    }
299
300
0
  return NULL;
301
0
}
302
303
static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
304
0
{
305
0
  if (decoration_flags == DECORATE_SHORT_REFS)
306
0
    strbuf_addstr(sb, prettify_refname(decoration->name));
307
0
  else
308
0
    strbuf_addstr(sb, decoration->name);
309
0
}
310
311
/*
312
 * The caller makes sure there is no funny color before calling.
313
 * format_decorations ensures the same after return.
314
 */
315
void format_decorations(struct strbuf *sb,
316
      const struct commit *commit,
317
      int use_color,
318
      const struct decoration_options *opts)
319
0
{
320
0
  const struct name_decoration *decoration;
321
0
  const struct name_decoration *current_and_HEAD;
322
0
  const char *color_commit, *color_reset;
323
324
0
  const char *prefix = " (";
325
0
  const char *suffix = ")";
326
0
  const char *separator = ", ";
327
0
  const char *pointer = " -> ";
328
0
  const char *tag = "tag: ";
329
330
0
  decoration = get_name_decoration(&commit->object);
331
0
  if (!decoration)
332
0
    return;
333
334
0
  if (opts) {
335
0
    if (opts->prefix)
336
0
      prefix = opts->prefix;
337
0
    if (opts->suffix)
338
0
      suffix = opts->suffix;
339
0
    if (opts->separator)
340
0
      separator = opts->separator;
341
0
    if (opts->pointer)
342
0
      pointer = opts->pointer;
343
0
    if (opts->tag)
344
0
      tag = opts->tag;
345
0
  }
346
347
0
  color_commit = diff_get_color(use_color, DIFF_COMMIT);
348
0
  color_reset = decorate_get_color(use_color, DECORATION_NONE);
349
350
0
  current_and_HEAD = current_pointed_by_HEAD(decoration);
351
0
  while (decoration) {
352
    /*
353
     * When both current and HEAD are there, only
354
     * show HEAD->current where HEAD would have
355
     * appeared, skipping the entry for current.
356
     */
357
0
    if (decoration != current_and_HEAD) {
358
0
      const char *color =
359
0
        decorate_get_color(use_color, decoration->type);
360
361
0
      if (*prefix) {
362
0
        strbuf_addstr(sb, color_commit);
363
0
        strbuf_addstr(sb, prefix);
364
0
        strbuf_addstr(sb, color_reset);
365
0
      }
366
367
0
      if (*tag && decoration->type == DECORATION_REF_TAG) {
368
0
        strbuf_addstr(sb, color);
369
0
        strbuf_addstr(sb, tag);
370
0
        strbuf_addstr(sb, color_reset);
371
0
      }
372
373
0
      strbuf_addstr(sb, color);
374
0
      show_name(sb, decoration);
375
0
      strbuf_addstr(sb, color_reset);
376
377
0
      if (current_and_HEAD &&
378
0
          decoration->type == DECORATION_REF_HEAD) {
379
0
        strbuf_addstr(sb, color_commit);
380
0
        strbuf_addstr(sb, pointer);
381
0
        strbuf_addstr(sb, color_reset);
382
0
        strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
383
0
        show_name(sb, current_and_HEAD);
384
0
        strbuf_addstr(sb, color_reset);
385
0
      }
386
387
0
      prefix = separator;
388
0
    }
389
0
    decoration = decoration->next;
390
0
  }
391
0
  if (*suffix) {
392
0
    strbuf_addstr(sb, color_commit);
393
0
    strbuf_addstr(sb, suffix);
394
0
    strbuf_addstr(sb, color_reset);
395
0
  }
396
0
}
397
398
void show_decorations(struct rev_info *opt, struct commit *commit)
399
0
{
400
0
  struct strbuf sb = STRBUF_INIT;
401
402
0
  if (opt->sources) {
403
0
    char **slot = revision_sources_peek(opt->sources, commit);
404
405
0
    if (slot && *slot)
406
0
      fprintf(opt->diffopt.file, "\t%s", *slot);
407
0
  }
408
0
  if (!opt->show_decorations)
409
0
    return;
410
0
  format_decorations(&sb, commit, opt->diffopt.use_color, NULL);
411
0
  fputs(sb.buf, opt->diffopt.file);
412
0
  strbuf_release(&sb);
413
0
}
414
415
void fmt_output_subject(struct strbuf *filename,
416
      const char *subject,
417
      struct rev_info *info)
418
0
{
419
0
  const char *suffix = info->patch_suffix;
420
0
  int nr = info->nr;
421
0
  int start_len = filename->len;
422
0
  int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1);
423
424
0
  if (info->reroll_count) {
425
0
    struct strbuf temp = STRBUF_INIT;
426
427
0
    strbuf_addf(&temp, "v%s", info->reroll_count);
428
0
    format_sanitized_subject(filename, temp.buf, temp.len);
429
0
    strbuf_addstr(filename, "-");
430
0
    strbuf_release(&temp);
431
0
  }
432
0
  strbuf_addf(filename, "%04d-%s", nr, subject);
433
434
0
  if (max_len < filename->len)
435
0
    strbuf_setlen(filename, max_len);
436
0
  strbuf_addstr(filename, suffix);
437
0
}
438
439
void fmt_output_commit(struct strbuf *filename,
440
           struct commit *commit,
441
           struct rev_info *info)
442
0
{
443
0
  struct pretty_print_context ctx = {0};
444
0
  struct strbuf subject = STRBUF_INIT;
445
446
0
  repo_format_commit_message(the_repository, commit, "%f", &subject,
447
0
           &ctx);
448
0
  fmt_output_subject(filename, subject.buf, info);
449
0
  strbuf_release(&subject);
450
0
}
451
452
void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
453
0
{
454
0
  if (opt->total > 0) {
455
0
    strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
456
0
          opt->subject_prefix,
457
0
          *opt->subject_prefix ? " " : "",
458
0
          decimal_width(opt->total),
459
0
          opt->nr, opt->total);
460
0
  } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
461
0
    strbuf_addf(sb, "Subject: [%s] ",
462
0
          opt->subject_prefix);
463
0
  } else {
464
0
    strbuf_addstr(sb, "Subject: ");
465
0
  }
466
0
}
467
468
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
469
           char **extra_headers_p,
470
           int *need_8bit_cte_p,
471
           int maybe_multipart)
472
0
{
473
0
  struct strbuf headers = STRBUF_INIT;
474
0
  const char *name = oid_to_hex(opt->zero_commit ?
475
0
              null_oid() : &commit->object.oid);
476
477
0
  *need_8bit_cte_p = 0; /* unknown */
478
479
0
  if (opt->extra_headers && *opt->extra_headers)
480
0
    strbuf_addstr(&headers, opt->extra_headers);
481
482
0
  fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
483
0
  graph_show_oneline(opt->graph);
484
0
  if (opt->message_id) {
485
0
    fprintf(opt->diffopt.file, "Message-ID: <%s>\n", opt->message_id);
486
0
    graph_show_oneline(opt->graph);
487
0
  }
488
0
  if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
489
0
    int i, n;
490
0
    n = opt->ref_message_ids->nr;
491
0
    fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
492
0
    for (i = 0; i < n; i++)
493
0
      fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
494
0
             opt->ref_message_ids->items[i].string);
495
0
    graph_show_oneline(opt->graph);
496
0
  }
497
0
  if (opt->mime_boundary && maybe_multipart) {
498
0
    static struct strbuf buffer = STRBUF_INIT;
499
0
    struct strbuf filename =  STRBUF_INIT;
500
0
    *need_8bit_cte_p = -1; /* NEVER */
501
502
0
    strbuf_reset(&buffer);
503
504
0
    strbuf_addf(&headers,
505
0
       "MIME-Version: 1.0\n"
506
0
       "Content-Type: multipart/mixed;"
507
0
       " boundary=\"%s%s\"\n"
508
0
       "\n"
509
0
       "This is a multi-part message in MIME "
510
0
       "format.\n"
511
0
       "--%s%s\n"
512
0
       "Content-Type: text/plain; "
513
0
       "charset=UTF-8; format=fixed\n"
514
0
       "Content-Transfer-Encoding: 8bit\n\n",
515
0
       mime_boundary_leader, opt->mime_boundary,
516
0
       mime_boundary_leader, opt->mime_boundary);
517
518
0
    if (opt->numbered_files)
519
0
      strbuf_addf(&filename, "%d", opt->nr);
520
0
    else
521
0
      fmt_output_commit(&filename, commit, opt);
522
0
    strbuf_addf(&buffer,
523
0
       "\n--%s%s\n"
524
0
       "Content-Type: text/x-patch;"
525
0
       " name=\"%s\"\n"
526
0
       "Content-Transfer-Encoding: 8bit\n"
527
0
       "Content-Disposition: %s;"
528
0
       " filename=\"%s\"\n\n",
529
0
       mime_boundary_leader, opt->mime_boundary,
530
0
       filename.buf,
531
0
       opt->no_inline ? "attachment" : "inline",
532
0
       filename.buf);
533
0
    opt->diffopt.stat_sep = buffer.buf;
534
0
    strbuf_release(&filename);
535
0
  }
536
0
  *extra_headers_p = headers.len ? strbuf_detach(&headers, NULL) : NULL;
537
0
}
538
539
static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
540
0
{
541
0
  const char *color, *reset, *eol;
542
543
0
  color = diff_get_color_opt(&opt->diffopt,
544
0
           status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
545
0
  reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
546
0
  while (*bol) {
547
0
    eol = strchrnul(bol, '\n');
548
0
    fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
549
0
           *eol ? "\n" : "");
550
0
    graph_show_oneline(opt->graph);
551
0
    bol = (*eol) ? (eol + 1) : eol;
552
0
  }
553
0
}
554
555
static void show_signature(struct rev_info *opt, struct commit *commit)
556
0
{
557
0
  struct strbuf payload = STRBUF_INIT;
558
0
  struct strbuf signature = STRBUF_INIT;
559
0
  struct signature_check sigc = { 0 };
560
0
  int status;
561
562
0
  if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
563
0
    goto out;
564
565
0
  sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT;
566
0
  sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
567
0
  status = check_signature(&sigc, signature.buf, signature.len);
568
0
  if (status && !sigc.output)
569
0
    show_sig_lines(opt, status, "No signature\n");
570
0
  else
571
0
    show_sig_lines(opt, status, sigc.output);
572
0
  signature_check_clear(&sigc);
573
574
0
 out:
575
0
  strbuf_release(&payload);
576
0
  strbuf_release(&signature);
577
0
}
578
579
static int which_parent(const struct object_id *oid, const struct commit *commit)
580
0
{
581
0
  int nth;
582
0
  const struct commit_list *parent;
583
584
0
  for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
585
0
    if (oideq(&parent->item->object.oid, oid))
586
0
      return nth;
587
0
    nth++;
588
0
  }
589
0
  return -1;
590
0
}
591
592
static int is_common_merge(const struct commit *commit)
593
0
{
594
0
  return (commit->parents
595
0
    && commit->parents->next
596
0
    && !commit->parents->next->next);
597
0
}
598
599
static int show_one_mergetag(struct commit *commit,
600
           struct commit_extra_header *extra,
601
           void *data)
602
0
{
603
0
  struct rev_info *opt = (struct rev_info *)data;
604
0
  struct object_id oid;
605
0
  struct tag *tag;
606
0
  struct strbuf verify_message;
607
0
  struct signature_check sigc = { 0 };
608
0
  int status, nth;
609
0
  struct strbuf payload = STRBUF_INIT;
610
0
  struct strbuf signature = STRBUF_INIT;
611
612
0
  hash_object_file(the_hash_algo, extra->value, extra->len,
613
0
       OBJ_TAG, &oid);
614
0
  tag = lookup_tag(the_repository, &oid);
615
0
  if (!tag)
616
0
    return -1; /* error message already given */
617
618
0
  strbuf_init(&verify_message, 256);
619
0
  if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
620
0
    strbuf_addstr(&verify_message, "malformed mergetag\n");
621
0
  else if (is_common_merge(commit) &&
622
0
     oideq(&tag->tagged->oid,
623
0
           &commit->parents->next->item->object.oid))
624
0
    strbuf_addf(&verify_message,
625
0
          "merged tag '%s'\n", tag->tag);
626
0
  else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
627
0
    strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
628
0
            tag->tag, oid_to_hex(&tag->tagged->oid));
629
0
  else
630
0
    strbuf_addf(&verify_message,
631
0
          "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
632
633
0
  status = -1;
634
0
  if (parse_signature(extra->value, extra->len, &payload, &signature)) {
635
    /* could have a good signature */
636
0
    sigc.payload_type = SIGNATURE_PAYLOAD_TAG;
637
0
    sigc.payload = strbuf_detach(&payload, &sigc.payload_len);
638
0
    status = check_signature(&sigc, signature.buf, signature.len);
639
0
    if (sigc.output)
640
0
      strbuf_addstr(&verify_message, sigc.output);
641
0
    else
642
0
      strbuf_addstr(&verify_message, "No signature\n");
643
0
    signature_check_clear(&sigc);
644
    /* otherwise we couldn't verify, which is shown as bad */
645
0
  }
646
647
0
  show_sig_lines(opt, status, verify_message.buf);
648
0
  strbuf_release(&verify_message);
649
0
  strbuf_release(&payload);
650
0
  strbuf_release(&signature);
651
0
  return 0;
652
0
}
653
654
static int show_mergetag(struct rev_info *opt, struct commit *commit)
655
0
{
656
0
  return for_each_mergetag(show_one_mergetag, commit, opt);
657
0
}
658
659
static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
660
0
{
661
0
  const char *x = opt->shown_dashes ? "\n" : "---\n";
662
0
  if (sb)
663
0
    strbuf_addstr(sb, x);
664
0
  else
665
0
    fputs(x, opt->diffopt.file);
666
0
  opt->shown_dashes = 1;
667
0
}
668
669
static void show_diff_of_diff(struct rev_info *opt)
670
0
{
671
0
  if (!cmit_fmt_is_mail(opt->commit_format))
672
0
    return;
673
674
0
  if (opt->idiff_oid1) {
675
0
    struct diff_queue_struct dq;
676
677
0
    memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
678
0
    DIFF_QUEUE_CLEAR(&diff_queued_diff);
679
680
0
    fprintf_ln(opt->diffopt.file, "\n%s", opt->idiff_title);
681
0
    show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2,
682
0
             &opt->diffopt);
683
684
0
    memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
685
0
  }
686
687
0
  if (opt->rdiff1) {
688
0
    struct diff_queue_struct dq;
689
0
    struct diff_options opts;
690
0
    struct range_diff_options range_diff_opts = {
691
0
      .creation_factor = opt->creation_factor,
692
0
      .dual_color = 1,
693
0
      .diffopt = &opts
694
0
    };
695
696
0
    memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
697
0
    DIFF_QUEUE_CLEAR(&diff_queued_diff);
698
699
0
    fprintf_ln(opt->diffopt.file, "\n%s", opt->rdiff_title);
700
    /*
701
     * Pass minimum required diff-options to range-diff; others
702
     * can be added later if deemed desirable.
703
     */
704
0
    repo_diff_setup(the_repository, &opts);
705
0
    opts.file = opt->diffopt.file;
706
0
    opts.use_color = opt->diffopt.use_color;
707
0
    diff_setup_done(&opts);
708
0
    show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts);
709
710
0
    memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
711
0
  }
712
0
}
713
714
void show_log(struct rev_info *opt)
715
0
{
716
0
  struct strbuf msgbuf = STRBUF_INIT;
717
0
  struct log_info *log = opt->loginfo;
718
0
  struct commit *commit = log->commit, *parent = log->parent;
719
0
  int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
720
0
  struct pretty_print_context ctx = {0};
721
722
0
  opt->loginfo = NULL;
723
0
  if (!opt->verbose_header) {
724
0
    graph_show_commit(opt->graph);
725
726
0
    if (!opt->graph)
727
0
      put_revision_mark(opt, commit);
728
0
    fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit),
729
0
          opt->diffopt.file);
730
0
    if (opt->print_parents)
731
0
      show_parents(commit, abbrev_commit, opt->diffopt.file);
732
0
    if (opt->children.name)
733
0
      show_children(opt, commit, abbrev_commit);
734
0
    show_decorations(opt, commit);
735
0
    if (opt->graph && !graph_is_commit_finished(opt->graph)) {
736
0
      putc('\n', opt->diffopt.file);
737
0
      graph_show_remainder(opt->graph);
738
0
    }
739
0
    putc(opt->diffopt.line_termination, opt->diffopt.file);
740
0
    return;
741
0
  }
742
743
  /*
744
   * If use_terminator is set, we already handled any record termination
745
   * at the end of the last record.
746
   * Otherwise, add a diffopt.line_termination character before all
747
   * entries but the first.  (IOW, as a separator between entries)
748
   */
749
0
  if (opt->shown_one && !opt->use_terminator) {
750
    /*
751
     * If entries are separated by a newline, the output
752
     * should look human-readable.  If the last entry ended
753
     * with a newline, print the graph output before this
754
     * newline.  Otherwise it will end up as a completely blank
755
     * line and will look like a gap in the graph.
756
     *
757
     * If the entry separator is not a newline, the output is
758
     * primarily intended for programmatic consumption, and we
759
     * never want the extra graph output before the entry
760
     * separator.
761
     */
762
0
    if (opt->diffopt.line_termination == '\n' &&
763
0
        !opt->missing_newline)
764
0
      graph_show_padding(opt->graph);
765
0
    putc(opt->diffopt.line_termination, opt->diffopt.file);
766
0
  }
767
0
  opt->shown_one = 1;
768
769
  /*
770
   * If the history graph was requested,
771
   * print the graph, up to this commit's line
772
   */
773
0
  graph_show_commit(opt->graph);
774
775
  /*
776
   * Print header line of header..
777
   */
778
779
0
  if (cmit_fmt_is_mail(opt->commit_format)) {
780
0
    log_write_email_headers(opt, commit, &ctx.after_subject,
781
0
          &ctx.need_8bit_cte, 1);
782
0
    ctx.rev = opt;
783
0
  } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
784
0
    fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
785
0
    if (opt->commit_format != CMIT_FMT_ONELINE)
786
0
      fputs("commit ", opt->diffopt.file);
787
788
0
    if (!opt->graph)
789
0
      put_revision_mark(opt, commit);
790
0
    fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid,
791
0
                abbrev_commit),
792
0
          opt->diffopt.file);
793
0
    if (opt->print_parents)
794
0
      show_parents(commit, abbrev_commit, opt->diffopt.file);
795
0
    if (opt->children.name)
796
0
      show_children(opt, commit, abbrev_commit);
797
0
    if (parent)
798
0
      fprintf(opt->diffopt.file, " (from %s)",
799
0
             repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit));
800
0
    fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
801
0
    show_decorations(opt, commit);
802
0
    if (opt->commit_format == CMIT_FMT_ONELINE) {
803
0
      putc(' ', opt->diffopt.file);
804
0
    } else {
805
0
      putc('\n', opt->diffopt.file);
806
0
      graph_show_oneline(opt->graph);
807
0
    }
808
0
    if (opt->reflog_info) {
809
      /*
810
       * setup_revisions() ensures that opt->reflog_info
811
       * and opt->graph cannot both be set,
812
       * so we don't need to worry about printing the
813
       * graph info here.
814
       */
815
0
      show_reflog_message(opt->reflog_info,
816
0
              opt->commit_format == CMIT_FMT_ONELINE,
817
0
              opt->date_mode,
818
0
              opt->date_mode_explicit);
819
0
      if (opt->commit_format == CMIT_FMT_ONELINE)
820
0
        return;
821
0
    }
822
0
  }
823
824
0
  if (opt->show_signature) {
825
0
    show_signature(opt, commit);
826
0
    show_mergetag(opt, commit);
827
0
  }
828
829
0
  if (opt->show_notes) {
830
0
    int raw;
831
0
    struct strbuf notebuf = STRBUF_INIT;
832
833
0
    raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
834
0
    format_display_notes(&commit->object.oid, &notebuf,
835
0
             get_log_output_encoding(), raw);
836
0
    ctx.notes_message = strbuf_detach(&notebuf, NULL);
837
0
  }
838
839
  /*
840
   * And then the pretty-printed message itself
841
   */
842
0
  if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
843
0
    ctx.need_8bit_cte =
844
0
      has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
845
0
  ctx.date_mode = opt->date_mode;
846
0
  ctx.date_mode_explicit = opt->date_mode_explicit;
847
0
  ctx.abbrev = opt->diffopt.abbrev;
848
0
  ctx.preserve_subject = opt->preserve_subject;
849
0
  ctx.encode_email_headers = opt->encode_email_headers;
850
0
  ctx.reflog_info = opt->reflog_info;
851
0
  ctx.fmt = opt->commit_format;
852
0
  ctx.mailmap = opt->mailmap;
853
0
  ctx.color = opt->diffopt.use_color;
854
0
  ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
855
0
  ctx.output_encoding = get_log_output_encoding();
856
0
  ctx.rev = opt;
857
0
  if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
858
0
    ctx.from_ident = &opt->from_ident;
859
0
  if (opt->graph)
860
0
    ctx.graph_width = graph_width(opt->graph);
861
0
  pretty_print_commit(&ctx, commit, &msgbuf);
862
863
0
  if (opt->add_signoff)
864
0
    append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
865
866
0
  if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
867
0
      ctx.notes_message && *ctx.notes_message) {
868
0
    if (cmit_fmt_is_mail(ctx.fmt))
869
0
      next_commentary_block(opt, &msgbuf);
870
0
    strbuf_addstr(&msgbuf, ctx.notes_message);
871
0
  }
872
873
0
  if (opt->show_log_size) {
874
0
    fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
875
0
    graph_show_oneline(opt->graph);
876
0
  }
877
878
  /*
879
   * Set opt->missing_newline if msgbuf doesn't
880
   * end in a newline (including if it is empty)
881
   */
882
0
  if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
883
0
    opt->missing_newline = 1;
884
0
  else
885
0
    opt->missing_newline = 0;
886
887
0
  graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
888
0
  if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
889
0
    if (!opt->missing_newline)
890
0
      graph_show_padding(opt->graph);
891
0
    putc(opt->diffopt.line_termination, opt->diffopt.file);
892
0
  }
893
894
0
  strbuf_release(&msgbuf);
895
0
  free(ctx.notes_message);
896
0
  free(ctx.after_subject);
897
0
}
898
899
int log_tree_diff_flush(struct rev_info *opt)
900
0
{
901
0
  opt->shown_dashes = 0;
902
0
  diffcore_std(&opt->diffopt);
903
904
0
  if (diff_queue_is_empty(&opt->diffopt)) {
905
0
    int saved_fmt = opt->diffopt.output_format;
906
0
    opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
907
0
    diff_flush(&opt->diffopt);
908
0
    opt->diffopt.output_format = saved_fmt;
909
0
    return 0;
910
0
  }
911
912
0
  if (opt->loginfo && !opt->no_commit_id) {
913
0
    show_log(opt);
914
0
    if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
915
0
        opt->verbose_header &&
916
0
        opt->commit_format != CMIT_FMT_ONELINE &&
917
0
        !commit_format_is_empty(opt->commit_format)) {
918
      /*
919
       * When showing a verbose header (i.e. log message),
920
       * and not in --pretty=oneline format, we would want
921
       * an extra newline between the end of log and the
922
       * diff/diffstat output for readability.
923
       */
924
0
      int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
925
0
      if (opt->diffopt.output_prefix) {
926
0
        struct strbuf *msg = NULL;
927
0
        msg = opt->diffopt.output_prefix(&opt->diffopt,
928
0
          opt->diffopt.output_prefix_data);
929
0
        fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
930
0
      }
931
932
      /*
933
       * We may have shown three-dashes line early
934
       * between generated commentary (notes, etc.)
935
       * and the log message, in which case we only
936
       * want a blank line after the commentary
937
       * without (an extra) three-dashes line.
938
       * Otherwise, we show the three-dashes line if
939
       * we are showing the patch with diffstat, but
940
       * in that case, there is no extra blank line
941
       * after the three-dashes line.
942
       */
943
0
      if (!opt->shown_dashes &&
944
0
          (pch & opt->diffopt.output_format) == pch)
945
0
        fprintf(opt->diffopt.file, "---");
946
0
      putc('\n', opt->diffopt.file);
947
0
    }
948
0
  }
949
0
  diff_flush(&opt->diffopt);
950
0
  return 1;
951
0
}
952
953
static int do_diff_combined(struct rev_info *opt, struct commit *commit)
954
0
{
955
0
  diff_tree_combined_merge(commit, opt);
956
0
  return !opt->loginfo;
957
0
}
958
959
static void setup_additional_headers(struct diff_options *o,
960
             struct strmap *all_headers)
961
0
{
962
0
  struct hashmap_iter iter;
963
0
  struct strmap_entry *entry;
964
965
  /*
966
   * Make o->additional_path_headers contain the subset of all_headers
967
   * that match o->pathspec.  If there aren't any that match o->pathspec,
968
   * then make o->additional_path_headers be NULL.
969
   */
970
971
0
  if (!o->pathspec.nr) {
972
0
    o->additional_path_headers = all_headers;
973
0
    return;
974
0
  }
975
976
0
  o->additional_path_headers = xmalloc(sizeof(struct strmap));
977
0
  strmap_init_with_options(o->additional_path_headers, NULL, 0);
978
0
  strmap_for_each_entry(all_headers, &iter, entry) {
979
0
    if (match_pathspec(the_repository->index, &o->pathspec,
980
0
           entry->key, strlen(entry->key),
981
0
           0 /* prefix */, NULL /* seen */,
982
0
           0 /* is_dir */))
983
0
      strmap_put(o->additional_path_headers,
984
0
           entry->key, entry->value);
985
0
  }
986
0
  if (!strmap_get_size(o->additional_path_headers)) {
987
0
    strmap_clear(o->additional_path_headers, 0);
988
0
    FREE_AND_NULL(o->additional_path_headers);
989
0
  }
990
0
}
991
992
static void cleanup_additional_headers(struct diff_options *o)
993
0
{
994
0
  if (!o->pathspec.nr) {
995
0
    o->additional_path_headers = NULL;
996
0
    return;
997
0
  }
998
0
  if (!o->additional_path_headers)
999
0
    return;
1000
1001
0
  strmap_clear(o->additional_path_headers, 0);
1002
0
  FREE_AND_NULL(o->additional_path_headers);
1003
0
}
1004
1005
static int do_remerge_diff(struct rev_info *opt,
1006
         struct commit_list *parents,
1007
         struct object_id *oid)
1008
0
{
1009
0
  struct merge_options o;
1010
0
  struct commit_list *bases = NULL;
1011
0
  struct merge_result res = {0};
1012
0
  struct pretty_print_context ctx = {0};
1013
0
  struct commit *parent1 = parents->item;
1014
0
  struct commit *parent2 = parents->next->item;
1015
0
  struct strbuf parent1_desc = STRBUF_INIT;
1016
0
  struct strbuf parent2_desc = STRBUF_INIT;
1017
1018
  /*
1019
   * Lazily prepare a temporary object directory and rotate it
1020
   * into the alternative object store list as the primary.
1021
   */
1022
0
  if (opt->remerge_diff && !opt->remerge_objdir) {
1023
0
    opt->remerge_objdir = tmp_objdir_create("remerge-diff");
1024
0
    if (!opt->remerge_objdir)
1025
0
      return error(_("unable to create temporary object directory"));
1026
0
    tmp_objdir_replace_primary_odb(opt->remerge_objdir, 1);
1027
0
  }
1028
1029
  /* Setup merge options */
1030
0
  init_ui_merge_options(&o, the_repository);
1031
0
  o.show_rename_progress = 0;
1032
0
  o.record_conflict_msgs_as_headers = 1;
1033
0
  o.msg_header_prefix = "remerge";
1034
1035
0
  ctx.abbrev = DEFAULT_ABBREV;
1036
0
  repo_format_commit_message(the_repository, parent1, "%h (%s)",
1037
0
           &parent1_desc, &ctx);
1038
0
  repo_format_commit_message(the_repository, parent2, "%h (%s)",
1039
0
           &parent2_desc, &ctx);
1040
0
  o.branch1 = parent1_desc.buf;
1041
0
  o.branch2 = parent2_desc.buf;
1042
1043
  /* Parse the relevant commits and get the merge bases */
1044
0
  parse_commit_or_die(parent1);
1045
0
  parse_commit_or_die(parent2);
1046
0
  if (repo_get_merge_bases(the_repository, parent1, parent2, &bases) < 0)
1047
0
    exit(128);
1048
1049
  /* Re-merge the parents */
1050
0
  merge_incore_recursive(&o, bases, parent1, parent2, &res);
1051
1052
  /* Show the diff */
1053
0
  setup_additional_headers(&opt->diffopt, res.path_messages);
1054
0
  diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt);
1055
0
  log_tree_diff_flush(opt);
1056
1057
  /* Cleanup */
1058
0
  free_commit_list(bases);
1059
0
  cleanup_additional_headers(&opt->diffopt);
1060
0
  strbuf_release(&parent1_desc);
1061
0
  strbuf_release(&parent2_desc);
1062
0
  merge_finalize(&o, &res);
1063
1064
  /* Clean up the contents of the temporary object directory */
1065
0
  tmp_objdir_discard_objects(opt->remerge_objdir);
1066
1067
0
  return !opt->loginfo;
1068
0
}
1069
1070
/*
1071
 * Show the diff of a commit.
1072
 *
1073
 * Return true if we printed any log info messages
1074
 */
1075
static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
1076
0
{
1077
0
  int showed_log;
1078
0
  struct commit_list *parents;
1079
0
  struct object_id *oid;
1080
0
  int is_merge;
1081
0
  int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
1082
1083
0
  if (!all_need_diff && !opt->merges_need_diff)
1084
0
    return 0;
1085
1086
0
  parse_commit_or_die(commit);
1087
0
  oid = get_commit_tree_oid(commit);
1088
1089
0
  parents = get_saved_parents(opt, commit);
1090
0
  is_merge = parents && parents->next;
1091
0
  if (!is_merge && !all_need_diff)
1092
0
    return 0;
1093
1094
  /* Root commit? */
1095
0
  if (!parents) {
1096
0
    if (opt->show_root_diff) {
1097
0
      diff_root_tree_oid(oid, "", &opt->diffopt);
1098
0
      log_tree_diff_flush(opt);
1099
0
    }
1100
0
    return !opt->loginfo;
1101
0
  }
1102
1103
0
  if (is_merge) {
1104
0
    int octopus = (parents->next->next != NULL);
1105
1106
0
    if (opt->remerge_diff) {
1107
0
      if (octopus) {
1108
0
        show_log(opt);
1109
0
        fprintf(opt->diffopt.file,
1110
0
          "diff: warning: Skipping remerge-diff "
1111
0
          "for octopus merges.\n");
1112
0
        return 1;
1113
0
      }
1114
0
      return do_remerge_diff(opt, parents, oid);
1115
0
    }
1116
0
    if (opt->combine_merges)
1117
0
      return do_diff_combined(opt, commit);
1118
0
    if (opt->separate_merges) {
1119
0
      if (!opt->first_parent_merges) {
1120
        /* Show parent info for multiple diffs */
1121
0
        log->parent = parents->item;
1122
0
      }
1123
0
    } else
1124
0
      return 0;
1125
0
  }
1126
1127
0
  showed_log = 0;
1128
0
  for (;;) {
1129
0
    struct commit *parent = parents->item;
1130
1131
0
    parse_commit_or_die(parent);
1132
0
    diff_tree_oid(get_commit_tree_oid(parent),
1133
0
            oid, "", &opt->diffopt);
1134
0
    log_tree_diff_flush(opt);
1135
1136
0
    showed_log |= !opt->loginfo;
1137
1138
    /* Set up the log info for the next parent, if any.. */
1139
0
    parents = parents->next;
1140
0
    if (!parents || opt->first_parent_merges)
1141
0
      break;
1142
0
    log->parent = parents->item;
1143
0
    opt->loginfo = log;
1144
0
  }
1145
0
  return showed_log;
1146
0
}
1147
1148
int log_tree_commit(struct rev_info *opt, struct commit *commit)
1149
0
{
1150
0
  struct log_info log;
1151
0
  int shown;
1152
  /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */
1153
0
  int no_free = opt->diffopt.no_free;
1154
1155
0
  log.commit = commit;
1156
0
  log.parent = NULL;
1157
0
  opt->loginfo = &log;
1158
0
  opt->diffopt.no_free = 1;
1159
1160
  /* NEEDSWORK: no restoring of no_free?  Why? */
1161
0
  if (opt->line_level_traverse)
1162
0
    return line_log_print(opt, commit);
1163
1164
0
  if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1165
0
    fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1166
0
  shown = log_tree_diff(opt, commit, &log);
1167
0
  if (!shown && opt->loginfo && opt->always_show_header) {
1168
0
    log.parent = NULL;
1169
0
    show_log(opt);
1170
0
    shown = 1;
1171
0
  }
1172
0
  if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
1173
0
    fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1174
0
  if (shown)
1175
0
    show_diff_of_diff(opt);
1176
0
  opt->loginfo = NULL;
1177
0
  maybe_flush_or_die(opt->diffopt.file, "stdout");
1178
0
  opt->diffopt.no_free = no_free;
1179
1180
0
  diff_free(&opt->diffopt);
1181
0
  return shown;
1182
0
}