all repos — cgit @ 9ca2566972db968df4479108b29bb92551138b57

a hyperfast web frontend for git written in c

ui-log.c (view raw)

  1/* ui-log.c: functions for log output
  2 *
  3 * Copyright (C) 2006-2014 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-log.h"
 11#include "html.h"
 12#include "ui-shared.h"
 13#include "argv-array.h"
 14
 15static int files, add_lines, rem_lines, lines_counted;
 16
 17/*
 18 * The list of available column colors in the commit graph.
 19 */
 20static const char *column_colors_html[] = {
 21	"<span class='column1'>",
 22	"<span class='column2'>",
 23	"<span class='column3'>",
 24	"<span class='column4'>",
 25	"<span class='column5'>",
 26	"<span class='column6'>",
 27	"</span>",
 28};
 29
 30#define COLUMN_COLORS_HTML_MAX (ARRAY_SIZE(column_colors_html) - 1)
 31
 32static void count_lines(char *line, int size)
 33{
 34	if (size <= 0)
 35		return;
 36
 37	if (line[0] == '+')
 38		add_lines++;
 39
 40	else if (line[0] == '-')
 41		rem_lines++;
 42}
 43
 44static void inspect_files(struct diff_filepair *pair)
 45{
 46	unsigned long old_size = 0;
 47	unsigned long new_size = 0;
 48	int binary = 0;
 49
 50	files++;
 51	if (ctx.repo->enable_log_linecount)
 52		cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
 53				&new_size, &binary, 0, ctx.qry.ignorews,
 54				count_lines);
 55}
 56
 57void show_commit_decorations(struct commit *commit)
 58{
 59	const struct name_decoration *deco;
 60	static char buf[1024];
 61
 62	buf[sizeof(buf) - 1] = 0;
 63	deco = get_name_decoration(&commit->object);
 64	html("<span class='decoration'>");
 65	while (deco) {
 66		if (starts_with(deco->name, "refs/heads/")) {
 67			strncpy(buf, deco->name + 11, sizeof(buf) - 1);
 68			cgit_log_link(buf, NULL, "branch-deco", buf, NULL,
 69				      ctx.qry.vpath, 0, NULL, NULL,
 70				      ctx.qry.showmsg, 0);
 71		}
 72		else if (starts_with(deco->name, "tag: refs/tags/")) {
 73			strncpy(buf, deco->name + 15, sizeof(buf) - 1);
 74			cgit_tag_link(buf, NULL, "tag-deco", buf);
 75		}
 76		else if (starts_with(deco->name, "refs/tags/")) {
 77			strncpy(buf, deco->name + 10, sizeof(buf) - 1);
 78			cgit_tag_link(buf, NULL, "tag-deco", buf);
 79		}
 80		else if (starts_with(deco->name, "refs/remotes/")) {
 81			if (!ctx.repo->enable_remote_branches)
 82				goto next;
 83			strncpy(buf, deco->name + 13, sizeof(buf) - 1);
 84			cgit_log_link(buf, NULL, "remote-deco", NULL,
 85				      oid_to_hex(&commit->object.oid),
 86				      ctx.qry.vpath, 0, NULL, NULL,
 87				      ctx.qry.showmsg, 0);
 88		}
 89		else {
 90			strncpy(buf, deco->name, sizeof(buf) - 1);
 91			cgit_commit_link(buf, NULL, "deco", ctx.qry.head,
 92					 oid_to_hex(&commit->object.oid),
 93					 ctx.qry.vpath);
 94		}
 95next:
 96		deco = deco->next;
 97	}
 98	html("</span>");
 99}
100
101static void handle_rename(struct diff_filepair *pair)
102{
103	/*
104	 * After we have seen a rename, we generate links to the previous
105	 * name of the file so that commit & diff views get fed the path
106	 * that is correct for the commit they are showing, avoiding the
107	 * need to walk the entire history leading back to every commit we
108	 * show in order detect renames.
109	 */
110	if (0 != strcmp(ctx.qry.vpath, pair->two->path)) {
111		free(ctx.qry.vpath);
112		ctx.qry.vpath = xstrdup(pair->two->path);
113	}
114	inspect_files(pair);
115}
116
117static int show_commit(struct commit *commit, struct rev_info *revs)
118{
119	struct commit_list *parents = commit->parents;
120	struct commit *parent;
121	int found = 0, saved_fmt;
122	unsigned saved_flags = revs->diffopt.flags;
123
124
125	/* Always show if we're not in "follow" mode with a single file. */
126	if (!ctx.qry.follow)
127		return 1;
128
129	/*
130	 * In "follow" mode, we don't show merges.  This is consistent with
131	 * "git log --follow -- <file>".
132	 */
133	if (parents && parents->next)
134		return 0;
135
136	/*
137	 * If this is the root commit, do what rev_info tells us.
138	 */
139	if (!parents)
140		return revs->show_root_diff;
141
142	/* When we get here we have precisely one parent. */
143	parent = parents->item;
144	parse_commit(parent);
145
146	files = 0;
147	add_lines = 0;
148	rem_lines = 0;
149
150	DIFF_OPT_SET(&revs->diffopt, RECURSIVE);
151	diff_tree_sha1(parent->tree->object.oid.hash,
152		       commit->tree->object.oid.hash,
153		       "", &revs->diffopt);
154	diffcore_std(&revs->diffopt);
155
156	found = !diff_queue_is_empty();
157	saved_fmt = revs->diffopt.output_format;
158	revs->diffopt.output_format = DIFF_FORMAT_CALLBACK;
159	revs->diffopt.format_callback = cgit_diff_tree_cb;
160	revs->diffopt.format_callback_data = handle_rename;
161	diff_flush(&revs->diffopt);
162	revs->diffopt.output_format = saved_fmt;
163	revs->diffopt.flags = saved_flags;
164
165	lines_counted = 1;
166	return found;
167}
168
169static void print_commit(struct commit *commit, struct rev_info *revs)
170{
171	struct commitinfo *info;
172	int columns = revs->graph ? 4 : 3;
173	struct strbuf graphbuf = STRBUF_INIT;
174	struct strbuf msgbuf = STRBUF_INIT;
175
176	if (ctx.repo->enable_log_filecount)
177		columns++;
178	if (ctx.repo->enable_log_linecount)
179		columns++;
180
181	if (revs->graph) {
182		/* Advance graph until current commit */
183		while (!graph_next_line(revs->graph, &graphbuf)) {
184			/* Print graph segment in otherwise empty table row */
185			html("<tr class='nohover'><td class='commitgraph'>");
186			html(graphbuf.buf);
187			htmlf("</td><td colspan='%d' /></tr>\n", columns);
188			strbuf_setlen(&graphbuf, 0);
189		}
190		/* Current commit's graph segment is now ready in graphbuf */
191	}
192
193	info = cgit_parse_commit(commit);
194	htmlf("<tr%s>", ctx.qry.showmsg ? " class='logheader'" : "");
195
196	if (revs->graph) {
197		/* Print graph segment for current commit */
198		html("<td class='commitgraph'>");
199		html(graphbuf.buf);
200		html("</td>");
201		strbuf_setlen(&graphbuf, 0);
202	}
203	else {
204		html("<td>");
205		cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
206		html("</td>");
207	}
208
209	htmlf("<td%s>", ctx.qry.showmsg ? " class='logsubject'" : "");
210	if (ctx.qry.showmsg) {
211		/* line-wrap long commit subjects instead of truncating them */
212		size_t subject_len = strlen(info->subject);
213
214		if (subject_len > ctx.cfg.max_msg_len &&
215		    ctx.cfg.max_msg_len >= 15) {
216			/* symbol for signaling line-wrap (in PAGE_ENCODING) */
217			const char wrap_symbol[] = { ' ', 0xE2, 0x86, 0xB5, 0 };
218			int i = ctx.cfg.max_msg_len - strlen(wrap_symbol);
219
220			/* Rewind i to preceding space character */
221			while (i > 0 && !isspace(info->subject[i]))
222				--i;
223			if (!i) /* Oops, zero spaces. Reset i */
224				i = ctx.cfg.max_msg_len - strlen(wrap_symbol);
225
226			/* add remainder starting at i to msgbuf */
227			strbuf_add(&msgbuf, info->subject + i, subject_len - i);
228			strbuf_trim(&msgbuf);
229			strbuf_add(&msgbuf, "\n\n", 2);
230
231			/* Place wrap_symbol at position i in info->subject */
232			strcpy(info->subject + i, wrap_symbol);
233		}
234	}
235	cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
236			 oid_to_hex(&commit->object.oid), ctx.qry.vpath);
237	show_commit_decorations(commit);
238	html("</td><td>");
239	cgit_open_filter(ctx.repo->email_filter, info->author_email, "log");
240	html_txt(info->author);
241	cgit_close_filter(ctx.repo->email_filter);
242
243	if (revs->graph) {
244		html("</td><td>");
245		cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
246	}
247
248	if (!lines_counted && (ctx.repo->enable_log_filecount ||
249			       ctx.repo->enable_log_linecount)) {
250		files = 0;
251		add_lines = 0;
252		rem_lines = 0;
253		cgit_diff_commit(commit, inspect_files, ctx.qry.vpath);
254	}
255
256	if (ctx.repo->enable_log_filecount)
257		htmlf("</td><td>%d", files);
258	if (ctx.repo->enable_log_linecount)
259		htmlf("</td><td>-%d/+%d", rem_lines, add_lines);
260
261	html("</td></tr>\n");
262
263	if (revs->graph || ctx.qry.showmsg) { /* Print a second table row */
264		html("<tr class='nohover'>");
265
266		if (ctx.qry.showmsg) {
267			/* Concatenate commit message + notes in msgbuf */
268			if (info->msg && *(info->msg)) {
269				strbuf_addstr(&msgbuf, info->msg);
270				strbuf_addch(&msgbuf, '\n');
271			}
272			format_display_notes(commit->object.oid.hash,
273					     &msgbuf, PAGE_ENCODING, 0);
274			strbuf_addch(&msgbuf, '\n');
275			strbuf_ltrim(&msgbuf);
276		}
277
278		if (revs->graph) {
279			int lines = 0;
280
281			/* Calculate graph padding */
282			if (ctx.qry.showmsg) {
283				/* Count #lines in commit message + notes */
284				const char *p = msgbuf.buf;
285				lines = 1;
286				while ((p = strchr(p, '\n'))) {
287					p++;
288					lines++;
289				}
290			}
291
292			/* Print graph padding */
293			html("<td class='commitgraph'>");
294			while (lines > 0 || !graph_is_commit_finished(revs->graph)) {
295				if (graphbuf.len)
296					html("\n");
297				strbuf_setlen(&graphbuf, 0);
298				graph_next_line(revs->graph, &graphbuf);
299				html(graphbuf.buf);
300				lines--;
301			}
302			html("</td>\n");
303		}
304		else
305			html("<td/>"); /* Empty 'Age' column */
306
307		/* Print msgbuf into remainder of table row */
308		htmlf("<td colspan='%d'%s>\n", columns - (revs->graph ? 1 : 0),
309			ctx.qry.showmsg ? " class='logmsg'" : "");
310		html_txt(msgbuf.buf);
311		html("</td></tr>\n");
312	}
313
314	strbuf_release(&msgbuf);
315	strbuf_release(&graphbuf);
316	cgit_free_commitinfo(info);
317}
318
319static const char *disambiguate_ref(const char *ref, int *must_free_result)
320{
321	struct object_id oid;
322	struct strbuf longref = STRBUF_INIT;
323
324	strbuf_addf(&longref, "refs/heads/%s", ref);
325	if (get_sha1(longref.buf, oid.hash) == 0) {
326		*must_free_result = 1;
327		return strbuf_detach(&longref, NULL);
328	}
329
330	*must_free_result = 0;
331	strbuf_release(&longref);
332	return ref;
333}
334
335static char *next_token(char **src)
336{
337	char *result;
338
339	if (!src || !*src)
340		return NULL;
341	while (isspace(**src))
342		(*src)++;
343	if (!**src)
344		return NULL;
345	result = *src;
346	while (**src) {
347		if (isspace(**src)) {
348			**src = '\0';
349			(*src)++;
350			break;
351		}
352		(*src)++;
353	}
354	return result;
355}
356
357void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
358		    char *path, int pager, int commit_graph, int commit_sort)
359{
360	struct rev_info rev;
361	struct commit *commit;
362	struct argv_array rev_argv = ARGV_ARRAY_INIT;
363	int i, columns = commit_graph ? 4 : 3;
364	int must_free_tip = 0;
365
366	/* rev_argv.argv[0] will be ignored by setup_revisions */
367	argv_array_push(&rev_argv, "log_rev_setup");
368
369	if (!tip)
370		tip = ctx.qry.head;
371	tip = disambiguate_ref(tip, &must_free_tip);
372	argv_array_push(&rev_argv, tip);
373
374	if (grep && pattern && *pattern) {
375		pattern = xstrdup(pattern);
376		if (!strcmp(grep, "grep") || !strcmp(grep, "author") ||
377		    !strcmp(grep, "committer")) {
378			argv_array_pushf(&rev_argv, "--%s=%s", grep, pattern);
379		} else if (!strcmp(grep, "range")) {
380			char *arg;
381			/* Split the pattern at whitespace and add each token
382			 * as a revision expression. Do not accept other
383			 * rev-list options. Also, replace the previously
384			 * pushed tip (it's no longer relevant).
385			 */
386			argv_array_pop(&rev_argv);
387			while ((arg = next_token(&pattern))) {
388				if (*arg == '-') {
389					fprintf(stderr, "Bad range expr: %s\n",
390						arg);
391					break;
392				}
393				argv_array_push(&rev_argv, arg);
394			}
395		}
396	}
397
398	if (!path || !ctx.cfg.enable_follow_links) {
399		/*
400		 * If we don't have a path, "follow" is a no-op so make sure
401		 * the variable is set to false to avoid needing to check
402		 * both this and whether we have a path everywhere.
403		 */
404		ctx.qry.follow = 0;
405	}
406
407	if (commit_graph && !ctx.qry.follow) {
408		argv_array_push(&rev_argv, "--graph");
409		argv_array_push(&rev_argv, "--color");
410		graph_set_column_colors(column_colors_html,
411					COLUMN_COLORS_HTML_MAX);
412	}
413
414	if (commit_sort == 1)
415		argv_array_push(&rev_argv, "--date-order");
416	else if (commit_sort == 2)
417		argv_array_push(&rev_argv, "--topo-order");
418
419	if (path && ctx.qry.follow)
420		argv_array_push(&rev_argv, "--follow");
421	argv_array_push(&rev_argv, "--");
422	if (path)
423		argv_array_push(&rev_argv, path);
424
425	init_revisions(&rev, NULL);
426	rev.abbrev = DEFAULT_ABBREV;
427	rev.commit_format = CMIT_FMT_DEFAULT;
428	rev.verbose_header = 1;
429	rev.show_root_diff = 0;
430	rev.ignore_missing = 1;
431	rev.simplify_history = 1;
432	setup_revisions(rev_argv.argc, rev_argv.argv, &rev, NULL);
433	load_ref_decorations(DECORATE_FULL_REFS);
434	rev.show_decorations = 1;
435	rev.grep_filter.regflags |= REG_ICASE;
436
437	rev.diffopt.detect_rename = 1;
438	rev.diffopt.rename_limit = ctx.cfg.renamelimit;
439	if (ctx.qry.ignorews)
440		DIFF_XDL_SET(&rev.diffopt, IGNORE_WHITESPACE);
441
442	compile_grep_patterns(&rev.grep_filter);
443	prepare_revision_walk(&rev);
444
445	if (pager) {
446		cgit_print_layout_start();
447		html("<table class='list nowrap'>");
448	}
449
450	html("<tr class='nohover'>");
451	if (commit_graph)
452		html("<th></th>");
453	else
454		html("<th class='left'>Age</th>");
455	html("<th class='left'>Commit message");
456	if (pager) {
457		html(" (");
458		cgit_log_link(ctx.qry.showmsg ? "Collapse" : "Expand", NULL,
459			      NULL, ctx.qry.head, ctx.qry.sha1,
460			      ctx.qry.vpath, ctx.qry.ofs, ctx.qry.grep,
461			      ctx.qry.search, ctx.qry.showmsg ? 0 : 1,
462			      ctx.qry.follow);
463		html(")");
464	}
465	html("</th><th class='left'>Author</th>");
466	if (rev.graph)
467		html("<th class='left'>Age</th>");
468	if (ctx.repo->enable_log_filecount) {
469		html("<th class='left'>Files</th>");
470		columns++;
471	}
472	if (ctx.repo->enable_log_linecount) {
473		html("<th class='left'>Lines</th>");
474		columns++;
475	}
476	html("</tr>\n");
477
478	if (ofs<0)
479		ofs = 0;
480
481	for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; /* nop */) {
482		if (show_commit(commit, &rev))
483			i++;
484		free_commit_buffer(commit);
485		free_commit_list(commit->parents);
486		commit->parents = NULL;
487	}
488
489	for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; /* nop */) {
490		/*
491		 * In "follow" mode, we must count the files and lines the
492		 * first time we invoke diff on a given commit, and we need
493		 * to do that to see if the commit touches the path we care
494		 * about, so we do it in show_commit.  Hence we must clear
495		 * lines_counted here.
496		 *
497		 * This has the side effect of avoiding running diff twice
498		 * when we are both following renames and showing file
499		 * and/or line counts.
500		 */
501		lines_counted = 0;
502		if (show_commit(commit, &rev)) {
503			i++;
504			print_commit(commit, &rev);
505		}
506		free_commit_buffer(commit);
507		free_commit_list(commit->parents);
508		commit->parents = NULL;
509	}
510	if (pager) {
511		html("</table><ul class='pager'>");
512		if (ofs > 0) {
513			html("<li>");
514			cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
515				      ctx.qry.sha1, ctx.qry.vpath,
516				      ofs - cnt, ctx.qry.grep,
517				      ctx.qry.search, ctx.qry.showmsg,
518				      ctx.qry.follow);
519			html("</li>");
520		}
521		if ((commit = get_revision(&rev)) != NULL) {
522			html("<li>");
523			cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
524				      ctx.qry.sha1, ctx.qry.vpath,
525				      ofs + cnt, ctx.qry.grep,
526				      ctx.qry.search, ctx.qry.showmsg,
527				      ctx.qry.follow);
528			html("</li>");
529		}
530		html("</ul>");
531		cgit_print_layout_end();
532	} else if ((commit = get_revision(&rev)) != NULL) {
533		htmlf("<tr class='nohover'><td colspan='%d'>", columns);
534		cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL,
535			      ctx.qry.vpath, 0, NULL, NULL, ctx.qry.showmsg,
536			      ctx.qry.follow);
537		html("</td></tr>\n");
538	}
539
540	/* If we allocated tip then it is safe to cast away const. */
541	if (must_free_tip)
542		free((char*) tip);
543}