ui-shared: extract date formatting to a function
[cgit.git] / ui-log.c
blob6bff94807886c39e5e15ec58e66f67a81b50187b
1 /* ui-log.c: functions for log output
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
9 #include "cgit.h"
10 #include "ui-log.h"
11 #include "html.h"
12 #include "ui-shared.h"
13 #include "argv-array.h"
15 static int files, add_lines, rem_lines, lines_counted;
18 * The list of available column colors in the commit graph.
20 static const char *column_colors_html[] = {
21 "<span class='column1'>",
22 "<span class='column2'>",
23 "<span class='column3'>",
24 "<span class='column4'>",
25 "<span class='column5'>",
26 "<span class='column6'>",
27 "</span>",
30 #define COLUMN_COLORS_HTML_MAX (ARRAY_SIZE(column_colors_html) - 1)
32 static void count_lines(char *line, int size)
34 if (size <= 0)
35 return;
37 if (line[0] == '+')
38 add_lines++;
40 else if (line[0] == '-')
41 rem_lines++;
44 static void inspect_files(struct diff_filepair *pair)
46 unsigned long old_size = 0;
47 unsigned long new_size = 0;
48 int binary = 0;
50 files++;
51 if (ctx.repo->enable_log_linecount)
52 cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
53 &new_size, &binary, 0, ctx.qry.ignorews,
54 count_lines);
57 void show_commit_decorations(struct commit *commit)
59 const struct name_decoration *deco;
60 static char buf[1024];
62 buf[sizeof(buf) - 1] = 0;
63 deco = get_name_decoration(&commit->object);
64 html("<span class='decoration'>");
65 while (deco) {
66 if (starts_with(deco->name, "refs/heads/")) {
67 strncpy(buf, deco->name + 11, sizeof(buf) - 1);
68 cgit_log_link(buf, NULL, "branch-deco", buf, NULL,
69 ctx.qry.vpath, 0, NULL, NULL,
70 ctx.qry.showmsg, 0);
72 else if (starts_with(deco->name, "tag: refs/tags/")) {
73 strncpy(buf, deco->name + 15, sizeof(buf) - 1);
74 cgit_tag_link(buf, NULL, "tag-deco", buf);
76 else if (starts_with(deco->name, "refs/tags/")) {
77 strncpy(buf, deco->name + 10, sizeof(buf) - 1);
78 cgit_tag_link(buf, NULL, "tag-deco", buf);
80 else if (starts_with(deco->name, "refs/remotes/")) {
81 if (!ctx.repo->enable_remote_branches)
82 goto next;
83 strncpy(buf, deco->name + 13, sizeof(buf) - 1);
84 cgit_log_link(buf, NULL, "remote-deco", NULL,
85 sha1_to_hex(commit->object.sha1),
86 ctx.qry.vpath, 0, NULL, NULL,
87 ctx.qry.showmsg, 0);
89 else {
90 strncpy(buf, deco->name, sizeof(buf) - 1);
91 cgit_commit_link(buf, NULL, "deco", ctx.qry.head,
92 sha1_to_hex(commit->object.sha1),
93 ctx.qry.vpath);
95 next:
96 deco = deco->next;
98 html("</span>");
101 static void handle_rename(struct diff_filepair *pair)
104 * After we have seen a rename, we generate links to the previous
105 * name of the file so that commit & diff views get fed the path
106 * that is correct for the commit they are showing, avoiding the
107 * need to walk the entire history leading back to every commit we
108 * show in order detect renames.
110 if (0 != strcmp(ctx.qry.vpath, pair->two->path)) {
111 free(ctx.qry.vpath);
112 ctx.qry.vpath = xstrdup(pair->two->path);
114 inspect_files(pair);
117 static int show_commit(struct commit *commit, struct rev_info *revs)
119 struct commit_list *parents = commit->parents;
120 struct commit *parent;
121 int found = 0, saved_fmt;
122 unsigned saved_flags = revs->diffopt.flags;
125 /* Always show if we're not in "follow" mode with a single file. */
126 if (!ctx.qry.follow)
127 return 1;
130 * In "follow" mode, we don't show merges. This is consistent with
131 * "git log --follow -- <file>".
133 if (parents && parents->next)
134 return 0;
137 * If this is the root commit, do what rev_info tells us.
139 if (!parents)
140 return revs->show_root_diff;
142 /* When we get here we have precisely one parent. */
143 parent = parents->item;
144 parse_commit(parent);
146 files = 0;
147 add_lines = 0;
148 rem_lines = 0;
150 DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
151 diff_tree_sha1(parent->tree->object.sha1,
152 commit->tree->object.sha1,
153 "", &revs->diffopt);
154 diffcore_std(&revs->diffopt);
156 found = !diff_queue_is_empty();
157 saved_fmt = revs->diffopt.output_format;
158 revs->diffopt.output_format = DIFF_FORMAT_CALLBACK;
159 revs->diffopt.format_callback = cgit_diff_tree_cb;
160 revs->diffopt.format_callback_data = handle_rename;
161 diff_flush(&revs->diffopt);
162 revs->diffopt.output_format = saved_fmt;
163 revs->diffopt.flags = saved_flags;
165 lines_counted = 1;
166 return found;
169 static void print_commit(struct commit *commit, struct rev_info *revs)
171 struct commitinfo *info;
172 int columns = revs->graph ? 4 : 3;
173 struct strbuf graphbuf = STRBUF_INIT;
174 struct strbuf msgbuf = STRBUF_INIT;
176 if (ctx.repo->enable_log_filecount)
177 columns++;
178 if (ctx.repo->enable_log_linecount)
179 columns++;
181 if (revs->graph) {
182 /* Advance graph until current commit */
183 while (!graph_next_line(revs->graph, &graphbuf)) {
184 /* Print graph segment in otherwise empty table row */
185 html("<tr class='nohover'><td class='commitgraph'>");
186 html(graphbuf.buf);
187 htmlf("</td><td colspan='%d' /></tr>\n", columns);
188 strbuf_setlen(&graphbuf, 0);
190 /* Current commit's graph segment is now ready in graphbuf */
193 info = cgit_parse_commit(commit);
194 htmlf("<tr%s>", ctx.qry.showmsg ? " class='logheader'" : "");
196 if (revs->graph) {
197 /* Print graph segment for current commit */
198 html("<td class='commitgraph'>");
199 html(graphbuf.buf);
200 html("</td>");
201 strbuf_setlen(&graphbuf, 0);
203 else {
204 html("<td>");
205 cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
206 html("</td>");
209 htmlf("<td%s>", ctx.qry.showmsg ? " class='logsubject'" : "");
210 if (ctx.qry.showmsg) {
211 /* line-wrap long commit subjects instead of truncating them */
212 size_t subject_len = strlen(info->subject);
214 if (subject_len > ctx.cfg.max_msg_len &&
215 ctx.cfg.max_msg_len >= 15) {
216 /* symbol for signaling line-wrap (in PAGE_ENCODING) */
217 const char wrap_symbol[] = { ' ', 0xE2, 0x86, 0xB5, 0 };
218 int i = ctx.cfg.max_msg_len - strlen(wrap_symbol);
220 /* Rewind i to preceding space character */
221 while (i > 0 && !isspace(info->subject[i]))
222 --i;
223 if (!i) /* Oops, zero spaces. Reset i */
224 i = ctx.cfg.max_msg_len - strlen(wrap_symbol);
226 /* add remainder starting at i to msgbuf */
227 strbuf_add(&msgbuf, info->subject + i, subject_len - i);
228 strbuf_trim(&msgbuf);
229 strbuf_add(&msgbuf, "\n\n", 2);
231 /* Place wrap_symbol at position i in info->subject */
232 strcpy(info->subject + i, wrap_symbol);
235 cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
236 sha1_to_hex(commit->object.sha1), ctx.qry.vpath);
237 show_commit_decorations(commit);
238 html("</td><td>");
239 cgit_open_filter(ctx.repo->email_filter, info->author_email, "log");
240 html_txt(info->author);
241 cgit_close_filter(ctx.repo->email_filter);
243 if (revs->graph) {
244 html("</td><td>");
245 cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
248 if (!lines_counted && (ctx.repo->enable_log_filecount ||
249 ctx.repo->enable_log_linecount)) {
250 files = 0;
251 add_lines = 0;
252 rem_lines = 0;
253 cgit_diff_commit(commit, inspect_files, ctx.qry.vpath);
256 if (ctx.repo->enable_log_filecount)
257 htmlf("</td><td>%d", files);
258 if (ctx.repo->enable_log_linecount)
259 htmlf("</td><td>-%d/+%d", rem_lines, add_lines);
261 html("</td></tr>\n");
263 if (revs->graph || ctx.qry.showmsg) { /* Print a second table row */
264 html("<tr class='nohover'>");
266 if (ctx.qry.showmsg) {
267 /* Concatenate commit message + notes in msgbuf */
268 if (info->msg && *(info->msg)) {
269 strbuf_addstr(&msgbuf, info->msg);
270 strbuf_addch(&msgbuf, '\n');
272 format_display_notes(commit->object.sha1,
273 &msgbuf, PAGE_ENCODING, 0);
274 strbuf_addch(&msgbuf, '\n');
275 strbuf_ltrim(&msgbuf);
278 if (revs->graph) {
279 int lines = 0;
281 /* Calculate graph padding */
282 if (ctx.qry.showmsg) {
283 /* Count #lines in commit message + notes */
284 const char *p = msgbuf.buf;
285 lines = 1;
286 while ((p = strchr(p, '\n'))) {
287 p++;
288 lines++;
292 /* Print graph padding */
293 html("<td class='commitgraph'>");
294 while (lines > 0 || !graph_is_commit_finished(revs->graph)) {
295 if (graphbuf.len)
296 html("\n");
297 strbuf_setlen(&graphbuf, 0);
298 graph_next_line(revs->graph, &graphbuf);
299 html(graphbuf.buf);
300 lines--;
302 html("</td>\n");
304 else
305 html("<td/>"); /* Empty 'Age' column */
307 /* Print msgbuf into remainder of table row */
308 htmlf("<td colspan='%d'%s>\n", columns - (revs->graph ? 1 : 0),
309 ctx.qry.showmsg ? " class='logmsg'" : "");
310 html_txt(msgbuf.buf);
311 html("</td></tr>\n");
314 strbuf_release(&msgbuf);
315 strbuf_release(&graphbuf);
316 cgit_free_commitinfo(info);
319 static const char *disambiguate_ref(const char *ref, int *must_free_result)
321 struct object_id oid;
322 struct strbuf longref = STRBUF_INIT;
324 strbuf_addf(&longref, "refs/heads/%s", ref);
325 if (get_sha1(longref.buf, oid.hash) == 0) {
326 *must_free_result = 1;
327 return strbuf_detach(&longref, NULL);
330 *must_free_result = 0;
331 strbuf_release(&longref);
332 return ref;
335 static char *next_token(char **src)
337 char *result;
339 if (!src || !*src)
340 return NULL;
341 while (isspace(**src))
342 (*src)++;
343 if (!**src)
344 return NULL;
345 result = *src;
346 while (**src) {
347 if (isspace(**src)) {
348 **src = '\0';
349 (*src)++;
350 break;
352 (*src)++;
354 return result;
357 void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
358 char *path, int pager, int commit_graph, int commit_sort)
360 struct rev_info rev;
361 struct commit *commit;
362 struct argv_array rev_argv = ARGV_ARRAY_INIT;
363 int i, columns = commit_graph ? 4 : 3;
364 int must_free_tip = 0;
366 /* rev_argv.argv[0] will be ignored by setup_revisions */
367 argv_array_push(&rev_argv, "log_rev_setup");
369 if (!tip)
370 tip = ctx.qry.head;
371 tip = disambiguate_ref(tip, &must_free_tip);
372 argv_array_push(&rev_argv, tip);
374 if (grep && pattern && *pattern) {
375 pattern = xstrdup(pattern);
376 if (!strcmp(grep, "grep") || !strcmp(grep, "author") ||
377 !strcmp(grep, "committer")) {
378 argv_array_pushf(&rev_argv, "--%s=%s", grep, pattern);
379 } else if (!strcmp(grep, "range")) {
380 char *arg;
381 /* Split the pattern at whitespace and add each token
382 * as a revision expression. Do not accept other
383 * rev-list options. Also, replace the previously
384 * pushed tip (it's no longer relevant).
386 argv_array_pop(&rev_argv);
387 while ((arg = next_token(&pattern))) {
388 if (*arg == '-') {
389 fprintf(stderr, "Bad range expr: %s\n",
390 arg);
391 break;
393 argv_array_push(&rev_argv, arg);
398 if (!path || !ctx.cfg.enable_follow_links) {
400 * If we don't have a path, "follow" is a no-op so make sure
401 * the variable is set to false to avoid needing to check
402 * both this and whether we have a path everywhere.
404 ctx.qry.follow = 0;
407 if (commit_graph && !ctx.qry.follow) {
408 argv_array_push(&rev_argv, "--graph");
409 argv_array_push(&rev_argv, "--color");
410 graph_set_column_colors(column_colors_html,
411 COLUMN_COLORS_HTML_MAX);
414 if (commit_sort == 1)
415 argv_array_push(&rev_argv, "--date-order");
416 else if (commit_sort == 2)
417 argv_array_push(&rev_argv, "--topo-order");
419 if (path && ctx.qry.follow)
420 argv_array_push(&rev_argv, "--follow");
421 argv_array_push(&rev_argv, "--");
422 if (path)
423 argv_array_push(&rev_argv, path);
425 init_revisions(&rev, NULL);
426 rev.abbrev = DEFAULT_ABBREV;
427 rev.commit_format = CMIT_FMT_DEFAULT;
428 rev.verbose_header = 1;
429 rev.show_root_diff = 0;
430 rev.ignore_missing = 1;
431 rev.simplify_history = 1;
432 setup_revisions(rev_argv.argc, rev_argv.argv, &rev, NULL);
433 load_ref_decorations(DECORATE_FULL_REFS);
434 rev.show_decorations = 1;
435 rev.grep_filter.regflags |= REG_ICASE;
437 rev.diffopt.detect_rename = 1;
438 rev.diffopt.rename_limit = ctx.cfg.renamelimit;
439 if (ctx.qry.ignorews)
440 DIFF_XDL_SET(&rev.diffopt, IGNORE_WHITESPACE);
442 compile_grep_patterns(&rev.grep_filter);
443 prepare_revision_walk(&rev);
445 if (pager)
446 html("<table class='list nowrap'>");
448 html("<tr class='nohover'>");
449 if (commit_graph)
450 html("<th></th>");
451 else
452 html("<th class='left'>Age</th>");
453 html("<th class='left'>Commit message");
454 if (pager) {
455 html(" (");
456 cgit_log_link(ctx.qry.showmsg ? "Collapse" : "Expand", NULL,
457 NULL, ctx.qry.head, ctx.qry.sha1,
458 ctx.qry.vpath, ctx.qry.ofs, ctx.qry.grep,
459 ctx.qry.search, ctx.qry.showmsg ? 0 : 1,
460 ctx.qry.follow);
461 html(")");
463 html("</th><th class='left'>Author</th>");
464 if (rev.graph)
465 html("<th class='left'>Age</th>");
466 if (ctx.repo->enable_log_filecount) {
467 html("<th class='left'>Files</th>");
468 columns++;
470 if (ctx.repo->enable_log_linecount) {
471 html("<th class='left'>Lines</th>");
472 columns++;
474 html("</tr>\n");
476 if (ofs<0)
477 ofs = 0;
479 for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; /* nop */) {
480 if (show_commit(commit, &rev))
481 i++;
482 free_commit_buffer(commit);
483 free_commit_list(commit->parents);
484 commit->parents = NULL;
487 for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; /* nop */) {
489 * In "follow" mode, we must count the files and lines the
490 * first time we invoke diff on a given commit, and we need
491 * to do that to see if the commit touches the path we care
492 * about, so we do it in show_commit. Hence we must clear
493 * lines_counted here.
495 * This has the side effect of avoiding running diff twice
496 * when we are both following renames and showing file
497 * and/or line counts.
499 lines_counted = 0;
500 if (show_commit(commit, &rev)) {
501 i++;
502 print_commit(commit, &rev);
504 free_commit_buffer(commit);
505 free_commit_list(commit->parents);
506 commit->parents = NULL;
508 if (pager) {
509 html("</table><ul class='pager'>");
510 if (ofs > 0) {
511 html("<li>");
512 cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
513 ctx.qry.sha1, ctx.qry.vpath,
514 ofs - cnt, ctx.qry.grep,
515 ctx.qry.search, ctx.qry.showmsg,
516 ctx.qry.follow);
517 html("</li>");
519 if ((commit = get_revision(&rev)) != NULL) {
520 html("<li>");
521 cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
522 ctx.qry.sha1, ctx.qry.vpath,
523 ofs + cnt, ctx.qry.grep,
524 ctx.qry.search, ctx.qry.showmsg,
525 ctx.qry.follow);
526 html("</li>");
528 html("</ul>");
529 } else if ((commit = get_revision(&rev)) != NULL) {
530 htmlf("<tr class='nohover'><td colspan='%d'>", columns);
531 cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL,
532 ctx.qry.vpath, 0, NULL, NULL, ctx.qry.showmsg,
533 ctx.qry.follow);
534 html("</td></tr>\n");
537 /* If we allocated tip then it is safe to cast away const. */
538 if (must_free_tip)
539 free((char*) tip);