Coverage Report

Created: 2026-10-17 07:15

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