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)
9 #define USE_THE_REPOSITORY_VARIABLE
14 #include "ui-shared.h"
17 static int files, add_lines, rem_lines, lines_counted;
20 * The list of available column colors in the commit graph.
22 static const char *column_colors_html[] = {
23 "<span class='column1'>",
24 "<span class='column2'>",
25 "<span class='column3'>",
26 "<span class='column4'>",
27 "<span class='column5'>",
28 "<span class='column6'>",
32 #define COLUMN_COLORS_HTML_MAX (ARRAY_SIZE(column_colors_html) - 1)
34 static void count_lines(char *line, int size)
42 else if (line[0] == '-')
46 static void inspect_files(struct diff_filepair *pair)
48 unsigned long old_size = 0;
49 unsigned long new_size = 0;
53 if (ctx.repo->enable_log_linecount)
54 cgit_diff_files(&pair->one->oid, &pair->two->oid, &old_size,
55 &new_size, &binary, 0, ctx.qry.ignorews,
59 void show_commit_decorations(struct commit *commit)
61 const struct name_decoration *deco;
62 static char buf[1024];
64 buf[sizeof(buf) - 1] = 0;
65 deco = get_name_decoration(&commit->object);
68 html("<span class='decoration'>");
70 struct object_id oid_tag, peeled;
73 strlcpy(buf, prettify_refname(deco->name), sizeof(buf));
76 /* If the git-core doesn't recognize it,
77 * don't display anything. */
79 case DECORATION_REF_LOCAL:
80 cgit_log_link(buf, NULL, "branch-deco", buf, NULL,
81 ctx.qry.vpath, 0, NULL, NULL,
84 case DECORATION_REF_TAG:
85 if (!refs_read_ref(get_main_ref_store(the_repository), deco->name, &oid_tag) &&
86 !peel_iterated_oid(the_repository, &oid_tag, &peeled))
87 is_annotated = !oideq(&oid_tag, &peeled);
88 cgit_tag_link(buf, NULL, is_annotated ? "tag-annotated-deco" : "tag-deco", buf);
90 case DECORATION_REF_REMOTE:
91 if (!ctx.repo->enable_remote_branches)
93 cgit_log_link(buf, NULL, "remote-deco", NULL,
94 oid_to_hex(&commit->object.oid),
95 ctx.qry.vpath, 0, NULL, NULL,
99 cgit_commit_link(buf, NULL, "deco", ctx.qry.head,
100 oid_to_hex(&commit->object.oid),
109 static void handle_rename(struct diff_filepair *pair)
112 * After we have seen a rename, we generate links to the previous
113 * name of the file so that commit & diff views get fed the path
114 * that is correct for the commit they are showing, avoiding the
115 * need to walk the entire history leading back to every commit we
116 * show in order detect renames.
118 if (0 != strcmp(ctx.qry.vpath, pair->two->path)) {
120 ctx.qry.vpath = xstrdup(pair->two->path);
125 static int show_commit(struct commit *commit, struct rev_info *revs)
127 struct commit_list *parents = commit->parents;
128 struct commit *parent;
129 int found = 0, saved_fmt;
130 struct diff_flags saved_flags = revs->diffopt.flags;
132 /* Always show if we're not in "follow" mode with a single file. */
137 * In "follow" mode, we don't show merges. This is consistent with
138 * "git log --follow -- <file>".
140 if (parents && parents->next)
144 * If this is the root commit, do what rev_info tells us.
147 return revs->show_root_diff;
149 /* When we get here we have precisely one parent. */
150 parent = parents->item;
151 /* If we can't parse the commit, let print_commit() report an error. */
152 if (repo_parse_commit(the_repository, parent))
159 revs->diffopt.flags.recursive = 1;
160 diff_tree_oid(get_commit_tree_oid(parent),
161 get_commit_tree_oid(commit),
163 diffcore_std(&revs->diffopt);
165 found = !diff_queue_is_empty(&revs->diffopt);
166 saved_fmt = revs->diffopt.output_format;
167 revs->diffopt.output_format = DIFF_FORMAT_CALLBACK;
168 revs->diffopt.format_callback = cgit_diff_tree_cb;
169 revs->diffopt.format_callback_data = handle_rename;
170 diff_flush(&revs->diffopt);
171 revs->diffopt.output_format = saved_fmt;
172 revs->diffopt.flags = saved_flags;
178 static void print_commit(struct commit *commit, struct rev_info *revs)
180 struct commitinfo *info;
181 int columns = revs->graph ? 4 : 3;
182 struct strbuf graphbuf = STRBUF_INIT;
183 struct strbuf msgbuf = STRBUF_INIT;
185 if (ctx.repo->enable_log_filecount)
187 if (ctx.repo->enable_log_linecount)
191 /* Advance graph until current commit */
192 while (!graph_next_line(revs->graph, &graphbuf)) {
193 /* Print graph segment in otherwise empty table row */
194 html("<tr class='nohover'><td class='commitgraph'>");
196 htmlf("</td><td colspan='%d' /></tr>\n", columns);
197 strbuf_setlen(&graphbuf, 0);
199 /* Current commit's graph segment is now ready in graphbuf */
202 info = cgit_parse_commit(commit);
203 htmlf("<tr%s>", ctx.qry.showmsg ? " class='logheader'" : "");
206 /* Print graph segment for current commit */
207 html("<td class='commitgraph'>");
210 strbuf_setlen(&graphbuf, 0);
214 cgit_print_age(info->committer_date, info->committer_tz, TM_WEEK * 2);
218 htmlf("<td%s>", ctx.qry.showmsg ? " class='logsubject'" : "");
219 if (ctx.qry.showmsg) {
220 /* line-wrap long commit subjects instead of truncating them */
221 size_t subject_len = strlen(info->subject);
223 if (subject_len > ctx.cfg.max_msg_len &&
224 ctx.cfg.max_msg_len >= 15) {
225 /* symbol for signaling line-wrap (in PAGE_ENCODING) */
226 const char wrap_symbol[] = { ' ', 0xE2, 0x86, 0xB5, 0 };
227 int i = ctx.cfg.max_msg_len - strlen(wrap_symbol);
229 /* Rewind i to preceding space character */
230 while (i > 0 && !isspace(info->subject[i]))
232 if (!i) /* Oops, zero spaces. Reset i */
233 i = ctx.cfg.max_msg_len - strlen(wrap_symbol);
235 /* add remainder starting at i to msgbuf */
236 strbuf_add(&msgbuf, info->subject + i, subject_len - i);
237 strbuf_trim(&msgbuf);
238 strbuf_add(&msgbuf, "\n\n", 2);
240 /* Place wrap_symbol at position i in info->subject */
241 strlcpy(info->subject + i, wrap_symbol, subject_len - i + 1);
244 cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
245 oid_to_hex(&commit->object.oid), ctx.qry.vpath);
246 show_commit_decorations(commit);
248 cgit_open_filter(ctx.repo->email_filter, info->author_email, "log");
249 html_txt(info->author);
250 cgit_close_filter(ctx.repo->email_filter);
254 cgit_print_age(info->committer_date, info->committer_tz, TM_WEEK * 2);
257 if (!lines_counted && (ctx.repo->enable_log_filecount ||
258 ctx.repo->enable_log_linecount)) {
262 cgit_diff_commit(commit, inspect_files, ctx.qry.vpath);
265 if (ctx.repo->enable_log_filecount)
266 htmlf("</td><td>%d", files);
267 if (ctx.repo->enable_log_linecount)
268 htmlf("</td><td><span class='deletions'>-%d</span>/"
269 "<span class='insertions'>+%d</span>", rem_lines, add_lines);
271 html("</td></tr>\n");
273 if ((revs->graph && !graph_is_commit_finished(revs->graph))
274 || ctx.qry.showmsg) { /* Print a second table row */
275 html("<tr class='nohover-highlight'>");
277 if (ctx.qry.showmsg) {
278 /* Concatenate commit message + notes in msgbuf */
279 if (info->msg && *(info->msg)) {
280 strbuf_addstr(&msgbuf, info->msg);
281 strbuf_addch(&msgbuf, '\n');
283 format_display_notes(&commit->object.oid,
284 &msgbuf, PAGE_ENCODING, 0);
285 strbuf_addch(&msgbuf, '\n');
286 strbuf_ltrim(&msgbuf);
292 /* Calculate graph padding */
293 if (ctx.qry.showmsg) {
294 /* Count #lines in commit message + notes */
295 const char *p = msgbuf.buf;
297 while ((p = strchr(p, '\n'))) {
303 /* Print graph padding */
304 html("<td class='commitgraph'>");
305 while (lines > 0 || !graph_is_commit_finished(revs->graph)) {
308 strbuf_setlen(&graphbuf, 0);
309 graph_next_line(revs->graph, &graphbuf);
316 html("<td/>"); /* Empty 'Age' column */
318 /* Print msgbuf into remainder of table row */
319 htmlf("<td colspan='%d'%s>\n", columns - (revs->graph ? 1 : 0),
320 ctx.qry.showmsg ? " class='logmsg'" : "");
321 html_txt(msgbuf.buf);
322 html("</td></tr>\n");
325 strbuf_release(&msgbuf);
326 strbuf_release(&graphbuf);
327 cgit_free_commitinfo(info);
330 static const char *disambiguate_ref(const char *ref, int *must_free_result)
332 struct object_id oid;
333 struct strbuf longref = STRBUF_INIT;
335 strbuf_addf(&longref, "refs/heads/%s", ref);
336 if (repo_get_oid(the_repository, longref.buf, &oid) == 0) {
337 *must_free_result = 1;
338 return strbuf_detach(&longref, NULL);
341 *must_free_result = 0;
342 strbuf_release(&longref);
346 static char *next_token(char **src)
352 while (isspace(**src))
358 if (isspace(**src)) {
368 void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
369 const char *path, int pager, int commit_graph, int commit_sort)
372 struct commit *commit;
373 struct strvec rev_argv = STRVEC_INIT;
374 int i, columns = commit_graph ? 4 : 3;
375 int must_free_tip = 0;
377 /* rev_argv.argv[0] will be ignored by setup_revisions */
378 strvec_push(&rev_argv, "log_rev_setup");
382 tip = disambiguate_ref(tip, &must_free_tip);
383 strvec_push(&rev_argv, tip);
385 if (grep && pattern && *pattern) {
386 pattern = xstrdup(pattern);
387 if (!strcmp(grep, "grep") || !strcmp(grep, "author") ||
388 !strcmp(grep, "committer")) {
389 strvec_pushf(&rev_argv, "--%s=%s", grep, pattern);
390 } else if (!strcmp(grep, "range")) {
392 /* Split the pattern at whitespace and add each token
393 * as a revision expression. Do not accept other
394 * rev-list options. Also, replace the previously
395 * pushed tip (it's no longer relevant).
397 strvec_pop(&rev_argv);
398 while ((arg = next_token(&pattern))) {
400 fprintf(stderr, "Bad range expr: %s\n",
404 strvec_push(&rev_argv, arg);
409 if (!path || !ctx.cfg.enable_follow_links) {
411 * If we don't have a path, "follow" is a no-op so make sure
412 * the variable is set to false to avoid needing to check
413 * both this and whether we have a path everywhere.
418 if (commit_graph && !ctx.qry.follow) {
419 strvec_push(&rev_argv, "--graph");
420 strvec_push(&rev_argv, "--color");
421 graph_set_column_colors(column_colors_html,
422 COLUMN_COLORS_HTML_MAX);
425 if (commit_sort == 1)
426 strvec_push(&rev_argv, "--date-order");
427 else if (commit_sort == 2)
428 strvec_push(&rev_argv, "--topo-order");
430 if (path && ctx.qry.follow)
431 strvec_push(&rev_argv, "--follow");
432 strvec_push(&rev_argv, "--");
434 strvec_push(&rev_argv, path);
436 repo_init_revisions(the_repository, &rev, NULL);
437 rev.abbrev = DEFAULT_ABBREV;
438 rev.commit_format = CMIT_FMT_DEFAULT;
439 rev.verbose_header = 1;
440 rev.show_root_diff = 0;
441 rev.ignore_missing = 1;
442 rev.simplify_history = 1;
443 setup_revisions(rev_argv.nr, rev_argv.v, &rev, NULL);
444 load_ref_decorations(NULL, DECORATE_FULL_REFS);
445 rev.show_decorations = 1;
446 rev.grep_filter.ignore_case = 1;
448 rev.diffopt.detect_rename = 1;
449 rev.diffopt.rename_limit = ctx.cfg.renamelimit;
450 if (ctx.qry.ignorews)
451 DIFF_XDL_SET(&rev.diffopt, IGNORE_WHITESPACE);
453 compile_grep_patterns(&rev.grep_filter);
454 prepare_revision_walk(&rev);
457 cgit_print_layout_start();
458 html("<table class='list nowrap'>");
461 html("<tr class='nohover'>");
465 html("<th class='left'>Age</th>");
466 html("<th class='left'>Commit message");
469 cgit_log_link(ctx.qry.showmsg ? "Collapse" : "Expand", NULL,
470 NULL, ctx.qry.head, ctx.qry.oid,
471 ctx.qry.vpath, ctx.qry.ofs, ctx.qry.grep,
472 ctx.qry.search, ctx.qry.showmsg ? 0 : 1,
476 html("</th><th class='left'>Author</th>");
478 html("<th class='left'>Age</th>");
479 if (ctx.repo->enable_log_filecount) {
480 html("<th class='left'>Files</th>");
483 if (ctx.repo->enable_log_linecount) {
484 html("<th class='left'>Lines</th>");
492 for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; /* nop */) {
493 if (show_commit(commit, &rev))
495 release_commit_memory(the_repository->parsed_objects, commit);
496 commit->parents = NULL;
499 for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; /* nop */) {
501 * In "follow" mode, we must count the files and lines the
502 * first time we invoke diff on a given commit, and we need
503 * to do that to see if the commit touches the path we care
504 * about, so we do it in show_commit. Hence we must clear
505 * lines_counted here.
507 * This has the side effect of avoiding running diff twice
508 * when we are both following renames and showing file
509 * and/or line counts.
512 if (show_commit(commit, &rev)) {
514 print_commit(commit, &rev);
516 release_commit_memory(the_repository->parsed_objects, commit);
517 commit->parents = NULL;
520 html("</table><ul class='pager'>");
523 cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
524 ctx.qry.oid, ctx.qry.vpath,
525 ofs - cnt, ctx.qry.grep,
526 ctx.qry.search, ctx.qry.showmsg,
530 if ((commit = get_revision(&rev)) != NULL) {
532 cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
533 ctx.qry.oid, ctx.qry.vpath,
534 ofs + cnt, ctx.qry.grep,
535 ctx.qry.search, ctx.qry.showmsg,
540 cgit_print_layout_end();
541 } else if ((commit = get_revision(&rev)) != NULL) {
542 htmlf("<tr class='nohover'><td colspan='%d'>", columns);
543 cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL,
544 ctx.qry.vpath, 0, NULL, NULL, ctx.qry.showmsg,
546 html("</td></tr>\n");
549 /* If we allocated tip then it is safe to cast away const. */