THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

939 lines
26KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "diff.h"
  4. #include "object-store.h"
  5. #include "repository.h"
  6. #include "commit.h"
  7. #include "tag.h"
  8. #include "graph.h"
  9. #include "log-tree.h"
  10. #include "reflog-walk.h"
  11. #include "refs.h"
  12. #include "string-list.h"
  13. #include "color.h"
  14. #include "gpg-interface.h"
  15. #include "sequencer.h"
  16. #include "line-log.h"
  17. #include "help.h"
  18. #include "interdiff.h"
  19. #include "range-diff.h"
  20. static struct decoration name_decoration = { "object names" };
  21. static int decoration_loaded;
  22. static int decoration_flags;
  23. static char decoration_colors[][COLOR_MAXLEN] = {
  24. GIT_COLOR_RESET,
  25. GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
  26. GIT_COLOR_BOLD_RED, /* REF_REMOTE */
  27. GIT_COLOR_BOLD_YELLOW, /* REF_TAG */
  28. GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */
  29. GIT_COLOR_BOLD_CYAN, /* REF_HEAD */
  30. GIT_COLOR_BOLD_BLUE, /* GRAFTED */
  31. };
  32. static const char *color_decorate_slots[] = {
  33. [DECORATION_REF_LOCAL] = "branch",
  34. [DECORATION_REF_REMOTE] = "remoteBranch",
  35. [DECORATION_REF_TAG] = "tag",
  36. [DECORATION_REF_STASH] = "stash",
  37. [DECORATION_REF_HEAD] = "HEAD",
  38. [DECORATION_GRAFTED] = "grafted",
  39. };
  40. static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix)
  41. {
  42. if (want_color(decorate_use_color))
  43. return decoration_colors[ix];
  44. return "";
  45. }
  46. define_list_config_array(color_decorate_slots);
  47. int parse_decorate_color_config(const char *var, const char *slot_name, const char *value)
  48. {
  49. int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name);
  50. if (slot < 0)
  51. return 0;
  52. if (!value)
  53. return config_error_nonbool(var);
  54. return color_parse(value, decoration_colors[slot]);
  55. }
  56. /*
  57. * log-tree.c uses DIFF_OPT_TST for determining whether to use color
  58. * for showing the commit sha1, use the same check for --decorate
  59. */
  60. #define decorate_get_color_opt(o, ix) \
  61. decorate_get_color((o)->use_color, ix)
  62. void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
  63. {
  64. struct name_decoration *res;
  65. FLEX_ALLOC_STR(res, name, name);
  66. res->type = type;
  67. res->next = add_decoration(&name_decoration, obj, res);
  68. }
  69. const struct name_decoration *get_name_decoration(const struct object *obj)
  70. {
  71. load_ref_decorations(NULL, DECORATE_SHORT_REFS);
  72. return lookup_decoration(&name_decoration, obj);
  73. }
  74. static int add_ref_decoration(const char *refname, const struct object_id *oid,
  75. int flags, void *cb_data)
  76. {
  77. struct object *obj;
  78. enum decoration_type type = DECORATION_NONE;
  79. struct decoration_filter *filter = (struct decoration_filter *)cb_data;
  80. if (filter && !ref_filter_match(refname,
  81. filter->include_ref_pattern,
  82. filter->exclude_ref_pattern))
  83. return 0;
  84. if (starts_with(refname, git_replace_ref_base)) {
  85. struct object_id original_oid;
  86. if (!read_replace_refs)
  87. return 0;
  88. if (get_oid_hex(refname + strlen(git_replace_ref_base),
  89. &original_oid)) {
  90. warning("invalid replace ref %s", refname);
  91. return 0;
  92. }
  93. obj = parse_object(the_repository, &original_oid);
  94. if (obj)
  95. add_name_decoration(DECORATION_GRAFTED, "replaced", obj);
  96. return 0;
  97. }
  98. obj = parse_object(the_repository, oid);
  99. if (!obj)
  100. return 0;
  101. if (starts_with(refname, "refs/heads/"))
  102. type = DECORATION_REF_LOCAL;
  103. else if (starts_with(refname, "refs/remotes/"))
  104. type = DECORATION_REF_REMOTE;
  105. else if (starts_with(refname, "refs/tags/"))
  106. type = DECORATION_REF_TAG;
  107. else if (!strcmp(refname, "refs/stash"))
  108. type = DECORATION_REF_STASH;
  109. else if (!strcmp(refname, "HEAD"))
  110. type = DECORATION_REF_HEAD;
  111. add_name_decoration(type, refname, obj);
  112. while (obj->type == OBJ_TAG) {
  113. obj = ((struct tag *)obj)->tagged;
  114. if (!obj)
  115. break;
  116. if (!obj->parsed)
  117. parse_object(the_repository, &obj->oid);
  118. add_name_decoration(DECORATION_REF_TAG, refname, obj);
  119. }
  120. return 0;
  121. }
  122. static int add_graft_decoration(const struct commit_graft *graft, void *cb_data)
  123. {
  124. struct commit *commit = lookup_commit(the_repository, &graft->oid);
  125. if (!commit)
  126. return 0;
  127. add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object);
  128. return 0;
  129. }
  130. void load_ref_decorations(struct decoration_filter *filter, int flags)
  131. {
  132. if (!decoration_loaded) {
  133. if (filter) {
  134. struct string_list_item *item;
  135. for_each_string_list_item(item, filter->exclude_ref_pattern) {
  136. normalize_glob_ref(item, NULL, item->string);
  137. }
  138. for_each_string_list_item(item, filter->include_ref_pattern) {
  139. normalize_glob_ref(item, NULL, item->string);
  140. }
  141. }
  142. decoration_loaded = 1;
  143. decoration_flags = flags;
  144. for_each_ref(add_ref_decoration, filter);
  145. head_ref(add_ref_decoration, filter);
  146. for_each_commit_graft(add_graft_decoration, filter);
  147. }
  148. }
  149. static void show_parents(struct commit *commit, int abbrev, FILE *file)
  150. {
  151. struct commit_list *p;
  152. for (p = commit->parents; p ; p = p->next) {
  153. struct commit *parent = p->item;
  154. fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev));
  155. }
  156. }
  157. static void show_children(struct rev_info *opt, struct commit *commit, int abbrev)
  158. {
  159. struct commit_list *p = lookup_decoration(&opt->children, &commit->object);
  160. for ( ; p; p = p->next) {
  161. fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev));
  162. }
  163. }
  164. /*
  165. * Do we have HEAD in the output, and also the branch it points at?
  166. * If so, find that decoration entry for that current branch.
  167. */
  168. static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration)
  169. {
  170. const struct name_decoration *list, *head = NULL;
  171. const char *branch_name = NULL;
  172. int rru_flags;
  173. /* First find HEAD */
  174. for (list = decoration; list; list = list->next)
  175. if (list->type == DECORATION_REF_HEAD) {
  176. head = list;
  177. break;
  178. }
  179. if (!head)
  180. return NULL;
  181. /* Now resolve and find the matching current branch */
  182. branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags);
  183. if (!branch_name || !(rru_flags & REF_ISSYMREF))
  184. return NULL;
  185. if (!starts_with(branch_name, "refs/"))
  186. return NULL;
  187. /* OK, do we have that ref in the list? */
  188. for (list = decoration; list; list = list->next)
  189. if ((list->type == DECORATION_REF_LOCAL) &&
  190. !strcmp(branch_name, list->name)) {
  191. return list;
  192. }
  193. return NULL;
  194. }
  195. static void show_name(struct strbuf *sb, const struct name_decoration *decoration)
  196. {
  197. if (decoration_flags == DECORATE_SHORT_REFS)
  198. strbuf_addstr(sb, prettify_refname(decoration->name));
  199. else
  200. strbuf_addstr(sb, decoration->name);
  201. }
  202. /*
  203. * The caller makes sure there is no funny color before calling.
  204. * format_decorations_extended makes sure the same after return.
  205. */
  206. void format_decorations_extended(struct strbuf *sb,
  207. const struct commit *commit,
  208. int use_color,
  209. const char *prefix,
  210. const char *separator,
  211. const char *suffix)
  212. {
  213. const struct name_decoration *decoration;
  214. const struct name_decoration *current_and_HEAD;
  215. const char *color_commit =
  216. diff_get_color(use_color, DIFF_COMMIT);
  217. const char *color_reset =
  218. decorate_get_color(use_color, DECORATION_NONE);
  219. decoration = get_name_decoration(&commit->object);
  220. if (!decoration)
  221. return;
  222. current_and_HEAD = current_pointed_by_HEAD(decoration);
  223. while (decoration) {
  224. /*
  225. * When both current and HEAD are there, only
  226. * show HEAD->current where HEAD would have
  227. * appeared, skipping the entry for current.
  228. */
  229. if (decoration != current_and_HEAD) {
  230. strbuf_addstr(sb, color_commit);
  231. strbuf_addstr(sb, prefix);
  232. strbuf_addstr(sb, color_reset);
  233. strbuf_addstr(sb, decorate_get_color(use_color, decoration->type));
  234. if (decoration->type == DECORATION_REF_TAG)
  235. strbuf_addstr(sb, "tag: ");
  236. show_name(sb, decoration);
  237. if (current_and_HEAD &&
  238. decoration->type == DECORATION_REF_HEAD) {
  239. strbuf_addstr(sb, " -> ");
  240. strbuf_addstr(sb, color_reset);
  241. strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type));
  242. show_name(sb, current_and_HEAD);
  243. }
  244. strbuf_addstr(sb, color_reset);
  245. prefix = separator;
  246. }
  247. decoration = decoration->next;
  248. }
  249. strbuf_addstr(sb, color_commit);
  250. strbuf_addstr(sb, suffix);
  251. strbuf_addstr(sb, color_reset);
  252. }
  253. void show_decorations(struct rev_info *opt, struct commit *commit)
  254. {
  255. struct strbuf sb = STRBUF_INIT;
  256. if (opt->sources) {
  257. char **slot = revision_sources_peek(opt->sources, commit);
  258. if (slot && *slot)
  259. fprintf(opt->diffopt.file, "\t%s", *slot);
  260. }
  261. if (!opt->show_decorations)
  262. return;
  263. format_decorations(&sb, commit, opt->diffopt.use_color);
  264. fputs(sb.buf, opt->diffopt.file);
  265. strbuf_release(&sb);
  266. }
  267. static unsigned int digits_in_number(unsigned int number)
  268. {
  269. unsigned int i = 10, result = 1;
  270. while (i <= number) {
  271. i *= 10;
  272. result++;
  273. }
  274. return result;
  275. }
  276. void fmt_output_subject(struct strbuf *filename,
  277. const char *subject,
  278. struct rev_info *info)
  279. {
  280. const char *suffix = info->patch_suffix;
  281. int nr = info->nr;
  282. int start_len = filename->len;
  283. int max_len = start_len + FORMAT_PATCH_NAME_MAX - (strlen(suffix) + 1);
  284. if (0 < info->reroll_count)
  285. strbuf_addf(filename, "v%d-", info->reroll_count);
  286. strbuf_addf(filename, "%04d-%s", nr, subject);
  287. if (max_len < filename->len)
  288. strbuf_setlen(filename, max_len);
  289. strbuf_addstr(filename, suffix);
  290. }
  291. void fmt_output_commit(struct strbuf *filename,
  292. struct commit *commit,
  293. struct rev_info *info)
  294. {
  295. struct pretty_print_context ctx = {0};
  296. struct strbuf subject = STRBUF_INIT;
  297. format_commit_message(commit, "%f", &subject, &ctx);
  298. fmt_output_subject(filename, subject.buf, info);
  299. strbuf_release(&subject);
  300. }
  301. void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt)
  302. {
  303. if (opt->total > 0) {
  304. strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ",
  305. opt->subject_prefix,
  306. *opt->subject_prefix ? " " : "",
  307. digits_in_number(opt->total),
  308. opt->nr, opt->total);
  309. } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
  310. strbuf_addf(sb, "Subject: [%s] ",
  311. opt->subject_prefix);
  312. } else {
  313. strbuf_addstr(sb, "Subject: ");
  314. }
  315. }
  316. void log_write_email_headers(struct rev_info *opt, struct commit *commit,
  317. const char **extra_headers_p,
  318. int *need_8bit_cte_p,
  319. int maybe_multipart)
  320. {
  321. const char *extra_headers = opt->extra_headers;
  322. const char *name = oid_to_hex(opt->zero_commit ?
  323. &null_oid : &commit->object.oid);
  324. *need_8bit_cte_p = 0; /* unknown */
  325. fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name);
  326. graph_show_oneline(opt->graph);
  327. if (opt->message_id) {
  328. fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id);
  329. graph_show_oneline(opt->graph);
  330. }
  331. if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) {
  332. int i, n;
  333. n = opt->ref_message_ids->nr;
  334. fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string);
  335. for (i = 0; i < n; i++)
  336. fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "),
  337. opt->ref_message_ids->items[i].string);
  338. graph_show_oneline(opt->graph);
  339. }
  340. if (opt->mime_boundary && maybe_multipart) {
  341. static struct strbuf subject_buffer = STRBUF_INIT;
  342. static struct strbuf buffer = STRBUF_INIT;
  343. struct strbuf filename = STRBUF_INIT;
  344. *need_8bit_cte_p = -1; /* NEVER */
  345. strbuf_reset(&subject_buffer);
  346. strbuf_reset(&buffer);
  347. strbuf_addf(&subject_buffer,
  348. "%s"
  349. "MIME-Version: 1.0\n"
  350. "Content-Type: multipart/mixed;"
  351. " boundary=\"%s%s\"\n"
  352. "\n"
  353. "This is a multi-part message in MIME "
  354. "format.\n"
  355. "--%s%s\n"
  356. "Content-Type: text/plain; "
  357. "charset=UTF-8; format=fixed\n"
  358. "Content-Transfer-Encoding: 8bit\n\n",
  359. extra_headers ? extra_headers : "",
  360. mime_boundary_leader, opt->mime_boundary,
  361. mime_boundary_leader, opt->mime_boundary);
  362. extra_headers = subject_buffer.buf;
  363. if (opt->numbered_files)
  364. strbuf_addf(&filename, "%d", opt->nr);
  365. else
  366. fmt_output_commit(&filename, commit, opt);
  367. strbuf_addf(&buffer,
  368. "\n--%s%s\n"
  369. "Content-Type: text/x-patch;"
  370. " name=\"%s\"\n"
  371. "Content-Transfer-Encoding: 8bit\n"
  372. "Content-Disposition: %s;"
  373. " filename=\"%s\"\n\n",
  374. mime_boundary_leader, opt->mime_boundary,
  375. filename.buf,
  376. opt->no_inline ? "attachment" : "inline",
  377. filename.buf);
  378. opt->diffopt.stat_sep = buffer.buf;
  379. strbuf_release(&filename);
  380. }
  381. *extra_headers_p = extra_headers;
  382. }
  383. static void show_sig_lines(struct rev_info *opt, int status, const char *bol)
  384. {
  385. const char *color, *reset, *eol;
  386. color = diff_get_color_opt(&opt->diffopt,
  387. status ? DIFF_WHITESPACE : DIFF_FRAGINFO);
  388. reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET);
  389. while (*bol) {
  390. eol = strchrnul(bol, '\n');
  391. fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset,
  392. *eol ? "\n" : "");
  393. graph_show_oneline(opt->graph);
  394. bol = (*eol) ? (eol + 1) : eol;
  395. }
  396. }
  397. static void show_signature(struct rev_info *opt, struct commit *commit)
  398. {
  399. struct strbuf payload = STRBUF_INIT;
  400. struct strbuf signature = STRBUF_INIT;
  401. struct strbuf gpg_output = STRBUF_INIT;
  402. int status;
  403. if (parse_signed_commit(commit, &payload, &signature) <= 0)
  404. goto out;
  405. status = verify_signed_buffer(payload.buf, payload.len,
  406. signature.buf, signature.len,
  407. &gpg_output, NULL);
  408. if (status && !gpg_output.len)
  409. strbuf_addstr(&gpg_output, "No signature\n");
  410. show_sig_lines(opt, status, gpg_output.buf);
  411. out:
  412. strbuf_release(&gpg_output);
  413. strbuf_release(&payload);
  414. strbuf_release(&signature);
  415. }
  416. static int which_parent(const struct object_id *oid, const struct commit *commit)
  417. {
  418. int nth;
  419. const struct commit_list *parent;
  420. for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
  421. if (oideq(&parent->item->object.oid, oid))
  422. return nth;
  423. nth++;
  424. }
  425. return -1;
  426. }
  427. static int is_common_merge(const struct commit *commit)
  428. {
  429. return (commit->parents
  430. && commit->parents->next
  431. && !commit->parents->next->next);
  432. }
  433. static int show_one_mergetag(struct commit *commit,
  434. struct commit_extra_header *extra,
  435. void *data)
  436. {
  437. struct rev_info *opt = (struct rev_info *)data;
  438. struct object_id oid;
  439. struct tag *tag;
  440. struct strbuf verify_message;
  441. int status, nth;
  442. size_t payload_size, gpg_message_offset;
  443. hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &oid);
  444. tag = lookup_tag(the_repository, &oid);
  445. if (!tag)
  446. return -1; /* error message already given */
  447. strbuf_init(&verify_message, 256);
  448. if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
  449. strbuf_addstr(&verify_message, "malformed mergetag\n");
  450. else if (is_common_merge(commit) &&
  451. oideq(&tag->tagged->oid,
  452. &commit->parents->next->item->object.oid))
  453. strbuf_addf(&verify_message,
  454. "merged tag '%s'\n", tag->tag);
  455. else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
  456. strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
  457. tag->tag, tag->tagged->oid.hash);
  458. else
  459. strbuf_addf(&verify_message,
  460. "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
  461. gpg_message_offset = verify_message.len;
  462. payload_size = parse_signature(extra->value, extra->len);
  463. status = -1;
  464. if (extra->len > payload_size) {
  465. /* could have a good signature */
  466. if (!verify_signed_buffer(extra->value, payload_size,
  467. extra->value + payload_size,
  468. extra->len - payload_size,
  469. &verify_message, NULL))
  470. status = 0; /* good */
  471. else if (verify_message.len <= gpg_message_offset)
  472. strbuf_addstr(&verify_message, "No signature\n");
  473. /* otherwise we couldn't verify, which is shown as bad */
  474. }
  475. show_sig_lines(opt, status, verify_message.buf);
  476. strbuf_release(&verify_message);
  477. return 0;
  478. }
  479. static int show_mergetag(struct rev_info *opt, struct commit *commit)
  480. {
  481. return for_each_mergetag(show_one_mergetag, commit, opt);
  482. }
  483. static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
  484. {
  485. const char *x = opt->shown_dashes ? "\n" : "---\n";
  486. if (sb)
  487. strbuf_addstr(sb, x);
  488. else
  489. fputs(x, opt->diffopt.file);
  490. opt->shown_dashes = 1;
  491. }
  492. void show_log(struct rev_info *opt)
  493. {
  494. struct strbuf msgbuf = STRBUF_INIT;
  495. struct log_info *log = opt->loginfo;
  496. struct commit *commit = log->commit, *parent = log->parent;
  497. int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
  498. const char *extra_headers = opt->extra_headers;
  499. struct pretty_print_context ctx = {0};
  500. opt->loginfo = NULL;
  501. if (!opt->verbose_header) {
  502. graph_show_commit(opt->graph);
  503. if (!opt->graph)
  504. put_revision_mark(opt, commit);
  505. fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
  506. if (opt->print_parents)
  507. show_parents(commit, abbrev_commit, opt->diffopt.file);
  508. if (opt->children.name)
  509. show_children(opt, commit, abbrev_commit);
  510. show_decorations(opt, commit);
  511. if (opt->graph && !graph_is_commit_finished(opt->graph)) {
  512. putc('\n', opt->diffopt.file);
  513. graph_show_remainder(opt->graph);
  514. }
  515. putc(opt->diffopt.line_termination, opt->diffopt.file);
  516. return;
  517. }
  518. /*
  519. * If use_terminator is set, we already handled any record termination
  520. * at the end of the last record.
  521. * Otherwise, add a diffopt.line_termination character before all
  522. * entries but the first. (IOW, as a separator between entries)
  523. */
  524. if (opt->shown_one && !opt->use_terminator) {
  525. /*
  526. * If entries are separated by a newline, the output
  527. * should look human-readable. If the last entry ended
  528. * with a newline, print the graph output before this
  529. * newline. Otherwise it will end up as a completely blank
  530. * line and will look like a gap in the graph.
  531. *
  532. * If the entry separator is not a newline, the output is
  533. * primarily intended for programmatic consumption, and we
  534. * never want the extra graph output before the entry
  535. * separator.
  536. */
  537. if (opt->diffopt.line_termination == '\n' &&
  538. !opt->missing_newline)
  539. graph_show_padding(opt->graph);
  540. putc(opt->diffopt.line_termination, opt->diffopt.file);
  541. }
  542. opt->shown_one = 1;
  543. /*
  544. * If the history graph was requested,
  545. * print the graph, up to this commit's line
  546. */
  547. graph_show_commit(opt->graph);
  548. /*
  549. * Print header line of header..
  550. */
  551. if (cmit_fmt_is_mail(opt->commit_format)) {
  552. log_write_email_headers(opt, commit, &extra_headers,
  553. &ctx.need_8bit_cte, 1);
  554. ctx.rev = opt;
  555. ctx.print_email_subject = 1;
  556. } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
  557. fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
  558. if (opt->commit_format != CMIT_FMT_ONELINE)
  559. fputs("commit ", opt->diffopt.file);
  560. if (!opt->graph)
  561. put_revision_mark(opt, commit);
  562. fputs(find_unique_abbrev(&commit->object.oid,
  563. abbrev_commit),
  564. opt->diffopt.file);
  565. if (opt->print_parents)
  566. show_parents(commit, abbrev_commit, opt->diffopt.file);
  567. if (opt->children.name)
  568. show_children(opt, commit, abbrev_commit);
  569. if (parent)
  570. fprintf(opt->diffopt.file, " (from %s)",
  571. find_unique_abbrev(&parent->object.oid, abbrev_commit));
  572. fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
  573. show_decorations(opt, commit);
  574. if (opt->commit_format == CMIT_FMT_ONELINE) {
  575. putc(' ', opt->diffopt.file);
  576. } else {
  577. putc('\n', opt->diffopt.file);
  578. graph_show_oneline(opt->graph);
  579. }
  580. if (opt->reflog_info) {
  581. /*
  582. * setup_revisions() ensures that opt->reflog_info
  583. * and opt->graph cannot both be set,
  584. * so we don't need to worry about printing the
  585. * graph info here.
  586. */
  587. show_reflog_message(opt->reflog_info,
  588. opt->commit_format == CMIT_FMT_ONELINE,
  589. &opt->date_mode,
  590. opt->date_mode_explicit);
  591. if (opt->commit_format == CMIT_FMT_ONELINE)
  592. return;
  593. }
  594. }
  595. if (opt->show_signature) {
  596. show_signature(opt, commit);
  597. show_mergetag(opt, commit);
  598. }
  599. if (opt->show_notes) {
  600. int raw;
  601. struct strbuf notebuf = STRBUF_INIT;
  602. raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
  603. format_display_notes(&commit->object.oid, &notebuf,
  604. get_log_output_encoding(), raw);
  605. ctx.notes_message = strbuf_detach(&notebuf, NULL);
  606. }
  607. /*
  608. * And then the pretty-printed message itself
  609. */
  610. if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
  611. ctx.need_8bit_cte =
  612. has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
  613. ctx.date_mode = opt->date_mode;
  614. ctx.date_mode_explicit = opt->date_mode_explicit;
  615. ctx.abbrev = opt->diffopt.abbrev;
  616. ctx.after_subject = extra_headers;
  617. ctx.preserve_subject = opt->preserve_subject;
  618. ctx.reflog_info = opt->reflog_info;
  619. ctx.fmt = opt->commit_format;
  620. ctx.mailmap = opt->mailmap;
  621. ctx.color = opt->diffopt.use_color;
  622. ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
  623. ctx.output_encoding = get_log_output_encoding();
  624. ctx.rev = opt;
  625. if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
  626. ctx.from_ident = &opt->from_ident;
  627. if (opt->graph)
  628. ctx.graph_width = graph_width(opt->graph);
  629. pretty_print_commit(&ctx, commit, &msgbuf);
  630. if (opt->add_signoff)
  631. append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
  632. if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
  633. ctx.notes_message && *ctx.notes_message) {
  634. if (cmit_fmt_is_mail(ctx.fmt))
  635. next_commentary_block(opt, &msgbuf);
  636. strbuf_addstr(&msgbuf, ctx.notes_message);
  637. }
  638. if (opt->show_log_size) {
  639. fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
  640. graph_show_oneline(opt->graph);
  641. }
  642. /*
  643. * Set opt->missing_newline if msgbuf doesn't
  644. * end in a newline (including if it is empty)
  645. */
  646. if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
  647. opt->missing_newline = 1;
  648. else
  649. opt->missing_newline = 0;
  650. graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
  651. if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
  652. if (!opt->missing_newline)
  653. graph_show_padding(opt->graph);
  654. putc(opt->diffopt.line_termination, opt->diffopt.file);
  655. }
  656. strbuf_release(&msgbuf);
  657. free(ctx.notes_message);
  658. if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
  659. struct diff_queue_struct dq;
  660. memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
  661. DIFF_QUEUE_CLEAR(&diff_queued_diff);
  662. next_commentary_block(opt, NULL);
  663. fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
  664. show_interdiff(opt, 2);
  665. memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
  666. }
  667. if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
  668. struct diff_queue_struct dq;
  669. struct diff_options opts;
  670. memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
  671. DIFF_QUEUE_CLEAR(&diff_queued_diff);
  672. next_commentary_block(opt, NULL);
  673. fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
  674. /*
  675. * Pass minimum required diff-options to range-diff; others
  676. * can be added later if deemed desirable.
  677. */
  678. diff_setup(&opts);
  679. opts.file = opt->diffopt.file;
  680. opts.use_color = opt->diffopt.use_color;
  681. diff_setup_done(&opts);
  682. show_range_diff(opt->rdiff1, opt->rdiff2,
  683. opt->creation_factor, 1, &opts);
  684. memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
  685. }
  686. }
  687. int log_tree_diff_flush(struct rev_info *opt)
  688. {
  689. opt->shown_dashes = 0;
  690. diffcore_std(&opt->diffopt);
  691. if (diff_queue_is_empty()) {
  692. int saved_fmt = opt->diffopt.output_format;
  693. opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
  694. diff_flush(&opt->diffopt);
  695. opt->diffopt.output_format = saved_fmt;
  696. return 0;
  697. }
  698. if (opt->loginfo && !opt->no_commit_id) {
  699. show_log(opt);
  700. if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
  701. opt->verbose_header &&
  702. opt->commit_format != CMIT_FMT_ONELINE &&
  703. !commit_format_is_empty(opt->commit_format)) {
  704. /*
  705. * When showing a verbose header (i.e. log message),
  706. * and not in --pretty=oneline format, we would want
  707. * an extra newline between the end of log and the
  708. * diff/diffstat output for readability.
  709. */
  710. int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
  711. if (opt->diffopt.output_prefix) {
  712. struct strbuf *msg = NULL;
  713. msg = opt->diffopt.output_prefix(&opt->diffopt,
  714. opt->diffopt.output_prefix_data);
  715. fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
  716. }
  717. /*
  718. * We may have shown three-dashes line early
  719. * between generated commentary (notes, etc.)
  720. * and the log message, in which case we only
  721. * want a blank line after the commentary
  722. * without (an extra) three-dashes line.
  723. * Otherwise, we show the three-dashes line if
  724. * we are showing the patch with diffstat, but
  725. * in that case, there is no extra blank line
  726. * after the three-dashes line.
  727. */
  728. if (!opt->shown_dashes &&
  729. (pch & opt->diffopt.output_format) == pch)
  730. fprintf(opt->diffopt.file, "---");
  731. putc('\n', opt->diffopt.file);
  732. }
  733. }
  734. diff_flush(&opt->diffopt);
  735. return 1;
  736. }
  737. static int do_diff_combined(struct rev_info *opt, struct commit *commit)
  738. {
  739. diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
  740. return !opt->loginfo;
  741. }
  742. /*
  743. * Show the diff of a commit.
  744. *
  745. * Return true if we printed any log info messages
  746. */
  747. static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
  748. {
  749. int showed_log;
  750. struct commit_list *parents;
  751. struct object_id *oid;
  752. if (!opt->diff && !opt->diffopt.flags.exit_with_status)
  753. return 0;
  754. parse_commit_or_die(commit);
  755. oid = get_commit_tree_oid(commit);
  756. /* Root commit? */
  757. parents = get_saved_parents(opt, commit);
  758. if (!parents) {
  759. if (opt->show_root_diff) {
  760. diff_root_tree_oid(oid, "", &opt->diffopt);
  761. log_tree_diff_flush(opt);
  762. }
  763. return !opt->loginfo;
  764. }
  765. /* More than one parent? */
  766. if (parents && parents->next) {
  767. if (opt->ignore_merges)
  768. return 0;
  769. else if (opt->combine_merges)
  770. return do_diff_combined(opt, commit);
  771. else if (opt->first_parent_only) {
  772. /*
  773. * Generate merge log entry only for the first
  774. * parent, showing summary diff of the others
  775. * we merged _in_.
  776. */
  777. parse_commit_or_die(parents->item);
  778. diff_tree_oid(get_commit_tree_oid(parents->item),
  779. oid, "", &opt->diffopt);
  780. log_tree_diff_flush(opt);
  781. return !opt->loginfo;
  782. }
  783. /* If we show individual diffs, show the parent info */
  784. log->parent = parents->item;
  785. }
  786. showed_log = 0;
  787. for (;;) {
  788. struct commit *parent = parents->item;
  789. parse_commit_or_die(parent);
  790. diff_tree_oid(get_commit_tree_oid(parent),
  791. oid, "", &opt->diffopt);
  792. log_tree_diff_flush(opt);
  793. showed_log |= !opt->loginfo;
  794. /* Set up the log info for the next parent, if any.. */
  795. parents = parents->next;
  796. if (!parents)
  797. break;
  798. log->parent = parents->item;
  799. opt->loginfo = log;
  800. }
  801. return showed_log;
  802. }
  803. int log_tree_commit(struct rev_info *opt, struct commit *commit)
  804. {
  805. struct log_info log;
  806. int shown, close_file = opt->diffopt.close_file;
  807. log.commit = commit;
  808. log.parent = NULL;
  809. opt->loginfo = &log;
  810. opt->diffopt.close_file = 0;
  811. if (opt->line_level_traverse)
  812. return line_log_print(opt, commit);
  813. if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
  814. fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
  815. shown = log_tree_diff(opt, commit, &log);
  816. if (!shown && opt->loginfo && opt->always_show_header) {
  817. log.parent = NULL;
  818. show_log(opt);
  819. shown = 1;
  820. }
  821. if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
  822. fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
  823. opt->loginfo = NULL;
  824. maybe_flush_or_die(opt->diffopt.file, "stdout");
  825. if (close_file)
  826. fclose(opt->diffopt.file);
  827. return shown;
  828. }