Merge branch 'master' into js/fmt-patch
[git/git.git] / log-tree.c
CommitLineData
5f1c3f07
JH
1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
4#include "log-tree.h"
5
c8c893c6
LT
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
91539833
LT
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;
a4d34e2d 21 const char *extra;
91539833 22 int len;
596524b3 23 char* subject = NULL;
91539833
LT
24
25 opt->loginfo = NULL;
26 if (!opt->verbose_header) {
c8c893c6
LT
27 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
28 if (opt->parents)
29 show_parents(commit, abbrev_commit);
30 putchar('\n');
91539833
LT
31 return;
32 }
33
34 /*
a4d34e2d
LT
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.
91539833 42 */
a4d34e2d
LT
43 extra = "";
44 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
45 extra = "\n";
91539833
LT
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 */
3eefc189 53
596524b3
JS
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;
8ac80a57 61 } else if (opt->total == 0)
596524b3 62 subject = "Subject: [PATCH] ";
8ac80a57
JS
63 else
64 subject = "Subject: ";
65
3eefc189
JH
66 printf("From %s Thu Apr 7 15:13:13 2005\n",
67 sha1_to_hex(commit->object.sha1));
596524b3 68 } else {
3eefc189
JH
69 printf("%s%s",
70 opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
71 diff_unique_abbrev(commit->object.sha1, abbrev_commit));
c66b6c06
JH
72 if (opt->parents)
73 show_parents(commit, abbrev_commit);
3eefc189
JH
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 }
91539833
LT
80
81 /*
82 * And then the pretty-printed message itself
83 */
596524b3 84 len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev, subject);
a4d34e2d 85 printf("%s%s%s", this_header, extra, sep);
91539833
LT
86}
87
cd2bdc53 88int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07
JH
89{
90 diffcore_std(&opt->diffopt);
91539833 91
5f1c3f07
JH
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 }
91539833
LT
99
100 if (opt->loginfo && !opt->no_commit_id)
eab144ac 101 show_log(opt, opt->loginfo, opt->diffopt.with_stat ? "---\n" : "\n");
5f1c3f07
JH
102 diff_flush(&opt->diffopt);
103 return 1;
104}
105
cd2bdc53 106static int diff_root_tree(struct rev_info *opt,
5f1c3f07
JH
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
cd2bdc53 126static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07
JH
127{
128 unsigned const char *sha1 = commit->object.sha1;
129
91539833
LT
130 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
131 return !opt->loginfo;
5f1c3f07
JH
132}
133
91539833
LT
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)
5f1c3f07 140{
91539833 141 int showed_log;
5f1c3f07
JH
142 struct commit_list *parents;
143 unsigned const char *sha1 = commit->object.sha1;
144
91539833
LT
145 if (!opt->diff)
146 return 0;
147
5f1c3f07 148 /* Root commit? */
91539833
LT
149 parents = commit->parents;
150 if (!parents) {
151 if (opt->show_root_diff)
152 diff_root_tree(opt, sha1, "");
153 return !opt->loginfo;
5f1c3f07
JH
154 }
155
156 /* More than one parent? */
91539833 157 if (parents && parents->next) {
5f1c3f07
JH
158 if (opt->ignore_merges)
159 return 0;
160 else if (opt->combine_merges)
161 return do_diff_combined(opt, commit);
91539833
LT
162
163 /* If we show individual diffs, show the parent info */
164 log->parent = parents->item;
5f1c3f07
JH
165 }
166
91539833
LT
167 showed_log = 0;
168 for (;;) {
5f1c3f07 169 struct commit *parent = parents->item;
5f1c3f07 170
91539833
LT
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;
3eefc189 189 int shown;
91539833
LT
190
191 log.commit = commit;
192 log.parent = NULL;
193 opt->loginfo = &log;
194
3eefc189
JH
195 shown = log_tree_diff(opt, commit, &log);
196 if (!shown && opt->loginfo && opt->always_show_header) {
91539833
LT
197 log.parent = NULL;
198 show_log(opt, opt->loginfo, "");
3eefc189 199 shown = 1;
5f1c3f07 200 }
91539833 201 opt->loginfo = NULL;
3eefc189 202 return shown;
5f1c3f07 203}