blob: 0536b23ebaa1705e627dca637b742dbb713d053f [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) {
Jeff Smith39c2e222010-07-29 09:32:31 -0500114 struct strbuf notes = STRBUF_INIT;
Lars Hjemli6d7552b2010-08-22 13:29:57 +0200115 format_note(NULL, commit->object.sha1, &notes, PAGE_ENCODING, 0);
Jeff Smith39c2e222010-07-29 09:32:31 -0500116
Lars Hjemli0274b572008-11-29 18:39:41 +0100117 if (ctx.repo->enable_log_filecount) {
Lars Hjemli951f5502008-11-29 18:58:31 +0100118 cols++;
Lars Hjemli0274b572008-11-29 18:39:41 +0100119 if (ctx.repo->enable_log_linecount)
Lars Hjemli951f5502008-11-29 18:58:31 +0100120 cols++;
Lars Hjemli0274b572008-11-29 18:39:41 +0100121 }
Lars Hjemliab671642008-11-29 19:11:26 +0100122 htmlf("<tr class='nohover'><td/><td colspan='%d' class='logmsg'>",
Lars Hjemli951f5502008-11-29 18:58:31 +0100123 cols);
124 html_txt(info->msg);
Lars Hjemliab671642008-11-29 19:11:26 +0100125 html("</td></tr>\n");
Jeff Smith39c2e222010-07-29 09:32:31 -0500126 if (notes.len != 0) {
127 html("<tr class='nohover'>");
128 html("<td class='lognotes-label'>Notes:</td>");
129 htmlf("<td colspan='%d' class='lognotes'>",
130 cols);
131 html_txt(notes.buf);
132 html("</td></tr>\n");
133 }
134 strbuf_release(&notes);
Lars Hjemli0274b572008-11-29 18:39:41 +0100135 }
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100136 cgit_free_commitinfo(info);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100137}
138
Lars Hjemlia1b01b22008-12-03 17:34:23 +0100139static const char *disambiguate_ref(const char *ref)
140{
141 unsigned char sha1[20];
142 const char *longref;
143
144 longref = fmt("refs/heads/%s", ref);
145 if (get_sha1(longref, sha1) == 0)
146 return longref;
147
148 return ref;
149}
Lars Hjemli2101e262006-12-15 18:17:36 +0100150
Lars Hjemli5764fe92008-04-14 22:13:38 +0200151void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
152 char *path, int pager)
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100153{
154 struct rev_info rev;
155 struct commit *commit;
Lars Hjemlia1b01b22008-12-03 17:34:23 +0100156 const char *argv[] = {NULL, NULL, NULL, NULL, NULL};
Lars Hjemli732d68d2006-12-28 02:45:28 +0100157 int argc = 2;
Lars Hjemli5764fe92008-04-14 22:13:38 +0200158 int i, columns = 3;
Lars Hjemli48dc0032007-05-13 11:27:46 +0200159
Lars Hjemlicd79c162007-06-17 14:58:45 +0200160 if (!tip)
Lars Hjemlia1b01b22008-12-03 17:34:23 +0100161 tip = ctx.qry.head;
162
163 argv[1] = disambiguate_ref(tip);
Lars Hjemlicd79c162007-06-17 14:58:45 +0200164
Lars Hjemlia579fb02010-06-19 14:32:37 +0200165 if (grep && pattern) {
166 if (!strcmp(grep, "grep") || !strcmp(grep, "author") ||
167 !strcmp(grep, "committer"))
168 argv[argc++] = fmt("--%s=%s", grep, pattern);
169 if (!strcmp(grep, "range"))
170 argv[1] = pattern;
171 }
Lars Hjemli68ca0322007-10-28 15:23:00 +0100172
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200173 if (path) {
174 argv[argc++] = "--";
175 argv[argc++] = path;
176 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100177 init_revisions(&rev, NULL);
178 rev.abbrev = DEFAULT_ABBREV;
179 rev.commit_format = CMIT_FMT_DEFAULT;
180 rev.verbose_header = 1;
181 rev.show_root_diff = 0;
Lars Hjemli732d68d2006-12-28 02:45:28 +0100182 setup_revisions(argc, argv, &rev, NULL);
Lars Hjemli45c49d62009-09-13 21:56:45 +0200183 load_ref_decorations(DECORATE_FULL_REFS);
Lars Hjemlief2dc552009-01-11 12:16:18 +0100184 rev.show_decorations = 1;
Lars Hjemlib7f33782008-10-05 19:19:59 +0200185 rev.grep_filter.regflags |= REG_ICASE;
186 compile_grep_patterns(&rev.grep_filter);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100187 prepare_revision_walk(&rev);
188
Lars Hjemli5764fe92008-04-14 22:13:38 +0200189 if (pager)
190 html("<table class='list nowrap'>");
Lars Hjemlie1893442007-05-18 13:55:52 +0200191
Lars Hjemli5764fe92008-04-14 22:13:38 +0200192 html("<tr class='nohover'><th class='left'>Age</th>"
Lars Hjemli0274b572008-11-29 18:39:41 +0100193 "<th class='left'>Commit message");
194 if (pager) {
195 html(" (");
Justin Waters1383fe32009-01-09 17:35:10 -0500196 cgit_log_link(ctx.qry.showmsg ? "Collapse" : "Expand", NULL,
197 NULL, ctx.qry.head, ctx.qry.sha1,
Johan Herlandafc40722010-06-11 14:50:47 +0200198 ctx.qry.vpath, ctx.qry.ofs, ctx.qry.grep,
Lars Hjemli0274b572008-11-29 18:39:41 +0100199 ctx.qry.search, ctx.qry.showmsg ? 0 : 1);
200 html(")");
201 }
202 html("</th><th class='left'>Author</th>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100203 if (ctx.repo->enable_log_filecount) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200204 html("<th class='left'>Files</th>");
205 columns++;
206 if (ctx.repo->enable_log_linecount) {
207 html("<th class='left'>Lines</th>");
208 columns++;
209 }
Lars Hjemlie1893442007-05-18 13:55:52 +0200210 }
Lars Hjemli5764fe92008-04-14 22:13:38 +0200211 html("</tr>\n");
Lars Hjemli420712a2006-12-14 00:40:34 +0100212
213 if (ofs<0)
214 ofs = 0;
215
216 for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) {
217 free(commit->buffer);
218 commit->buffer = NULL;
219 free_commit_list(commit->parents);
220 commit->parents = NULL;
221 }
222
223 for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) {
Lars Hjemli2101e262006-12-15 18:17:36 +0100224 print_commit(commit);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100225 free(commit->buffer);
226 commit->buffer = NULL;
227 free_commit_list(commit->parents);
228 commit->parents = NULL;
229 }
Ondrej Jirmana9226152007-05-26 03:26:14 +0200230 if (pager) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200231 htmlf("</table><div class='pager'>",
232 columns);
Ondrej Jirmana9226152007-05-26 03:26:14 +0200233 if (ofs > 0) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100234 cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
Johan Herlandafc40722010-06-11 14:50:47 +0200235 ctx.qry.sha1, ctx.qry.vpath,
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100236 ofs - cnt, ctx.qry.grep,
Lars Hjemli0274b572008-11-29 18:39:41 +0100237 ctx.qry.search, ctx.qry.showmsg);
Lars Hjemli103940f2007-06-29 20:27:41 +0200238 html("&nbsp;");
Ondrej Jirmana9226152007-05-26 03:26:14 +0200239 }
Ondrej Jirmana9226152007-05-26 03:26:14 +0200240 if ((commit = get_revision(&rev)) != NULL) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100241 cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
Johan Herlandafc40722010-06-11 14:50:47 +0200242 ctx.qry.sha1, ctx.qry.vpath,
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100243 ofs + cnt, ctx.qry.grep,
Lars Hjemli0274b572008-11-29 18:39:41 +0100244 ctx.qry.search, ctx.qry.showmsg);
Ondrej Jirmana9226152007-05-26 03:26:14 +0200245 }
246 html("</div>");
Lars Hjemli5764fe92008-04-14 22:13:38 +0200247 } else if ((commit = get_revision(&rev)) != NULL) {
248 html("<tr class='nohover'><td colspan='3'>");
Johan Herlandafc40722010-06-11 14:50:47 +0200249 cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL,
250 ctx.qry.vpath, 0, NULL, NULL, ctx.qry.showmsg);
Lars Hjemli5764fe92008-04-14 22:13:38 +0200251 html("</td></tr>\n");
Lars Hjemli420712a2006-12-14 00:40:34 +0100252 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100253}