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