Log message printout cleanups (#2)
[git/jrn.git] / log-tree.c
blob9e5416427a38359078ab35902bad2e7cffac2a0b
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "log-tree.h"
6 void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
8 static char this_header[16384];
9 struct commit *commit = log->commit, *parent = log->parent;
10 int abbrev = opt->diffopt.abbrev;
11 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
12 int len;
14 opt->loginfo = NULL;
15 if (!opt->verbose_header) {
16 puts(sha1_to_hex(commit->object.sha1));
17 return;
21 * Whitespace between commit messages, unless we are oneline
23 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
24 putchar('\n');
25 opt->shown_one = 1;
28 * Print header line of header..
30 printf("%s%s",
31 opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
32 diff_unique_abbrev(commit->object.sha1, abbrev_commit));
33 if (parent)
34 printf(" (from %s)", diff_unique_abbrev(parent->object.sha1, abbrev_commit));
35 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
38 * And then the pretty-printed message itself
40 len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev);
41 printf("%s%s", this_header, sep);
44 int log_tree_diff_flush(struct rev_info *opt)
46 diffcore_std(&opt->diffopt);
48 if (diff_queue_is_empty()) {
49 int saved_fmt = opt->diffopt.output_format;
50 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
51 diff_flush(&opt->diffopt);
52 opt->diffopt.output_format = saved_fmt;
53 return 0;
56 if (opt->loginfo && !opt->no_commit_id)
57 show_log(opt, opt->loginfo, opt->diffopt.with_stat ? "---\n" : "\n");
58 diff_flush(&opt->diffopt);
59 return 1;
62 static int diff_root_tree(struct rev_info *opt,
63 const unsigned char *new, const char *base)
65 int retval;
66 void *tree;
67 struct tree_desc empty, real;
69 tree = read_object_with_reference(new, tree_type, &real.size, NULL);
70 if (!tree)
71 die("unable to read root tree (%s)", sha1_to_hex(new));
72 real.buf = tree;
74 empty.buf = "";
75 empty.size = 0;
76 retval = diff_tree(&empty, &real, base, &opt->diffopt);
77 free(tree);
78 log_tree_diff_flush(opt);
79 return retval;
82 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
84 unsigned const char *sha1 = commit->object.sha1;
86 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
87 return !opt->loginfo;
91 * Show the diff of a commit.
93 * Return true if we printed any log info messages
95 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
97 int showed_log;
98 struct commit_list *parents;
99 unsigned const char *sha1 = commit->object.sha1;
101 if (!opt->diff)
102 return 0;
104 /* Root commit? */
105 parents = commit->parents;
106 if (!parents) {
107 if (opt->show_root_diff)
108 diff_root_tree(opt, sha1, "");
109 return !opt->loginfo;
112 /* More than one parent? */
113 if (parents && parents->next) {
114 if (opt->ignore_merges)
115 return 0;
116 else if (opt->combine_merges)
117 return do_diff_combined(opt, commit);
119 /* If we show individual diffs, show the parent info */
120 log->parent = parents->item;
123 showed_log = 0;
124 for (;;) {
125 struct commit *parent = parents->item;
127 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
128 log_tree_diff_flush(opt);
130 showed_log |= !opt->loginfo;
132 /* Set up the log info for the next parent, if any.. */
133 parents = parents->next;
134 if (!parents)
135 break;
136 log->parent = parents->item;
137 opt->loginfo = log;
139 return showed_log;
142 int log_tree_commit(struct rev_info *opt, struct commit *commit)
144 struct log_info log;
146 log.commit = commit;
147 log.parent = NULL;
148 opt->loginfo = &log;
150 if (!log_tree_diff(opt, commit, &log) && opt->loginfo && opt->always_show_header) {
151 log.parent = NULL;
152 show_log(opt, opt->loginfo, "");
154 opt->loginfo = NULL;
155 return 0;