ui-blame: Distinguish hashes column from lines column
[cgit.git] / ui-blame.c
blob62647a8e2e4918bb107001576df240ab7af12774
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)
7 */
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"
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(struct blame_scoreboard *sb,
45 struct blame_entry *ent)
47 struct blame_origin *suspect = ent->suspect;
48 struct object_id *oid = &suspect->commit->object.oid;
49 const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
50 const char *cp, *cpend;
52 char *detail = emit_suspect_detail(suspect);
54 html("<tr><td class='sha1 hashes'>");
55 cgit_commit_link(find_unique_abbrev(oid->hash, DEFAULT_ABBREV), detail,
56 NULL, ctx.qry.head, oid_to_hex(oid), suspect->path);
57 html("</td>\n");
59 free(detail);
61 if (ctx.cfg.enable_tree_linenumbers) {
62 unsigned long lineno = ent->lno;
63 html("<td class='linenumbers'><pre>");
64 while (lineno < ent->lno + ent->num_lines)
65 htmlf(numberfmt, ++lineno);
66 html("</pre></td>\n");
69 cp = blame_nth_line(sb, ent->lno);
70 cpend = blame_nth_line(sb, ent->lno + ent->num_lines);
72 html("<td class='lines'><pre><code>");
73 html_ntxt(cp, cpend - cp);
74 html("</code></pre></td></tr>\n");
77 struct walk_tree_context {
78 char *curr_rev;
79 int match_baselen;
80 int state;
83 static void print_object(const unsigned char *sha1, const char *path,
84 const char *basename, const char *rev)
86 enum object_type type;
87 unsigned long size;
88 struct argv_array rev_argv = ARGV_ARRAY_INIT;
89 struct rev_info revs;
90 struct blame_scoreboard sb;
91 struct blame_origin *o;
92 struct blame_entry *ent = NULL;
94 type = sha1_object_info(sha1, &size);
95 if (type == OBJ_BAD) {
96 cgit_print_error_page(404, "Not found", "Bad object name: %s",
97 sha1_to_hex(sha1));
98 return;
101 argv_array_push(&rev_argv, "blame");
102 argv_array_push(&rev_argv, rev);
103 init_revisions(&revs, NULL);
104 revs.diffopt.flags.allow_textconv = 1;
105 setup_revisions(rev_argv.argc, rev_argv.argv, &revs, NULL);
106 init_scoreboard(&sb);
107 sb.revs = &revs;
108 setup_scoreboard(&sb, path, &o);
109 o->suspects = blame_entry_prepend(NULL, 0, sb.num_lines, o);
110 prio_queue_put(&sb.commits, o->commit);
111 blame_origin_decref(o);
112 sb.ent = NULL;
113 sb.path = path;
114 assign_blame(&sb, 0);
115 blame_sort_final(&sb);
116 blame_coalesce(&sb);
118 cgit_set_title_from_path(path);
120 cgit_print_layout_start();
121 htmlf("blob: %s (", sha1_to_hex(sha1));
122 cgit_plain_link("plain", NULL, NULL, ctx.qry.head, rev, path);
123 html(") (");
124 cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path);
125 html(")\n");
127 if (ctx.cfg.max_blob_size && size / 1024 > ctx.cfg.max_blob_size) {
128 htmlf("<div class='error'>blob size (%ldKB)"
129 " exceeds display size limit (%dKB).</div>",
130 size / 1024, ctx.cfg.max_blob_size);
131 return;
134 html("<table class='blame blob'>");
135 for (ent = sb.ent; ent; ) {
136 struct blame_entry *e = ent->next;
137 emit_blame_entry(&sb, ent);
138 free(ent);
139 ent = e;
141 html("</table>\n");
142 free((void *)sb.final_buf);
144 cgit_print_layout_end();
147 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
148 const char *pathname, unsigned mode, int stage,
149 void *cbdata)
151 struct walk_tree_context *walk_tree_ctx = cbdata;
153 if (base->len == walk_tree_ctx->match_baselen) {
154 if (S_ISREG(mode)) {
155 struct strbuf buffer = STRBUF_INIT;
156 strbuf_addbuf(&buffer, base);
157 strbuf_addstr(&buffer, pathname);
158 print_object(sha1, buffer.buf, pathname,
159 walk_tree_ctx->curr_rev);
160 strbuf_release(&buffer);
161 walk_tree_ctx->state = 1;
162 } else if (S_ISDIR(mode)) {
163 walk_tree_ctx->state = 2;
165 } else if (base->len < INT_MAX
166 && (int)base->len > walk_tree_ctx->match_baselen) {
167 walk_tree_ctx->state = 2;
168 } else if (S_ISDIR(mode)) {
169 return READ_TREE_RECURSIVE;
171 return 0;
174 static int basedir_len(const char *path)
176 char *p = strrchr(path, '/');
177 if (p)
178 return p - path + 1;
179 return 0;
182 void cgit_print_blame(void)
184 const char *rev = ctx.qry.sha1;
185 struct object_id oid;
186 struct commit *commit;
187 struct pathspec_item path_items = {
188 .match = ctx.qry.path,
189 .len = ctx.qry.path ? strlen(ctx.qry.path) : 0
191 struct pathspec paths = {
192 .nr = 1,
193 .items = &path_items
195 struct walk_tree_context walk_tree_ctx = {
196 .state = 0
199 if (!rev)
200 rev = ctx.qry.head;
202 if (get_oid(rev, &oid)) {
203 cgit_print_error_page(404, "Not found",
204 "Invalid revision name: %s", rev);
205 return;
207 commit = lookup_commit_reference(&oid);
208 if (!commit || parse_commit(commit)) {
209 cgit_print_error_page(404, "Not found",
210 "Invalid commit reference: %s", rev);
211 return;
214 walk_tree_ctx.curr_rev = xstrdup(rev);
215 walk_tree_ctx.match_baselen = (path_items.match) ?
216 basedir_len(path_items.match) : -1;
218 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree,
219 &walk_tree_ctx);
220 if (!walk_tree_ctx.state)
221 cgit_print_error_page(404, "Not found", "Not found");
222 else if (walk_tree_ctx.state == 2)
223 cgit_print_error_page(404, "No blame for folders",
224 "Blame is not available for folders.");
226 free(walk_tree_ctx.curr_rev);