all repos — cgit @ db8b8cb9467625cb8d9ecb67863aa67590e59cc0

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 Lars Hjemli
  4 *
  5 * Licensed under GNU General Public License v2
  6 *   (see COPYING for full license text)
  7 */
  8
  9#include "cgit.h"
 10#include "html.h"
 11#include "ui-shared.h"
 12
 13int files, add_lines, rem_lines;
 14
 15void count_lines(char *line, int size)
 16{
 17	if (size <= 0)
 18		return;
 19
 20	if (line[0] == '+')
 21		add_lines++;
 22
 23	else if (line[0] == '-')
 24		rem_lines++;
 25}
 26
 27void inspect_files(struct diff_filepair *pair)
 28{
 29	unsigned long old_size = 0;
 30	unsigned long new_size = 0;
 31	int binary = 0;
 32
 33	files++;
 34	if (ctx.repo->enable_log_linecount)
 35		cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
 36				&new_size, &binary, count_lines);
 37}
 38
 39void show_commit_decorations(struct commit *commit)
 40{
 41	struct name_decoration *deco;
 42	static char buf[1024];
 43
 44	buf[sizeof(buf) - 1] = 0;
 45	deco = lookup_decoration(&name_decoration, &commit->object);
 46	while (deco) {
 47		if (!prefixcmp(deco->name, "refs/heads/")) {
 48			strncpy(buf, deco->name + 11, sizeof(buf) - 1);
 49			cgit_log_link(buf, NULL, "branch-deco", buf, NULL, NULL,
 50				0, NULL, NULL, ctx.qry.showmsg);
 51		}
 52		else if (!prefixcmp(deco->name, "tag: refs/tags/")) {
 53			strncpy(buf, deco->name + 15, sizeof(buf) - 1);
 54			cgit_tag_link(buf, NULL, "tag-deco", ctx.qry.head, buf);
 55		}
 56		else if (!prefixcmp(deco->name, "refs/remotes/")) {
 57			strncpy(buf, deco->name + 13, sizeof(buf) - 1);
 58			cgit_log_link(buf, NULL, "remote-deco", NULL,
 59				sha1_to_hex(commit->object.sha1), NULL,
 60				0, NULL, NULL, ctx.qry.showmsg);
 61		}
 62		else {
 63			strncpy(buf, deco->name, sizeof(buf) - 1);
 64			cgit_commit_link(buf, NULL, "deco", ctx.qry.head,
 65				sha1_to_hex(commit->object.sha1));
 66		}
 67		deco = deco->next;
 68	}
 69}
 70
 71void print_commit(struct commit *commit)
 72{
 73	struct commitinfo *info;
 74	char *tmp;
 75	int cols = 2;
 76
 77	info = cgit_parse_commit(commit);
 78	htmlf("<tr%s><td>",
 79		ctx.qry.showmsg ? " class='logheader'" : "");
 80	tmp = fmt("id=%s", sha1_to_hex(commit->object.sha1));
 81	tmp = cgit_pageurl(ctx.repo->url, "commit", tmp);
 82	html_link_open(tmp, NULL, NULL);
 83	cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
 84	html_link_close();
 85	htmlf("</td><td%s>",
 86		ctx.qry.showmsg ? " class='logsubject'" : "");
 87	cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
 88			 sha1_to_hex(commit->object.sha1));
 89	show_commit_decorations(commit);
 90	html("</td><td>");
 91	html_txt(info->author);
 92	if (ctx.repo->enable_log_filecount) {
 93		files = 0;
 94		add_lines = 0;
 95		rem_lines = 0;
 96		cgit_diff_commit(commit, inspect_files);
 97		html("</td><td>");
 98		htmlf("%d", files);
 99		if (ctx.repo->enable_log_linecount) {
100			html("</td><td>");
101			htmlf("-%d/+%d", rem_lines, add_lines);
102		}
103	}
104	html("</td></tr>\n");
105	if (ctx.qry.showmsg) {
106		if (ctx.repo->enable_log_filecount) {
107			cols++;
108			if (ctx.repo->enable_log_linecount)
109				cols++;
110		}
111		htmlf("<tr class='nohover'><td/><td colspan='%d' class='logmsg'>",
112			cols);
113		html_txt(info->msg);
114		html("</td></tr>\n");
115	}
116	cgit_free_commitinfo(info);
117}
118
119static const char *disambiguate_ref(const char *ref)
120{
121	unsigned char sha1[20];
122	const char *longref;
123
124	longref = fmt("refs/heads/%s", ref);
125	if (get_sha1(longref, sha1) == 0)
126		return longref;
127
128	return ref;
129}
130
131void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
132		    char *path, int pager)
133{
134	struct rev_info rev;
135	struct commit *commit;
136	const char *argv[] = {NULL, NULL, NULL, NULL, NULL};
137	int argc = 2;
138	int i, columns = 3;
139
140	if (!tip)
141		tip = ctx.qry.head;
142
143	argv[1] = disambiguate_ref(tip);
144
145	if (grep && pattern && (!strcmp(grep, "grep") ||
146				!strcmp(grep, "author") ||
147				!strcmp(grep, "committer")))
148		argv[argc++] = fmt("--%s=%s", grep, pattern);
149
150	if (path) {
151		argv[argc++] = "--";
152		argv[argc++] = path;
153	}
154	init_revisions(&rev, NULL);
155	rev.abbrev = DEFAULT_ABBREV;
156	rev.commit_format = CMIT_FMT_DEFAULT;
157	rev.verbose_header = 1;
158	rev.show_root_diff = 0;
159	setup_revisions(argc, argv, &rev, NULL);
160	load_ref_decorations();
161	rev.show_decorations = 1;
162	rev.grep_filter.regflags |= REG_ICASE;
163	compile_grep_patterns(&rev.grep_filter);
164	prepare_revision_walk(&rev);
165
166	if (pager)
167		html("<table class='list nowrap'>");
168
169	html("<tr class='nohover'><th class='left'>Age</th>"
170	      "<th class='left'>Commit message");
171	if (pager) {
172		html(" (");
173		cgit_log_link(ctx.qry.showmsg ? "Collapse" : "Expand", NULL,
174			      NULL, ctx.qry.head, ctx.qry.sha1,
175			      ctx.qry.path, ctx.qry.ofs, ctx.qry.grep,
176			      ctx.qry.search, ctx.qry.showmsg ? 0 : 1);
177		html(")");
178	}
179	html("</th><th class='left'>Author</th>");
180	if (ctx.repo->enable_log_filecount) {
181		html("<th class='left'>Files</th>");
182		columns++;
183		if (ctx.repo->enable_log_linecount) {
184			html("<th class='left'>Lines</th>");
185			columns++;
186		}
187	}
188	html("</tr>\n");
189
190	if (ofs<0)
191		ofs = 0;
192
193	for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) {
194		free(commit->buffer);
195		commit->buffer = NULL;
196		free_commit_list(commit->parents);
197		commit->parents = NULL;
198	}
199
200	for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) {
201		print_commit(commit);
202		free(commit->buffer);
203		commit->buffer = NULL;
204		free_commit_list(commit->parents);
205		commit->parents = NULL;
206	}
207	if (pager) {
208		htmlf("</table><div class='pager'>",
209		     columns);
210		if (ofs > 0) {
211			cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
212				      ctx.qry.sha1, ctx.qry.path,
213				      ofs - cnt, ctx.qry.grep,
214				      ctx.qry.search, ctx.qry.showmsg);
215			html("&nbsp;");
216		}
217		if ((commit = get_revision(&rev)) != NULL) {
218			cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
219				      ctx.qry.sha1, ctx.qry.path,
220				      ofs + cnt, ctx.qry.grep,
221				      ctx.qry.search, ctx.qry.showmsg);
222		}
223		html("</div>");
224	} else if ((commit = get_revision(&rev)) != NULL) {
225		html("<tr class='nohover'><td colspan='3'>");
226		cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL, NULL, 0,
227			      NULL, NULL, ctx.qry.showmsg);
228		html("</td></tr>\n");
229	}
230}