blob: 354ee080e0b9f7b73853e0f311b262e82bd7a2d4 [file] [log] [blame]
Lars Hjemlid14c5f62006-12-11 17:04:19 +01001/* ui-log.c: functions for log output
2 *
3 * Copyright (C) 2006 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010010#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010011#include "ui-shared.h"
Lars Hjemlid14c5f62006-12-11 17:04:19 +010012
Lars Hjemlid04c4732007-11-06 00:35:12 +010013int files, add_lines, rem_lines;
Lars Hjemli48dc0032007-05-13 11:27:46 +020014
Lars Hjemli80e577c2007-05-13 17:03:27 +020015void count_lines(char *line, int size)
16{
Lars Hjemlid04c4732007-11-06 00:35:12 +010017 if (size <= 0)
18 return;
19
20 if (line[0] == '+')
21 add_lines++;
22
23 else if (line[0] == '-')
24 rem_lines++;
Lars Hjemli80e577c2007-05-13 17:03:27 +020025}
26
Lars Hjemli48dc0032007-05-13 11:27:46 +020027void inspect_files(struct diff_filepair *pair)
28{
Lars Hjemlic495cf02009-01-31 10:40:40 +010029 unsigned long old_size = 0;
30 unsigned long new_size = 0;
31 int binary = 0;
32
Lars Hjemli48dc0032007-05-13 11:27:46 +020033 files++;
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010034 if (ctx.repo->enable_log_linecount)
Lars Hjemlic495cf02009-01-31 10:40:40 +010035 cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
Johan Herland2cc8b992010-06-24 17:52:57 +020036 &new_size, &binary, 0, ctx.qry.ignorews,
37 count_lines);
Lars Hjemli48dc0032007-05-13 11:27:46 +020038}
39
Lars Hjemlief2dc552009-01-11 12:16:18 +010040void show_commit_decorations(struct commit *commit)
41{
42 struct name_decoration *deco;
43 static char buf[1024];
44
45 buf[sizeof(buf) - 1] = 0;
46 deco = lookup_decoration(&name_decoration, &commit->object);
47 while (deco) {
48 if (!prefixcmp(deco->name, "refs/heads/")) {
49 strncpy(buf, deco->name + 11, sizeof(buf) - 1);
Johan Herlandafc40722010-06-11 14:50:47 +020050 cgit_log_link(buf, NULL, "branch-deco", buf, NULL,
51 ctx.qry.vpath, 0, NULL, NULL,
52 ctx.qry.showmsg);
Lars Hjemlief2dc552009-01-11 12:16:18 +010053 }
54 else if (!prefixcmp(deco->name, "tag: refs/tags/")) {
55 strncpy(buf, deco->name + 15, sizeof(buf) - 1);
56 cgit_tag_link(buf, NULL, "tag-deco", ctx.qry.head, buf);
57 }
Lars Hjemli0730ee62009-08-16 19:52:27 +020058 else if (!prefixcmp(deco->name, "refs/tags/")) {
59 strncpy(buf, deco->name + 10, sizeof(buf) - 1);
60 cgit_tag_link(buf, NULL, "tag-deco", ctx.qry.head, buf);
61 }
Lars Hjemlief2dc552009-01-11 12:16:18 +010062 else if (!prefixcmp(deco->name, "refs/remotes/")) {
63 strncpy(buf, deco->name + 13, sizeof(buf) - 1);
64 cgit_log_link(buf, NULL, "remote-deco", NULL,
Johan Herlandafc40722010-06-11 14:50:47 +020065 sha1_to_hex(commit->object.sha1),
66 ctx.qry.vpath, 0, NULL, NULL,
67 ctx.qry.showmsg);
Lars Hjemlief2dc552009-01-11 12:16:18 +010068 }
69 else {
70 strncpy(buf, deco->name, sizeof(buf) - 1);
71 cgit_commit_link(buf, NULL, "deco", ctx.qry.head,
Johan Herlandafc40722010-06-11 14:50:47 +020072 sha1_to_hex(commit->object.sha1),
73 ctx.qry.vpath, 0);
Lars Hjemlief2dc552009-01-11 12:16:18 +010074 }
75 deco = deco->next;
76 }
77}
78
Lars Hjemli2101e262006-12-15 18:17:36 +010079void print_commit(struct commit *commit)
Lars Hjemlid14c5f62006-12-11 17:04:19 +010080{
Lars Hjemli2101e262006-12-15 18:17:36 +010081 struct commitinfo *info;
Lars Hjemlie9a70422008-04-14 22:23:48 +020082 char *tmp;
Lars Hjemli951f5502008-11-29 18:58:31 +010083 int cols = 2;
Lars Hjemlid14c5f62006-12-11 17:04:19 +010084
Lars Hjemli2101e262006-12-15 18:17:36 +010085 info = cgit_parse_commit(commit);
Lars Hjemliab671642008-11-29 19:11:26 +010086 htmlf("<tr%s><td>",
87 ctx.qry.showmsg ? " class='logheader'" : "");
Lars Hjemlie9a70422008-04-14 22:23:48 +020088 tmp = fmt("id=%s", sha1_to_hex(commit->object.sha1));
Johan Herlandafc40722010-06-11 14:50:47 +020089 tmp = cgit_fileurl(ctx.repo->url, "commit", ctx.qry.vpath, tmp);
Lars Hjemlie9a70422008-04-14 22:23:48 +020090 html_link_open(tmp, NULL, NULL);
Lars Hjemli237ef7b2007-05-22 23:15:36 +020091 cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
Lars Hjemlie9a70422008-04-14 22:23:48 +020092 html_link_close();
Lars Hjemliab671642008-11-29 19:11:26 +010093 htmlf("</td><td%s>",
94 ctx.qry.showmsg ? " class='logsubject'" : "");
Lars Hjemlid14d77f2008-02-16 11:53:40 +010095 cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
Johan Herlandafc40722010-06-11 14:50:47 +020096 sha1_to_hex(commit->object.sha1), ctx.qry.vpath, 0);
Lars Hjemlief2dc552009-01-11 12:16:18 +010097 show_commit_decorations(commit);
Lars Hjemli5764fe92008-04-14 22:13:38 +020098 html("</td><td>");
99 html_txt(info->author);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100100 if (ctx.repo->enable_log_filecount) {
Lars Hjemlie1893442007-05-18 13:55:52 +0200101 files = 0;
Lars Hjemlid04c4732007-11-06 00:35:12 +0100102 add_lines = 0;
103 rem_lines = 0;
Lars Hjemlie1893442007-05-18 13:55:52 +0200104 cgit_diff_commit(commit, inspect_files);
Lars Hjemli5764fe92008-04-14 22:13:38 +0200105 html("</td><td>");
Lars Hjemlie1893442007-05-18 13:55:52 +0200106 htmlf("%d", files);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100107 if (ctx.repo->enable_log_linecount) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200108 html("</td><td>");
Lars Hjemlid04c4732007-11-06 00:35:12 +0100109 htmlf("-%d/+%d", rem_lines, add_lines);
Lars Hjemlie1893442007-05-18 13:55:52 +0200110 }
111 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100112 html("</td></tr>\n");
Lars Hjemli0274b572008-11-29 18:39:41 +0100113 if (ctx.qry.showmsg) {
Lars Hjemli0274b572008-11-29 18:39:41 +0100114 if (ctx.repo->enable_log_filecount) {
Lars Hjemli951f5502008-11-29 18:58:31 +0100115 cols++;
Lars Hjemli0274b572008-11-29 18:39:41 +0100116 if (ctx.repo->enable_log_linecount)
Lars Hjemli951f5502008-11-29 18:58:31 +0100117 cols++;
Lars Hjemli0274b572008-11-29 18:39:41 +0100118 }
Lars Hjemliab671642008-11-29 19:11:26 +0100119 htmlf("<tr class='nohover'><td/><td colspan='%d' class='logmsg'>",
Lars Hjemli951f5502008-11-29 18:58:31 +0100120 cols);
121 html_txt(info->msg);
Lars Hjemliab671642008-11-29 19:11:26 +0100122 html("</td></tr>\n");
Lars Hjemli0274b572008-11-29 18:39:41 +0100123 }
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100124 cgit_free_commitinfo(info);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100125}
126
Lars Hjemlia1b01b22008-12-03 17:34:23 +0100127static const char *disambiguate_ref(const char *ref)
128{
129 unsigned char sha1[20];
130 const char *longref;
131
132 longref = fmt("refs/heads/%s", ref);
133 if (get_sha1(longref, sha1) == 0)
134 return longref;
135
136 return ref;
137}
Lars Hjemli2101e262006-12-15 18:17:36 +0100138
Lars Hjemli5764fe92008-04-14 22:13:38 +0200139void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
140 char *path, int pager)
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100141{
142 struct rev_info rev;
143 struct commit *commit;
Lars Hjemlia1b01b22008-12-03 17:34:23 +0100144 const char *argv[] = {NULL, NULL, NULL, NULL, NULL};
Lars Hjemli732d68d2006-12-28 02:45:28 +0100145 int argc = 2;
Lars Hjemli5764fe92008-04-14 22:13:38 +0200146 int i, columns = 3;
Lars Hjemli48dc0032007-05-13 11:27:46 +0200147
Lars Hjemlicd79c162007-06-17 14:58:45 +0200148 if (!tip)
Lars Hjemlia1b01b22008-12-03 17:34:23 +0100149 tip = ctx.qry.head;
150
151 argv[1] = disambiguate_ref(tip);
Lars Hjemlicd79c162007-06-17 14:58:45 +0200152
Lars Hjemli68ca0322007-10-28 15:23:00 +0100153 if (grep && pattern && (!strcmp(grep, "grep") ||
154 !strcmp(grep, "author") ||
155 !strcmp(grep, "committer")))
156 argv[argc++] = fmt("--%s=%s", grep, pattern);
157
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200158 if (path) {
159 argv[argc++] = "--";
160 argv[argc++] = path;
161 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100162 init_revisions(&rev, NULL);
163 rev.abbrev = DEFAULT_ABBREV;
164 rev.commit_format = CMIT_FMT_DEFAULT;
165 rev.verbose_header = 1;
166 rev.show_root_diff = 0;
Lars Hjemli732d68d2006-12-28 02:45:28 +0100167 setup_revisions(argc, argv, &rev, NULL);
Lars Hjemli45c49d62009-09-13 21:56:45 +0200168 load_ref_decorations(DECORATE_FULL_REFS);
Lars Hjemlief2dc552009-01-11 12:16:18 +0100169 rev.show_decorations = 1;
Lars Hjemlib7f33782008-10-05 19:19:59 +0200170 rev.grep_filter.regflags |= REG_ICASE;
171 compile_grep_patterns(&rev.grep_filter);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100172 prepare_revision_walk(&rev);
173
Lars Hjemli5764fe92008-04-14 22:13:38 +0200174 if (pager)
175 html("<table class='list nowrap'>");
Lars Hjemlie1893442007-05-18 13:55:52 +0200176
Lars Hjemli5764fe92008-04-14 22:13:38 +0200177 html("<tr class='nohover'><th class='left'>Age</th>"
Lars Hjemli0274b572008-11-29 18:39:41 +0100178 "<th class='left'>Commit message");
179 if (pager) {
180 html(" (");
Justin Waters1383fe32009-01-09 17:35:10 -0500181 cgit_log_link(ctx.qry.showmsg ? "Collapse" : "Expand", NULL,
182 NULL, ctx.qry.head, ctx.qry.sha1,
Johan Herlandafc40722010-06-11 14:50:47 +0200183 ctx.qry.vpath, ctx.qry.ofs, ctx.qry.grep,
Lars Hjemli0274b572008-11-29 18:39:41 +0100184 ctx.qry.search, ctx.qry.showmsg ? 0 : 1);
185 html(")");
186 }
187 html("</th><th class='left'>Author</th>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100188 if (ctx.repo->enable_log_filecount) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200189 html("<th class='left'>Files</th>");
190 columns++;
191 if (ctx.repo->enable_log_linecount) {
192 html("<th class='left'>Lines</th>");
193 columns++;
194 }
Lars Hjemlie1893442007-05-18 13:55:52 +0200195 }
Lars Hjemli5764fe92008-04-14 22:13:38 +0200196 html("</tr>\n");
Lars Hjemli420712a2006-12-14 00:40:34 +0100197
198 if (ofs<0)
199 ofs = 0;
200
201 for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) {
202 free(commit->buffer);
203 commit->buffer = NULL;
204 free_commit_list(commit->parents);
205 commit->parents = NULL;
206 }
207
208 for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) {
Lars Hjemli2101e262006-12-15 18:17:36 +0100209 print_commit(commit);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100210 free(commit->buffer);
211 commit->buffer = NULL;
212 free_commit_list(commit->parents);
213 commit->parents = NULL;
214 }
Ondrej Jirmana9226152007-05-26 03:26:14 +0200215 if (pager) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200216 htmlf("</table><div class='pager'>",
217 columns);
Ondrej Jirmana9226152007-05-26 03:26:14 +0200218 if (ofs > 0) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100219 cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
Johan Herlandafc40722010-06-11 14:50:47 +0200220 ctx.qry.sha1, ctx.qry.vpath,
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100221 ofs - cnt, ctx.qry.grep,
Lars Hjemli0274b572008-11-29 18:39:41 +0100222 ctx.qry.search, ctx.qry.showmsg);
Lars Hjemli103940f2007-06-29 20:27:41 +0200223 html("&nbsp;");
Ondrej Jirmana9226152007-05-26 03:26:14 +0200224 }
Ondrej Jirmana9226152007-05-26 03:26:14 +0200225 if ((commit = get_revision(&rev)) != NULL) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100226 cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
Johan Herlandafc40722010-06-11 14:50:47 +0200227 ctx.qry.sha1, ctx.qry.vpath,
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100228 ofs + cnt, ctx.qry.grep,
Lars Hjemli0274b572008-11-29 18:39:41 +0100229 ctx.qry.search, ctx.qry.showmsg);
Ondrej Jirmana9226152007-05-26 03:26:14 +0200230 }
231 html("</div>");
Lars Hjemli5764fe92008-04-14 22:13:38 +0200232 } else if ((commit = get_revision(&rev)) != NULL) {
233 html("<tr class='nohover'><td colspan='3'>");
Johan Herlandafc40722010-06-11 14:50:47 +0200234 cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL,
235 ctx.qry.vpath, 0, NULL, NULL, ctx.qry.showmsg);
Lars Hjemli5764fe92008-04-14 22:13:38 +0200236 html("</td></tr>\n");
Lars Hjemli420712a2006-12-14 00:40:34 +0100237 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100238}