all repos — cgit @ 2b95c9d49c8581e2b19efca1613ada292f56bf08

a hyperfast web frontend for git written in c

ui-blame.c (view raw)

  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
 17static 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
 44static 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
 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);
 52	free(detail);
 53}
 54
 55static void emit_blame_entry_linenumber(struct blame_entry *ent)
 56{
 57	const char *numberfmt = "<a id='n%1$d' href='#n%1$d'>%1$d</a>\n";
 58
 59	unsigned long lineno = ent->lno;
 60	while (lineno < ent->lno + ent->num_lines)
 61		htmlf(numberfmt, ++lineno);
 62}
 63
 64static void emit_blame_entry_line(struct blame_scoreboard *sb,
 65				  struct blame_entry *ent)
 66{
 67	const char *cp, *cpend;
 68
 69	cp = blame_nth_line(sb, ent->lno);
 70	cpend = blame_nth_line(sb, ent->lno + ent->num_lines);
 71
 72	html_ntxt(cp, cpend - cp);
 73}
 74
 75static void emit_blame_entry(struct blame_scoreboard *sb,
 76			     struct blame_entry *ent)
 77{
 78	html("<tr><td class='sha1 hashes'>");
 79	emit_blame_entry_hash(ent);
 80	html("</td>\n");
 81
 82	if (ctx.cfg.enable_tree_linenumbers) {
 83		html("<td class='linenumbers'><pre>");
 84		emit_blame_entry_linenumber(ent);
 85		html("</pre></td>\n");
 86	}
 87
 88	html("<td class='lines'><pre><code>");
 89	emit_blame_entry_line(sb, ent);
 90	html("</code></pre></td></tr>\n");
 91}
 92
 93struct walk_tree_context {
 94	char *curr_rev;
 95	int match_baselen;
 96	int state;
 97};
 98
 99static void print_object(const unsigned char *sha1, const char *path,
100			 const char *basename, const char *rev)
101{
102	enum object_type type;
103	unsigned long size;
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;
109
110	type = sha1_object_info(sha1, &size);
111	if (type == OBJ_BAD) {
112		cgit_print_error_page(404, "Not found", "Bad object name: %s",
113				      sha1_to_hex(sha1));
114		return;
115	}
116
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);
123	sb.revs = &revs;
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);
128	sb.ent = NULL;
129	sb.path = path;
130	assign_blame(&sb, 0);
131	blame_sort_final(&sb);
132	blame_coalesce(&sb);
133
134	cgit_set_title_from_path(path);
135
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);
139	html(") (");
140	cgit_tree_link("tree", NULL, NULL, ctx.qry.head, rev, path);
141	html(")\n");
142
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);
147		return;
148	}
149
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);
154		free(ent);
155		ent = e;
156	}
157	html("</table>\n");
158	free((void *)sb.final_buf);
159
160	cgit_print_layout_end();
161}
162
163static int walk_tree(const unsigned char *sha1, struct strbuf *base,
164		     const char *pathname, unsigned mode, int stage,
165		     void *cbdata)
166{
167	struct walk_tree_context *walk_tree_ctx = cbdata;
168
169	if (base->len == walk_tree_ctx->match_baselen) {
170		if (S_ISREG(mode)) {
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;
180		}
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;
186	}
187	return 0;
188}
189
190static int basedir_len(const char *path)
191{
192	char *p = strrchr(path, '/');
193	if (p)
194		return p - path + 1;
195	return 0;
196}
197
198void cgit_print_blame(void)
199{
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
206	};
207	struct pathspec paths = {
208		.nr = 1,
209		.items = &path_items
210	};
211	struct walk_tree_context walk_tree_ctx = {
212		.state = 0
213	};
214
215	if (!rev)
216		rev = ctx.qry.head;
217
218	if (get_oid(rev, &oid)) {
219		cgit_print_error_page(404, "Not found",
220			"Invalid revision name: %s", rev);
221		return;
222	}
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);
227		return;
228	}
229
230	walk_tree_ctx.curr_rev = xstrdup(rev);
231	walk_tree_ctx.match_baselen = (path_items.match) ?
232				       basedir_len(path_items.match) : -1;
233
234	read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree,
235		&walk_tree_ctx);
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.");
241
242	free(walk_tree_ctx.curr_rev);
243}