diff options
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | cgit.c | 53 | ||||
| -rw-r--r-- | cgit.css | 148 | ||||
| -rw-r--r-- | cgit.h | 6 | ||||
| -rw-r--r-- | cgitrc.5.txt | 17 | ||||
| -rw-r--r-- | cmd.c | 10 | ||||
| m--------- | git | 0 | ||||
| -rw-r--r-- | shared.c | 6 | ||||
| -rwxr-xr-x | tests/t0104-tree.sh | 4 | ||||
| -rwxr-xr-x | tests/t0105-commit.sh | 2 | ||||
| -rwxr-xr-x | tests/t0107-snapshot.sh | 22 | ||||
| -rw-r--r-- | ui-commit.c | 4 | ||||
| -rw-r--r-- | ui-log.c | 71 | ||||
| -rw-r--r-- | ui-log.h | 1 | ||||
| -rw-r--r-- | ui-refs.c | 42 | ||||
| -rw-r--r-- | ui-repolist.c | 137 | ||||
| -rw-r--r-- | ui-shared.c | 44 | ||||
| -rw-r--r-- | ui-shared.h | 7 | ||||
| -rw-r--r-- | ui-snapshot.c | 100 | ||||
| -rw-r--r-- | ui-snapshot.h | 3 | ||||
| -rw-r--r-- | ui-stats.c | 410 | ||||
| -rw-r--r-- | ui-stats.h | 27 | ||||
| -rw-r--r-- | ui-tag.c | 11 | ||||
| -rw-r--r-- | ui-tree.c | 36 | 
24 files changed, 1007 insertions, 157 deletions
| @@ -5,7 +5,7 @@ CGIT_DATA_PATH = $(CGIT_SCRIPT_PATH)  CGIT_CONFIG = /etc/cgitrc  CACHE_ROOT = /var/cache/cgit  SHA1_HEADER = <openssl/sha.h> -GIT_VER = 1.6.0.3 +GIT_VER = 1.6.1  GIT_URL = http://www.kernel.org/pub/software/scm/git/git-$(GIT_VER).tar.bz2  INSTALL = install @@ -90,6 +90,7 @@ OBJECTS += ui-refs.o  OBJECTS += ui-repolist.o  OBJECTS += ui-shared.o  OBJECTS += ui-snapshot.o +OBJECTS += ui-stats.o  OBJECTS += ui-summary.o  OBJECTS += ui-tag.o  OBJECTS += ui-tree.o @@ -12,6 +12,7 @@  #include "configfile.h"  #include "html.h"  #include "ui-shared.h" +#include "ui-stats.h"  #include "scan-tree.h"  const char *cgit_version = CGIT_VERSION; @@ -54,6 +55,8 @@ void config_cb(const char *name, const char *value)  		ctx.cfg.enable_log_filecount = atoi(value);  	else if (!strcmp(name, "enable-log-linecount"))  		ctx.cfg.enable_log_linecount = atoi(value); +	else if (!strcmp(name, "max-stats")) +		ctx.cfg.max_stats = cgit_find_stats_period(value, NULL);  	else if (!strcmp(name, "cache-size"))  		ctx.cfg.cache_size = atoi(value);  	else if (!strcmp(name, "cache-root")) @@ -112,6 +115,8 @@ void config_cb(const char *name, const char *value)  		ctx.repo->enable_log_filecount = ctx.cfg.enable_log_filecount * atoi(value);  	else if (ctx.repo && !strcmp(name, "repo.enable-log-linecount"))  		ctx.repo->enable_log_linecount = ctx.cfg.enable_log_linecount * atoi(value); +	else if (ctx.repo && !strcmp(name, "repo.max-stats")) +		ctx.repo->max_stats = cgit_find_stats_period(value, NULL);  	else if (ctx.repo && !strcmp(name, "repo.module-link"))  		ctx.repo->module_link= xstrdup(value);  	else if (ctx.repo && !strcmp(name, "repo.readme") && value != NULL) { @@ -154,6 +159,12 @@ static void querystring_cb(const char *name, const char *value)  		ctx.qry.name = xstrdup(value);  	} else if (!strcmp(name, "mimetype")) {  		ctx.qry.mimetype = xstrdup(value); +	} else if (!strcmp(name, "s")){ +		ctx.qry.sort = xstrdup(value); +	} else if (!strcmp(name, "showmsg")) { +		ctx.qry.showmsg = atoi(value); +	} else if (!strcmp(name, "period")) { +		ctx.qry.period = xstrdup(value);  	}  } @@ -177,6 +188,7 @@ static void prepare_context(struct cgit_context *ctx)  	ctx->cfg.max_lock_attempts = 5;  	ctx->cfg.max_msg_len = 80;  	ctx->cfg.max_repodesc_len = 80; +	ctx->cfg.max_stats = 0;  	ctx->cfg.module_link = "./?repo=%s&page=commit&id=%s";  	ctx->cfg.renamelimit = -1;  	ctx->cfg.robots = "index, nofollow"; @@ -434,28 +446,29 @@ int main(int argc, const char **argv)  	ctx.repo = NULL;  	http_parse_querystring(ctx.qry.raw, querystring_cb); -	/* If virtual-root isn't specified in cgitrc and no url -	 * parameter is specified on the querystring, lets pretend -	 * that virtualroot equals SCRIPT_NAME and use PATH_INFO as -	 * url. This allows cgit to work with virtual urls without -	 * the need for rewriterules in the webserver (as long as -	 * PATH_INFO is included in the cache lookup key). +	/* If virtual-root isn't specified in cgitrc, lets pretend +	 * that virtual-root equals SCRIPT_NAME.  	 */ -	if (!ctx.cfg.virtual_root && !ctx.qry.url) { +	if (!ctx.cfg.virtual_root)  		ctx.cfg.virtual_root = ctx.cfg.script_name; -		path = getenv("PATH_INFO"); -		if (path) { -			if (path[0] == '/') -				path++; -			ctx.qry.url = xstrdup(path); -			if (ctx.qry.raw) { -				qry = ctx.qry.raw; -				ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); -				free(qry); -			} else -				ctx.qry.raw = ctx.qry.url; -			cgit_parse_url(ctx.qry.url); -		} + +	/* If no url parameter is specified on the querystring, lets +	 * use PATH_INFO as url. This allows cgit to work with virtual +	 * urls without the need for rewriterules in the webserver (as +	 * long as PATH_INFO is included in the cache lookup key). +	 */ +	path = getenv("PATH_INFO"); +	if (!ctx.qry.url && path) { +		if (path[0] == '/') +			path++; +		ctx.qry.url = xstrdup(path); +		if (ctx.qry.raw) { +			qry = ctx.qry.raw; +			ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); +			free(qry); +		} else +			ctx.qry.raw = ctx.qry.url; +		cgit_parse_url(ctx.qry.url);  	}  	ttl = calc_ttl(); @@ -120,6 +120,10 @@ table.list tr {  	background: white;  } +table.list tr.logheader { +	background: #eee; +} +  table.list tr:hover {  	background: #eee;  } @@ -143,6 +147,17 @@ table.list td {  	padding: 0.1em 0.5em 0.1em 0.5em;  } +table.list td.logsubject { +	font-family: monospace; +	font-weight: bold; +} + +table.list td.logmsg { +	font-family: monospace; +	white-space: pre; +	padding: 1em 0em 2em 0em; +} +  table.list td a {  	color: black;  } @@ -221,26 +236,31 @@ table.blob {  	border-top: solid 1px black;  } -table.blob td.no { -	border-right: solid 1px black; +table.blob td.lines { +	margin: 0; padding: 0; +	vertical-align: top;  	color: black; +} + +table.blob td.linenumbers { +	margin: 0; padding: 0; +	vertical-align: top; +	border-right: 1px solid gray;  	background-color: #eee; -	text-align: right;  } -table.blob td.no a { -	color: black; +table.blob pre { +	padding: 0; margin: 0;  } -table.blob td.no a:hover { -	color: black; +table.blob a.no { +	color: gray; +	text-align: right;  	text-decoration: none;  } -table.blob td.txt { -	white-space: pre; -	font-family: monospace; -	padding-left: 0.5em; +table.blob a.no a:hover { +	color: black;  }  table.nowrap td { @@ -456,3 +476,109 @@ div.footer {  	font-size: 80%;  	color: #ccc;  } +a.branch-deco { +	margin: 0px 0.5em; +	padding: 0px 0.25em; +	background-color: #88ff88; +	border: solid 1px #007700; +} +a.tag-deco { +	margin: 0px 0.5em; +	padding: 0px 0.25em; +	background-color: #ffff88; +	border: solid 1px #777700; +} +a.remote-deco { +	margin: 0px 0.5em; +	padding: 0px 0.25em; +	background-color: #ccccff; +	border: solid 1px #000077; +} +a.deco { +	margin: 0px 0.5em; +	padding: 0px 0.25em; +	background-color: #ff8888; +	border: solid 1px #770000; +} + +div.commit-subject a { +	margin-left: 1em; +	font-size: 75%; +} + +table.stats { +	border: solid 1px black; +	border-collapse: collapse; +} + +table.stats th { +	text-align: left; +	padding: 1px 0.5em; +	background-color: #eee; +	border: solid 1px black; +} + +table.stats td { +	text-align: right; +	padding: 1px 0.5em; +	border: solid 1px black; +} + +table.stats td.total { +	font-weight: bold; +	text-align: left; +} + +table.stats td.sum { +	color: #c00; +	font-weight: bold; +/*	background-color: #eee; */ +} + +table.stats td.left { +	text-align: left; +} + +table.vgraph { +	border-collapse: separate; +	border: solid 1px black; +	height: 200px; +} + +table.vgraph th { +	background-color: #eee; +	font-weight: bold; +	border: solid 1px white; +	padding: 1px 0.5em; +} + +table.vgraph td { +	vertical-align: bottom; +	padding: 0px 10px; +} + +table.vgraph div.bar { +	background-color: #eee; +} + +table.hgraph { +	border: solid 1px black; +	width: 800px; +} + +table.hgraph th { +	background-color: #eee; +	font-weight: bold; +	border: solid 1px black; +	padding: 1px 0.5em; +} + +table.hgraph td { +	vertical-align: center; +	padding: 2px 2px; +} + +table.hgraph div.bar { +	background-color: #eee; +	height: 1em; +} @@ -61,6 +61,8 @@ struct cgit_repo {  	int snapshots;  	int enable_log_filecount;  	int enable_log_linecount; +	int max_stats; +	time_t mtime;  };  struct cgit_repolist { @@ -119,8 +121,11 @@ struct cgit_query {  	char *name;  	char *mimetype;  	char *url; +	char *period;  	int   ofs;  	int nohead; +	char *sort; +	int showmsg;  };  struct cgit_config { @@ -157,6 +162,7 @@ struct cgit_config {  	int max_lock_attempts;  	int max_msg_len;  	int max_repodesc_len; +	int max_stats;  	int nocache;  	int renamelimit;  	int snapshots; diff --git a/cgitrc.5.txt b/cgitrc.5.txt index ab9ab66..09f56a6 100644 --- a/cgitrc.5.txt +++ b/cgitrc.5.txt @@ -129,6 +129,11 @@ max-repodesc-length  	Specifies the maximum number of repo description characters to display  	on the repository index page. Default value: "80". +max-stats +	Set the default maximum statistics period. Valid values are "week", +	"month", "quarter" and "year". If unspecified, statistics are +	disabled. Default value: none. See also: "repo.max-stats". +  module-link  	Text which will be used as the formatstring for a hyperlink when a  	submodule is printed in a directory listing. The arguments for the @@ -218,6 +223,11 @@ repo.enable-log-linecount  	A flag which can be used to disable the global setting  	`enable-log-linecount'. Default value: none. +repo.max-stats +	Override the default maximum statistics period. Valid values are equal +	to the values specified for the global "max-stats" setting. Default +	value: none. +  repo.name  	The value to show as repository name. Default value: <repo.url>. @@ -276,6 +286,10 @@ favicon=/favicon.ico  logo=/img/mylogo.png +# Enable statistics per week, month and quarter +max-stats=quarter + +  # Set the title and heading of the repository index page  root-title=foobar.com git repositories @@ -348,6 +362,9 @@ repo.snapshots=0  # Disable line-counts for this repo  repo.enable-log-linecount=0 +# Restrict the max statistics period for this repo +repo.max-stats=month +  BUGS  ---- @@ -21,6 +21,7 @@  #include "ui-refs.h"  #include "ui-repolist.h"  #include "ui-snapshot.h" +#include "ui-stats.h"  #include "ui-summary.h"  #include "ui-tag.h"  #include "ui-tree.h" @@ -104,11 +105,15 @@ static void refs_fn(struct cgit_context *ctx)  static void snapshot_fn(struct cgit_context *ctx)  { -	cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, -			    cgit_repobasename(ctx->repo->url), ctx->qry.path, +	cgit_print_snapshot(ctx->qry.head, ctx->qry.sha1, ctx->qry.path,  			    ctx->repo->snapshots, ctx->qry.nohead);  } +static void stats_fn(struct cgit_context *ctx) +{ +	cgit_show_stats(ctx); +} +  static void summary_fn(struct cgit_context *ctx)  {  	cgit_print_summary(); @@ -145,6 +150,7 @@ struct cgit_cmd *cgit_get_cmd(struct cgit_context *ctx)  		def_cmd(refs, 1, 1),  		def_cmd(repolist, 0, 0),  		def_cmd(snapshot, 1, 0), +		def_cmd(stats, 1, 1),  		def_cmd(summary, 1, 1),  		def_cmd(tag, 1, 1),  		def_cmd(tree, 1, 1), diff --git a/git b/git -Subproject 031e6c898f61db1ae0c0be641eac6532c1000d5 +Subproject 8104ebfe8276657ee803cca7eb8665a78cf3ef8 @@ -58,8 +58,10 @@ struct cgit_repo *cgit_add_repo(const char *url)  	ret->snapshots = ctx.cfg.snapshots;  	ret->enable_log_filecount = ctx.cfg.enable_log_filecount;  	ret->enable_log_linecount = ctx.cfg.enable_log_linecount; +	ret->max_stats = ctx.cfg.max_stats;  	ret->module_link = ctx.cfg.module_link;  	ret->readme = NULL; +	ret->mtime = -1;  	return ret;  } @@ -266,10 +268,12 @@ int cgit_diff_files(const unsigned char *old_sha1,  	if (!load_mmfile(&file1, old_sha1) || !load_mmfile(&file2, new_sha1))  		return 1; +	memset(&diff_params, 0, sizeof(diff_params)); +	memset(&emit_params, 0, sizeof(emit_params)); +	memset(&emit_cb, 0, sizeof(emit_cb));  	diff_params.flags = XDF_NEED_MINIMAL;  	emit_params.ctxlen = 3;  	emit_params.flags = XDL_EMIT_FUNCNAMES; -	emit_params.find_func = NULL;  	emit_cb.outf = filediff_cb;  	emit_cb.priv = fn;  	xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); diff --git a/tests/t0104-tree.sh b/tests/t0104-tree.sh index 33f4eb0..2ce1251 100755 --- a/tests/t0104-tree.sh +++ b/tests/t0104-tree.sh @@ -11,11 +11,11 @@ run_test 'find file-50' 'grep -e "file-50" trash/tmp'  run_test 'generate bar/tree/file-50' 'cgit_url "bar/tree/file-50" >trash/tmp'  run_test 'find line 1' ' -	grep -e "<a id=.n1. name=.n1. href=.#n1.>1</a>" trash/tmp +	grep -e "<a class=.no. id=.n1. name=.n1. href=.#n1.>1</a>" trash/tmp  '  run_test 'no line 2' ' -	! grep -e "<a id=.n2. name=.n2. href=.#n2.>2</a>" trash/tmp +	! grep -e "<a class=.no. id=.n2. name=.n2. href=.#n2.>2</a>" trash/tmp  '  run_test 'generate foo+bar/tree' 'cgit_url "foo%2bbar/tree" >trash/tmp' diff --git a/tests/t0105-commit.sh b/tests/t0105-commit.sh index a864612..ae794c8 100755 --- a/tests/t0105-commit.sh +++ b/tests/t0105-commit.sh @@ -9,7 +9,7 @@ run_test 'find tree link' 'grep -e "<a href=./foo/tree/.>" trash/tmp'  run_test 'find parent link' 'grep -E "<a href=./foo/commit/\?id=.+>" trash/tmp'  run_test 'find commit subject' ' -	grep -e "<div class=.commit-subject.>commit 5</div>" trash/tmp +	grep -e "<div class=.commit-subject.>commit 5<" trash/tmp  '  run_test 'find commit msg' 'grep -e "<div class=.commit-msg.></div>" trash/tmp' diff --git a/tests/t0107-snapshot.sh b/tests/t0107-snapshot.sh index d97c465..8ab4912 100755 --- a/tests/t0107-snapshot.sh +++ b/tests/t0107-snapshot.sh @@ -4,36 +4,36 @@  prepare_tests "Verify snapshot" -run_test 'get foo/snapshot/test.tar.gz' ' -	cgit_url "foo/snapshot/test.tar.gz" >trash/tmp +run_test 'get foo/snapshot/master.tar.gz' ' +	cgit_url "foo/snapshot/master.tar.gz" >trash/tmp  '  run_test 'check html headers' '  	head -n 1 trash/tmp | -	     grep -e "Content-Type: application/x-tar" && +	     grep -e "Content-Type: application/x-gzip" &&  	head -n 2 trash/tmp | -	     grep -e "Content-Disposition: inline; filename=.test.tar.gz." +	     grep -e "Content-Disposition: inline; filename=.master.tar.gz."  '  run_test 'strip off the header lines' ' -	 tail -n +6 trash/tmp > trash/test.tar.gz +	 tail -n +6 trash/tmp > trash/master.tar.gz  ' -run_test 'verify gzip format' 'gunzip --test trash/test.tar.gz' +run_test 'verify gzip format' 'gunzip --test trash/master.tar.gz'  run_test 'untar' ' -	rm -rf trash/foo && -	tar -xf trash/test.tar.gz -C trash +	rm -rf trash/master && +	tar -xf trash/master.tar.gz -C trash  '  run_test 'count files' ' -	c=$(ls -1 trash/foo/ | wc -l) && +	c=$(ls -1 trash/master/ | wc -l) &&  	test $c = 5  '  run_test 'verify untarred file-5' ' -	 grep -e "^5$" trash/foo/file-5 && -	 test $(cat trash/foo/file-5 | wc -l) = 1 +	 grep -e "^5$" trash/master/file-5 && +	 test $(cat trash/master/file-5 | wc -l) = 1  '  tests_done diff --git a/ui-commit.c b/ui-commit.c index a6a85a4..41ce70e 100644 --- a/ui-commit.c +++ b/ui-commit.c @@ -10,6 +10,7 @@  #include "html.h"  #include "ui-shared.h"  #include "ui-diff.h" +#include "ui-log.h"  void cgit_print_commit(char *hex)  { @@ -34,6 +35,8 @@ void cgit_print_commit(char *hex)  	}  	info = cgit_parse_commit(commit); +	load_ref_decorations(); +  	html("<table summary='commit info' class='commit-info'>\n");  	html("<tr><th>author</th><td>");  	html_txt(info->author); @@ -87,6 +90,7 @@ void cgit_print_commit(char *hex)  	html("</table>\n");  	html("<div class='commit-subject'>");  	html_txt(info->subject); +	show_commit_decorations(commit);  	html("</div>");  	html("<div class='commit-msg'>");  	html_txt(info->msg); @@ -31,21 +31,57 @@ void inspect_files(struct diff_filepair *pair)  		cgit_diff_files(pair->one->sha1, pair->two->sha1, count_lines);  } +void show_commit_decorations(struct commit *commit) +{ +	struct name_decoration *deco; +	static char buf[1024]; + +	buf[sizeof(buf) - 1] = 0; +	deco = lookup_decoration(&name_decoration, &commit->object); +	while (deco) { +		if (!prefixcmp(deco->name, "refs/heads/")) { +			strncpy(buf, deco->name + 11, sizeof(buf) - 1); +			cgit_log_link(buf, NULL, "branch-deco", buf, NULL, NULL, +				0, NULL, NULL, ctx.qry.showmsg); +		} +		else if (!prefixcmp(deco->name, "tag: refs/tags/")) { +			strncpy(buf, deco->name + 15, sizeof(buf) - 1); +			cgit_tag_link(buf, NULL, "tag-deco", ctx.qry.head, buf); +		} +		else if (!prefixcmp(deco->name, "refs/remotes/")) { +			strncpy(buf, deco->name + 13, sizeof(buf) - 1); +			cgit_log_link(buf, NULL, "remote-deco", NULL, +				sha1_to_hex(commit->object.sha1), NULL, +				0, NULL, NULL, ctx.qry.showmsg); +		} +		else { +			strncpy(buf, deco->name, sizeof(buf) - 1); +			cgit_commit_link(buf, NULL, "deco", ctx.qry.head, +				sha1_to_hex(commit->object.sha1)); +		} +		deco = deco->next; +	} +} +  void print_commit(struct commit *commit)  {  	struct commitinfo *info;  	char *tmp; +	int cols = 2;  	info = cgit_parse_commit(commit); -	html("<tr><td>"); +	htmlf("<tr%s><td>", +		ctx.qry.showmsg ? " class='logheader'" : "");  	tmp = fmt("id=%s", sha1_to_hex(commit->object.sha1));  	tmp = cgit_pageurl(ctx.repo->url, "commit", tmp);  	html_link_open(tmp, NULL, NULL);  	cgit_print_age(commit->date, TM_WEEK * 2, FMT_SHORTDATE);  	html_link_close(); -	html("</td><td>"); +	htmlf("</td><td%s>", +		ctx.qry.showmsg ? " class='logsubject'" : "");  	cgit_commit_link(info->subject, NULL, NULL, ctx.qry.head,  			 sha1_to_hex(commit->object.sha1)); +	show_commit_decorations(commit);  	html("</td><td>");  	html_txt(info->author);  	if (ctx.repo->enable_log_filecount) { @@ -61,6 +97,17 @@ void print_commit(struct commit *commit)  		}  	}  	html("</td></tr>\n"); +	if (ctx.qry.showmsg) { +		if (ctx.repo->enable_log_filecount) { +			cols++; +			if (ctx.repo->enable_log_linecount) +				cols++; +		} +		htmlf("<tr class='nohover'><td/><td colspan='%d' class='logmsg'>", +			cols); +		html_txt(info->msg); +		html("</td></tr>\n"); +	}  	cgit_free_commitinfo(info);  } @@ -105,6 +152,8 @@ void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern  	rev.verbose_header = 1;  	rev.show_root_diff = 0;  	setup_revisions(argc, argv, &rev, NULL); +	load_ref_decorations(); +	rev.show_decorations = 1;  	rev.grep_filter.regflags |= REG_ICASE;  	compile_grep_patterns(&rev.grep_filter);  	prepare_revision_walk(&rev); @@ -113,8 +162,16 @@ void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern  		html("<table class='list nowrap'>");  	html("<tr class='nohover'><th class='left'>Age</th>" -	     "<th class='left'>Commit message</th>" -	     "<th class='left'>Author</th>"); +	      "<th class='left'>Commit message"); +	if (pager) { +		html(" ("); +		cgit_log_link(ctx.qry.showmsg ? "Collapse" : "Expand", NULL, +			      NULL, ctx.qry.head, ctx.qry.sha1, +			      ctx.qry.path, ctx.qry.ofs, ctx.qry.grep, +			      ctx.qry.search, ctx.qry.showmsg ? 0 : 1); +		html(")"); +	} +	html("</th><th class='left'>Author</th>");  	if (ctx.repo->enable_log_filecount) {  		html("<th class='left'>Files</th>");  		columns++; @@ -149,20 +206,20 @@ void cgit_print_log(const char *tip, int ofs, int cnt, char *grep, char *pattern  			cgit_log_link("[prev]", NULL, NULL, ctx.qry.head,  				      ctx.qry.sha1, ctx.qry.path,  				      ofs - cnt, ctx.qry.grep, -				      ctx.qry.search); +				      ctx.qry.search, ctx.qry.showmsg);  			html(" ");  		}  		if ((commit = get_revision(&rev)) != NULL) {  			cgit_log_link("[next]", NULL, NULL, ctx.qry.head,  				      ctx.qry.sha1, ctx.qry.path,  				      ofs + cnt, ctx.qry.grep, -				      ctx.qry.search); +				      ctx.qry.search, ctx.qry.showmsg);  		}  		html("</div>");  	} else if ((commit = get_revision(&rev)) != NULL) {  		html("<tr class='nohover'><td colspan='3'>");  		cgit_log_link("[...]", NULL, NULL, ctx.qry.head, NULL, NULL, 0, -			      NULL, NULL); +			      NULL, NULL, ctx.qry.showmsg);  		html("</td></tr>\n");  	}  } @@ -3,5 +3,6 @@  extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep,  			   char *pattern, char *path, int pager); +extern void show_commit_decorations(struct commit *commit);  #endif /* UI_LOG_H */ @@ -58,7 +58,8 @@ static int print_branch(struct refinfo *ref)  	if (!info)  		return 1;  	html("<tr><td>"); -	cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL); +	cgit_log_link(name, NULL, NULL, name, NULL, NULL, 0, NULL, NULL, +		      ctx.qry.showmsg);  	html("</td><td>");  	if (ref->object->type == OBJ_COMMIT) { @@ -78,12 +79,37 @@ static int print_branch(struct refinfo *ref)  static void print_tag_header()  {  	html("<tr class='nohover'><th class='left'>Tag</th>" -	     "<th class='left'>Reference</th>" +	     "<th class='left'>Download</th>"  	     "<th class='left'>Author</th>"  	     "<th class='left' colspan='2'>Age</th></tr>\n");  	header = 1;  } +static void print_tag_downloads(const struct cgit_repo *repo, const char *ref) +{ +	const struct cgit_snapshot_format* f; +    	char *filename; +	const char *basename; + +	if (!ref || strlen(ref) < 2) +		return; + +	basename = cgit_repobasename(repo->url); +	if (prefixcmp(ref, basename) != 0) { +		if ((ref[0] == 'v' || ref[0] == 'V') && isdigit(ref[1])) +			ref++; +		if (isdigit(ref[0])) +			ref = xstrdup(fmt("%s-%s", basename, ref)); +	} + +	for (f = cgit_snapshot_formats; f->suffix; f++) { +		if (!(repo->snapshots & f->bit)) +			continue; +		filename = fmt("%s%s", ref, f->suffix); +		cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename); +		html("  "); +	} +}  static int print_tag(struct refinfo *ref)  {  	struct tag *tag; @@ -98,7 +124,10 @@ static int print_tag(struct refinfo *ref)  		html("<tr><td>");  		cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);  		html("</td><td>"); -		cgit_object_link(tag->tagged); +		if (ctx.repo->snapshots && (tag->tagged->type == OBJ_COMMIT)) +			print_tag_downloads(ctx.repo, name); +		else +			cgit_object_link(tag->tagged);  		html("</td><td>");  		if (info->tagger)  			html(info->tagger); @@ -110,9 +139,12 @@ static int print_tag(struct refinfo *ref)  		if (!header)  			print_tag_header();  		html("<tr><td>"); -		html_txt(name); +		cgit_tag_link(name, NULL, NULL, ctx.qry.head, name);  		html("</td><td>"); -		cgit_object_link(ref->object); +		if (ctx.repo->snapshots && (ref->object->type == OBJ_COMMIT)) +			print_tag_downloads(ctx.repo, name); +		else +			cgit_object_link(ref->object);  		html("</td></tr>\n");  	}  	return 0; diff --git a/ui-repolist.c b/ui-repolist.c index 2324273..2c13d50 100644 --- a/ui-repolist.c +++ b/ui-repolist.c @@ -32,21 +32,38 @@ time_t read_agefile(char *path)  		return 0;  } -static void print_modtime(struct cgit_repo *repo) +static int get_repo_modtime(const struct cgit_repo *repo, time_t *mtime)  {  	char *path;  	struct stat s; +	struct cgit_repo *r = (struct cgit_repo *)repo; +	if (repo->mtime != -1) { +		*mtime = repo->mtime; +		return 1; +	}  	path = fmt("%s/%s", repo->path, ctx.cfg.agefile);  	if (stat(path, &s) == 0) { -		cgit_print_age(read_agefile(path), -1, NULL); -		return; +		*mtime = read_agefile(path); +		r->mtime = *mtime; +		return 1;  	}  	path = fmt("%s/refs/heads/%s", repo->path, repo->defbranch); -	if (stat(path, &s) != 0) -		return; -	cgit_print_age(s.st_mtime, -1, NULL); +	if (stat(path, &s) == 0) +		*mtime = s.st_mtime; +	else +		*mtime = 0; + +	r->mtime = *mtime; +	return (r->mtime != 0); +} + +static void print_modtime(struct cgit_repo *repo) +{ +	time_t t; +	if (get_repo_modtime(repo, &t)) +		cgit_print_age(t, -1, NULL);  }  int is_match(struct cgit_repo *repo) @@ -73,13 +90,23 @@ int is_in_url(struct cgit_repo *repo)  	return 0;  } +void print_sort_header(const char *title, const char *sort) +{ +	htmlf("<th class='left'><a href='./?s=%s", sort); +	if (ctx.qry.search) { +		html("&q="); +		html_url_arg(ctx.qry.search); +	} +	htmlf("'>%s</a></th>", title); +} +  void print_header(int columns)  { -	html("<tr class='nohover'>" -	     "<th class='left'>Name</th>" -	     "<th class='left'>Description</th>" -	     "<th class='left'>Owner</th>" -	     "<th class='left'>Idle</th>"); +	html("<tr class='nohover'>"); +	print_sort_header("Name", "name"); +	print_sort_header("Description", "desc"); +	print_sort_header("Owner", "owner"); +	print_sort_header("Idle", "idle");  	if (ctx.cfg.enable_index_links)  		html("<th class='left'>Links</th>");  	html("</tr>\n"); @@ -96,10 +123,86 @@ void print_pager(int items, int pagelen, char *search)  	html("</div>");  } +static int cmp(const char *s1, const char *s2) +{ +	if (s1 && s2) +		return strcmp(s1, s2); +	if (s1 && !s2) +		return -1; +	if (s2 && !s1) +		return 1; +	return 0; +} + +static int sort_name(const void *a, const void *b) +{ +	const struct cgit_repo *r1 = a; +	const struct cgit_repo *r2 = b; + +	return cmp(r1->name, r2->name); +} + +static int sort_desc(const void *a, const void *b) +{ +	const struct cgit_repo *r1 = a; +	const struct cgit_repo *r2 = b; + +	return cmp(r1->desc, r2->desc); +} + +static int sort_owner(const void *a, const void *b) +{ +	const struct cgit_repo *r1 = a; +	const struct cgit_repo *r2 = b; + +	return cmp(r1->owner, r2->owner); +} + +static int sort_idle(const void *a, const void *b) +{ +	const struct cgit_repo *r1 = a; +	const struct cgit_repo *r2 = b; +	time_t t1, t2; + +	t1 = t2 = 0; +	get_repo_modtime(r1, &t1); +	get_repo_modtime(r2, &t2); +	return t2 - t1; +} + +struct sortcolumn { +	const char *name; +	int (*fn)(const void *a, const void *b); +}; + +struct sortcolumn sortcolumn[] = { +	{"name", sort_name}, +	{"desc", sort_desc}, +	{"owner", sort_owner}, +	{"idle", sort_idle}, +	{NULL, NULL} +}; + +int sort_repolist(char *field) +{ +	struct sortcolumn *column; + +	for (column = &sortcolumn[0]; column->name; column++) { +		if (strcmp(field, column->name)) +			continue; +		qsort(cgit_repolist.repos, cgit_repolist.count, +			sizeof(struct cgit_repo), column->fn); +		return 1; +	} +	return 0; +} + +  void cgit_print_repolist()  {  	int i, columns = 4, hits = 0, header = 0;  	char *last_group = NULL; +	int sorted = 0;  	if (ctx.cfg.enable_index_links)  		columns++; @@ -112,6 +215,9 @@ void cgit_print_repolist()  	if (ctx.cfg.index_header)  		html_include(ctx.cfg.index_header); +	if(ctx.qry.sort) +		sorted = sort_repolist(ctx.qry.sort); +  	html("<table summary='repository list' class='list nowrap'>");  	for (i=0; i<cgit_repolist.count; i++) {  		ctx.repo = &cgit_repolist.repos[i]; @@ -124,10 +230,11 @@ void cgit_print_repolist()  			continue;  		if (!header++)  			print_header(columns); -		if ((last_group == NULL && ctx.repo->group != NULL) || +		if (!sorted && +		    ((last_group == NULL && ctx.repo->group != NULL) ||  		    (last_group != NULL && ctx.repo->group == NULL) ||  		    (last_group != NULL && ctx.repo->group != NULL && -		     strcmp(ctx.repo->group, last_group))) { +		     strcmp(ctx.repo->group, last_group)))) {  			htmlf("<tr class='nohover'><td colspan='%d' class='repogroup'>",  			      columns);  			html_txt(ctx.repo->group); @@ -135,7 +242,7 @@ void cgit_print_repolist()  			last_group = ctx.repo->group;  		}  		htmlf("<tr><td class='%s'>", -		      ctx.repo->group ? "sublevel-repo" : "toplevel-repo"); +		      !sorted && ctx.repo->group ? "sublevel-repo" : "toplevel-repo");  		cgit_summary_link(ctx.repo->name, ctx.repo->name, NULL, NULL);  		html("</td><td>");  		html_link_open(cgit_repourl(ctx.repo->url), NULL, NULL); @@ -150,7 +257,7 @@ void cgit_print_repolist()  			html("<td>");  			cgit_summary_link("summary", NULL, "button", NULL);  			cgit_log_link("log", NULL, "button", NULL, NULL, NULL, -				      0, NULL, NULL); +				      0, NULL, NULL, ctx.qry.showmsg);  			cgit_tree_link("tree", NULL, "button", NULL, NULL, NULL);  			html("</td>");  		} diff --git a/ui-shared.c b/ui-shared.c index 76cd00d..4f28512 100644 --- a/ui-shared.c +++ b/ui-shared.c @@ -281,7 +281,8 @@ void cgit_plain_link(char *name, char *title, char *class, char *head,  }  void cgit_log_link(char *name, char *title, char *class, char *head, -		   char *rev, char *path, int ofs, char *grep, char *pattern) +		   char *rev, char *path, int ofs, char *grep, char *pattern, +		   int showmsg)  {  	char *delim; @@ -305,6 +306,11 @@ void cgit_log_link(char *name, char *title, char *class, char *head,  		html(delim);  		html("ofs=");  		htmlf("%d", ofs); +		delim = "&"; +	} +	if (showmsg) { +		html(delim); +		html("showmsg=1");  	}  	html("'>");  	html_txt(name); @@ -363,13 +369,22 @@ void cgit_patch_link(char *name, char *title, char *class, char *head,  	reporevlink("patch", name, title, class, head, rev, NULL);  } +void cgit_stats_link(char *name, char *title, char *class, char *head, +		     char *path) +{ +	reporevlink("stats", name, title, class, head, NULL, path); +} +  void cgit_object_link(struct object *obj)  { -	char *page, *rev, *name; +	char *page, *shortrev, *fullrev, *name; +	fullrev = sha1_to_hex(obj->sha1); +	shortrev = xstrdup(fullrev); +	shortrev[10] = '\0';  	if (obj->type == OBJ_COMMIT) { -                cgit_commit_link(fmt("commit %s", sha1_to_hex(obj->sha1)), NULL, NULL, -				 ctx.qry.head, sha1_to_hex(obj->sha1)); +                cgit_commit_link(fmt("commit %s...", shortrev), NULL, NULL, +				 ctx.qry.head, fullrev);  		return;  	} else if (obj->type == OBJ_TREE)  		page = "tree"; @@ -377,9 +392,8 @@ void cgit_object_link(struct object *obj)  		page = "tag";  	else  		page = "blob"; -	rev = sha1_to_hex(obj->sha1); -	name = fmt("%s %s", typename(obj->type), rev); -	reporevlink(page, name, NULL, NULL, ctx.qry.head, rev, NULL); +	name = fmt("%s %s...", typename(obj->type), shortrev); +	reporevlink(page, name, NULL, NULL, ctx.qry.head, fullrev, NULL);  }  void cgit_print_date(time_t secs, char *format, int local_time) @@ -549,7 +563,7 @@ int print_archive_ref(const char *refname, const unsigned char *sha1,  	return 0;  } -void add_hidden_formfields(int incl_head, int incl_search, char *page) +void cgit_add_hidden_formfields(int incl_head, int incl_search, char *page)  {  	char *url; @@ -568,6 +582,8 @@ void add_hidden_formfields(int incl_head, int incl_search, char *page)  		html_hidden("id", ctx.qry.sha1);  	if (ctx.qry.sha2)  		html_hidden("id2", ctx.qry.sha2); +	if (ctx.qry.showmsg) +		html_hidden("showmsg", "1");  	if (incl_search) {  		if (ctx.qry.grep) @@ -609,7 +625,7 @@ void cgit_print_pageheader(struct cgit_context *ctx)  		cgit_summary_link(ctx->repo->name, ctx->repo->name, NULL, NULL);  		html("</td><td class='form'>");  		html("<form method='get' action=''>\n"); -		add_hidden_formfields(0, 1, ctx->qry.page); +		cgit_add_hidden_formfields(0, 1, ctx->qry.page);  		html("<select name='h' onchange='this.form.submit();'>\n");  		for_each_branch_ref(print_branch_option, ctx->qry.head);  		html("</select> "); @@ -639,13 +655,16 @@ void cgit_print_pageheader(struct cgit_context *ctx)  		cgit_refs_link("refs", NULL, hc(cmd, "refs"), ctx->qry.head,  			       ctx->qry.sha1, NULL);  		cgit_log_link("log", NULL, hc(cmd, "log"), ctx->qry.head, -			      NULL, NULL, 0, NULL, NULL); +			      NULL, NULL, 0, NULL, NULL, ctx->qry.showmsg);  		cgit_tree_link("tree", NULL, hc(cmd, "tree"), ctx->qry.head,  			       ctx->qry.sha1, NULL);  		cgit_commit_link("commit", NULL, hc(cmd, "commit"),  				 ctx->qry.head, ctx->qry.sha1);  		cgit_diff_link("diff", NULL, hc(cmd, "diff"), ctx->qry.head,  			       ctx->qry.sha1, ctx->qry.sha2, NULL); +		if (ctx->repo->max_stats) +			cgit_stats_link("stats", NULL, hc(cmd, "stats"), +					ctx->qry.head, NULL);  		if (ctx->repo->readme)  			reporevlink("about", "about", NULL,  				    hc(cmd, "about"), ctx->qry.head, NULL, @@ -656,7 +675,7 @@ void cgit_print_pageheader(struct cgit_context *ctx)  			html_url_path(cgit_fileurl(ctx->qry.repo, "log",  						   ctx->qry.path, NULL));  		html("'>\n"); -		add_hidden_formfields(1, 0, "log"); +		cgit_add_hidden_formfields(1, 0, "log");  		html("<select name='qt'>\n");  		html_option("grep", "log msg", ctx->qry.grep);  		html_option("author", "author", ctx->qry.grep); @@ -712,8 +731,7 @@ void cgit_print_snapshot_links(const char *repo, const char *head,  			continue;  		filename = fmt("%s-%s%s", cgit_repobasename(repo), hex,  			       f->suffix); -		cgit_snapshot_link(filename, NULL, NULL, (char *)head, -				   (char *)hex, filename); +		cgit_snapshot_link(filename, NULL, NULL, NULL, NULL, filename);  		html("<br/>");  	}  } diff --git a/ui-shared.h b/ui-shared.h index 3c8a6d0..5a3821f 100644 --- a/ui-shared.h +++ b/ui-shared.h @@ -19,7 +19,7 @@ extern void cgit_plain_link(char *name, char *title, char *class, char *head,  			    char *rev, char *path);  extern void cgit_log_link(char *name, char *title, char *class, char *head,  			  char *rev, char *path, int ofs, char *grep, -			  char *pattern); +			  char *pattern, int showmsg);  extern void cgit_commit_link(char *name, char *title, char *class, char *head,  			     char *rev);  extern void cgit_patch_link(char *name, char *title, char *class, char *head, @@ -30,6 +30,8 @@ extern void cgit_snapshot_link(char *name, char *title, char *class,  			       char *head, char *rev, char *archivename);  extern void cgit_diff_link(char *name, char *title, char *class, char *head,  			   char *new_rev, char *old_rev, char *path); +extern void cgit_stats_link(char *name, char *title, char *class, char *head, +			    char *path);  extern void cgit_object_link(struct object *obj);  extern void cgit_print_error(char *msg); @@ -42,5 +44,6 @@ extern void cgit_print_pageheader(struct cgit_context *ctx);  extern void cgit_print_filemode(unsigned short mode);  extern void cgit_print_snapshot_links(const char *repo, const char *head,  				      const char *hex, int snapshots); - +extern void cgit_add_hidden_formfields(int incl_head, int incl_search, +				       char *page);  #endif /* UI_SHARED_H */ diff --git a/ui-snapshot.c b/ui-snapshot.c index 9c4d086..f25613e 100644 --- a/ui-snapshot.c +++ b/ui-snapshot.c @@ -58,8 +58,8 @@ static int write_tar_bzip2_archive(struct archiver_args *args)  const struct cgit_snapshot_format cgit_snapshot_formats[] = {  	{ ".zip", "application/x-zip", write_zip_archive, 0x1 }, -	{ ".tar.gz", "application/x-tar", write_tar_gzip_archive, 0x2 }, -	{ ".tar.bz2", "application/x-tar", write_tar_bzip2_archive, 0x4 }, +	{ ".tar.gz", "application/x-gzip", write_tar_gzip_archive, 0x2 }, +	{ ".tar.bz2", "application/x-bzip2", write_tar_bzip2_archive, 0x4 },  	{ ".tar", "application/x-tar", write_tar_archive, 0x8 },  	{}  }; @@ -114,58 +114,53 @@ static int make_snapshot(const struct cgit_snapshot_format *format,  	return 0;  } -char *dwim_filename = NULL; -const char *dwim_refname = NULL; - -static int ref_cb(const char *refname, const unsigned char *sha1, int flags, -		  void *cb_data) -{ -	const char *r = refname; -	while (r && *r) { -		fprintf(stderr, "  cmp %s with %s:", dwim_filename, r); -		if (!strcmp(dwim_filename, r)) { -			fprintf(stderr, "MATCH!\n"); -			dwim_refname = refname; -			return 1; -		} -		fprintf(stderr, "no match\n"); -		if (isdigit(*r)) -			break; -		r++; -	} -	return 0; -} - -/* Try to guess the requested revision by combining repo name and tag name - * and comparing this to the requested snapshot name. E.g. the requested - * snapshot is "cgit-0.7.2.tar.gz" while repo name is "cgit" and tag name - * is "v0.7.2". First, the reponame is stripped off, leaving "-0.7.2.tar.gz". - * Next, any '-' and '_' characters are stripped, leaving "0.7.2.tar.gz". - * Finally, the requested format suffix is removed and we end up with "0.7.2". - * Then we test each tag against this dwimmed filename, and for each tag - * we even try to remove any leading characters which are non-digits. I.e. - * we first compare with "v0.7.2", then with "0.7.2" and we've got a match. +/* Try to guess the requested revision from the requested snapshot name. + * First the format extension is stripped, e.g. "cgit-0.7.2.tar.gz" become + * "cgit-0.7.2". If this is a valid commit object name we've got a winner. + * Otherwise, if the snapshot name has a prefix matching the result from + * repo_basename(), we strip the basename and any following '-' and '_' + * characters ("cgit-0.7.2" -> "0.7.2") and check the resulting name once + * more. If this still isn't a valid commit object name, we check if pre- + * pending a 'v' to the remaining snapshot name ("0.7.2" -> "v0.7.2") gives + * us something valid.   */  static const char *get_ref_from_filename(const char *url, const char *filename, -					 const struct cgit_snapshot_format *fmt) +					 const struct cgit_snapshot_format *format)  { -	const char *reponame = cgit_repobasename(url); -	fprintf(stderr, "reponame=%s, filename=%s\n", reponame, filename); -	if (prefixcmp(filename, reponame)) -		return NULL; -	filename += strlen(reponame); -	while (filename && (*filename == '-' || *filename == '_')) -		filename++; -	dwim_filename = xstrdup(filename); -	dwim_filename[strlen(filename) - strlen(fmt->suffix)] = '\0'; -	for_each_tag_ref(ref_cb, NULL); -	return dwim_refname; +	const char *reponame; +	unsigned char sha1[20]; +	char *snapshot; + +	snapshot = xstrdup(filename); +	snapshot[strlen(snapshot) - strlen(format->suffix)] = '\0'; +	fprintf(stderr, "snapshot=%s\n", snapshot); + +	if (get_sha1(snapshot, sha1) == 0) +		return snapshot; + +	reponame = cgit_repobasename(url); +	fprintf(stderr, "reponame=%s\n", reponame); +	if (prefixcmp(snapshot, reponame) == 0) { +		snapshot += strlen(reponame); +		while (snapshot && (*snapshot == '-' || *snapshot == '_')) +			snapshot++; +	} + +	if (get_sha1(snapshot, sha1) == 0) +		return snapshot; + +	snapshot = fmt("v%s", snapshot); +	if (get_sha1(snapshot, sha1) == 0) +		return snapshot; + +	return NULL;  } -void cgit_print_snapshot(const char *head, const char *hex, const char *prefix, +void cgit_print_snapshot(const char *head, const char *hex,  			 const char *filename, int snapshots, int dwim)  {  	const struct cgit_snapshot_format* f; +	char *prefix = NULL;  	f = get_format(filename);  	if (!f) { @@ -178,11 +173,22 @@ void cgit_print_snapshot(const char *head, const char *hex, const char *prefix,  		return;  	} -	if (!hex && dwim) +	if (!hex && dwim) {  		hex = get_ref_from_filename(ctx.repo->url, filename, f); +		if (hex == NULL) { +			html_status(404, "Not found", 0); +			return; +		} +		prefix = xstrdup(filename); +		prefix[strlen(filename) - strlen(f->suffix)] = '\0'; +	}  	if (!hex)  		hex = head; +	if (!prefix) +		prefix = xstrdup(cgit_repobasename(ctx.repo->url)); +  	make_snapshot(f, hex, prefix, filename); +	free(prefix);  } diff --git a/ui-snapshot.h b/ui-snapshot.h index 3540303..b6ede52 100644 --- a/ui-snapshot.h +++ b/ui-snapshot.h @@ -2,7 +2,6 @@  #define UI_SNAPSHOT_H  extern void cgit_print_snapshot(const char *head, const char *hex, -				const char *prefix, const char *filename, -				int snapshot, int dwim); +				const char *filename, int snapshot, int dwim);  #endif /* UI_SNAPSHOT_H */ diff --git a/ui-stats.c b/ui-stats.c new file mode 100644 index 0000000..9fc06d3 --- /dev/null +++ b/ui-stats.c @@ -0,0 +1,410 @@ +#include <string-list.h> + +#include "cgit.h" +#include "html.h" +#include "ui-shared.h" +#include "ui-stats.h" + +#define MONTHS 6 + +struct authorstat { +	long total; +	struct string_list list; +}; + +#define DAY_SECS (60 * 60 * 24) +#define WEEK_SECS (DAY_SECS * 7) + +static void trunc_week(struct tm *tm) +{ +	time_t t = timegm(tm); +	t -= ((tm->tm_wday + 6) % 7) * DAY_SECS; +	gmtime_r(&t, tm);	 +} + +static void dec_week(struct tm *tm) +{ +	time_t t = timegm(tm); +	t -= WEEK_SECS; +	gmtime_r(&t, tm);	 +} + +static void inc_week(struct tm *tm) +{ +	time_t t = timegm(tm); +	t += WEEK_SECS; +	gmtime_r(&t, tm);	 +} + +static char *pretty_week(struct tm *tm) +{ +	static char buf[10]; + +	strftime(buf, sizeof(buf), "W%V %G", tm); +	return buf; +} + +static void trunc_month(struct tm *tm) +{ +	tm->tm_mday = 1; +} + +static void dec_month(struct tm *tm) +{ +	tm->tm_mon--; +	if (tm->tm_mon < 0) { +		tm->tm_year--; +		tm->tm_mon = 11; +	} +} + +static void inc_month(struct tm *tm) +{ +	tm->tm_mon++; +	if (tm->tm_mon > 11) { +		tm->tm_year++; +		tm->tm_mon = 0; +	} +} + +static char *pretty_month(struct tm *tm) +{ +	static const char *months[] = { +		"Jan", "Feb", "Mar", "Apr", "May", "Jun", +		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" +	}; +	return fmt("%s %d", months[tm->tm_mon], tm->tm_year + 1900); +} + +static void trunc_quarter(struct tm *tm) +{ +	trunc_month(tm); +	while(tm->tm_mon % 3 != 0) +		dec_month(tm); +} + +static void dec_quarter(struct tm *tm) +{ +	dec_month(tm); +	dec_month(tm); +	dec_month(tm); +} + +static void inc_quarter(struct tm *tm) +{ +	inc_month(tm); +	inc_month(tm); +	inc_month(tm); +} + +static char *pretty_quarter(struct tm *tm) +{ +	return fmt("Q%d %d", tm->tm_mon / 3 + 1, tm->tm_year + 1900); +} + +static void trunc_year(struct tm *tm) +{ +	trunc_month(tm); +	tm->tm_mon = 0; +} + +static void dec_year(struct tm *tm) +{ +	tm->tm_year--; +} + +static void inc_year(struct tm *tm) +{ +	tm->tm_year++; +} + +static char *pretty_year(struct tm *tm) +{ +	return fmt("%d", tm->tm_year + 1900); +} + +struct cgit_period periods[] = { +	{'w', "week", 12, 4, trunc_week, dec_week, inc_week, pretty_week}, +	{'m', "month", 12, 4, trunc_month, dec_month, inc_month, pretty_month}, +	{'q', "quarter", 12, 4, trunc_quarter, dec_quarter, inc_quarter, pretty_quarter}, +	{'y', "year", 12, 4, trunc_year, dec_year, inc_year, pretty_year}, +}; + +/* Given a period code or name, return a period index (1, 2, 3 or 4) + * and update the period pointer to the correcsponding struct. + * If no matching code is found, return 0. + */ +int cgit_find_stats_period(const char *expr, struct cgit_period **period) +{ +	int i; +	char code = '\0'; + +	if (!expr) +		return 0; + +	if (strlen(expr) == 1) +		code = expr[0]; + +	for (i = 0; i < sizeof(periods) / sizeof(periods[0]); i++) +		if (periods[i].code == code || !strcmp(periods[i].name, expr)) { +			if (period) +				*period = &periods[i]; +			return i+1; +		} +	return 0; +} + +static void add_commit(struct string_list *authors, struct commit *commit, +	struct cgit_period *period) +{ +	struct commitinfo *info; +	struct string_list_item *author, *item; +	struct authorstat *authorstat; +	struct string_list *items; +	char *tmp; +	struct tm *date; +	time_t t; + +	info = cgit_parse_commit(commit); +	tmp = xstrdup(info->author); +	author = string_list_insert(tmp, authors); +	if (!author->util) +		author->util = xcalloc(1, sizeof(struct authorstat)); +	else +		free(tmp); +	authorstat = author->util; +	items = &authorstat->list; +	t = info->committer_date; +	date = gmtime(&t); +	period->trunc(date); +	tmp = xstrdup(period->pretty(date)); +	item = string_list_insert(tmp, items); +	if (item->util) +		free(tmp); +	item->util++; +	authorstat->total++; +	cgit_free_commitinfo(info); +} + +static int cmp_total_commits(const void *a1, const void *a2) +{ +	const struct string_list_item *i1 = a1; +	const struct string_list_item *i2 = a2; +	const struct authorstat *auth1 = i1->util; +	const struct authorstat *auth2 = i2->util; + +	return auth2->total - auth1->total; +} + +/* Walk the commit DAG and collect number of commits per author per + * timeperiod into a nested string_list collection. + */ +struct string_list collect_stats(struct cgit_context *ctx, +	struct cgit_period *period) +{ +	struct string_list authors; +	struct rev_info rev; +	struct commit *commit; +	const char *argv[] = {NULL, ctx->qry.head, NULL, NULL, NULL, NULL}; +	int argc = 3; +	time_t now; +	long i; +	struct tm *tm; +	char tmp[11]; + +	time(&now); +	tm = gmtime(&now); +	period->trunc(tm); +	for (i = 1; i < period->count; i++) +		period->dec(tm); +	strftime(tmp, sizeof(tmp), "%Y-%m-%d", tm); +	argv[2] = xstrdup(fmt("--since=%s", tmp)); +	if (ctx->qry.path) { +		argv[3] = "--"; +		argv[4] = ctx->qry.path; +		argc += 2; +	} +	init_revisions(&rev, NULL); +	rev.abbrev = DEFAULT_ABBREV; +	rev.commit_format = CMIT_FMT_DEFAULT; +	rev.no_merges = 1; +	rev.verbose_header = 1; +	rev.show_root_diff = 0; +	setup_revisions(argc, argv, &rev, NULL); +	prepare_revision_walk(&rev); +	memset(&authors, 0, sizeof(authors)); +	while ((commit = get_revision(&rev)) != NULL) { +		add_commit(&authors, commit, period); +		free(commit->buffer); +		free_commit_list(commit->parents); +	} +	return authors; +} + +void print_combined_authorrow(struct string_list *authors, int from, int to, +	const char *name, const char *leftclass, const char *centerclass, +	const char *rightclass, struct cgit_period *period) +{ +	struct string_list_item *author; +	struct authorstat *authorstat; +	struct string_list *items; +	struct string_list_item *date; +	time_t now; +	long i, j, total, subtotal; +	struct tm *tm; +	char *tmp; + +	time(&now); +	tm = gmtime(&now); +	period->trunc(tm); +	for (i = 1; i < period->count; i++) +		period->dec(tm); + +	total = 0; +	htmlf("<tr><td class='%s'>%s</td>", leftclass, +		fmt(name, to - from + 1)); +	for (j = 0; j < period->count; j++) { +		tmp = period->pretty(tm); +		period->inc(tm); +		subtotal = 0; +		for (i = from; i <= to; i++) { +			author = &authors->items[i]; +			authorstat = author->util; +			items = &authorstat->list; +			date = string_list_lookup(tmp, items); +			if (date) +				subtotal += (size_t)date->util; +		} +		htmlf("<td class='%s'>%d</td>", centerclass, subtotal); +		total += subtotal; +	} +	htmlf("<td class='%s'>%d</td></tr>", rightclass, total); +} + +void print_authors(struct string_list *authors, int top, +		   struct cgit_period *period) +{ +	struct string_list_item *author; +	struct authorstat *authorstat; +	struct string_list *items; +	struct string_list_item *date; +	time_t now; +	long i, j, total; +	struct tm *tm; +	char *tmp; + +	time(&now); +	tm = gmtime(&now); +	period->trunc(tm); +	for (i = 1; i < period->count; i++) +		period->dec(tm); + +	html("<table class='stats'><tr><th>Author</th>"); +	for (j = 0; j < period->count; j++) { +		tmp = period->pretty(tm); +		htmlf("<th>%s</th>", tmp); +		period->inc(tm); +	} +	html("<th>Total</th></tr>\n"); + +	if (top <= 0 || top > authors->nr) +		top = authors->nr; + +	for (i = 0; i < top; i++) { +		author = &authors->items[i]; +		html("<tr><td class='left'>"); +		html_txt(author->string); +		html("</td>"); +		authorstat = author->util; +		items = &authorstat->list; +		total = 0; +		for (j = 0; j < period->count; j++) +			period->dec(tm); +		for (j = 0; j < period->count; j++) { +			tmp = period->pretty(tm); +			period->inc(tm); +			date = string_list_lookup(tmp, items); +			if (!date) +				html("<td>0</td>"); +			else { +				htmlf("<td>%d</td>", date->util); +				total += (size_t)date->util; +			} +		} +		htmlf("<td class='sum'>%d</td></tr>", total); +	} + +	if (top < authors->nr) +		print_combined_authorrow(authors, top, authors->nr - 1, +			"Others (%d)", "left", "", "sum", period); + +	print_combined_authorrow(authors, 0, authors->nr - 1, "Total", +		"total", "sum", "sum", period); +	html("</table>"); +} + +/* Create a sorted string_list with one entry per author. The util-field + * for each author is another string_list which is used to calculate the + * number of commits per time-interval. + */ +void cgit_show_stats(struct cgit_context *ctx) +{ +	struct string_list authors; +	struct cgit_period *period; +	int top, i; +	const char *code = "w"; + +	if (ctx->qry.period) +		code = ctx->qry.period; + +	i = cgit_find_stats_period(code, &period); +	if (!i) { +		cgit_print_error(fmt("Unknown statistics type: %c", code)); +		return; +	} +	if (i > ctx->repo->max_stats) { +		cgit_print_error(fmt("Statistics type disabled: %s", +				     period->name)); +		return; +	} +	authors = collect_stats(ctx, period); +	qsort(authors.items, authors.nr, sizeof(struct string_list_item), +		cmp_total_commits); + +	top = ctx->qry.ofs; +	if (!top) +		top = 10; +	htmlf("<h2>Commits per author per %s", period->name); +	if (ctx->qry.path) { +		html(" (path '"); +		html_txt(ctx->qry.path); +		html("')"); +	} +	html("</h2>"); + +	html("<form method='get' action='' style='float: right; text-align: right;'>"); +	cgit_add_hidden_formfields(1, 0, "stats"); +	if (ctx->repo->max_stats > 1) { +		html("Period: "); +		html("<select name='period' onchange='this.form.submit();'>"); +		for (i = 0; i < ctx->repo->max_stats; i++) +			htmlf("<option value='%c'%s>%s</option>", +				periods[i].code, +				period == &periods[i] ? " selected" : "", +				periods[i].name); +		html("</select><br/><br/>"); +	} +	html("Authors: "); +	html(""); +	html("<select name='ofs' onchange='this.form.submit();'>"); +	htmlf("<option value='10'%s>10</option>", top == 10 ? " selected" : ""); +	htmlf("<option value='25'%s>25</option>", top == 25 ? " selected" : ""); +	htmlf("<option value='50'%s>50</option>", top == 50 ? " selected" : ""); +	htmlf("<option value='100'%s>100</option>", top == 100 ? " selected" : ""); +	htmlf("<option value='-1'%s>All</option>", top == -1 ? " selected" : ""); +	html("</select>"); +	html("<noscript>  <input type='submit' value='Reload'/></noscript>"); +	html("</form>"); +	print_authors(&authors, top, period); +} + diff --git a/ui-stats.h b/ui-stats.h new file mode 100644 index 0000000..4f13dba --- /dev/null +++ b/ui-stats.h @@ -0,0 +1,27 @@ +#ifndef UI_STATS_H +#define UI_STATS_H + +#include "cgit.h" + +struct cgit_period { +	const char code; +	const char *name; +	int max_periods; +	int count; + +	/* Convert a tm value to the first day in the period */ +	void (*trunc)(struct tm *tm); + +	/* Update tm value to start of next/previous period */ +	void (*dec)(struct tm *tm); +	void (*inc)(struct tm *tm); + +	/* Pretty-print a tm value */ +	char *(*pretty)(struct tm *tm); +}; + +extern int cgit_find_stats_period(const char *expr, struct cgit_period **period); + +extern void cgit_show_stats(struct cgit_context *ctx); + +#endif /* UI_STATS_H */ @@ -75,6 +75,15 @@ void cgit_print_tag(char *revname)  		html("</td></tr>\n");  		html("</table>\n");  		print_tag_content(info->msg); -	} +	} else { +		html("<table class='commit-info'>\n"); +		htmlf("<tr><td>Tag name</td><td>"); +		html_txt(revname); +		html("</td></tr>\n"); +		html("<tr><td>Tagged object</td><td>"); +		cgit_object_link(obj); +		html("</td></tr>\n"); +		html("</table>\n"); +        }  	return;  } @@ -18,8 +18,8 @@ static void print_object(const unsigned char *sha1, char *path)  {  	enum object_type type;  	char *buf; -	unsigned long size, lineno, start, idx; -	const char *linefmt = "<tr><td class='no'><a id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a></td><td class='txt'>"; +	unsigned long size, lineno, idx; +	const char *numberfmt = "<a class='no' id='n%1$d' name='n%1$d' href='#n%1$d'>%1$d</a>\n";  	type = sha1_object_info(sha1, &size);  	if (type == OBJ_BAD) { @@ -38,27 +38,28 @@ static void print_object(const unsigned char *sha1, char *path)  	html(" (");  	cgit_plain_link("plain", NULL, NULL, ctx.qry.head,  		        curr_rev, path); -	htmlf(")<br/>blob: %s", sha1_to_hex(sha1)); +	htmlf(")<br/>blob: %s\n", sha1_to_hex(sha1));  	html("<table summary='blob content' class='blob'>\n"); +	html("<tr>\n"); + +	html("<td class='linenumbers'><pre>");  	idx = 0; -	start = 0;  	lineno = 0; -	while(idx < size) { +	htmlf(numberfmt, ++lineno); +	while(idx < size - 1) { // skip absolute last newline  		if (buf[idx] == '\n') { -			buf[idx] = '\0'; -			htmlf(linefmt, ++lineno); -			html_txt(buf + start); -			html("</td></tr>\n"); -			start = idx + 1; +			htmlf(numberfmt, ++lineno);  		}  		idx++;  	} -	if (start < idx) { -		htmlf(linefmt, ++lineno); -		html_txt(buf + start); -	} -	html("</td></tr>\n"); +	html("</pre></td>\n"); + +	html("<td class='lines'><pre><code>"); +	html_txt(buf); +	html("</code></pre></td>\n"); + +	html("</tr>\n");  	html("</table>\n");  } @@ -108,7 +109,10 @@ static int ls_item(const unsigned char *sha1, const char *base, int baselen,  	html("<td>");  	cgit_log_link("log", NULL, "button", ctx.qry.head, curr_rev, -		      fullpath, 0, NULL, NULL); +		      fullpath, 0, NULL, NULL, ctx.qry.showmsg); +	if (ctx.repo->max_stats) +		cgit_stats_link("stats", NULL, "button", ctx.qry.head, +				fullpath);  	html("</td></tr>\n");  	free(name);  	return 0; | 
