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;
49 char *detail = emit_suspect_detail(suspect);
50 cgit_commit_link(find_unique_abbrev(oid->hash, DEFAULT_ABBREV), detail,
51 NULL, ctx.qry.head, oid_to_hex(oid), suspect->path);
55 static void emit_blame_entry_linenumber(struct blame_entry *ent)
57 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
59 unsigned long lineno = ent->lno;
60 while (lineno < ent->lno + ent->num_lines)
61 htmlf(numberfmt, ++lineno);
64 static void emit_blame_entry_line(struct blame_scoreboard *sb,
65 struct blame_entry *ent)
67 const char *cp, *cpend;
69 cp = blame_nth_line(sb, ent->lno);
70 cpend = blame_nth_line(sb, ent->lno + ent->num_lines);
72 html_ntxt(cp, cpend - cp);
75 static void emit_blame_entry(struct blame_scoreboard *sb,
76 struct blame_entry *ent)
78 html("<tr><td class='sha1 hashes'>");
79 emit_blame_entry_hash(ent);
82 if (ctx.cfg.enable_tree_linenumbers) {
83 html("<td class='linenumbers'><pre>");
84 emit_blame_entry_linenumber(ent);
85 html("</pre></td>\n");
88 html("<td class='lines'><pre><code>");
89 emit_blame_entry_line(sb, ent);
90 html("</code></pre></td></tr>\n");
93 struct walk_tree_context {
99 static void print_object(const unsigned char *sha1, const char *path,
100 const char *basename, const char *rev)
102 enum object_type type;
104 struct argv_array rev_argv = ARGV_ARRAY_INIT;
105 struct rev_info revs;
106 struct blame_scoreboard sb;
107 struct blame_origin *o;
108 struct blame_entry *ent = NULL;
110 type = sha1_object_info(sha1, &size);
111 if (type == OBJ_BAD) {
112 cgit_print_error_page(404, "Not found", "Bad object name: %s",
117 argv_array_push(&rev_argv, "blame");
118 argv_array_push(&rev_argv, rev);
119 init_revisions(&revs, NULL);
120 revs.diffopt.flags.allow_textconv = 1;
121 setup_revisions(rev_argv.argc, rev_argv.argv, &revs, NULL);
122 init_scoreboard(&sb);
124 setup_scoreboard(&sb, path, &o);
125 o->suspects = blame_entry_prepend(NULL, 0, sb.num_lines, o);
126 prio_queue_put(&sb.commits, o->commit);
127 blame_origin_decref(o);
130 assign_blame(&sb, 0);
131 blame_sort_final(&sb);
134 cgit_set_title_from_path(path);
136 cgit_print_layout_start();
137 htmlf("blob: %s (", sha1_to_hex(sha1));
138 cgit_plain_link("plain", NULL, NULL, ctx.qry.head, rev, path);
140 cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path);
143 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
144 htmlf("<div class='error'>blob size (%ldKB)"
145 " exceeds display size limit (%dKB).</div>",
146 size / 1024, ctx.cfg.max_blob_size);
150 html("<table class='blame blob'>");
151 for (ent = sb.ent; ent; ) {
152 struct blame_entry *e = ent->next;
153 emit_blame_entry(&sb, ent);
158 free((void *)sb.final_buf);
160 cgit_print_layout_end();
163 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
164 const char *pathname, unsigned mode, int stage,
167 struct walk_tree_context *walk_tree_ctx = cbdata;
169 if (base->len == walk_tree_ctx->match_baselen) {
171 struct strbuf buffer = STRBUF_INIT;
172 strbuf_addbuf(&buffer, base);
173 strbuf_addstr(&buffer, pathname);
174 print_object(sha1, buffer.buf, pathname,
175 walk_tree_ctx->curr_rev);
176 strbuf_release(&buffer);
177 walk_tree_ctx->state = 1;
178 } else if (S_ISDIR(mode)) {
179 walk_tree_ctx->state = 2;
181 } else if (base->len < INT_MAX
182 && (int)base->len > walk_tree_ctx->match_baselen) {
183 walk_tree_ctx->state = 2;
184 } else if (S_ISDIR(mode)) {
185 return READ_TREE_RECURSIVE;
190 static int basedir_len(const char *path)
192 char *p = strrchr(path, '/');
198 void cgit_print_blame(void)
200 const char *rev = ctx.qry.sha1;
201 struct object_id oid;
202 struct commit *commit;
203 struct pathspec_item path_items = {
204 .match = ctx.qry.path,
205 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
207 struct pathspec paths = {
211 struct walk_tree_context walk_tree_ctx = {
218 if (get_oid(rev, &oid)) {
219 cgit_print_error_page(404, "Not found",
220 "Invalid revision name: %s", rev);
223 commit = lookup_commit_reference(&oid);
224 if (!commit || parse_commit(commit)) {
225 cgit_print_error_page(404, "Not found",
226 "Invalid commit reference: %s", rev);
230 walk_tree_ctx.curr_rev = xstrdup(rev);
231 walk_tree_ctx.match_baselen = (path_items.match) ?
232 basedir_len(path_items.match) : -1;
234 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree,
236 if (!walk_tree_ctx.state)
237 cgit_print_error_page(404, "Not found", "Not found");
238 else if (walk_tree_ctx.state == 2)
239 cgit_print_error_page(404, "No blame for folders",
240 "Blame is not available for folders.");
242 free(walk_tree_ctx.curr_rev);