]> gitweb.ps.run Git - ps-cgit/blob - ui-blame.c
ui-commit: show subject in commit page title
[ps-cgit] / ui-blame.c
1 /* ui-blame.c: functions for blame output
2  *
3  * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10 #include "ui-blame.h"
11 #include "html.h"
12 #include "ui-shared.h"
13 #include "strvec.h"
14 #include "blame.h"
15
16
17 static char *emit_suspect_detail(struct blame_origin *suspect)
18 {
19         struct commitinfo *info;
20         struct strbuf detail = STRBUF_INIT;
21
22         info = cgit_parse_commit(suspect->commit);
23
24         strbuf_addf(&detail, "author  %s", info->author);
25         if (!ctx.cfg.noplainemail)
26                 strbuf_addf(&detail, " %s", info->author_email);
27         strbuf_addf(&detail, "  %s\n",
28                     show_date(info->author_date, info->author_tz,
29                                     cgit_date_mode(DATE_ISO8601)));
30
31         strbuf_addf(&detail, "committer  %s", info->committer);
32         if (!ctx.cfg.noplainemail)
33                 strbuf_addf(&detail, " %s", info->committer_email);
34         strbuf_addf(&detail, "  %s\n\n",
35                     show_date(info->committer_date, info->committer_tz,
36                                     cgit_date_mode(DATE_ISO8601)));
37
38         strbuf_addstr(&detail, info->subject);
39
40         cgit_free_commitinfo(info);
41         return strbuf_detach(&detail, NULL);
42 }
43
44 static void emit_blame_entry_hash(struct blame_entry *ent)
45 {
46         struct blame_origin *suspect = ent->suspect;
47         struct object_id *oid = &suspect->commit->object.oid;
48         unsigned long line = 0;
49
50         char *detail = emit_suspect_detail(suspect);
51         html("<span class='oid'>");
52         cgit_commit_link(find_unique_abbrev(oid, DEFAULT_ABBREV), detail,
53                          NULL, ctx.qry.head, oid_to_hex(oid), suspect->path);
54         html("</span>");
55         free(detail);
56
57         while (line++ < ent->num_lines)
58                 html("\n");
59 }
60
61 static void emit_blame_entry_linenumber(struct blame_entry *ent)
62 {
63         const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
64
65         unsigned long lineno = ent->lno;
66         while (lineno < ent->lno + ent->num_lines)
67                 htmlf(numberfmt, ++lineno);
68 }
69
70 static void emit_blame_entry_line_background(struct blame_scoreboard *sb,
71                                              struct blame_entry *ent)
72 {
73         unsigned long line;
74         size_t len, maxlen = 2;
75         const char* pos, *endpos;
76
77         for (line = ent->lno; line < ent->lno + ent->num_lines; line++) {
78                 html("\n");
79                 pos = blame_nth_line(sb, line);
80                 endpos = blame_nth_line(sb, line + 1);
81                 len = 0;
82                 while (pos < endpos) {
83                         len++;
84                         if (*pos++ == '\t')
85                                 len = (len + 7) & ~7;
86                 }
87                 if (len > maxlen)
88                         maxlen = len;
89         }
90
91         for (len = 0; len < maxlen - 1; len++)
92                 html(" ");
93 }
94
95 struct walk_tree_context {
96         char *curr_rev;
97         int match_baselen;
98         int state;
99 };
100
101 static void print_object(const struct object_id *oid, const char *path,
102                          const char *basename, const char *rev)
103 {
104         enum object_type type;
105         char *buf;
106         unsigned long size;
107         struct strvec rev_argv = STRVEC_INIT;
108         struct rev_info revs;
109         struct blame_scoreboard sb;
110         struct blame_origin *o;
111         struct blame_entry *ent = NULL;
112
113         type = oid_object_info(the_repository, oid, &size);
114         if (type == OBJ_BAD) {
115                 cgit_print_error_page(404, "Not found", "Bad object name: %s",
116                                       oid_to_hex(oid));
117                 return;
118         }
119
120         buf = read_object_file(oid, &type, &size);
121         if (!buf) {
122                 cgit_print_error_page(500, "Internal server error",
123                         "Error reading object %s", oid_to_hex(oid));
124                 return;
125         }
126
127         strvec_push(&rev_argv, "blame");
128         strvec_push(&rev_argv, rev);
129         init_revisions(&revs, NULL);
130         revs.diffopt.flags.allow_textconv = 1;
131         setup_revisions(rev_argv.nr, rev_argv.v, &revs, NULL);
132         init_scoreboard(&sb);
133         sb.revs = &revs;
134         sb.repo = the_repository;
135         sb.path = path;
136         setup_scoreboard(&sb, &o);
137         o->suspects = blame_entry_prepend(NULL, 0, sb.num_lines, o);
138         prio_queue_put(&sb.commits, o->commit);
139         blame_origin_decref(o);
140         sb.ent = NULL;
141         sb.path = path;
142         assign_blame(&sb, 0);
143         blame_sort_final(&sb);
144         blame_coalesce(&sb);
145
146         cgit_set_title_from_path(path);
147
148         cgit_print_layout_start();
149         htmlf("blob: %s (", oid_to_hex(oid));
150         cgit_plain_link("plain", NULL, NULL, ctx.qry.head, rev, path);
151         html(") (");
152         cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path);
153         html(")\n");
154
155         if (buffer_is_binary(buf, size)) {
156                 html("<div class='error'>blob is binary.</div>");
157                 goto cleanup;
158         }
159         if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
160                 htmlf("<div class='error'>blob size (%ldKB)"
161                       " exceeds display size limit (%dKB).</div>",
162                       size / 1024, ctx.cfg.max_blob_size);
163                 goto cleanup;
164         }
165
166         html("<table class='blame blob'>\n<tr>\n");
167
168         /* Commit hashes */
169         html("<td class='hashes'>");
170         for (ent = sb.ent; ent; ent = ent->next) {
171                 html("<div class='alt'><pre>");
172                 emit_blame_entry_hash(ent);
173                 html("</pre></div>");
174         }
175         html("</td>\n");
176
177         /* Line numbers */
178         if (ctx.cfg.enable_tree_linenumbers) {
179                 html("<td class='linenumbers'>");
180                 for (ent = sb.ent; ent; ent = ent->next) {
181                         html("<div class='alt'><pre>");
182                         emit_blame_entry_linenumber(ent);
183                         html("</pre></div>");
184                 }
185                 html("</td>\n");
186         }
187
188         html("<td class='lines'><div>");
189
190         /* Colored bars behind lines */
191         html("<div>");
192         for (ent = sb.ent; ent; ) {
193                 struct blame_entry *e = ent->next;
194                 html("<div class='alt'><pre>");
195                 emit_blame_entry_line_background(&sb, ent);
196                 html("</pre></div>");
197                 free(ent);
198                 ent = e;
199         }
200         html("</div>");
201
202         free((void *)sb.final_buf);
203
204         /* Lines */
205         html("<pre><code>");
206         if (ctx.repo->source_filter) {
207                 char *filter_arg = xstrdup(basename);
208                 cgit_open_filter(ctx.repo->source_filter, filter_arg);
209                 html_raw(buf, size);
210                 cgit_close_filter(ctx.repo->source_filter);
211                 free(filter_arg);
212         } else {
213                 html_txt(buf);
214         }
215         html("</code></pre>");
216
217         html("</div></td>\n");
218
219         html("</tr>\n</table>\n");
220
221         cgit_print_layout_end();
222
223 cleanup:
224         free(buf);
225 }
226
227 static int walk_tree(const struct object_id *oid, struct strbuf *base,
228                      const char *pathname, unsigned mode, void *cbdata)
229 {
230         struct walk_tree_context *walk_tree_ctx = cbdata;
231
232         if (base->len == walk_tree_ctx->match_baselen) {
233                 if (S_ISREG(mode)) {
234                         struct strbuf buffer = STRBUF_INIT;
235                         strbuf_addbuf(&buffer, base);
236                         strbuf_addstr(&buffer, pathname);
237                         print_object(oid, buffer.buf, pathname,
238                                      walk_tree_ctx->curr_rev);
239                         strbuf_release(&buffer);
240                         walk_tree_ctx->state = 1;
241                 } else if (S_ISDIR(mode)) {
242                         walk_tree_ctx->state = 2;
243                 }
244         } else if (base->len < INT_MAX
245                         && (int)base->len > walk_tree_ctx->match_baselen) {
246                 walk_tree_ctx->state = 2;
247         } else if (S_ISDIR(mode)) {
248                 return READ_TREE_RECURSIVE;
249         }
250         return 0;
251 }
252
253 static int basedir_len(const char *path)
254 {
255         char *p = strrchr(path, '/');
256         if (p)
257                 return p - path + 1;
258         return 0;
259 }
260
261 void cgit_print_blame(void)
262 {
263         const char *rev = ctx.qry.oid;
264         struct object_id oid;
265         struct commit *commit;
266         struct pathspec_item path_items = {
267                 .match = ctx.qry.path,
268                 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
269         };
270         struct pathspec paths = {
271                 .nr = 1,
272                 .items = &path_items
273         };
274         struct walk_tree_context walk_tree_ctx = {
275                 .state = 0
276         };
277
278         if (!rev)
279                 rev = ctx.qry.head;
280
281         if (get_oid(rev, &oid)) {
282                 cgit_print_error_page(404, "Not found",
283                         "Invalid revision name: %s", rev);
284                 return;
285         }
286         commit = lookup_commit_reference(the_repository, &oid);
287         if (!commit || parse_commit(commit)) {
288                 cgit_print_error_page(404, "Not found",
289                         "Invalid commit reference: %s", rev);
290                 return;
291         }
292
293         walk_tree_ctx.curr_rev = xstrdup(rev);
294         walk_tree_ctx.match_baselen = (path_items.match) ?
295                                        basedir_len(path_items.match) : -1;
296
297         read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
298                   &paths, walk_tree, &walk_tree_ctx);
299         if (!walk_tree_ctx.state)
300                 cgit_print_error_page(404, "Not found", "Not found");
301         else if (walk_tree_ctx.state == 2)
302                 cgit_print_error_page(404, "No blame for folders",
303                         "Blame is not available for folders.");
304
305         free(walk_tree_ctx.curr_rev);
306 }