typechange tests for git apply (currently failing)
[git] / log-tree.c
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "log-tree.h"
5
6 static 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
15 static int append_signoff(char *buf, int buf_sz, int at, const char *signoff)
16 {
17         int signoff_len = strlen(signoff);
18         static const char signed_off_by[] = "Signed-off-by: ";
19         char *cp = buf;
20
21         /* Do we have enough space to add it? */
22         if (buf_sz - at <= strlen(signed_off_by) + signoff_len + 2)
23                 return at;
24
25         /* First see if we already have the sign-off by the signer */
26         while (1) {
27                 cp = strstr(cp, signed_off_by);
28                 if (!cp)
29                         break;
30                 cp += strlen(signed_off_by);
31                 if ((cp + signoff_len < buf + at) &&
32                     !strncmp(cp, signoff, signoff_len) &&
33                     isspace(cp[signoff_len]))
34                         return at; /* we already have him */
35         }
36
37         strcpy(buf + at, signed_off_by);
38         at += strlen(signed_off_by);
39         strcpy(buf + at, signoff);
40         at += signoff_len;
41         buf[at++] = '\n';
42         buf[at] = 0;
43         return at;
44 }
45
46 void show_log(struct rev_info *opt, const char *sep)
47 {
48         static char this_header[16384];
49         struct log_info *log = opt->loginfo;
50         struct commit *commit = log->commit, *parent = log->parent;
51         int abbrev = opt->diffopt.abbrev;
52         int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
53         const char *extra;
54         int len;
55         const char *subject = NULL, *extra_headers = opt->extra_headers;
56
57         opt->loginfo = NULL;
58         if (!opt->verbose_header) {
59                 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
60                 if (opt->parents)
61                         show_parents(commit, abbrev_commit);
62                 putchar('\n');
63                 return;
64         }
65
66         /*
67          * The "oneline" format has several special cases:
68          *  - The pretty-printed commit lacks a newline at the end
69          *    of the buffer, but we do want to make sure that we
70          *    have a newline there. If the separator isn't already
71          *    a newline, add an extra one.
72          *  - unlike other log messages, the one-line format does
73          *    not have an empty line between entries.
74          */
75         extra = "";
76         if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
77                 extra = "\n";
78         if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
79                 putchar('\n');
80         opt->shown_one = 1;
81
82         /*
83          * Print header line of header..
84          */
85
86         if (opt->commit_format == CMIT_FMT_EMAIL) {
87                 char *sha1 = sha1_to_hex(commit->object.sha1);
88                 if (opt->total > 0) {
89                         static char buffer[64];
90                         snprintf(buffer, sizeof(buffer),
91                                         "Subject: [PATCH %d/%d] ",
92                                         opt->nr, opt->total);
93                         subject = buffer;
94                 } else if (opt->total == 0)
95                         subject = "Subject: [PATCH] ";
96                 else
97                         subject = "Subject: ";
98
99                 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
100                 if (opt->mime_boundary) {
101                         static char subject_buffer[1024];
102                         static char buffer[1024];
103                         snprintf(subject_buffer, sizeof(subject_buffer) - 1,
104                                  "%s"
105                                  "MIME-Version: 1.0\n"
106                                  "Content-Type: multipart/mixed;\n"
107                                  " boundary=\"%s%s\"\n"
108                                  "\n"
109                                  "This is a multi-part message in MIME "
110                                  "format.\n"
111                                  "--%s%s\n"
112                                  "Content-Type: text/plain; "
113                                  "charset=UTF-8; format=fixed\n"
114                                  "Content-Transfer-Encoding: 8bit\n\n",
115                                  extra_headers ? extra_headers : "",
116                                  mime_boundary_leader, opt->mime_boundary,
117                                  mime_boundary_leader, opt->mime_boundary);
118                         extra_headers = subject_buffer;
119
120                         snprintf(buffer, sizeof(buffer) - 1,
121                                  "--%s%s\n"
122                                  "Content-Type: text/x-patch;\n"
123                                  " name=\"%s.diff\"\n"
124                                  "Content-Transfer-Encoding: 8bit\n"
125                                  "Content-Disposition: inline;\n"
126                                  " filename=\"%s.diff\"\n\n",
127                                  mime_boundary_leader, opt->mime_boundary,
128                                  sha1, sha1);
129                         opt->diffopt.stat_sep = buffer;
130                 }
131         } else {
132                 printf("%s%s",
133                        opt->commit_format == CMIT_FMT_ONELINE ? "" : "commit ",
134                        diff_unique_abbrev(commit->object.sha1, abbrev_commit));
135                 if (opt->parents)
136                         show_parents(commit, abbrev_commit);
137                 if (parent)
138                         printf(" (from %s)",
139                                diff_unique_abbrev(parent->object.sha1,
140                                                   abbrev_commit));
141                 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
142         }
143
144         /*
145          * And then the pretty-printed message itself
146          */
147         len = pretty_print_commit(opt->commit_format, commit, ~0u, this_header, sizeof(this_header), abbrev, subject, extra_headers);
148
149         if (opt->add_signoff)
150                 len = append_signoff(this_header, sizeof(this_header), len,
151                                      opt->add_signoff);
152         printf("%s%s%s", this_header, extra, sep);
153 }
154
155 int log_tree_diff_flush(struct rev_info *opt)
156 {
157         diffcore_std(&opt->diffopt);
158
159         if (diff_queue_is_empty()) {
160                 int saved_fmt = opt->diffopt.output_format;
161                 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
162                 diff_flush(&opt->diffopt);
163                 opt->diffopt.output_format = saved_fmt;
164                 return 0;
165         }
166
167         if (opt->loginfo && !opt->no_commit_id) {
168                 /* When showing a verbose header (i.e. log message),
169                  * and not in --pretty=oneline format, we would want
170                  * an extra newline between the end of log and the
171                  * output for readability.
172                  */
173                 show_log(opt, opt->diffopt.msg_sep);
174                 if (opt->verbose_header &&
175                     opt->commit_format != CMIT_FMT_ONELINE) {
176                         int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
177                         if ((pch & opt->diffopt.output_format) == pch)
178                                 printf("---%c", opt->diffopt.line_termination);
179                         else
180                                 putchar(opt->diffopt.line_termination);
181                 }
182         }
183         diff_flush(&opt->diffopt);
184         return 1;
185 }
186
187 static int diff_root_tree(struct rev_info *opt,
188                           const unsigned char *new, const char *base)
189 {
190         int retval;
191         void *tree;
192         struct tree_desc empty, real;
193
194         tree = read_object_with_reference(new, tree_type, &real.size, NULL);
195         if (!tree)
196                 die("unable to read root tree (%s)", sha1_to_hex(new));
197         real.buf = tree;
198
199         empty.buf = "";
200         empty.size = 0;
201         retval = diff_tree(&empty, &real, base, &opt->diffopt);
202         free(tree);
203         log_tree_diff_flush(opt);
204         return retval;
205 }
206
207 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
208 {
209         unsigned const char *sha1 = commit->object.sha1;
210
211         diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
212         return !opt->loginfo;
213 }
214
215 /*
216  * Show the diff of a commit.
217  *
218  * Return true if we printed any log info messages
219  */
220 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
221 {
222         int showed_log;
223         struct commit_list *parents;
224         unsigned const char *sha1 = commit->object.sha1;
225
226         if (!opt->diff)
227                 return 0;
228
229         /* Root commit? */
230         parents = commit->parents;
231         if (!parents) {
232                 if (opt->show_root_diff)
233                         diff_root_tree(opt, sha1, "");
234                 return !opt->loginfo;
235         }
236
237         /* More than one parent? */
238         if (parents && parents->next) {
239                 if (opt->ignore_merges)
240                         return 0;
241                 else if (opt->combine_merges)
242                         return do_diff_combined(opt, commit);
243
244                 /* If we show individual diffs, show the parent info */
245                 log->parent = parents->item;
246         }
247
248         showed_log = 0;
249         for (;;) {
250                 struct commit *parent = parents->item;
251
252                 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
253                 log_tree_diff_flush(opt);
254
255                 showed_log |= !opt->loginfo;
256
257                 /* Set up the log info for the next parent, if any.. */
258                 parents = parents->next;
259                 if (!parents)
260                         break;
261                 log->parent = parents->item;
262                 opt->loginfo = log;
263         }
264         return showed_log;
265 }
266
267 int log_tree_commit(struct rev_info *opt, struct commit *commit)
268 {
269         struct log_info log;
270         int shown;
271
272         log.commit = commit;
273         log.parent = NULL;
274         opt->loginfo = &log;
275
276         shown = log_tree_diff(opt, commit, &log);
277         if (!shown && opt->loginfo && opt->always_show_header) {
278                 log.parent = NULL;
279                 show_log(opt, "");
280                 shown = 1;
281         }
282         opt->loginfo = NULL;
283         return shown;
284 }