blob: d2129844032d59aebfa8172c3a6f34e8ad85c2cf [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{
29 files++;
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010030 if (ctx.repo->enable_log_linecount)
Lars Hjemlie1893442007-05-18 13:55:52 +020031 cgit_diff_files(pair->one->sha1, pair->two->sha1, count_lines);
Lars Hjemli48dc0032007-05-13 11:27:46 +020032}
33
Lars Hjemli2101e262006-12-15 18:17:36 +010034void print_commit(struct commit *commit)
Lars Hjemlid14c5f62006-12-11 17:04:19 +010035{
Lars Hjemli2101e262006-12-15 18:17:36 +010036 struct commitinfo *info;
Lars Hjemlie9a70422008-04-14 22:23:48 +020037 char *tmp;
Lars Hjemlid14c5f62006-12-11 17:04:19 +010038
Lars Hjemli2101e262006-12-15 18:17:36 +010039 info = cgit_parse_commit(commit);
Lars Hjemlid14c5f62006-12-11 17:04:19 +010040 html("<tr><td>");
Lars Hjemlie9a70422008-04-14 22:23:48 +020041 tmp = fmt("id=%s", sha1_to_hex(commit->object.sha1));
42 tmp = cgit_pageurl(ctx.repo->url, "commit", tmp);
43 html_link_open(tmp, NULL, NULL);
Lars Hjemli237ef7b2007-05-22 23:15:36 +020044 cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
Lars Hjemlie9a70422008-04-14 22:23:48 +020045 html_link_close();
Lars Hjemlid14c5f62006-12-11 17:04:19 +010046 html("</td><td>");
Lars Hjemlid14d77f2008-02-16 11:53:40 +010047 cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
Lars Hjemli42a7eb92007-06-17 14:53:02 +020048 sha1_to_hex(commit->object.sha1));
Lars Hjemli5764fe92008-04-14 22:13:38 +020049 html("</td><td>");
50 html_txt(info->author);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010051 if (ctx.repo->enable_log_filecount) {
Lars Hjemlie1893442007-05-18 13:55:52 +020052 files = 0;
Lars Hjemlid04c4732007-11-06 00:35:12 +010053 add_lines = 0;
54 rem_lines = 0;
Lars Hjemlie1893442007-05-18 13:55:52 +020055 cgit_diff_commit(commit, inspect_files);
Lars Hjemli5764fe92008-04-14 22:13:38 +020056 html("</td><td>");
Lars Hjemlie1893442007-05-18 13:55:52 +020057 htmlf("%d", files);
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +010058 if (ctx.repo->enable_log_linecount) {
Lars Hjemli5764fe92008-04-14 22:13:38 +020059 html("</td><td>");
Lars Hjemlid04c4732007-11-06 00:35:12 +010060 htmlf("-%d/+%d", rem_lines, add_lines);
Lars Hjemlie1893442007-05-18 13:55:52 +020061 }
62 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +010063 html("</td></tr>\n");
Lars Hjemliaaa24bd2006-12-16 14:58:20 +010064 cgit_free_commitinfo(info);
Lars Hjemlid14c5f62006-12-11 17:04:19 +010065}
66
Lars Hjemlia1b01b22008-12-03 17:34:23 +010067static const char *disambiguate_ref(const char *ref)
68{
69 unsigned char sha1[20];
70 const char *longref;
71
72 longref = fmt("refs/heads/%s", ref);
73 if (get_sha1(longref, sha1) == 0)
74 return longref;
75
76 return ref;
77}
Lars Hjemli2101e262006-12-15 18:17:36 +010078
Lars Hjemli5764fe92008-04-14 22:13:38 +020079void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
80 char *path, int pager)
Lars Hjemlid14c5f62006-12-11 17:04:19 +010081{
82 struct rev_info rev;
83 struct commit *commit;
Lars Hjemlia1b01b22008-12-03 17:34:23 +010084 const char *argv[] = {NULL, NULL, NULL, NULL, NULL};
Lars Hjemli732d68d2006-12-28 02:45:28 +010085 int argc = 2;
Lars Hjemli5764fe92008-04-14 22:13:38 +020086 int i, columns = 3;
Lars Hjemli48dc0032007-05-13 11:27:46 +020087
Lars Hjemlicd79c162007-06-17 14:58:45 +020088 if (!tip)
Lars Hjemlia1b01b22008-12-03 17:34:23 +010089 tip = ctx.qry.head;
90
91 argv[1] = disambiguate_ref(tip);
Lars Hjemlicd79c162007-06-17 14:58:45 +020092
Lars Hjemli68ca0322007-10-28 15:23:00 +010093 if (grep && pattern && (!strcmp(grep, "grep") ||
94 !strcmp(grep, "author") ||
95 !strcmp(grep, "committer")))
96 argv[argc++] = fmt("--%s=%s", grep, pattern);
97
Lars Hjemli9fb53af2007-05-14 11:10:59 +020098 if (path) {
99 argv[argc++] = "--";
100 argv[argc++] = path;
101 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100102 init_revisions(&rev, NULL);
103 rev.abbrev = DEFAULT_ABBREV;
104 rev.commit_format = CMIT_FMT_DEFAULT;
105 rev.verbose_header = 1;
106 rev.show_root_diff = 0;
Lars Hjemli732d68d2006-12-28 02:45:28 +0100107 setup_revisions(argc, argv, &rev, NULL);
Lars Hjemlib7f33782008-10-05 19:19:59 +0200108 rev.grep_filter.regflags |= REG_ICASE;
109 compile_grep_patterns(&rev.grep_filter);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100110 prepare_revision_walk(&rev);
111
Lars Hjemli5764fe92008-04-14 22:13:38 +0200112 if (pager)
113 html("<table class='list nowrap'>");
Lars Hjemlie1893442007-05-18 13:55:52 +0200114
Lars Hjemli5764fe92008-04-14 22:13:38 +0200115 html("<tr class='nohover'><th class='left'>Age</th>"
116 "<th class='left'>Commit message</th>"
117 "<th class='left'>Author</th>");
Lars Hjemlid1f3bbe2008-02-16 13:56:09 +0100118 if (ctx.repo->enable_log_filecount) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200119 html("<th class='left'>Files</th>");
120 columns++;
121 if (ctx.repo->enable_log_linecount) {
122 html("<th class='left'>Lines</th>");
123 columns++;
124 }
Lars Hjemlie1893442007-05-18 13:55:52 +0200125 }
Lars Hjemli5764fe92008-04-14 22:13:38 +0200126 html("</tr>\n");
Lars Hjemli420712a2006-12-14 00:40:34 +0100127
128 if (ofs<0)
129 ofs = 0;
130
131 for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) {
132 free(commit->buffer);
133 commit->buffer = NULL;
134 free_commit_list(commit->parents);
135 commit->parents = NULL;
136 }
137
138 for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) {
Lars Hjemli2101e262006-12-15 18:17:36 +0100139 print_commit(commit);
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100140 free(commit->buffer);
141 commit->buffer = NULL;
142 free_commit_list(commit->parents);
143 commit->parents = NULL;
144 }
Ondrej Jirmana9226152007-05-26 03:26:14 +0200145 if (pager) {
Lars Hjemli5764fe92008-04-14 22:13:38 +0200146 htmlf("</table><div class='pager'>",
147 columns);
Ondrej Jirmana9226152007-05-26 03:26:14 +0200148 if (ofs > 0) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100149 cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
150 ctx.qry.sha1, ctx.qry.path,
151 ofs - cnt, ctx.qry.grep,
152 ctx.qry.search);
Lars Hjemli103940f2007-06-29 20:27:41 +0200153 html("&nbsp;");
Ondrej Jirmana9226152007-05-26 03:26:14 +0200154 }
Ondrej Jirmana9226152007-05-26 03:26:14 +0200155 if ((commit = get_revision(&rev)) != NULL) {
Lars Hjemlid14d77f2008-02-16 11:53:40 +0100156 cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
157 ctx.qry.sha1, ctx.qry.path,
158 ofs + cnt, ctx.qry.grep,
159 ctx.qry.search);
Ondrej Jirmana9226152007-05-26 03:26:14 +0200160 }
161 html("</div>");
Lars Hjemli5764fe92008-04-14 22:13:38 +0200162 } else if ((commit = get_revision(&rev)) != NULL) {
163 html("<tr class='nohover'><td colspan='3'>");
164 cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL, NULL, 0,
165 NULL, NULL);
166 html("</td></tr>\n");
Lars Hjemli420712a2006-12-14 00:40:34 +0100167 }
Lars Hjemlid14c5f62006-12-11 17:04:19 +0100168}