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"
13 #include "argv-array.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='sha1'>");
52 cgit_commit_link(find_unique_abbrev(oid, DEFAULT_ABBREV), detail,
53 NULL, ctx.qry.head, oid_to_hex(oid), suspect->path);
57 while (line++ < ent->num_lines)
61 static void emit_blame_entry_linenumber(struct blame_entry *ent)
63 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
65 unsigned long lineno = ent->lno;
66 while (lineno < ent->lno + ent->num_lines)
67 htmlf(numberfmt, ++lineno);
70 static void emit_blame_entry_line_background(struct blame_scoreboard *sb,
71 struct blame_entry *ent)
74 size_t len, maxlen = 2;
75 const char* pos, *endpos;
77 for (line = ent->lno; line < ent->lno + ent->num_lines; line++) {
79 pos = blame_nth_line(sb, line);
80 endpos = blame_nth_line(sb, line + 1);
82 while (pos < endpos) {
91 for (len = 0; len < maxlen - 1; len++)
95 struct walk_tree_context {
101 static void print_object(const struct object_id *oid, const char *path,
102 const char *basename, const char *rev)
104 enum object_type type;
107 struct argv_array rev_argv = ARGV_ARRAY_INIT;
108 struct rev_info revs;
109 struct blame_scoreboard sb;
110 struct blame_origin *o;
111 struct blame_entry *ent = NULL;
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",
120 buf = read_object_file(oid, &type, &size);
122 cgit_print_error_page(500, "Internal server error",
123 "Error reading object %s", oid_to_hex(oid));
127 argv_array_push(&rev_argv, "blame");
128 argv_array_push(&rev_argv, rev);
129 init_revisions(&revs, NULL);
130 revs.diffopt.flags.allow_textconv = 1;
131 setup_revisions(rev_argv.argc, rev_argv.argv, &revs, NULL);
132 init_scoreboard(&sb);
134 setup_scoreboard(&sb, path, &o);
135 o->suspects = blame_entry_prepend(NULL, 0, sb.num_lines, o);
136 prio_queue_put(&sb.commits, o->commit);
137 blame_origin_decref(o);
140 assign_blame(&sb, 0);
141 blame_sort_final(&sb);
144 cgit_set_title_from_path(path);
146 cgit_print_layout_start();
147 htmlf("blob: %s (", oid_to_hex(oid));
148 cgit_plain_link("plain", NULL, NULL, ctx.qry.head, rev, path);
150 cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path);
153 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
154 htmlf("<div class='error'>blob size (%ldKB)"
155 " exceeds display size limit (%dKB).</div>",
156 size / 1024, ctx.cfg.max_blob_size);
160 html("<table class='blame blob'>\n<tr>\n");
163 html("<td class='hashes'>");
164 for (ent = sb.ent; ent; ent = ent->next) {
165 html("<div class='alt'><pre>");
166 emit_blame_entry_hash(ent);
167 html("</pre></div>");
172 if (ctx.cfg.enable_tree_linenumbers) {
173 html("<td class='linenumbers'>");
174 for (ent = sb.ent; ent; ent = ent->next) {
175 html("<div class='alt'><pre>");
176 emit_blame_entry_linenumber(ent);
177 html("</pre></div>");
182 html("<td class='lines'><div>");
184 /* Colored bars behind lines */
186 for (ent = sb.ent; ent; ) {
187 struct blame_entry *e = ent->next;
188 html("<div class='alt'><pre>");
189 emit_blame_entry_line_background(&sb, ent);
190 html("</pre></div>");
196 free((void *)sb.final_buf);
200 if (ctx.repo->source_filter) {
201 char *filter_arg = xstrdup(basename);
202 cgit_open_filter(ctx.repo->source_filter, filter_arg);
204 cgit_close_filter(ctx.repo->source_filter);
209 html("</code></pre>");
211 html("</div></td>\n");
213 html("</tr>\n</table>\n");
215 cgit_print_layout_end();
221 static int walk_tree(const struct object_id *oid, struct strbuf *base,
222 const char *pathname, unsigned mode, int stage,
225 struct walk_tree_context *walk_tree_ctx = cbdata;
227 if (base->len == walk_tree_ctx->match_baselen) {
229 struct strbuf buffer = STRBUF_INIT;
230 strbuf_addbuf(&buffer, base);
231 strbuf_addstr(&buffer, pathname);
232 print_object(oid, buffer.buf, pathname,
233 walk_tree_ctx->curr_rev);
234 strbuf_release(&buffer);
235 walk_tree_ctx->state = 1;
236 } else if (S_ISDIR(mode)) {
237 walk_tree_ctx->state = 2;
239 } else if (base->len < INT_MAX
240 && (int)base->len > walk_tree_ctx->match_baselen) {
241 walk_tree_ctx->state = 2;
242 } else if (S_ISDIR(mode)) {
243 return READ_TREE_RECURSIVE;
248 static int basedir_len(const char *path)
250 char *p = strrchr(path, '/');
256 void cgit_print_blame(void)
258 const char *rev = ctx.qry.sha1;
259 struct object_id oid;
260 struct commit *commit;
261 struct pathspec_item path_items = {
262 .match = ctx.qry.path,
263 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
265 struct pathspec paths = {
269 struct walk_tree_context walk_tree_ctx = {
276 if (get_oid(rev, &oid)) {
277 cgit_print_error_page(404, "Not found",
278 "Invalid revision name: %s", rev);
281 commit = lookup_commit_reference(&oid);
282 if (!commit || parse_commit(commit)) {
283 cgit_print_error_page(404, "Not found",
284 "Invalid commit reference: %s", rev);
288 walk_tree_ctx.curr_rev = xstrdup(rev);
289 walk_tree_ctx.match_baselen = (path_items.match) ?
290 basedir_len(path_items.match) : -1;
292 read_tree_recursive(commit->maybe_tree, "", 0, 0, &paths, walk_tree,
294 if (!walk_tree_ctx.state)
295 cgit_print_error_page(404, "Not found", "Not found");
296 else if (walk_tree_ctx.state == 2)
297 cgit_print_error_page(404, "No blame for folders",
298 "Blame is not available for folders.");
300 free(walk_tree_ctx.curr_rev);