]> gitweb.ps.run Git - ps-cgit/blob - ui-log.c
Add URL parameter 'context' for changing the number of context lines in diffs
[ps-cgit] / ui-log.c
1 /* 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"
10 #include "html.h"
11 #include "ui-shared.h"
12
13 int files, add_lines, rem_lines;
14
15 void count_lines(char *line, int size)
16 {
17         if (size <= 0)
18                 return;
19
20         if (line[0] == '+')
21                 add_lines++;
22
23         else if (line[0] == '-')
24                 rem_lines++;
25 }
26
27 void inspect_files(struct diff_filepair *pair)
28 {
29         unsigned long old_size = 0;
30         unsigned long new_size = 0;
31         int binary = 0;
32
33         files++;
34         if (ctx.repo->enable_log_linecount)
35                 cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
36                                 &new_size, &binary, 0, count_lines);
37 }
38
39 void 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,
50                                       ctx.qry.vpath, 0, NULL, NULL,
51                                       ctx.qry.showmsg);
52                 }
53                 else if (!prefixcmp(deco->name, "tag: refs/tags/")) {
54                         strncpy(buf, deco->name + 15, sizeof(buf) - 1);
55                         cgit_tag_link(buf, NULL, "tag-deco", ctx.qry.head, buf);
56                 }
57                 else if (!prefixcmp(deco->name, "refs/tags/")) {
58                         strncpy(buf, deco->name + 10, sizeof(buf) - 1);
59                         cgit_tag_link(buf, NULL, "tag-deco", ctx.qry.head, buf);
60                 }
61                 else if (!prefixcmp(deco->name, "refs/remotes/")) {
62                         strncpy(buf, deco->name + 13, sizeof(buf) - 1);
63                         cgit_log_link(buf, NULL, "remote-deco", NULL,
64                                       sha1_to_hex(commit->object.sha1),
65                                       ctx.qry.vpath, 0, NULL, NULL,
66                                       ctx.qry.showmsg);
67                 }
68                 else {
69                         strncpy(buf, deco->name, sizeof(buf) - 1);
70                         cgit_commit_link(buf, NULL, "deco", ctx.qry.head,
71                                          sha1_to_hex(commit->object.sha1),
72                                          ctx.qry.vpath, 0);
73                 }
74                 deco = deco->next;
75         }
76 }
77
78 void print_commit(struct commit *commit)
79 {
80         struct commitinfo *info;
81         char *tmp;
82         int cols = 2;
83
84         info = cgit_parse_commit(commit);
85         htmlf("<tr%s><td>",
86                 ctx.qry.showmsg ? " class='logheader'" : "");
87         tmp = fmt("id=%s", sha1_to_hex(commit->object.sha1));
88         tmp = cgit_fileurl(ctx.repo->url, "commit", ctx.qry.vpath, tmp);
89         html_link_open(tmp, NULL, NULL);
90         cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
91         html_link_close();
92         htmlf("</td><td%s>",
93                 ctx.qry.showmsg ? " class='logsubject'" : "");
94         cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
95                          sha1_to_hex(commit->object.sha1), ctx.qry.vpath, 0);
96         show_commit_decorations(commit);
97         html("</td><td>");
98         html_txt(info->author);
99         if (ctx.repo->enable_log_filecount) {
100                 files = 0;
101                 add_lines = 0;
102                 rem_lines = 0;
103                 cgit_diff_commit(commit, inspect_files);
104                 html("</td><td>");
105                 htmlf("%d", files);
106                 if (ctx.repo->enable_log_linecount) {
107                         html("</td><td>");
108                         htmlf("-%d/+%d", rem_lines, add_lines);
109                 }
110         }
111         html("</td></tr>\n");
112         if (ctx.qry.showmsg) {
113                 if (ctx.repo->enable_log_filecount) {
114                         cols++;
115                         if (ctx.repo->enable_log_linecount)
116                                 cols++;
117                 }
118                 htmlf("<tr class='nohover'><td/><td colspan='%d' class='logmsg'>",
119                         cols);
120                 html_txt(info->msg);
121                 html("</td></tr>\n");
122         }
123         cgit_free_commitinfo(info);
124 }
125
126 static const char *disambiguate_ref(const char *ref)
127 {
128         unsigned char sha1[20];
129         const char *longref;
130
131         longref = fmt("refs/heads/%s", ref);
132         if (get_sha1(longref, sha1) == 0)
133                 return longref;
134
135         return ref;
136 }
137
138 void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
139                     char *path, int pager)
140 {
141         struct rev_info rev;
142         struct commit *commit;
143         const char *argv[] = {NULL, NULL, NULL, NULL, NULL};
144         int argc = 2;
145         int i, columns = 3;
146
147         if (!tip)
148                 tip = ctx.qry.head;
149
150         argv[1] = disambiguate_ref(tip);
151
152         if (grep && pattern && (!strcmp(grep, "grep") ||
153                                 !strcmp(grep, "author") ||
154                                 !strcmp(grep, "committer")))
155                 argv[argc++] = fmt("--%s=%s", grep, pattern);
156
157         if (path) {
158                 argv[argc++] = "--";
159                 argv[argc++] = path;
160         }
161         init_revisions(&rev, NULL);
162         rev.abbrev = DEFAULT_ABBREV;
163         rev.commit_format = CMIT_FMT_DEFAULT;
164         rev.verbose_header = 1;
165         rev.show_root_diff = 0;
166         setup_revisions(argc, argv, &rev, NULL);
167         load_ref_decorations(DECORATE_FULL_REFS);
168         rev.show_decorations = 1;
169         rev.grep_filter.regflags |= REG_ICASE;
170         compile_grep_patterns(&rev.grep_filter);
171         prepare_revision_walk(&rev);
172
173         if (pager)
174                 html("<table class='list nowrap'>");
175
176         html("<tr class='nohover'><th class='left'>Age</th>"
177               "<th class='left'>Commit message");
178         if (pager) {
179                 html(" (");
180                 cgit_log_link(ctx.qry.showmsg ? "Collapse" : "Expand", NULL,
181                               NULL, ctx.qry.head, ctx.qry.sha1,
182                               ctx.qry.vpath, ctx.qry.ofs, ctx.qry.grep,
183                               ctx.qry.search, ctx.qry.showmsg ? 0 : 1);
184                 html(")");
185         }
186         html("</th><th class='left'>Author</th>");
187         if (ctx.repo->enable_log_filecount) {
188                 html("<th class='left'>Files</th>");
189                 columns++;
190                 if (ctx.repo->enable_log_linecount) {
191                         html("<th class='left'>Lines</th>");
192                         columns++;
193                 }
194         }
195         html("</tr>\n");
196
197         if (ofs<0)
198                 ofs = 0;
199
200         for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) {
201                 free(commit->buffer);
202                 commit->buffer = NULL;
203                 free_commit_list(commit->parents);
204                 commit->parents = NULL;
205         }
206
207         for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) {
208                 print_commit(commit);
209                 free(commit->buffer);
210                 commit->buffer = NULL;
211                 free_commit_list(commit->parents);
212                 commit->parents = NULL;
213         }
214         if (pager) {
215                 htmlf("</table><div class='pager'>",
216                      columns);
217                 if (ofs > 0) {
218                         cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
219                                       ctx.qry.sha1, ctx.qry.vpath,
220                                       ofs - cnt, ctx.qry.grep,
221                                       ctx.qry.search, ctx.qry.showmsg);
222                         html("&nbsp;");
223                 }
224                 if ((commit = get_revision(&rev)) != NULL) {
225                         cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
226                                       ctx.qry.sha1, ctx.qry.vpath,
227                                       ofs + cnt, ctx.qry.grep,
228                                       ctx.qry.search, ctx.qry.showmsg);
229                 }
230                 html("</div>");
231         } else if ((commit = get_revision(&rev)) != NULL) {
232                 html("<tr class='nohover'><td colspan='3'>");
233                 cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL,
234                               ctx.qry.vpath, 0, NULL, NULL, ctx.qry.showmsg);
235                 html("</td></tr>\n");
236         }
237 }