cvsimport: miscellaneous packed-ref fixes
[git/dscho.git] / log-tree.c
blob9ebc24b68754a422a53270028b032395ad7c754a
1 #include "cache.h"
2 #include "diff.h"
3 #include "commit.h"
4 #include "log-tree.h"
5 #include "reflog-walk.h"
7 struct decoration name_decoration = { "object names" };
9 static void show_parents(struct commit *commit, int abbrev)
11 struct commit_list *p;
12 for (p = commit->parents; p ; p = p->next) {
13 struct commit *parent = p->item;
14 printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
18 static void show_decorations(struct commit *commit)
20 const char *prefix;
21 struct name_decoration *decoration;
23 decoration = lookup_decoration(&name_decoration, &commit->object);
24 if (!decoration)
25 return;
26 prefix = " (";
27 while (decoration) {
28 printf("%s%s", prefix, decoration->name);
29 prefix = ", ";
30 decoration = decoration->next;
32 putchar(')');
36 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
37 * Signed-off-by: and Acked-by: lines.
39 static int detect_any_signoff(char *letter, int size)
41 char ch, *cp;
42 int seen_colon = 0;
43 int seen_at = 0;
44 int seen_name = 0;
45 int seen_head = 0;
47 cp = letter + size;
48 while (letter <= --cp && (ch = *cp) == '\n')
49 continue;
51 while (letter <= cp) {
52 ch = *cp--;
53 if (ch == '\n')
54 break;
56 if (!seen_at) {
57 if (ch == '@')
58 seen_at = 1;
59 continue;
61 if (!seen_colon) {
62 if (ch == '@')
63 return 0;
64 else if (ch == ':')
65 seen_colon = 1;
66 else
67 seen_name = 1;
68 continue;
70 if (('A' <= ch && ch <= 'Z') ||
71 ('a' <= ch && ch <= 'z') ||
72 ch == '-') {
73 seen_head = 1;
74 continue;
76 /* no empty last line doesn't match */
77 return 0;
79 return seen_head && seen_name;
82 static unsigned long append_signoff(char **buf_p, unsigned long *buf_sz_p,
83 unsigned long at, const char *signoff)
85 static const char signed_off_by[] = "Signed-off-by: ";
86 size_t signoff_len = strlen(signoff);
87 int has_signoff = 0;
88 char *cp;
89 char *buf;
90 unsigned long buf_sz;
92 buf = *buf_p;
93 buf_sz = *buf_sz_p;
94 if (buf_sz <= at + strlen(signed_off_by) + signoff_len + 3) {
95 buf_sz += strlen(signed_off_by) + signoff_len + 3;
96 buf = xrealloc(buf, buf_sz);
97 *buf_p = buf;
98 *buf_sz_p = buf_sz;
100 cp = buf;
102 /* First see if we already have the sign-off by the signer */
103 while ((cp = strstr(cp, signed_off_by))) {
105 has_signoff = 1;
107 cp += strlen(signed_off_by);
108 if (cp + signoff_len >= buf + at)
109 break;
110 if (strncmp(cp, signoff, signoff_len))
111 continue;
112 if (!isspace(cp[signoff_len]))
113 continue;
114 /* we already have him */
115 return at;
118 if (!has_signoff)
119 has_signoff = detect_any_signoff(buf, at);
121 if (!has_signoff)
122 buf[at++] = '\n';
124 strcpy(buf + at, signed_off_by);
125 at += strlen(signed_off_by);
126 strcpy(buf + at, signoff);
127 at += signoff_len;
128 buf[at++] = '\n';
129 buf[at] = 0;
130 return at;
133 static unsigned int digits_in_number(unsigned int number)
135 unsigned int i = 10, result = 1;
136 while (i <= number) {
137 i *= 10;
138 result++;
140 return result;
143 static int has_non_ascii(const char *s)
145 int ch;
146 if (!s)
147 return 0;
148 while ((ch = *s++) != '\0') {
149 if (non_ascii(ch))
150 return 1;
152 return 0;
155 void show_log(struct rev_info *opt, const char *sep)
157 char *msgbuf = NULL;
158 unsigned long msgbuf_len = 0;
159 struct log_info *log = opt->loginfo;
160 struct commit *commit = log->commit, *parent = log->parent;
161 int abbrev = opt->diffopt.abbrev;
162 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
163 const char *extra;
164 int len;
165 const char *subject = NULL, *extra_headers = opt->extra_headers;
167 opt->loginfo = NULL;
168 if (!opt->verbose_header) {
169 if (opt->left_right) {
170 if (commit->object.flags & BOUNDARY)
171 putchar('-');
172 else if (commit->object.flags & SYMMETRIC_LEFT)
173 putchar('<');
174 else
175 putchar('>');
177 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
178 if (opt->parents)
179 show_parents(commit, abbrev_commit);
180 show_decorations(commit);
181 putchar(opt->diffopt.line_termination);
182 return;
186 * The "oneline" format has several special cases:
187 * - The pretty-printed commit lacks a newline at the end
188 * of the buffer, but we do want to make sure that we
189 * have a newline there. If the separator isn't already
190 * a newline, add an extra one.
191 * - unlike other log messages, the one-line format does
192 * not have an empty line between entries.
194 extra = "";
195 if (*sep != '\n' && opt->commit_format == CMIT_FMT_ONELINE)
196 extra = "\n";
197 if (opt->shown_one && opt->commit_format != CMIT_FMT_ONELINE)
198 putchar(opt->diffopt.line_termination);
199 opt->shown_one = 1;
202 * Print header line of header..
205 if (opt->commit_format == CMIT_FMT_EMAIL) {
206 char *sha1 = sha1_to_hex(commit->object.sha1);
207 if (opt->total > 0) {
208 static char buffer[64];
209 snprintf(buffer, sizeof(buffer),
210 "Subject: [%s %0*d/%d] ",
211 opt->subject_prefix,
212 digits_in_number(opt->total),
213 opt->nr, opt->total);
214 subject = buffer;
215 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
216 static char buffer[256];
217 snprintf(buffer, sizeof(buffer),
218 "Subject: [%s] ",
219 opt->subject_prefix);
220 subject = buffer;
221 } else {
222 subject = "Subject: ";
225 printf("From %s Mon Sep 17 00:00:00 2001\n", sha1);
226 if (opt->message_id)
227 printf("Message-Id: <%s>\n", opt->message_id);
228 if (opt->ref_message_id)
229 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
230 opt->ref_message_id, opt->ref_message_id);
231 if (opt->mime_boundary) {
232 static char subject_buffer[1024];
233 static char buffer[1024];
234 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
235 "%s"
236 "MIME-Version: 1.0\n"
237 "Content-Type: multipart/mixed;"
238 " boundary=\"%s%s\"\n"
239 "\n"
240 "This is a multi-part message in MIME "
241 "format.\n"
242 "--%s%s\n"
243 "Content-Type: text/plain; "
244 "charset=UTF-8; format=fixed\n"
245 "Content-Transfer-Encoding: 8bit\n\n",
246 extra_headers ? extra_headers : "",
247 mime_boundary_leader, opt->mime_boundary,
248 mime_boundary_leader, opt->mime_boundary);
249 extra_headers = subject_buffer;
251 snprintf(buffer, sizeof(buffer) - 1,
252 "--%s%s\n"
253 "Content-Type: text/x-patch;"
254 " name=\"%s.diff\"\n"
255 "Content-Transfer-Encoding: 8bit\n"
256 "Content-Disposition: %s;"
257 " filename=\"%s.diff\"\n\n",
258 mime_boundary_leader, opt->mime_boundary,
259 sha1,
260 opt->no_inline ? "attachment" : "inline",
261 sha1);
262 opt->diffopt.stat_sep = buffer;
264 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
265 fputs(diff_get_color(opt->diffopt.color_diff, DIFF_COMMIT),
266 stdout);
267 if (opt->commit_format != CMIT_FMT_ONELINE)
268 fputs("commit ", stdout);
269 if (commit->object.flags & BOUNDARY)
270 putchar('-');
271 else if (opt->left_right) {
272 if (commit->object.flags & SYMMETRIC_LEFT)
273 putchar('<');
274 else
275 putchar('>');
277 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
278 stdout);
279 if (opt->parents)
280 show_parents(commit, abbrev_commit);
281 if (parent)
282 printf(" (from %s)",
283 diff_unique_abbrev(parent->object.sha1,
284 abbrev_commit));
285 show_decorations(commit);
286 printf("%s",
287 diff_get_color(opt->diffopt.color_diff, DIFF_RESET));
288 putchar(opt->commit_format == CMIT_FMT_ONELINE ? ' ' : '\n');
289 if (opt->reflog_info) {
290 show_reflog_message(opt->reflog_info,
291 opt->commit_format == CMIT_FMT_ONELINE,
292 opt->date_mode);
293 if (opt->commit_format == CMIT_FMT_ONELINE) {
294 printf("%s", sep);
295 return;
301 * And then the pretty-printed message itself
303 len = pretty_print_commit(opt->commit_format, commit, ~0u,
304 &msgbuf, &msgbuf_len, abbrev, subject,
305 extra_headers, opt->date_mode,
306 has_non_ascii(opt->add_signoff));
308 if (opt->add_signoff)
309 len = append_signoff(&msgbuf, &msgbuf_len, len,
310 opt->add_signoff);
311 if (opt->show_log_size)
312 printf("log size %i\n", len);
314 printf("%s%s%s", msgbuf, extra, sep);
315 free(msgbuf);
318 int log_tree_diff_flush(struct rev_info *opt)
320 diffcore_std(&opt->diffopt);
322 if (diff_queue_is_empty()) {
323 int saved_fmt = opt->diffopt.output_format;
324 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
325 diff_flush(&opt->diffopt);
326 opt->diffopt.output_format = saved_fmt;
327 return 0;
330 if (opt->loginfo && !opt->no_commit_id) {
331 /* When showing a verbose header (i.e. log message),
332 * and not in --pretty=oneline format, we would want
333 * an extra newline between the end of log and the
334 * output for readability.
336 show_log(opt, opt->diffopt.msg_sep);
337 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
338 opt->verbose_header &&
339 opt->commit_format != CMIT_FMT_ONELINE) {
340 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
341 if ((pch & opt->diffopt.output_format) == pch)
342 printf("---");
343 putchar('\n');
346 diff_flush(&opt->diffopt);
347 return 1;
350 static int do_diff_combined(struct rev_info *opt, struct commit *commit)
352 unsigned const char *sha1 = commit->object.sha1;
354 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
355 return !opt->loginfo;
359 * Show the diff of a commit.
361 * Return true if we printed any log info messages
363 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
365 int showed_log;
366 struct commit_list *parents;
367 unsigned const char *sha1 = commit->object.sha1;
369 if (!opt->diff)
370 return 0;
372 /* Root commit? */
373 parents = commit->parents;
374 if (!parents) {
375 if (opt->show_root_diff) {
376 diff_root_tree_sha1(sha1, "", &opt->diffopt);
377 log_tree_diff_flush(opt);
379 return !opt->loginfo;
382 /* More than one parent? */
383 if (parents && parents->next) {
384 if (opt->ignore_merges)
385 return 0;
386 else if (opt->combine_merges)
387 return do_diff_combined(opt, commit);
389 /* If we show individual diffs, show the parent info */
390 log->parent = parents->item;
393 showed_log = 0;
394 for (;;) {
395 struct commit *parent = parents->item;
397 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
398 log_tree_diff_flush(opt);
400 showed_log |= !opt->loginfo;
402 /* Set up the log info for the next parent, if any.. */
403 parents = parents->next;
404 if (!parents)
405 break;
406 log->parent = parents->item;
407 opt->loginfo = log;
409 return showed_log;
412 int log_tree_commit(struct rev_info *opt, struct commit *commit)
414 struct log_info log;
415 int shown;
417 log.commit = commit;
418 log.parent = NULL;
419 opt->loginfo = &log;
421 shown = log_tree_diff(opt, commit, &log);
422 if (!shown && opt->loginfo && opt->always_show_header) {
423 log.parent = NULL;
424 show_log(opt, "");
425 shown = 1;
427 opt->loginfo = NULL;
428 maybe_flush_or_die(stdout, "stdout");
429 return shown;