Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 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 "argv-array.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 | |
Jeff Smith | 2b95c9d | 2017-10-17 23:17:33 -0500 | [diff] [blame] | 44 | static void emit_blame_entry_hash(struct blame_entry *ent) |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 45 | { |
| 46 | struct blame_origin *suspect = ent->suspect; |
| 47 | struct object_id *oid = &suspect->commit->object.oid; |
Jeff Smith | aafc42d | 2017-10-17 23:17:34 -0500 | [diff] [blame] | 48 | unsigned long line = 0; |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 49 | |
| 50 | char *detail = emit_suspect_detail(suspect); |
Jeff Smith | aafc42d | 2017-10-17 23:17:34 -0500 | [diff] [blame] | 51 | html("<span class='sha1'>"); |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 52 | cgit_commit_link(find_unique_abbrev(oid->hash, DEFAULT_ABBREV), detail, |
| 53 | NULL, ctx.qry.head, oid_to_hex(oid), suspect->path); |
Jeff Smith | aafc42d | 2017-10-17 23:17:34 -0500 | [diff] [blame] | 54 | html("</span>"); |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 55 | free(detail); |
Jeff Smith | aafc42d | 2017-10-17 23:17:34 -0500 | [diff] [blame] | 56 | |
| 57 | while (line++ < ent->num_lines) |
| 58 | html("\n"); |
Jeff Smith | 2b95c9d | 2017-10-17 23:17:33 -0500 | [diff] [blame] | 59 | } |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 60 | |
Jeff Smith | 2b95c9d | 2017-10-17 23:17:33 -0500 | [diff] [blame] | 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 | |
Jeff Smith | dbaee26 | 2017-10-28 21:43:26 -0500 | [diff] [blame^] | 70 | static void emit_blame_entry_line_background(struct blame_scoreboard *sb, |
| 71 | struct blame_entry *ent) |
Jeff Smith | 2b95c9d | 2017-10-17 23:17:33 -0500 | [diff] [blame] | 72 | { |
Jeff Smith | dbaee26 | 2017-10-28 21:43:26 -0500 | [diff] [blame^] | 73 | unsigned long line; |
| 74 | size_t len, maxlen = 2; |
| 75 | const char* pos, *endpos; |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 76 | |
Jeff Smith | dbaee26 | 2017-10-28 21:43:26 -0500 | [diff] [blame^] | 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 | } |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 90 | |
Jeff Smith | dbaee26 | 2017-10-28 21:43:26 -0500 | [diff] [blame^] | 91 | for (len = 0; len < maxlen - 1; len++) |
| 92 | html(" "); |
Jeff Smith | 2b95c9d | 2017-10-17 23:17:33 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 95 | struct walk_tree_context { |
| 96 | char *curr_rev; |
| 97 | int match_baselen; |
| 98 | int state; |
| 99 | }; |
| 100 | |
| 101 | static void print_object(const unsigned char *sha1, const char *path, |
| 102 | const char *basename, const char *rev) |
| 103 | { |
| 104 | enum object_type type; |
Jeff Smith | dbaee26 | 2017-10-28 21:43:26 -0500 | [diff] [blame^] | 105 | char *buf; |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 106 | unsigned long size; |
| 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; |
| 112 | |
| 113 | type = sha1_object_info(sha1, &size); |
| 114 | if (type == OBJ_BAD) { |
| 115 | cgit_print_error_page(404, "Not found", "Bad object name: %s", |
| 116 | sha1_to_hex(sha1)); |
| 117 | return; |
| 118 | } |
| 119 | |
Jeff Smith | dbaee26 | 2017-10-28 21:43:26 -0500 | [diff] [blame^] | 120 | buf = read_sha1_file(sha1, &type, &size); |
| 121 | if (!buf) { |
| 122 | cgit_print_error_page(500, "Internal server error", |
| 123 | "Error reading object %s", sha1_to_hex(sha1)); |
| 124 | return; |
| 125 | } |
| 126 | |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 127 | argv_array_push(&rev_argv, "blame"); |
| 128 | argv_array_push(&rev_argv, rev); |
| 129 | init_revisions(&revs, NULL); |
Christian Hesse | 1dd53e3 | 2018-01-18 09:19:31 +0100 | [diff] [blame] | 130 | revs.diffopt.flags.allow_textconv = 1; |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 131 | setup_revisions(rev_argv.argc, rev_argv.argv, &revs, NULL); |
| 132 | init_scoreboard(&sb); |
| 133 | sb.revs = &revs; |
| 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); |
| 138 | sb.ent = NULL; |
| 139 | sb.path = path; |
| 140 | assign_blame(&sb, 0); |
| 141 | blame_sort_final(&sb); |
| 142 | blame_coalesce(&sb); |
| 143 | |
| 144 | cgit_set_title_from_path(path); |
| 145 | |
| 146 | cgit_print_layout_start(); |
| 147 | htmlf("blob: %s (", sha1_to_hex(sha1)); |
| 148 | cgit_plain_link("plain", NULL, NULL, ctx.qry.head, rev, path); |
| 149 | html(") ("); |
| 150 | cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path); |
| 151 | html(")\n"); |
| 152 | |
| 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); |
| 157 | return; |
| 158 | } |
| 159 | |
Jeff Smith | aafc42d | 2017-10-17 23:17:34 -0500 | [diff] [blame] | 160 | html("<table class='blame blob'>\n<tr>\n"); |
| 161 | |
| 162 | /* Commit hashes */ |
| 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>"); |
| 168 | } |
| 169 | html("</td>\n"); |
| 170 | |
| 171 | /* Line numbers */ |
| 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>"); |
| 178 | } |
| 179 | html("</td>\n"); |
| 180 | } |
| 181 | |
Jeff Smith | dbaee26 | 2017-10-28 21:43:26 -0500 | [diff] [blame^] | 182 | html("<td class='lines'><div>"); |
| 183 | |
| 184 | /* Colored bars behind lines */ |
| 185 | html("<div>"); |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 186 | for (ent = sb.ent; ent; ) { |
| 187 | struct blame_entry *e = ent->next; |
Jeff Smith | dbaee26 | 2017-10-28 21:43:26 -0500 | [diff] [blame^] | 188 | html("<div class='alt'><pre>"); |
| 189 | emit_blame_entry_line_background(&sb, ent); |
| 190 | html("</pre></div>"); |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 191 | free(ent); |
| 192 | ent = e; |
| 193 | } |
Jeff Smith | dbaee26 | 2017-10-28 21:43:26 -0500 | [diff] [blame^] | 194 | html("</div>"); |
Jeff Smith | aafc42d | 2017-10-17 23:17:34 -0500 | [diff] [blame] | 195 | |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 196 | free((void *)sb.final_buf); |
| 197 | |
Jeff Smith | dbaee26 | 2017-10-28 21:43:26 -0500 | [diff] [blame^] | 198 | /* Lines */ |
| 199 | html("<pre><code>"); |
| 200 | if (ctx.repo->source_filter) { |
| 201 | char *filter_arg = xstrdup(basename); |
| 202 | cgit_open_filter(ctx.repo->source_filter, filter_arg); |
| 203 | html_raw(buf, size); |
| 204 | cgit_close_filter(ctx.repo->source_filter); |
| 205 | free(filter_arg); |
| 206 | } else { |
| 207 | html_txt(buf); |
| 208 | } |
| 209 | html("</code></pre>"); |
| 210 | |
| 211 | html("</div></td>\n"); |
| 212 | |
Jeff Smith | aafc42d | 2017-10-17 23:17:34 -0500 | [diff] [blame] | 213 | html("</tr>\n</table>\n"); |
| 214 | |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 215 | cgit_print_layout_end(); |
| 216 | } |
| 217 | |
| 218 | static int walk_tree(const unsigned char *sha1, struct strbuf *base, |
| 219 | const char *pathname, unsigned mode, int stage, |
| 220 | void *cbdata) |
| 221 | { |
| 222 | struct walk_tree_context *walk_tree_ctx = cbdata; |
| 223 | |
| 224 | if (base->len == walk_tree_ctx->match_baselen) { |
| 225 | if (S_ISREG(mode)) { |
| 226 | struct strbuf buffer = STRBUF_INIT; |
| 227 | strbuf_addbuf(&buffer, base); |
| 228 | strbuf_addstr(&buffer, pathname); |
| 229 | print_object(sha1, buffer.buf, pathname, |
| 230 | walk_tree_ctx->curr_rev); |
| 231 | strbuf_release(&buffer); |
| 232 | walk_tree_ctx->state = 1; |
| 233 | } else if (S_ISDIR(mode)) { |
| 234 | walk_tree_ctx->state = 2; |
| 235 | } |
| 236 | } else if (base->len < INT_MAX |
| 237 | && (int)base->len > walk_tree_ctx->match_baselen) { |
| 238 | walk_tree_ctx->state = 2; |
| 239 | } else if (S_ISDIR(mode)) { |
| 240 | return READ_TREE_RECURSIVE; |
| 241 | } |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int basedir_len(const char *path) |
| 246 | { |
| 247 | char *p = strrchr(path, '/'); |
| 248 | if (p) |
| 249 | return p - path + 1; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | void cgit_print_blame(void) |
| 254 | { |
| 255 | const char *rev = ctx.qry.sha1; |
| 256 | struct object_id oid; |
| 257 | struct commit *commit; |
| 258 | struct pathspec_item path_items = { |
| 259 | .match = ctx.qry.path, |
| 260 | .len = ctx.qry.path ? strlen(ctx.qry.path) : 0 |
| 261 | }; |
| 262 | struct pathspec paths = { |
| 263 | .nr = 1, |
| 264 | .items = &path_items |
| 265 | }; |
| 266 | struct walk_tree_context walk_tree_ctx = { |
| 267 | .state = 0 |
| 268 | }; |
| 269 | |
| 270 | if (!rev) |
| 271 | rev = ctx.qry.head; |
| 272 | |
| 273 | if (get_oid(rev, &oid)) { |
| 274 | cgit_print_error_page(404, "Not found", |
| 275 | "Invalid revision name: %s", rev); |
| 276 | return; |
| 277 | } |
| 278 | commit = lookup_commit_reference(&oid); |
| 279 | if (!commit || parse_commit(commit)) { |
| 280 | cgit_print_error_page(404, "Not found", |
| 281 | "Invalid commit reference: %s", rev); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | walk_tree_ctx.curr_rev = xstrdup(rev); |
| 286 | walk_tree_ctx.match_baselen = (path_items.match) ? |
| 287 | basedir_len(path_items.match) : -1; |
| 288 | |
| 289 | read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, |
| 290 | &walk_tree_ctx); |
| 291 | if (!walk_tree_ctx.state) |
| 292 | cgit_print_error_page(404, "Not found", "Not found"); |
| 293 | else if (walk_tree_ctx.state == 2) |
| 294 | cgit_print_error_page(404, "No blame for folders", |
| 295 | "Blame is not available for folders."); |
| 296 | |
| 297 | free(walk_tree_ctx.curr_rev); |
| 298 | } |