1 /* ui-blame.c: functions for blame output
3 * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 #include "ui-shared.h"
17 static char *emit_suspect_detail(struct blame_origin *suspect)
19 struct commitinfo *info;
20 struct strbuf detail = STRBUF_INIT;
22 info = cgit_parse_commit(suspect->commit);
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)));
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)));
38 strbuf_addstr(&detail, info->subject);
40 cgit_free_commitinfo(info);
41 return strbuf_detach(&detail, NULL);
44 static void emit_blame_entry_hash(struct blame_entry *ent)
46 struct blame_origin *suspect = ent->suspect;
47 struct object_id *oid = &suspect->commit->object.oid;
48 unsigned long line = 0;
50 char *detail = emit_suspect_detail(suspect);
51 html("<span class='oid'>");
52 cgit_commit_link(repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV), detail,
53 NULL, ctx.qry.head, oid_to_hex(oid), suspect->path);
57 if (!repo_parse_commit(the_repository, suspect->commit) && suspect->commit->parents) {
58 struct commit *parent = suspect->commit->parents->item;
61 cgit_blame_link("^", "Blame the previous revision", NULL,
62 ctx.qry.head, oid_to_hex(&parent->object.oid),
66 while (line++ < ent->num_lines)
70 static void emit_blame_entry_linenumber(struct blame_entry *ent)
72 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
74 unsigned long lineno = ent->lno;
75 while (lineno < ent->lno + ent->num_lines)
76 htmlf(numberfmt, ++lineno);
79 static void emit_blame_entry_line_background(struct blame_scoreboard *sb,
80 struct blame_entry *ent)
83 size_t len, maxlen = 2;
84 const char* pos, *endpos;
86 for (line = ent->lno; line < ent->lno + ent->num_lines; line++) {
88 pos = blame_nth_line(sb, line);
89 endpos = blame_nth_line(sb, line + 1);
91 while (pos < endpos) {
100 for (len = 0; len < maxlen - 1; len++)
104 struct walk_tree_context {
110 static void print_object(const struct object_id *oid, const char *path,
111 const char *basename, const char *rev)
113 enum object_type type;
116 struct strvec rev_argv = STRVEC_INIT;
117 struct rev_info revs;
118 struct blame_scoreboard sb;
119 struct blame_origin *o;
120 struct blame_entry *ent = NULL;
122 type = oid_object_info(the_repository, oid, &size);
123 if (type == OBJ_BAD) {
124 cgit_print_error_page(404, "Not found", "Bad object name: %s",
129 buf = repo_read_object_file(the_repository, oid, &type, &size);
131 cgit_print_error_page(500, "Internal server error",
132 "Error reading object %s", oid_to_hex(oid));
136 strvec_push(&rev_argv, "blame");
137 strvec_push(&rev_argv, rev);
138 repo_init_revisions(the_repository, &revs, NULL);
139 revs.diffopt.flags.allow_textconv = 1;
140 setup_revisions(rev_argv.nr, rev_argv.v, &revs, NULL);
141 init_scoreboard(&sb);
143 sb.repo = the_repository;
145 setup_scoreboard(&sb, &o);
146 o->suspects = blame_entry_prepend(NULL, 0, sb.num_lines, o);
147 prio_queue_put(&sb.commits, o->commit);
148 blame_origin_decref(o);
151 assign_blame(&sb, 0);
152 blame_sort_final(&sb);
155 cgit_set_title_from_path(path);
157 cgit_print_layout_start();
158 htmlf("blob: %s (", oid_to_hex(oid));
159 cgit_plain_link("plain", NULL, NULL, ctx.qry.head, rev, path);
161 cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path);
164 if (buffer_is_binary(buf, size)) {
165 html("<div class='error'>blob is binary.</div>");
168 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
169 htmlf("<div class='error'>blob size (%ldKB)"
170 " exceeds display size limit (%dKB).</div>",
171 size / 1024, ctx.cfg.max_blob_size);
175 html("<table class='blame blob'>\n<tr>\n");
178 html("<td class='hashes'>");
179 for (ent = sb.ent; ent; ent = ent->next) {
180 html("<div class='alt'><pre>");
181 emit_blame_entry_hash(ent);
182 html("</pre></div>");
187 if (ctx.cfg.enable_tree_linenumbers) {
188 html("<td class='linenumbers'>");
189 for (ent = sb.ent; ent; ent = ent->next) {
190 html("<div class='alt'><pre>");
191 emit_blame_entry_linenumber(ent);
192 html("</pre></div>");
197 html("<td class='lines'><div>");
199 /* Colored bars behind lines */
201 for (ent = sb.ent; ent; ) {
202 struct blame_entry *e = ent->next;
203 html("<div class='alt'><pre>");
204 emit_blame_entry_line_background(&sb, ent);
205 html("</pre></div>");
211 free((void *)sb.final_buf);
215 if (ctx.repo->source_filter) {
216 char *filter_arg = xstrdup(basename);
217 cgit_open_filter(ctx.repo->source_filter, filter_arg);
219 cgit_close_filter(ctx.repo->source_filter);
224 html("</code></pre>");
226 html("</div></td>\n");
228 html("</tr>\n</table>\n");
230 cgit_print_layout_end();
236 static int walk_tree(const struct object_id *oid, struct strbuf *base,
237 const char *pathname, unsigned mode, void *cbdata)
239 struct walk_tree_context *walk_tree_ctx = cbdata;
241 if (base->len == walk_tree_ctx->match_baselen) {
243 struct strbuf buffer = STRBUF_INIT;
244 strbuf_addbuf(&buffer, base);
245 strbuf_addstr(&buffer, pathname);
246 print_object(oid, buffer.buf, pathname,
247 walk_tree_ctx->curr_rev);
248 strbuf_release(&buffer);
249 walk_tree_ctx->state = 1;
250 } else if (S_ISDIR(mode)) {
251 walk_tree_ctx->state = 2;
253 } else if (base->len < INT_MAX
254 && (int)base->len > walk_tree_ctx->match_baselen) {
255 walk_tree_ctx->state = 2;
256 } else if (S_ISDIR(mode)) {
257 return READ_TREE_RECURSIVE;
262 static int basedir_len(const char *path)
264 char *p = strrchr(path, '/');
270 void cgit_print_blame(void)
272 const char *rev = ctx.qry.oid;
273 struct object_id oid;
274 struct commit *commit;
275 struct pathspec_item path_items = {
276 .match = ctx.qry.path,
277 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
279 struct pathspec paths = {
283 struct walk_tree_context walk_tree_ctx = {
290 if (repo_get_oid(the_repository, rev, &oid)) {
291 cgit_print_error_page(404, "Not found",
292 "Invalid revision name: %s", rev);
295 commit = lookup_commit_reference(the_repository, &oid);
296 if (!commit || repo_parse_commit(the_repository, commit)) {
297 cgit_print_error_page(404, "Not found",
298 "Invalid commit reference: %s", rev);
302 walk_tree_ctx.curr_rev = xstrdup(rev);
303 walk_tree_ctx.match_baselen = (path_items.match) ?
304 basedir_len(path_items.match) : -1;
306 read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
307 &paths, walk_tree, &walk_tree_ctx);
308 if (!walk_tree_ctx.state)
309 cgit_print_error_page(404, "Not found", "Not found");
310 else if (walk_tree_ctx.state == 2)
311 cgit_print_error_page(404, "No blame for folders",
312 "Blame is not available for folders.");
314 free(walk_tree_ctx.curr_rev);