]> git.ipfire.org Git - thirdparty/git.git/blame - log-tree.c
print an error message for invalid path
[thirdparty/git.git] / log-tree.c
CommitLineData
5f1c3f07
JH
1#include "cache.h"
2#include "diff.h"
3#include "commit.h"
cab4feb6 4#include "tag.h"
7fefda5c 5#include "graph.h"
5f1c3f07 6#include "log-tree.h"
8860fd42 7#include "reflog-walk.h"
cab4feb6 8#include "refs.h"
5f1c3f07 9
ca135e7a
LT
10struct decoration name_decoration = { "object names" };
11
cab4feb6
RS
12static void add_name_decoration(const char *prefix, const char *name, struct object *obj)
13{
14 int plen = strlen(prefix);
15 int nlen = strlen(name);
16 struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + plen + nlen);
17 memcpy(res->name, prefix, plen);
18 memcpy(res->name + plen, name, nlen + 1);
19 res->next = add_decoration(&name_decoration, obj, res);
20}
21
22static int add_ref_decoration(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
23{
24 struct object *obj = parse_object(sha1);
25 if (!obj)
26 return 0;
27 add_name_decoration("", refname, obj);
28 while (obj->type == OBJ_TAG) {
29 obj = ((struct tag *)obj)->tagged;
30 if (!obj)
31 break;
32 add_name_decoration("tag: ", refname, obj);
33 }
34 return 0;
35}
36
37void load_ref_decorations(void)
38{
39 static int loaded;
40 if (!loaded) {
41 loaded = 1;
42 for_each_ref(add_ref_decoration, NULL);
43 }
44}
45
c8c893c6
LT
46static void show_parents(struct commit *commit, int abbrev)
47{
48 struct commit_list *p;
49 for (p = commit->parents; p ; p = p->next) {
50 struct commit *parent = p->item;
51 printf(" %s", diff_unique_abbrev(parent->object.sha1, abbrev));
52 }
53}
54
50e62a8e 55void show_decorations(struct commit *commit)
ca135e7a
LT
56{
57 const char *prefix;
58 struct name_decoration *decoration;
59
60 decoration = lookup_decoration(&name_decoration, &commit->object);
61 if (!decoration)
62 return;
63 prefix = " (";
64 while (decoration) {
65 printf("%s%s", prefix, decoration->name);
66 prefix = ", ";
67 decoration = decoration->next;
68 }
69 putchar(')');
70}
71
fc1c75ec
FBH
72/*
73 * Search for "^[-A-Za-z]+: [^@]+@" pattern. It usually matches
74 * Signed-off-by: and Acked-by: lines.
75 */
76static int detect_any_signoff(char *letter, int size)
77{
78 char ch, *cp;
79 int seen_colon = 0;
80 int seen_at = 0;
81 int seen_name = 0;
82 int seen_head = 0;
83
84 cp = letter + size;
85 while (letter <= --cp && (ch = *cp) == '\n')
86 continue;
87
88 while (letter <= cp) {
89 ch = *cp--;
90 if (ch == '\n')
91 break;
92
93 if (!seen_at) {
94 if (ch == '@')
95 seen_at = 1;
96 continue;
97 }
98 if (!seen_colon) {
99 if (ch == '@')
100 return 0;
101 else if (ch == ':')
102 seen_colon = 1;
103 else
104 seen_name = 1;
105 continue;
106 }
107 if (('A' <= ch && ch <= 'Z') ||
108 ('a' <= ch && ch <= 'z') ||
109 ch == '-') {
110 seen_head = 1;
111 continue;
112 }
113 /* no empty last line doesn't match */
114 return 0;
115 }
116 return seen_head && seen_name;
117}
118
674d1727 119static void append_signoff(struct strbuf *sb, const char *signoff)
cf2251b6 120{
cf2251b6 121 static const char signed_off_by[] = "Signed-off-by: ";
80583c0e 122 size_t signoff_len = strlen(signoff);
fc1c75ec 123 int has_signoff = 0;
80583c0e 124 char *cp;
cf2251b6 125
674d1727 126 cp = sb->buf;
cf2251b6
JH
127
128 /* First see if we already have the sign-off by the signer */
fc1c75ec
FBH
129 while ((cp = strstr(cp, signed_off_by))) {
130
131 has_signoff = 1;
132
cf2251b6 133 cp += strlen(signed_off_by);
674d1727 134 if (cp + signoff_len >= sb->buf + sb->len)
fc1c75ec
FBH
135 break;
136 if (strncmp(cp, signoff, signoff_len))
137 continue;
138 if (!isspace(cp[signoff_len]))
139 continue;
140 /* we already have him */
674d1727 141 return;
cf2251b6
JH
142 }
143
fc1c75ec 144 if (!has_signoff)
674d1727 145 has_signoff = detect_any_signoff(sb->buf, sb->len);
fc1c75ec
FBH
146
147 if (!has_signoff)
674d1727 148 strbuf_addch(sb, '\n');
c35f4c37 149
674d1727
PH
150 strbuf_addstr(sb, signed_off_by);
151 strbuf_add(sb, signoff, signoff_len);
152 strbuf_addch(sb, '\n');
cf2251b6
JH
153}
154
e00de24b
JS
155static unsigned int digits_in_number(unsigned int number)
156{
157 unsigned int i = 10, result = 1;
158 while (i <= number) {
159 i *= 10;
160 result++;
161 }
162 return result;
163}
164
4593fb84
JH
165static int has_non_ascii(const char *s)
166{
167 int ch;
168 if (!s)
169 return 0;
170 while ((ch = *s++) != '\0') {
171 if (non_ascii(ch))
172 return 1;
173 }
174 return 0;
175}
176
b02bd65f 177void log_write_email_headers(struct rev_info *opt, const char *name,
267123b4
JH
178 const char **subject_p,
179 const char **extra_headers_p,
180 int *need_8bit_cte_p)
b02bd65f
DB
181{
182 const char *subject = NULL;
183 const char *extra_headers = opt->extra_headers;
267123b4
JH
184
185 *need_8bit_cte_p = 0; /* unknown */
b02bd65f
DB
186 if (opt->total > 0) {
187 static char buffer[64];
188 snprintf(buffer, sizeof(buffer),
189 "Subject: [%s %0*d/%d] ",
190 opt->subject_prefix,
191 digits_in_number(opt->total),
192 opt->nr, opt->total);
193 subject = buffer;
194 } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) {
195 static char buffer[256];
196 snprintf(buffer, sizeof(buffer),
197 "Subject: [%s] ",
198 opt->subject_prefix);
199 subject = buffer;
200 } else {
201 subject = "Subject: ";
202 }
203
204 printf("From %s Mon Sep 17 00:00:00 2001\n", name);
7fefda5c
AS
205 graph_show_oneline(opt->graph);
206 if (opt->message_id) {
b02bd65f 207 printf("Message-Id: <%s>\n", opt->message_id);
7fefda5c
AS
208 graph_show_oneline(opt->graph);
209 }
210 if (opt->ref_message_id) {
b02bd65f
DB
211 printf("In-Reply-To: <%s>\nReferences: <%s>\n",
212 opt->ref_message_id, opt->ref_message_id);
7fefda5c
AS
213 graph_show_oneline(opt->graph);
214 }
b02bd65f
DB
215 if (opt->mime_boundary) {
216 static char subject_buffer[1024];
217 static char buffer[1024];
267123b4 218 *need_8bit_cte_p = -1; /* NEVER */
b02bd65f
DB
219 snprintf(subject_buffer, sizeof(subject_buffer) - 1,
220 "%s"
221 "MIME-Version: 1.0\n"
222 "Content-Type: multipart/mixed;"
223 " boundary=\"%s%s\"\n"
224 "\n"
225 "This is a multi-part message in MIME "
226 "format.\n"
227 "--%s%s\n"
228 "Content-Type: text/plain; "
229 "charset=UTF-8; format=fixed\n"
230 "Content-Transfer-Encoding: 8bit\n\n",
231 extra_headers ? extra_headers : "",
232 mime_boundary_leader, opt->mime_boundary,
233 mime_boundary_leader, opt->mime_boundary);
234 extra_headers = subject_buffer;
235
236 snprintf(buffer, sizeof(buffer) - 1,
6b2fbaaf 237 "\n--%s%s\n"
b02bd65f
DB
238 "Content-Type: text/x-patch;"
239 " name=\"%s.diff\"\n"
240 "Content-Transfer-Encoding: 8bit\n"
241 "Content-Disposition: %s;"
242 " filename=\"%s.diff\"\n\n",
243 mime_boundary_leader, opt->mime_boundary,
244 name,
245 opt->no_inline ? "attachment" : "inline",
246 name);
247 opt->diffopt.stat_sep = buffer;
248 }
249 *subject_p = subject;
250 *extra_headers_p = extra_headers;
251}
252
02865655 253void show_log(struct rev_info *opt)
91539833 254{
674d1727 255 struct strbuf msgbuf;
39bc9a6c 256 struct log_info *log = opt->loginfo;
91539833
LT
257 struct commit *commit = log->commit, *parent = log->parent;
258 int abbrev = opt->diffopt.abbrev;
259 int abbrev_commit = opt->abbrev_commit ? opt->abbrev : 40;
20ff0680 260 const char *subject = NULL, *extra_headers = opt->extra_headers;
6bf4f1b4 261 int need_8bit_cte = 0;
91539833
LT
262
263 opt->loginfo = NULL;
264 if (!opt->verbose_header) {
7fefda5c
AS
265 graph_show_commit(opt->graph);
266
7528f27d
AS
267 if (!opt->graph) {
268 if (commit->object.flags & BOUNDARY)
269 putchar('-');
270 else if (commit->object.flags & UNINTERESTING)
271 putchar('^');
272 else if (opt->left_right) {
273 if (commit->object.flags & SYMMETRIC_LEFT)
274 putchar('<');
275 else
276 putchar('>');
277 }
74bd9029 278 }
c8c893c6 279 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit), stdout);
885cf808 280 if (opt->print_parents)
c8c893c6 281 show_parents(commit, abbrev_commit);
ca135e7a 282 show_decorations(commit);
7fefda5c
AS
283 if (opt->graph && !graph_is_commit_finished(opt->graph)) {
284 putchar('\n');
285 graph_show_remainder(opt->graph);
286 }
1dcb6922 287 putchar(opt->diffopt.line_termination);
91539833
LT
288 return;
289 }
290
291 /*
02865655
AS
292 * If use_terminator is set, add a newline at the end of the entry.
293 * Otherwise, add a diffopt.line_termination character before all
294 * entries but the first. (IOW, as a separator between entries)
91539833 295 */
7fefda5c
AS
296 if (opt->shown_one && !opt->use_terminator) {
297 /*
298 * If entries are separated by a newline, the output
299 * should look human-readable. If the last entry ended
300 * with a newline, print the graph output before this
301 * newline. Otherwise it will end up as a completely blank
302 * line and will look like a gap in the graph.
303 *
304 * If the entry separator is not a newline, the output is
305 * primarily intended for programmatic consumption, and we
306 * never want the extra graph output before the entry
307 * separator.
308 */
309 if (opt->diffopt.line_termination == '\n' &&
310 !opt->missing_newline)
311 graph_show_padding(opt->graph);
abd4e222 312 putchar(opt->diffopt.line_termination);
7fefda5c 313 }
91539833
LT
314 opt->shown_one = 1;
315
7fefda5c
AS
316 /*
317 * If the history graph was requested,
318 * print the graph, up to this commit's line
319 */
320 graph_show_commit(opt->graph);
321
91539833
LT
322 /*
323 * Print header line of header..
324 */
3eefc189 325
596524b3 326 if (opt->commit_format == CMIT_FMT_EMAIL) {
b02bd65f 327 log_write_email_headers(opt, sha1_to_hex(commit->object.sha1),
267123b4
JH
328 &subject, &extra_headers,
329 &need_8bit_cte);
e52a5de4 330 } else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
8f67f8ae 331 fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);
74bd9029
JH
332 if (opt->commit_format != CMIT_FMT_ONELINE)
333 fputs("commit ", stdout);
7528f27d
AS
334
335 if (!opt->graph) {
336 if (commit->object.flags & BOUNDARY)
337 putchar('-');
338 else if (commit->object.flags & UNINTERESTING)
339 putchar('^');
340 else if (opt->left_right) {
341 if (commit->object.flags & SYMMETRIC_LEFT)
342 putchar('<');
343 else
344 putchar('>');
345 }
74bd9029
JH
346 }
347 fputs(diff_unique_abbrev(commit->object.sha1, abbrev_commit),
348 stdout);
885cf808 349 if (opt->print_parents)
c66b6c06 350 show_parents(commit, abbrev_commit);
73f0a157 351 if (parent)
3eefc189
JH
352 printf(" (from %s)",
353 diff_unique_abbrev(parent->object.sha1,
354 abbrev_commit));
ca135e7a 355 show_decorations(commit);
8f67f8ae 356 printf("%s", diff_get_color_opt(&opt->diffopt, DIFF_RESET));
7fefda5c
AS
357 if (opt->commit_format == CMIT_FMT_ONELINE) {
358 putchar(' ');
359 } else {
360 putchar('\n');
361 graph_show_oneline(opt->graph);
362 }
903b45fe 363 if (opt->reflog_info) {
7fefda5c
AS
364 /*
365 * setup_revisions() ensures that opt->reflog_info
366 * and opt->graph cannot both be set,
367 * so we don't need to worry about printing the
368 * graph info here.
369 */
4d12a471 370 show_reflog_message(opt->reflog_info,
4e244cbc 371 opt->commit_format == CMIT_FMT_ONELINE,
a7b02ccf 372 opt->date_mode);
02865655 373 if (opt->commit_format == CMIT_FMT_ONELINE)
903b45fe 374 return;
903b45fe 375 }
3eefc189 376 }
91539833 377
3131b713
LT
378 if (!commit->buffer)
379 return;
380
91539833
LT
381 /*
382 * And then the pretty-printed message itself
383 */
674d1727 384 strbuf_init(&msgbuf, 0);
6bf4f1b4
JH
385 if (need_8bit_cte >= 0)
386 need_8bit_cte = has_non_ascii(opt->add_signoff);
674d1727 387 pretty_print_commit(opt->commit_format, commit, &msgbuf,
4593fb84 388 abbrev, subject, extra_headers, opt->date_mode,
6bf4f1b4 389 need_8bit_cte);
cf2251b6
JH
390
391 if (opt->add_signoff)
674d1727 392 append_signoff(&msgbuf, opt->add_signoff);
7fefda5c 393 if (opt->show_log_size) {
674d1727 394 printf("log size %i\n", (int)msgbuf.len);
7fefda5c
AS
395 graph_show_oneline(opt->graph);
396 }
9fa3465d 397
7fefda5c
AS
398 /*
399 * Set opt->missing_newline if msgbuf doesn't
400 * end in a newline (including if it is empty)
401 */
402 if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n')
403 opt->missing_newline = 1;
404 else
405 opt->missing_newline = 0;
406
407 if (opt->graph)
408 graph_show_commit_msg(opt->graph, &msgbuf);
409 else
42c8c74c 410 fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
7fefda5c
AS
411 if (opt->use_terminator) {
412 if (!opt->missing_newline)
413 graph_show_padding(opt->graph);
9b58bfe8 414 putchar('\n');
7fefda5c
AS
415 }
416
674d1727 417 strbuf_release(&msgbuf);
91539833
LT
418}
419
cd2bdc53 420int log_tree_diff_flush(struct rev_info *opt)
5f1c3f07
JH
421{
422 diffcore_std(&opt->diffopt);
91539833 423
5f1c3f07
JH
424 if (diff_queue_is_empty()) {
425 int saved_fmt = opt->diffopt.output_format;
426 opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT;
427 diff_flush(&opt->diffopt);
428 opt->diffopt.output_format = saved_fmt;
429 return 0;
430 }
91539833 431
3969cf7d
JH
432 if (opt->loginfo && !opt->no_commit_id) {
433 /* When showing a verbose header (i.e. log message),
434 * and not in --pretty=oneline format, we would want
435 * an extra newline between the end of log and the
436 * output for readability.
437 */
02865655 438 show_log(opt);
304b5af6
LT
439 if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) &&
440 opt->verbose_header &&
3969cf7d
JH
441 opt->commit_format != CMIT_FMT_ONELINE) {
442 int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;
443 if ((pch & opt->diffopt.output_format) == pch)
abd4e222
LT
444 printf("---");
445 putchar('\n');
3969cf7d
JH
446 }
447 }
5f1c3f07
JH
448 diff_flush(&opt->diffopt);
449 return 1;
450}
451
cd2bdc53 452static int do_diff_combined(struct rev_info *opt, struct commit *commit)
5f1c3f07
JH
453{
454 unsigned const char *sha1 = commit->object.sha1;
455
91539833
LT
456 diff_tree_combined_merge(sha1, opt->dense_combined_merges, opt);
457 return !opt->loginfo;
5f1c3f07
JH
458}
459
91539833
LT
460/*
461 * Show the diff of a commit.
462 *
463 * Return true if we printed any log info messages
464 */
465static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
5f1c3f07 466{
91539833 467 int showed_log;
5f1c3f07
JH
468 struct commit_list *parents;
469 unsigned const char *sha1 = commit->object.sha1;
470
84102a33 471 if (!opt->diff && !DIFF_OPT_TST(&opt->diffopt, EXIT_WITH_STATUS))
91539833
LT
472 return 0;
473
5f1c3f07 474 /* Root commit? */
91539833
LT
475 parents = commit->parents;
476 if (!parents) {
2b60356d
RS
477 if (opt->show_root_diff) {
478 diff_root_tree_sha1(sha1, "", &opt->diffopt);
479 log_tree_diff_flush(opt);
480 }
91539833 481 return !opt->loginfo;
5f1c3f07
JH
482 }
483
484 /* More than one parent? */
91539833 485 if (parents && parents->next) {
5f1c3f07
JH
486 if (opt->ignore_merges)
487 return 0;
488 else if (opt->combine_merges)
489 return do_diff_combined(opt, commit);
91539833
LT
490
491 /* If we show individual diffs, show the parent info */
492 log->parent = parents->item;
5f1c3f07
JH
493 }
494
91539833
LT
495 showed_log = 0;
496 for (;;) {
5f1c3f07 497 struct commit *parent = parents->item;
5f1c3f07 498
91539833
LT
499 diff_tree_sha1(parent->object.sha1, sha1, "", &opt->diffopt);
500 log_tree_diff_flush(opt);
501
502 showed_log |= !opt->loginfo;
503
504 /* Set up the log info for the next parent, if any.. */
505 parents = parents->next;
506 if (!parents)
507 break;
508 log->parent = parents->item;
509 opt->loginfo = log;
510 }
511 return showed_log;
512}
513
514int log_tree_commit(struct rev_info *opt, struct commit *commit)
515{
516 struct log_info log;
3eefc189 517 int shown;
91539833
LT
518
519 log.commit = commit;
520 log.parent = NULL;
521 opt->loginfo = &log;
522
3eefc189
JH
523 shown = log_tree_diff(opt, commit, &log);
524 if (!shown && opt->loginfo && opt->always_show_header) {
91539833 525 log.parent = NULL;
02865655 526 show_log(opt);
3eefc189 527 shown = 1;
5f1c3f07 528 }
91539833 529 opt->loginfo = NULL;
06f59e9f 530 maybe_flush_or_die(stdout, "stdout");
3eefc189 531 return shown;
5f1c3f07 532}