]> git.cameronkatri.com Git - cgit.git/blob - ui-blame.c
ui-blame: Make each column into a single table cell
[cgit.git] / ui-blame.c
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
44 static void emit_blame_entry_hash(struct blame_entry *ent)
45 {
46 struct blame_origin *suspect = ent->suspect;
47 struct object_id *oid = &suspect->commit->object.oid;
48 unsigned long line = 0;
49
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);
54 html("</span>");
55 free(detail);
56
57 while (line++ < ent->num_lines)
58 html("\n");
59 }
60
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
70 static void emit_blame_entry_line(struct blame_scoreboard *sb,
71 struct blame_entry *ent)
72 {
73 const char *cp, *cpend;
74
75 cp = blame_nth_line(sb, ent->lno);
76 cpend = blame_nth_line(sb, ent->lno + ent->num_lines);
77
78 html_ntxt(cp, cpend - cp);
79 }
80
81 struct walk_tree_context {
82 char *curr_rev;
83 int match_baselen;
84 int state;
85 };
86
87 static void print_object(const unsigned char *sha1, const char *path,
88 const char *basename, const char *rev)
89 {
90 enum object_type type;
91 unsigned long size;
92 struct argv_array rev_argv = ARGV_ARRAY_INIT;
93 struct rev_info revs;
94 struct blame_scoreboard sb;
95 struct blame_origin *o;
96 struct blame_entry *ent = NULL;
97
98 type = sha1_object_info(sha1, &size);
99 if (type == OBJ_BAD) {
100 cgit_print_error_page(404, "Not found", "Bad object name: %s",
101 sha1_to_hex(sha1));
102 return;
103 }
104
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);
111 sb.revs = &revs;
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);
116 sb.ent = NULL;
117 sb.path = path;
118 assign_blame(&sb, 0);
119 blame_sort_final(&sb);
120 blame_coalesce(&sb);
121
122 cgit_set_title_from_path(path);
123
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);
127 html(") (");
128 cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path);
129 html(")\n");
130
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);
135 return;
136 }
137
138 html("<table class='blame blob'>\n<tr>\n");
139
140 /* Commit hashes */
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>");
146 }
147 html("</td>\n");
148
149 /* Line numbers */
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>");
156 }
157 html("</td>\n");
158 }
159
160 /* Lines */
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>");
167 free(ent);
168 ent = e;
169 }
170 html("</td>\n");
171
172 free((void *)sb.final_buf);
173
174 html("</tr>\n</table>\n");
175
176 cgit_print_layout_end();
177 }
178
179 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
180 const char *pathname, unsigned mode, int stage,
181 void *cbdata)
182 {
183 struct walk_tree_context *walk_tree_ctx = cbdata;
184
185 if (base->len == walk_tree_ctx->match_baselen) {
186 if (S_ISREG(mode)) {
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;
196 }
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;
202 }
203 return 0;
204 }
205
206 static int basedir_len(const char *path)
207 {
208 char *p = strrchr(path, '/');
209 if (p)
210 return p - path + 1;
211 return 0;
212 }
213
214 void cgit_print_blame(void)
215 {
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
222 };
223 struct pathspec paths = {
224 .nr = 1,
225 .items = &path_items
226 };
227 struct walk_tree_context walk_tree_ctx = {
228 .state = 0
229 };
230
231 if (!rev)
232 rev = ctx.qry.head;
233
234 if (get_oid(rev, &oid)) {
235 cgit_print_error_page(404, "Not found",
236 "Invalid revision name: %s", rev);
237 return;
238 }
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);
243 return;
244 }
245
246 walk_tree_ctx.curr_rev = xstrdup(rev);
247 walk_tree_ctx.match_baselen = (path_items.match) ?
248 basedir_len(path_items.match) : -1;
249
250 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree,
251 &walk_tree_ctx);
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.");
257
258 free(walk_tree_ctx.curr_rev);
259 }