526d578e98eef5a099be43bf39a300b33ac1991c
1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
4#include "log-tree.h"
5
6static void show_parents(struct commit *commit, int abbrev)
7{
8 struct commit_list *p;
9 for (p = commit->parents; p ; p = p->next) {
10 struct commit *parent = p->item;
11 printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
12 }
13}
14
15void show_log(struct rev_info *opt, struct log_info *log, const char *sep)
16{
17 static char this_header[16384];
18 struct commit *commit = log->commit, *parent = log->parent;
19 int abbrev = opt->diffopt.abbrev;
20 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
21 const char *extra;
22 int len;
23 char* subject = NULL;
24
25 opt->loginfo = NULL;
26 if (!opt->verbose_header) {
27 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
28 if (opt->parents)
29 show_parents(commit, abbrev_commit);
30 putchar('\n');
31 return;
32 }
33
34 /*
35 * The "oneline" format has several special cases:
36 * - The pretty-printed commit lacks a newline at the end
37 * of the buffer, but we do want to make sure that we
38 * have a newline there. If the separator isn't already
39 * a newline, add an extra one.
40 * - unlike other log messages, the one-line format does
41 * not have an empty line between entries.
42 */
43 extra = "";
44 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
45 extra = "\n";
46 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
47 putchar('\n');
48 opt->shown_one = 1;
49
50 /*
51 * Print header line of header..
52 */
53
54 if (opt->commit_format == CMIT_FMT_EMAIL) {
55 if (opt->total > 0) {
56 static char buffer[64];
57 snprintf(buffer, sizeof(buffer),
58 "Subject: [PATCH %d/%d] ",
59 opt->nr, opt->total);
60 subject = buffer;
61 } else if (opt->total == 0)
62 subject = "Subject: [PATCH] ";
63 else
64 subject = "Subject: ";
65
66 printf("From %s Thu Apr 7 15:13:13 2005\n",
67 sha1_to_hex(commit->object.sha1));
68 } else {
69 printf("%s%s",
70 opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
71 diff_unique_abbrev(commit->object.sha1, abbrev_commit));
72 if (opt->parents)
73 show_parents(commit, abbrev_commit);
74 if (parent)
75 printf(" (from %s)",
76 diff_unique_abbrev(parent->object.sha1,
77 abbrev_commit));
78 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
79 }
80
81 /*
82 * And then the pretty-printed message itself
83 */
84 len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev, subject);
85 printf("%s%s%s", this_header, extra, sep);
86}
87
88int log_tree_diff_flush(struct rev_info *opt)
89{
90 diffcore_std(&opt->diffopt);
91
92 if (diff_queue_is_empty()) {
93 int saved_fmt = opt->diffopt.output_format;
94 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
95 diff_flush(&opt->diffopt);
96 opt->diffopt.output_format = saved_fmt;
97 return 0;
98 }
99
100 if (opt->loginfo && !opt->no_commit_id)
101 show_log(opt, opt->loginfo, opt->diffopt.with_stat ? "---\n" : "\n");
102 diff_flush(&opt->diffopt);
103 return 1;
104}
105
106static int diff_root_tree(struct rev_info *opt,
107 const unsigned char *new, const char *base)
108{
109 int retval;
110 void *tree;
111 struct tree_desc empty, real;
112
113 tree = read_object_with_reference(new, tree_type, &real.size, NULL);
114 if (!tree)
115 die("unable to read root tree (%s)", sha1_to_hex(new));
116 real.buf = tree;
117
118 empty.buf = "";
119 empty.size = 0;
120 retval = diff_tree(&empty, &real, base, &opt->diffopt);
121 free(tree);
122 log_tree_diff_flush(opt);
123 return retval;
124}
125
126static int do_diff_combined(struct rev_info *opt, struct commit *commit)
127{
128 unsigned const char *sha1 = commit->object.sha1;
129
130 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
131 return !opt->loginfo;
132}
133
134/*
135 * Show the diff of a commit.
136 *
137 * Return true if we printed any log info messages
138 */
139static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
140{
141 int showed_log;
142 struct commit_list *parents;
143 unsigned const char *sha1 = commit->object.sha1;
144
145 if (!opt->diff)
146 return 0;
147
148 /* Root commit? */
149 parents = commit->parents;
150 if (!parents) {
151 if (opt->show_root_diff)
152 diff_root_tree(opt, sha1, "");
153 return !opt->loginfo;
154 }
155
156 /* More than one parent? */
157 if (parents && parents->next) {
158 if (opt->ignore_merges)
159 return 0;
160 else if (opt->combine_merges)
161 return do_diff_combined(opt, commit);
162
163 /* If we show individual diffs, show the parent info */
164 log->parent = parents->item;
165 }
166
167 showed_log = 0;
168 for (;;) {
169 struct commit *parent = parents->item;
170
171 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
172 log_tree_diff_flush(opt);
173
174 showed_log |= !opt->loginfo;
175
176 /* Set up the log info for the next parent, if any.. */
177 parents = parents->next;
178 if (!parents)
179 break;
180 log->parent = parents->item;
181 opt->loginfo = log;
182 }
183 return showed_log;
184}
185
186int log_tree_commit(struct rev_info *opt, struct commit *commit)
187{
188 struct log_info log;
189 int shown;
190
191 log.commit = commit;
192 log.parent = NULL;
193 opt->loginfo = &log;
194
195 shown = log_tree_diff(opt, commit, &log);
196 if (!shown && opt->loginfo && opt->always_show_header) {
197 log.parent = NULL;
198 show_log(opt, opt->loginfo, "");
199 shown = 1;
200 }
201 opt->loginfo = NULL;
202 return shown;
203}