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)
9 #define USE_THE_REPOSITORY_VARIABLE
14 #include "ui-shared.h"
19 static char *emit_suspect_detail(struct blame_origin *suspect)
21 struct commitinfo *info;
22 struct strbuf detail = STRBUF_INIT;
24 info = cgit_parse_commit(suspect->commit);
26 strbuf_addf(&detail, "author %s", info->author);
27 if (!ctx.cfg.noplainemail)
28 strbuf_addf(&detail, " %s", info->author_email);
29 strbuf_addf(&detail, " %s\n",
30 show_date(info->author_date, info->author_tz,
31 cgit_date_mode(DATE_ISO8601)));
33 strbuf_addf(&detail, "committer %s", info->committer);
34 if (!ctx.cfg.noplainemail)
35 strbuf_addf(&detail, " %s", info->committer_email);
36 strbuf_addf(&detail, " %s\n\n",
37 show_date(info->committer_date, info->committer_tz,
38 cgit_date_mode(DATE_ISO8601)));
40 strbuf_addstr(&detail, info->subject);
42 cgit_free_commitinfo(info);
43 return strbuf_detach(&detail, NULL);
46 static void emit_blame_entry_hash(struct blame_entry *ent)
48 struct blame_origin *suspect = ent->suspect;
49 struct object_id *oid = &suspect->commit->object.oid;
50 unsigned long line = 0;
52 char *detail = emit_suspect_detail(suspect);
53 html("<span class='oid'>");
54 cgit_commit_link(repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV), detail,
55 NULL, ctx.qry.head, oid_to_hex(oid), suspect->path);
59 if (!repo_parse_commit(the_repository, suspect->commit) && suspect->commit->parents) {
60 struct commit *parent = suspect->commit->parents->item;
63 cgit_blame_link("^", "Blame the previous revision", NULL,
64 ctx.qry.head, oid_to_hex(&parent->object.oid),
68 while (line++ < ent->num_lines)
72 static void emit_blame_entry_linenumber(struct blame_entry *ent)
74 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
76 unsigned long lineno = ent->lno;
77 while (lineno < ent->lno + ent->num_lines)
78 htmlf(numberfmt, ++lineno);
81 static void emit_blame_entry_line_background(struct blame_scoreboard *sb,
82 struct blame_entry *ent)
85 size_t len, maxlen = 2;
86 const char* pos, *endpos;
88 for (line = ent->lno; line < ent->lno + ent->num_lines; line++) {
90 pos = blame_nth_line(sb, line);
91 endpos = blame_nth_line(sb, line + 1);
93 while (pos < endpos) {
102 for (len = 0; len < maxlen - 1; len++)
106 struct walk_tree_context {
112 static void print_object(const struct object_id *oid, const char *path,
113 const char *basename, const char *rev)
115 enum object_type type;
118 struct strvec rev_argv = STRVEC_INIT;
119 struct rev_info revs;
120 struct blame_scoreboard sb;
121 struct blame_origin *o;
122 struct blame_entry *ent = NULL;
124 type = oid_object_info(the_repository, oid, &size);
125 if (type == OBJ_BAD) {
126 cgit_print_error_page(404, "Not found", "Bad object name: %s",
131 buf = repo_read_object_file(the_repository, oid, &type, &size);
133 cgit_print_error_page(500, "Internal server error",
134 "Error reading object %s", oid_to_hex(oid));
138 strvec_push(&rev_argv, "blame");
139 strvec_push(&rev_argv, rev);
140 repo_init_revisions(the_repository, &revs, NULL);
141 revs.diffopt.flags.allow_textconv = 1;
142 setup_revisions(rev_argv.nr, rev_argv.v, &revs, NULL);
143 init_scoreboard(&sb);
145 sb.repo = the_repository;
147 setup_scoreboard(&sb, &o);
148 o->suspects = blame_entry_prepend(NULL, 0, sb.num_lines, o);
149 prio_queue_put(&sb.commits, o->commit);
150 blame_origin_decref(o);
153 assign_blame(&sb, 0);
154 blame_sort_final(&sb);
157 cgit_set_title_from_path(path);
159 cgit_print_layout_start();
160 htmlf("blob: %s (", oid_to_hex(oid));
161 cgit_plain_link("plain", NULL, NULL, ctx.qry.head, rev, path);
163 cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path);
166 if (buffer_is_binary(buf, size)) {
167 html("<div class='error'>blob is binary.</div>");
170 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
171 htmlf("<div class='error'>blob size (%ldKB)"
172 " exceeds display size limit (%dKB).</div>",
173 size / 1024, ctx.cfg.max_blob_size);
177 html("<table class='blame blob'>\n<tr>\n");
180 html("<td class='hashes'>");
181 for (ent = sb.ent; ent; ent = ent->next) {
182 html("<div class='alt'><pre>");
183 emit_blame_entry_hash(ent);
184 html("</pre></div>");
189 if (ctx.cfg.enable_tree_linenumbers) {
190 html("<td class='linenumbers'>");
191 for (ent = sb.ent; ent; ent = ent->next) {
192 html("<div class='alt'><pre>");
193 emit_blame_entry_linenumber(ent);
194 html("</pre></div>");
199 html("<td class='lines'><div>");
201 /* Colored bars behind lines */
203 for (ent = sb.ent; ent; ) {
204 struct blame_entry *e = ent->next;
205 html("<div class='alt'><pre>");
206 emit_blame_entry_line_background(&sb, ent);
207 html("</pre></div>");
213 free((void *)sb.final_buf);
217 if (ctx.repo->source_filter) {
218 char *filter_arg = xstrdup(basename);
219 cgit_open_filter(ctx.repo->source_filter, filter_arg);
221 cgit_close_filter(ctx.repo->source_filter);
226 html("</code></pre>");
228 html("</div></td>\n");
230 html("</tr>\n</table>\n");
232 cgit_print_layout_end();
238 static int walk_tree(const struct object_id *oid, struct strbuf *base,
239 const char *pathname, unsigned mode, void *cbdata)
241 struct walk_tree_context *walk_tree_ctx = cbdata;
243 if (base->len == walk_tree_ctx->match_baselen) {
245 struct strbuf buffer = STRBUF_INIT;
246 strbuf_addbuf(&buffer, base);
247 strbuf_addstr(&buffer, pathname);
248 print_object(oid, buffer.buf, pathname,
249 walk_tree_ctx->curr_rev);
250 strbuf_release(&buffer);
251 walk_tree_ctx->state = 1;
252 } else if (S_ISDIR(mode)) {
253 walk_tree_ctx->state = 2;
255 } else if (base->len < INT_MAX
256 && (int)base->len > walk_tree_ctx->match_baselen) {
257 walk_tree_ctx->state = 2;
258 } else if (S_ISDIR(mode)) {
259 return READ_TREE_RECURSIVE;
264 static int basedir_len(const char *path)
266 char *p = strrchr(path, '/');
272 void cgit_print_blame(void)
274 const char *rev = ctx.qry.oid;
275 struct object_id oid;
276 struct commit *commit;
277 struct pathspec_item path_items = {
278 .match = ctx.qry.path,
279 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
281 struct pathspec paths = {
285 struct walk_tree_context walk_tree_ctx = {
292 if (repo_get_oid(the_repository, rev, &oid)) {
293 cgit_print_error_page(404, "Not found",
294 "Invalid revision name: %s", rev);
297 commit = lookup_commit_reference(the_repository, &oid);
298 if (!commit || repo_parse_commit(the_repository, commit)) {
299 cgit_print_error_page(404, "Not found",
300 "Invalid commit reference: %s", rev);
304 walk_tree_ctx.curr_rev = xstrdup(rev);
305 walk_tree_ctx.match_baselen = (path_items.match) ?
306 basedir_len(path_items.match) : -1;
308 read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
309 &paths, walk_tree, &walk_tree_ctx);
310 if (!walk_tree_ctx.state)
311 cgit_print_error_page(404, "Not found", "Not found");
312 else if (walk_tree_ctx.state == 2)
313 cgit_print_error_page(404, "No blame for folders",
314 "Blame is not available for folders.");
316 free(walk_tree_ctx.curr_rev);