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->hash, 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(struct blame_scoreboard *sb,
71 struct blame_entry *ent)
73 const char *cp, *cpend;
75 cp = blame_nth_line(sb, ent->lno);
76 cpend = blame_nth_line(sb, ent->lno + ent->num_lines);
78 html_ntxt(cp, cpend - cp);
81 struct walk_tree_context {
87 static void print_object(const unsigned char *sha1, const char *path,
88 const char *basename, const char *rev)
90 enum object_type type;
92 struct argv_array rev_argv = ARGV_ARRAY_INIT;
94 struct blame_scoreboard sb;
95 struct blame_origin *o;
96 struct blame_entry *ent = NULL;
98 type = sha1_object_info(sha1, &size);
99 if (type == OBJ_BAD) {
100 cgit_print_error_page(404, "Not found", "Bad object name: %s",
105 argv_array_push(&rev_argv, "blame");
106 argv_array_push(&rev_argv, rev);
107 init_revisions(&revs, NULL);
108 revs.diffopt.flags.allow_textconv = 1;
109 setup_revisions(rev_argv.argc, rev_argv.argv, &revs, NULL);
110 init_scoreboard(&sb);
112 setup_scoreboard(&sb, path, &o);
113 o->suspects = blame_entry_prepend(NULL, 0, sb.num_lines, o);
114 prio_queue_put(&sb.commits, o->commit);
115 blame_origin_decref(o);
118 assign_blame(&sb, 0);
119 blame_sort_final(&sb);
122 cgit_set_title_from_path(path);
124 cgit_print_layout_start();
125 htmlf("blob: %s (", sha1_to_hex(sha1));
126 cgit_plain_link("plain", NULL, NULL, ctx.qry.head, rev, path);
128 cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path);
131 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
132 htmlf("<div class='error'>blob size (%ldKB)"
133 " exceeds display size limit (%dKB).</div>",
134 size / 1024, ctx.cfg.max_blob_size);
138 html("<table class='blame blob'>\n<tr>\n");
141 html("<td class='hashes'>");
142 for (ent = sb.ent; ent; ent = ent->next) {
143 html("<div class='alt'><pre>");
144 emit_blame_entry_hash(ent);
145 html("</pre></div>");
150 if (ctx.cfg.enable_tree_linenumbers) {
151 html("<td class='linenumbers'>");
152 for (ent = sb.ent; ent; ent = ent->next) {
153 html("<div class='alt'><pre>");
154 emit_blame_entry_linenumber(ent);
155 html("</pre></div>");
161 html("<td class='lines'>");
162 for (ent = sb.ent; ent; ) {
163 struct blame_entry *e = ent->next;
164 html("<div class='alt'><pre><code>");
165 emit_blame_entry_line(&sb, ent);
166 html("</code></pre></div>");
172 free((void *)sb.final_buf);
174 html("</tr>\n</table>\n");
176 cgit_print_layout_end();
179 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
180 const char *pathname, unsigned mode, int stage,
183 struct walk_tree_context *walk_tree_ctx = cbdata;
185 if (base->len == walk_tree_ctx->match_baselen) {
187 struct strbuf buffer = STRBUF_INIT;
188 strbuf_addbuf(&buffer, base);
189 strbuf_addstr(&buffer, pathname);
190 print_object(sha1, buffer.buf, pathname,
191 walk_tree_ctx->curr_rev);
192 strbuf_release(&buffer);
193 walk_tree_ctx->state = 1;
194 } else if (S_ISDIR(mode)) {
195 walk_tree_ctx->state = 2;
197 } else if (base->len < INT_MAX
198 && (int)base->len > walk_tree_ctx->match_baselen) {
199 walk_tree_ctx->state = 2;
200 } else if (S_ISDIR(mode)) {
201 return READ_TREE_RECURSIVE;
206 static int basedir_len(const char *path)
208 char *p = strrchr(path, '/');
214 void cgit_print_blame(void)
216 const char *rev = ctx.qry.sha1;
217 struct object_id oid;
218 struct commit *commit;
219 struct pathspec_item path_items = {
220 .match = ctx.qry.path,
221 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
223 struct pathspec paths = {
227 struct walk_tree_context walk_tree_ctx = {
234 if (get_oid(rev, &oid)) {
235 cgit_print_error_page(404, "Not found",
236 "Invalid revision name: %s", rev);
239 commit = lookup_commit_reference(&oid);
240 if (!commit || parse_commit(commit)) {
241 cgit_print_error_page(404, "Not found",
242 "Invalid commit reference: %s", rev);
246 walk_tree_ctx.curr_rev = xstrdup(rev);
247 walk_tree_ctx.match_baselen = (path_items.match) ?
248 basedir_len(path_items.match) : -1;
250 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree,
252 if (!walk_tree_ctx.state)
253 cgit_print_error_page(404, "Not found", "Not found");
254 else if (walk_tree_ctx.state == 2)
255 cgit_print_error_page(404, "No blame for folders",
256 "Blame is not available for folders.");
258 free(walk_tree_ctx.curr_rev);