blob: ba2ab0314186fbbed847907f113227d879767cea [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,
36 &new_size, &binary, count_lines);
Lars Hjemli48dc0032007-05-13 11:27:46 +020037}
38
Lars Hjemlief2dc552009-01-11 12:16:18 +010039void show_commit_decorations(struct commit *commit)
40{
41 struct name_decoration *deco;
42 static char buf[1024];
43
44 buf[sizeof(buf) - 1] = 0;
45 deco = lookup_decoration(&name_decoration, &commit->object);
46 while (deco) {
47 if (!prefixcmp(deco->name, "refs/heads/")) {
48 strncpy(buf, deco->name + 11, sizeof(buf) - 1);
49 cgit_log_link(buf, NULL, "branch-deco", buf, NULL, NULL,
50 0, NULL, NULL, ctx.qry.showmsg);
51 }
52 else if (!prefixcmp(deco->name, "tag: refs/tags/")) {
53 strncpy(buf, deco->name + 15, sizeof(buf) - 1);
54 cgit_tag_link(buf, NULL, "tag-deco", ctx.qry.head, buf);
55 }
56 else if (!prefixcmp(deco->name, "refs/remotes/")) {
57 strncpy(buf, deco->name + 13, sizeof(buf) - 1);
58 cgit_log_link(buf, NULL, "remote-deco", NULL,
59 sha1_to_hex(commit->object.sha1), NULL,
60 0, NULL, NULL, ctx.qry.showmsg);
61 }
62 else {
63 strncpy(buf, deco->name, sizeof(buf) - 1);
64 cgit_commit_link(buf, NULL, "deco", ctx.qry.head,
65 sha1_to_hex(commit->object.sha1));
66 }
67 deco = deco->next;
68 }
69}
70
Lars Hjemli2101e262006-12-15 18:17:36 +010071void print_commit(struct commit *commit)
Lars Hjemlid14c5f62006-12-11 17:04:19 +010072{
Lars Hjemli2101e262006-12-15 18:17:36 +010073 struct commitinfo *info;
Lars Hjemlie9a70422008-04-14 22:23:48 +020074 char *tmp;
Lars Hjemli951f5502008-11-29 18:58:31 +010075 int cols = 2;
Lars Hjemlid14c5f62006-12-11 17:04:19 +010076
Lars Hjemli2101e262006-12-15 18:17:36 +010077 info = cgit_parse_commit(commit);
Lars Hjemliab671642008-11-29 19:11:26 +010078 htmlf("<tr%s><td>",
79 ctx.qry.showmsg ? " class='logheader'" : "");
Lars Hjemlie9a70422008-04-14 22:23:48 +020080 tmp = fmt("id=%s", sha1_to_hex(commit->object.sha1));
81 tmp = cgit_pageurl(ctx.repo->url, "commit", tmp);
82 html_link_open(tmp, NULL, NULL);
Lars Hjemli237ef7b2007-05-22 23:15:36 +020083 cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
Lars Hjemlie9a70422008-04-14 22:23:48 +020084 html_link_close();
Lars Hjemliab671642008-11-29 19:11:26 +010085 htmlf("</td><td%s>",
86 ctx.qry.showmsg ? " class='logsubject'" : "");
Lars Hjemlid14d77f2008-02-16 11:53:40 +010087 cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
Lars Hjemli42a7eb92007-06-17 14:53:02 +020088 sha1_to_hex(commit->object.sha1));
Lars Hjemlief2dc552009-01-11 12:16:18 +010089 show_commit_decorations(commit);
Lars Hjemli5764fe92008-04-14 22:13:38 +020090 html("</td><td>");
91 html_txt(info->author);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010092 if (ctx.repo->enable_log_filecount) {
Lars Hjemlie1893442007-05-18 13:55:52 +020093 files = 0;
Lars Hjemlid04c4732007-11-06 00:35:12 +010094 add_lines = 0;
95 rem_lines = 0;
Lars Hjemlie1893442007-05-18 13:55:52 +020096 cgit_diff_commit(commit, inspect_files);
Lars Hjemli5764fe92008-04-14 22:13:38 +020097 html("</td><td>");
Lars Hjemlie1893442007-05-18 13:55:52 +020098 htmlf("%d", files);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010099 if (ctx.repo->enable_log_linecount) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200100 html("</td><td>");
Lars Hjemlid04c4732007-11-06 00:35:12 +0100101 htmlf("-%d/+%d", rem_lines, add_lines);
Lars Hjemlie1893442007-05-18 13:55:52 +0200102 }
103 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100104 html("</td></tr>\n");
Lars Hjemli0274b572008-11-29 18:39:41 +0100105 if (ctx.qry.showmsg) {
Lars Hjemli0274b572008-11-29 18:39:41 +0100106 if (ctx.repo->enable_log_filecount) {
Lars Hjemli951f5502008-11-29 18:58:31 +0100107 cols++;
Lars Hjemli0274b572008-11-29 18:39:41 +0100108 if (ctx.repo->enable_log_linecount)
Lars Hjemli951f5502008-11-29 18:58:31 +0100109 cols++;
Lars Hjemli0274b572008-11-29 18:39:41 +0100110 }
Lars Hjemliab671642008-11-29 19:11:26 +0100111 htmlf("<tr class='nohover'><td/><td colspan='%d' class='logmsg'>",
Lars Hjemli951f5502008-11-29 18:58:31 +0100112 cols);
113 html_txt(info->msg);
Lars Hjemliab671642008-11-29 19:11:26 +0100114 html("</td></tr>\n");
Lars Hjemli0274b572008-11-29 18:39:41 +0100115 }
Lars Hjemliaaa24bd2006-12-16 14:58:20 +0100116 cgit_free_commitinfo(info);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100117}
118
Lars Hjemlia1b01b22008-12-03 17:34:23 +0100119static const char *disambiguate_ref(const char *ref)
120{
121 unsigned char sha1[20];
122 const char *longref;
123
124 longref = fmt("refs/heads/%s", ref);
125 if (get_sha1(longref, sha1) == 0)
126 return longref;
127
128 return ref;
129}
Lars Hjemli2101e262006-12-15 18:17:36 +0100130
Lars Hjemli5764fe92008-04-14 22:13:38 +0200131void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
132 char *path, int pager)
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100133{
134 struct rev_info rev;
135 struct commit *commit;
Lars Hjemlia1b01b22008-12-03 17:34:23 +0100136 const char *argv[] = {NULL, NULL, NULL, NULL, NULL};
Lars Hjemli732d68d2006-12-28 02:45:28 +0100137 int argc = 2;
Lars Hjemli5764fe92008-04-14 22:13:38 +0200138 int i, columns = 3;
Lars Hjemli48dc0032007-05-13 11:27:46 +0200139
Lars Hjemlicd79c162007-06-17 14:58:45 +0200140 if (!tip)
Lars Hjemlia1b01b22008-12-03 17:34:23 +0100141 tip = ctx.qry.head;
142
143 argv[1] = disambiguate_ref(tip);
Lars Hjemlicd79c162007-06-17 14:58:45 +0200144
Lars Hjemli68ca0322007-10-28 15:23:00 +0100145 if (grep && pattern && (!strcmp(grep, "grep") ||
146 !strcmp(grep, "author") ||
147 !strcmp(grep, "committer")))
148 argv[argc++] = fmt("--%s=%s", grep, pattern);
149
Lars Hjemli9fb53af2007-05-14 11:10:59 +0200150 if (path) {
151 argv[argc++] = "--";
152 argv[argc++] = path;
153 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100154 init_revisions(&rev, NULL);
155 rev.abbrev = DEFAULT_ABBREV;
156 rev.commit_format = CMIT_FMT_DEFAULT;
157 rev.verbose_header = 1;
158 rev.show_root_diff = 0;
Lars Hjemli732d68d2006-12-28 02:45:28 +0100159 setup_revisions(argc, argv, &rev, NULL);
Lars Hjemlief2dc552009-01-11 12:16:18 +0100160 load_ref_decorations();
161 rev.show_decorations = 1;
Lars Hjemlib7f33782008-10-05 19:19:59 +0200162 rev.grep_filter.regflags |= REG_ICASE;
163 compile_grep_patterns(&rev.grep_filter);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100164 prepare_revision_walk(&rev);
165
Lars Hjemli5764fe92008-04-14 22:13:38 +0200166 if (pager)
167 html("<table class='list nowrap'>");
Lars Hjemlie1893442007-05-18 13:55:52 +0200168
Lars Hjemli5764fe92008-04-14 22:13:38 +0200169 html("<tr class='nohover'><th class='left'>Age</th>"
Lars Hjemli0274b572008-11-29 18:39:41 +0100170 "<th class='left'>Commit message");
171 if (pager) {
172 html(" (");
Justin Waters1383fe32009-01-09 17:35:10 -0500173 cgit_log_link(ctx.qry.showmsg ? "Collapse" : "Expand", NULL,
174 NULL, ctx.qry.head, ctx.qry.sha1,
Lars Hjemli0274b572008-11-29 18:39:41 +0100175 ctx.qry.path, ctx.qry.ofs, ctx.qry.grep,
176 ctx.qry.search, ctx.qry.showmsg ? 0 : 1);
177 html(")");
178 }
179 html("</th><th class='left'>Author</th>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100180 if (ctx.repo->enable_log_filecount) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200181 html("<th class='left'>Files</th>");
182 columns++;
183 if (ctx.repo->enable_log_linecount) {
184 html("<th class='left'>Lines</th>");
185 columns++;
186 }
Lars Hjemlie1893442007-05-18 13:55:52 +0200187 }
Lars Hjemli5764fe92008-04-14 22:13:38 +0200188 html("</tr>\n");
Lars Hjemli420712a2006-12-14 00:40:34 +0100189
190 if (ofs<0)
191 ofs = 0;
192
193 for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) {
194 free(commit->buffer);
195 commit->buffer = NULL;
196 free_commit_list(commit->parents);
197 commit->parents = NULL;
198 }
199
200 for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) {
Lars Hjemli2101e262006-12-15 18:17:36 +0100201 print_commit(commit);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100202 free(commit->buffer);
203 commit->buffer = NULL;
204 free_commit_list(commit->parents);
205 commit->parents = NULL;
206 }
Ondrej Jirmana9226152007-05-26 03:26:14 +0200207 if (pager) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200208 htmlf("</table><div class='pager'>",
209 columns);
Ondrej Jirmana9226152007-05-26 03:26:14 +0200210 if (ofs > 0) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100211 cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
212 ctx.qry.sha1, ctx.qry.path,
213 ofs - cnt, ctx.qry.grep,
Lars Hjemli0274b572008-11-29 18:39:41 +0100214 ctx.qry.search, ctx.qry.showmsg);
Lars Hjemli103940f2007-06-29 20:27:41 +0200215 html("&nbsp;");
Ondrej Jirmana9226152007-05-26 03:26:14 +0200216 }
Ondrej Jirmana9226152007-05-26 03:26:14 +0200217 if ((commit = get_revision(&rev)) != NULL) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100218 cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
219 ctx.qry.sha1, ctx.qry.path,
220 ofs + cnt, ctx.qry.grep,
Lars Hjemli0274b572008-11-29 18:39:41 +0100221 ctx.qry.search, ctx.qry.showmsg);
Ondrej Jirmana9226152007-05-26 03:26:14 +0200222 }
223 html("</div>");
Lars Hjemli5764fe92008-04-14 22:13:38 +0200224 } else if ((commit = get_revision(&rev)) != NULL) {
225 html("<tr class='nohover'><td colspan='3'>");
226 cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL, NULL, 0,
Lars Hjemli0274b572008-11-29 18:39:41 +0100227 NULL, NULL, ctx.qry.showmsg);
Lars Hjemli5764fe92008-04-14 22:13:38 +0200228 html("</td></tr>\n");
Lars Hjemli420712a2006-12-14 00:40:34 +0100229 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100230}