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.

941 lines
27KB

  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 signature_check sigc = { 0 };
  402. int status;
  403. if (parse_signed_commit(commit, &payload, &signature) <= 0)
  404. goto out;
  405. status = check_signature(payload.buf, payload.len, signature.buf,
  406. signature.len, &sigc);
  407. if (status && sigc.result == 'N')
  408. show_sig_lines(opt, status, "No signature\n");
  409. else {
  410. show_sig_lines(opt, status, sigc.gpg_output);
  411. signature_check_clear(&sigc);
  412. }
  413. out:
  414. strbuf_release(&payload);
  415. strbuf_release(&signature);
  416. }
  417. static int which_parent(const struct object_id *oid, const struct commit *commit)
  418. {
  419. int nth;
  420. const struct commit_list *parent;
  421. for (nth = 0, parent = commit->parents; parent; parent = parent->next) {
  422. if (oideq(&parent->item->object.oid, oid))
  423. return nth;
  424. nth++;
  425. }
  426. return -1;
  427. }
  428. static int is_common_merge(const struct commit *commit)
  429. {
  430. return (commit->parents
  431. && commit->parents->next
  432. && !commit->parents->next->next);
  433. }
  434. static int show_one_mergetag(struct commit *commit,
  435. struct commit_extra_header *extra,
  436. void *data)
  437. {
  438. struct rev_info *opt = (struct rev_info *)data;
  439. struct object_id oid;
  440. struct tag *tag;
  441. struct strbuf verify_message;
  442. struct signature_check sigc = { 0 };
  443. int status, nth;
  444. size_t payload_size, gpg_message_offset;
  445. hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &oid);
  446. tag = lookup_tag(the_repository, &oid);
  447. if (!tag)
  448. return -1; /* error message already given */
  449. strbuf_init(&verify_message, 256);
  450. if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
  451. strbuf_addstr(&verify_message, "malformed mergetag\n");
  452. else if (is_common_merge(commit) &&
  453. oideq(&tag->tagged->oid,
  454. &commit->parents->next->item->object.oid))
  455. strbuf_addf(&verify_message,
  456. "merged tag '%s'\n", tag->tag);
  457. else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0)
  458. strbuf_addf(&verify_message, "tag %s names a non-parent %s\n",
  459. tag->tag, tag->tagged->oid.hash);
  460. else
  461. strbuf_addf(&verify_message,
  462. "parent #%d, tagged '%s'\n", nth + 1, tag->tag);
  463. gpg_message_offset = verify_message.len;
  464. payload_size = parse_signature(extra->value, extra->len);
  465. status = -1;
  466. if (extra->len > payload_size) {
  467. /* could have a good signature */
  468. if (!check_signature(extra->value, payload_size,
  469. extra->value + payload_size,
  470. extra->len - payload_size, &sigc)) {
  471. strbuf_addstr(&verify_message, sigc.gpg_output);
  472. signature_check_clear(&sigc);
  473. status = 0; /* good */
  474. } else if (verify_message.len <= gpg_message_offset)
  475. strbuf_addstr(&verify_message, "No signature\n");
  476. /* otherwise we couldn't verify, which is shown as bad */
  477. }
  478. show_sig_lines(opt, status, verify_message.buf);
  479. strbuf_release(&verify_message);
  480. return 0;
  481. }
  482. static int show_mergetag(struct rev_info *opt, struct commit *commit)
  483. {
  484. return for_each_mergetag(show_one_mergetag, commit, opt);
  485. }
  486. static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
  487. {
  488. const char *x = opt->shown_dashes ? "\n" : "---\n";
  489. if (sb)
  490. strbuf_addstr(sb, x);
  491. else
  492. fputs(x, opt->diffopt.file);
  493. opt->shown_dashes = 1;
  494. }
  495. void show_log(struct rev_info *opt)
  496. {
  497. struct strbuf msgbuf = STRBUF_INIT;
  498. struct log_info *log = opt->loginfo;
  499. struct commit *commit = log->commit, *parent = log->parent;
  500. int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz;
  501. const char *extra_headers = opt->extra_headers;
  502. struct pretty_print_context ctx = {0};
  503. opt->loginfo = NULL;
  504. if (!opt->verbose_header) {
  505. graph_show_commit(opt->graph);
  506. if (!opt->graph)
  507. put_revision_mark(opt, commit);
  508. fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file);
  509. if (opt->print_parents)
  510. show_parents(commit, abbrev_commit, opt->diffopt.file);
  511. if (opt->children.name)
  512. show_children(opt, commit, abbrev_commit);
  513. show_decorations(opt, commit);
  514. if (opt->graph && !graph_is_commit_finished(opt->graph)) {
  515. putc('\n', opt->diffopt.file);
  516. graph_show_remainder(opt->graph);
  517. }
  518. putc(opt->diffopt.line_termination, opt->diffopt.file);
  519. return;
  520. }
  521. /*
  522. * If use_terminator is set, we already handled any record termination
  523. * at the end of the last record.
  524. * Otherwise, add a diffopt.line_termination character before all
  525. * entries but the first. (IOW, as a separator between entries)
  526. */
  527. if (opt->shown_one && !opt->use_terminator) {
  528. /*
  529. * If entries are separated by a newline, the output
  530. * should look human-readable. If the last entry ended
  531. * with a newline, print the graph output before this
  532. * newline. Otherwise it will end up as a completely blank
  533. * line and will look like a gap in the graph.
  534. *
  535. * If the entry separator is not a newline, the output is
  536. * primarily intended for programmatic consumption, and we
  537. * never want the extra graph output before the entry
  538. * separator.
  539. */
  540. if (opt->diffopt.line_termination == '\n' &&
  541. !opt->missing_newline)
  542. graph_show_padding(opt->graph);
  543. putc(opt->diffopt.line_termination, opt->diffopt.file);
  544. }
  545. opt->shown_one = 1;
  546. /*
  547. * If the history graph was requested,
  548. * print the graph, up to this commit's line
  549. */
  550. graph_show_commit(opt->graph);
  551. /*
  552. * Print header line of header..
  553. */
  554. if (cmit_fmt_is_mail(opt->commit_format)) {
  555. log_write_email_headers(opt, commit, &extra_headers,
  556. &ctx.need_8bit_cte, 1);
  557. ctx.rev = opt;
  558. ctx.print_email_subject = 1;
  559. } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
  560. fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
  561. if (opt->commit_format != CMIT_FMT_ONELINE)
  562. fputs("commit ", opt->diffopt.file);
  563. if (!opt->graph)
  564. put_revision_mark(opt, commit);
  565. fputs(find_unique_abbrev(&commit->object.oid,
  566. abbrev_commit),
  567. opt->diffopt.file);
  568. if (opt->print_parents)
  569. show_parents(commit, abbrev_commit, opt->diffopt.file);
  570. if (opt->children.name)
  571. show_children(opt, commit, abbrev_commit);
  572. if (parent)
  573. fprintf(opt->diffopt.file, " (from %s)",
  574. find_unique_abbrev(&parent->object.oid, abbrev_commit));
  575. fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
  576. show_decorations(opt, commit);
  577. if (opt->commit_format == CMIT_FMT_ONELINE) {
  578. putc(' ', opt->diffopt.file);
  579. } else {
  580. putc('\n', opt->diffopt.file);
  581. graph_show_oneline(opt->graph);
  582. }
  583. if (opt->reflog_info) {
  584. /*
  585. * setup_revisions() ensures that opt->reflog_info
  586. * and opt->graph cannot both be set,
  587. * so we don't need to worry about printing the
  588. * graph info here.
  589. */
  590. show_reflog_message(opt->reflog_info,
  591. opt->commit_format == CMIT_FMT_ONELINE,
  592. &opt->date_mode,
  593. opt->date_mode_explicit);
  594. if (opt->commit_format == CMIT_FMT_ONELINE)
  595. return;
  596. }
  597. }
  598. if (opt->show_signature) {
  599. show_signature(opt, commit);
  600. show_mergetag(opt, commit);
  601. }
  602. if (opt->show_notes) {
  603. int raw;
  604. struct strbuf notebuf = STRBUF_INIT;
  605. raw = (opt->commit_format == CMIT_FMT_USERFORMAT);
  606. format_display_notes(&commit->object.oid, &notebuf,
  607. get_log_output_encoding(), raw);
  608. ctx.notes_message = strbuf_detach(&notebuf, NULL);
  609. }
  610. /*
  611. * And then the pretty-printed message itself
  612. */
  613. if (ctx.need_8bit_cte >= 0 && opt->add_signoff)
  614. ctx.need_8bit_cte =
  615. has_non_ascii(fmt_name(WANT_COMMITTER_IDENT));
  616. ctx.date_mode = opt->date_mode;
  617. ctx.date_mode_explicit = opt->date_mode_explicit;
  618. ctx.abbrev = opt->diffopt.abbrev;
  619. ctx.after_subject = extra_headers;
  620. ctx.preserve_subject = opt->preserve_subject;
  621. ctx.reflog_info = opt->reflog_info;
  622. ctx.fmt = opt->commit_format;
  623. ctx.mailmap = opt->mailmap;
  624. ctx.color = opt->diffopt.use_color;
  625. ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
  626. ctx.output_encoding = get_log_output_encoding();
  627. ctx.rev = opt;
  628. if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
  629. ctx.from_ident = &opt->from_ident;
  630. if (opt->graph)
  631. ctx.graph_width = graph_width(opt->graph);
  632. pretty_print_commit(&ctx, commit, &msgbuf);
  633. if (opt->add_signoff)
  634. append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP);
  635. if ((ctx.fmt != CMIT_FMT_USERFORMAT) &&
  636. ctx.notes_message && *ctx.notes_message) {
  637. if (cmit_fmt_is_mail(ctx.fmt))
  638. next_commentary_block(opt, &msgbuf);
  639. strbuf_addstr(&msgbuf, ctx.notes_message);
  640. }
  641. if (opt->show_log_size) {
  642. fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len);
  643. graph_show_oneline(opt->graph);
  644. }
  645. /*
  646. * Set opt->missing_newline if msgbuf doesn't
  647. * end in a newline (including if it is empty)
  648. */
  649. if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
  650. opt->missing_newline = 1;
  651. else
  652. opt->missing_newline = 0;
  653. graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf);
  654. if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) {
  655. if (!opt->missing_newline)
  656. graph_show_padding(opt->graph);
  657. putc(opt->diffopt.line_termination, opt->diffopt.file);
  658. }
  659. strbuf_release(&msgbuf);
  660. free(ctx.notes_message);
  661. if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) {
  662. struct diff_queue_struct dq;
  663. memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
  664. DIFF_QUEUE_CLEAR(&diff_queued_diff);
  665. next_commentary_block(opt, NULL);
  666. fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title);
  667. show_interdiff(opt, 2);
  668. memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
  669. }
  670. if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) {
  671. struct diff_queue_struct dq;
  672. struct diff_options opts;
  673. memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff));
  674. DIFF_QUEUE_CLEAR(&diff_queued_diff);
  675. next_commentary_block(opt, NULL);
  676. fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title);
  677. /*
  678. * Pass minimum required diff-options to range-diff; others
  679. * can be added later if deemed desirable.
  680. */
  681. diff_setup(&opts);
  682. opts.file = opt->diffopt.file;
  683. opts.use_color = opt->diffopt.use_color;
  684. diff_setup_done(&opts);
  685. show_range_diff(opt->rdiff1, opt->rdiff2,
  686. opt->creation_factor, 1, &opts, NULL);
  687. memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff));
  688. }
  689. }
  690. int log_tree_diff_flush(struct rev_info *opt)
  691. {
  692. opt->shown_dashes = 0;
  693. diffcore_std(&opt->diffopt);
  694. if (diff_queue_is_empty()) {
  695. int saved_fmt = opt->diffopt.output_format;
  696. opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
  697. diff_flush(&opt->diffopt);
  698. opt->diffopt.output_format = saved_fmt;
  699. return 0;
  700. }
  701. if (opt->loginfo && !opt->no_commit_id) {
  702. show_log(opt);
  703. if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
  704. opt->verbose_header &&
  705. opt->commit_format != CMIT_FMT_ONELINE &&
  706. !commit_format_is_empty(opt->commit_format)) {
  707. /*
  708. * When showing a verbose header (i.e. log message),
  709. * and not in --pretty=oneline format, we would want
  710. * an extra newline between the end of log and the
  711. * diff/diffstat output for readability.
  712. */
  713. int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
  714. if (opt->diffopt.output_prefix) {
  715. struct strbuf *msg = NULL;
  716. msg = opt->diffopt.output_prefix(&opt->diffopt,
  717. opt->diffopt.output_prefix_data);
  718. fwrite(msg->buf, msg->len, 1, opt->diffopt.file);
  719. }
  720. /*
  721. * We may have shown three-dashes line early
  722. * between generated commentary (notes, etc.)
  723. * and the log message, in which case we only
  724. * want a blank line after the commentary
  725. * without (an extra) three-dashes line.
  726. * Otherwise, we show the three-dashes line if
  727. * we are showing the patch with diffstat, but
  728. * in that case, there is no extra blank line
  729. * after the three-dashes line.
  730. */
  731. if (!opt->shown_dashes &&
  732. (pch & opt->diffopt.output_format) == pch)
  733. fprintf(opt->diffopt.file, "---");
  734. putc('\n', opt->diffopt.file);
  735. }
  736. }
  737. diff_flush(&opt->diffopt);
  738. return 1;
  739. }
  740. static int do_diff_combined(struct rev_info *opt, struct commit *commit)
  741. {
  742. diff_tree_combined_merge(commit, opt->dense_combined_merges, opt);
  743. return !opt->loginfo;
  744. }
  745. /*
  746. * Show the diff of a commit.
  747. *
  748. * Return true if we printed any log info messages
  749. */
  750. static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
  751. {
  752. int showed_log;
  753. struct commit_list *parents;
  754. struct object_id *oid;
  755. if (!opt->diff && !opt->diffopt.flags.exit_with_status)
  756. return 0;
  757. parse_commit_or_die(commit);
  758. oid = get_commit_tree_oid(commit);
  759. /* Root commit? */
  760. parents = get_saved_parents(opt, commit);
  761. if (!parents) {
  762. if (opt->show_root_diff) {
  763. diff_root_tree_oid(oid, "", &opt->diffopt);
  764. log_tree_diff_flush(opt);
  765. }
  766. return !opt->loginfo;
  767. }
  768. /* More than one parent? */
  769. if (parents && parents->next) {
  770. if (opt->ignore_merges)
  771. return 0;
  772. else if (opt->combine_merges)
  773. return do_diff_combined(opt, commit);
  774. else if (opt->first_parent_only) {
  775. /*
  776. * Generate merge log entry only for the first
  777. * parent, showing summary diff of the others
  778. * we merged _in_.
  779. */
  780. parse_commit_or_die(parents->item);
  781. diff_tree_oid(get_commit_tree_oid(parents->item),
  782. oid, "", &opt->diffopt);
  783. log_tree_diff_flush(opt);
  784. return !opt->loginfo;
  785. }
  786. /* If we show individual diffs, show the parent info */
  787. log->parent = parents->item;
  788. }
  789. showed_log = 0;
  790. for (;;) {
  791. struct commit *parent = parents->item;
  792. parse_commit_or_die(parent);
  793. diff_tree_oid(get_commit_tree_oid(parent),
  794. oid, "", &opt->diffopt);
  795. log_tree_diff_flush(opt);
  796. showed_log |= !opt->loginfo;
  797. /* Set up the log info for the next parent, if any.. */
  798. parents = parents->next;
  799. if (!parents)
  800. break;
  801. log->parent = parents->item;
  802. opt->loginfo = log;
  803. }
  804. return showed_log;
  805. }
  806. int log_tree_commit(struct rev_info *opt, struct commit *commit)
  807. {
  808. struct log_info log;
  809. int shown, close_file = opt->diffopt.close_file;
  810. log.commit = commit;
  811. log.parent = NULL;
  812. opt->loginfo = &log;
  813. opt->diffopt.close_file = 0;
  814. if (opt->line_level_traverse)
  815. return line_log_print(opt, commit);
  816. if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
  817. fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
  818. shown = log_tree_diff(opt, commit, &log);
  819. if (!shown && opt->loginfo && opt->always_show_header) {
  820. log.parent = NULL;
  821. show_log(opt);
  822. shown = 1;
  823. }
  824. if (opt->track_linear && !opt->linear && opt->reverse_output_stage)
  825. fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
  826. opt->loginfo = NULL;
  827. maybe_flush_or_die(opt->diffopt.file, "stdout");
  828. if (close_file)
  829. fclose(opt->diffopt.file);
  830. return shown;
  831. }