all repos — cgit @ 50fbd46c917a06f60235da59adb860697ea94eb0

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	files++;
 30	if (ctx.repo->enable_log_linecount)
 31		cgit_diff_files(pair->one->sha1, pair->two->sha1, count_lines);
 32}
 33
 34void print_commit(struct commit *commit)
 35{
 36	struct commitinfo *info;
 37	char *tmp;
 38	int cols = 2;
 39
 40	info = cgit_parse_commit(commit);
 41	htmlf("<tr%s><td>",
 42		ctx.qry.showmsg ? " class='logheader'" : "");
 43	tmp = fmt("id=%s", sha1_to_hex(commit->object.sha1));
 44	tmp = cgit_pageurl(ctx.repo->url, "commit", tmp);
 45	html_link_open(tmp, NULL, NULL);
 46	cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);
 47	html_link_close();
 48	htmlf("</td><td%s>",
 49		ctx.qry.showmsg ? " class='logsubject'" : "");
 50	cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,
 51			 sha1_to_hex(commit->object.sha1));
 52	html("</td><td>");
 53	html_txt(info->author);
 54	if (ctx.repo->enable_log_filecount) {
 55		files = 0;
 56		add_lines = 0;
 57		rem_lines = 0;
 58		cgit_diff_commit(commit, inspect_files);
 59		html("</td><td>");
 60		htmlf("%d", files);
 61		if (ctx.repo->enable_log_linecount) {
 62			html("</td><td>");
 63			htmlf("-%d/+%d", rem_lines, add_lines);
 64		}
 65	}
 66	html("</td></tr>\n");
 67	if (ctx.qry.showmsg) {
 68		if (ctx.repo->enable_log_filecount) {
 69			cols++;
 70			if (ctx.repo->enable_log_linecount)
 71				cols++;
 72		}
 73		htmlf("<tr class='nohover'><td/><td colspan='%d' class='logmsg'>",
 74			cols);
 75		html_txt(info->msg);
 76		html("</td></tr>\n");
 77	}
 78	cgit_free_commitinfo(info);
 79}
 80
 81static const char *disambiguate_ref(const char *ref)
 82{
 83	unsigned char sha1[20];
 84	const char *longref;
 85
 86	longref = fmt("refs/heads/%s", ref);
 87	if (get_sha1(longref, sha1) == 0)
 88		return longref;
 89
 90	return ref;
 91}
 92
 93void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern,
 94		    char *path, int pager)
 95{
 96	struct rev_info rev;
 97	struct commit *commit;
 98	const char *argv[] = {NULL, NULL, NULL, NULL, NULL};
 99	int argc = 2;
100	int i, columns = 3;
101
102	if (!tip)
103		tip = ctx.qry.head;
104
105	argv[1] = disambiguate_ref(tip);
106
107	if (grep && pattern && (!strcmp(grep, "grep") ||
108				!strcmp(grep, "author") ||
109				!strcmp(grep, "committer")))
110		argv[argc++] = fmt("--%s=%s", grep, pattern);
111
112	if (path) {
113		argv[argc++] = "--";
114		argv[argc++] = path;
115	}
116	init_revisions(&rev, NULL);
117	rev.abbrev = DEFAULT_ABBREV;
118	rev.commit_format = CMIT_FMT_DEFAULT;
119	rev.verbose_header = 1;
120	rev.show_root_diff = 0;
121	setup_revisions(argc, argv, &rev, NULL);
122	rev.grep_filter.regflags |= REG_ICASE;
123	compile_grep_patterns(&rev.grep_filter);
124	prepare_revision_walk(&rev);
125
126	if (pager)
127		html("<table class='list nowrap'>");
128
129	html("<tr class='nohover'><th class='left'>Age</th>"
130	      "<th class='left'>Commit message");
131	if (pager) {
132		html(" (");
133		cgit_log_link("toggle", NULL, NULL, ctx.qry.head, ctx.qry.sha1,
134			      ctx.qry.path, ctx.qry.ofs, ctx.qry.grep,
135			      ctx.qry.search, ctx.qry.showmsg ? 0 : 1);
136		html(")");
137	}
138	html("</th><th class='left'>Author</th>");
139	if (ctx.repo->enable_log_filecount) {
140		html("<th class='left'>Files</th>");
141		columns++;
142		if (ctx.repo->enable_log_linecount) {
143			html("<th class='left'>Lines</th>");
144			columns++;
145		}
146	}
147	html("</tr>\n");
148
149	if (ofs<0)
150		ofs = 0;
151
152	for (i = 0; i < ofs && (commit = get_revision(&rev)) != NULL; i++) {
153		free(commit->buffer);
154		commit->buffer = NULL;
155		free_commit_list(commit->parents);
156		commit->parents = NULL;
157	}
158
159	for (i = 0; i < cnt && (commit = get_revision(&rev)) != NULL; i++) {
160		print_commit(commit);
161		free(commit->buffer);
162		commit->buffer = NULL;
163		free_commit_list(commit->parents);
164		commit->parents = NULL;
165	}
166	if (pager) {
167		htmlf("</table><div class='pager'>",
168		     columns);
169		if (ofs > 0) {
170			cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,
171				      ctx.qry.sha1, ctx.qry.path,
172				      ofs - cnt, ctx.qry.grep,
173				      ctx.qry.search, ctx.qry.showmsg);
174			html("&nbsp;");
175		}
176		if ((commit = get_revision(&rev)) != NULL) {
177			cgit_log_link("[next]", NULL, NULL, ctx.qry.head,
178				      ctx.qry.sha1, ctx.qry.path,
179				      ofs + cnt, ctx.qry.grep,
180				      ctx.qry.search, ctx.qry.showmsg);
181		}
182		html("</div>");
183	} else if ((commit = get_revision(&rev)) != NULL) {
184		html("<tr class='nohover'><td colspan='3'>");
185		cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL, NULL, 0,
186			      NULL, NULL, ctx.qry.showmsg);
187		html("</td></tr>\n");
188	}
189}