blob: 57206ddff1518b3a757df6c6fc390797fc34a410 [file] [log] [blame]
Lars Hjemli14180342006-12-11 16:55:07 +01001/* ui-summary.c: functions for generating repo summary page
2 *
3 * Copyright (C) 2006 Lars Hjemli
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +02004 * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
Lars Hjemli14180342006-12-11 16:55:07 +01005 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
10#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010011#include "ui-summary.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010012#include "html.h"
Lars Hjemlic5984a92008-03-24 16:38:47 +010013#include "ui-log.h"
14#include "ui-refs.h"
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020015#include "ui-blob.h"
Lars Hjemli14180342006-12-11 16:55:07 +010016
Lars Hjemli78af25c2008-07-27 11:54:06 +020017static void print_url(char *base, char *suffix)
18{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010019 int columns = 3;
John Keepingfb3655d2013-04-06 10:28:57 +010020 struct strbuf basebuf = STRBUF_INIT;
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010021
22 if (ctx.repo->enable_log_filecount)
23 columns++;
24 if (ctx.repo->enable_log_linecount)
25 columns++;
26
Lars Hjemli78af25c2008-07-27 11:54:06 +020027 if (!base || !*base)
28 return;
John Keepingfb3655d2013-04-06 10:28:57 +010029 if (suffix && *suffix) {
30 strbuf_addf(&basebuf, "%s/%s", base, suffix);
31 base = basebuf.buf;
32 }
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010033 htmlf("<tr><td colspan='%d'><a href='", columns);
Lars Hjemli0071aa82008-10-05 21:21:42 +020034 html_url_path(base);
Lars Hjemli78af25c2008-07-27 11:54:06 +020035 html("'>");
36 html_txt(base);
37 html("</a></td></tr>\n");
John Keepingfb3655d2013-04-06 10:28:57 +010038 strbuf_release(&basebuf);
Lars Hjemli78af25c2008-07-27 11:54:06 +020039}
40
41static void print_urls(char *txt, char *suffix)
42{
43 char *h = txt, *t, c;
Lukas Fleischer977a3ad2013-03-05 16:48:27 +010044 int urls = 0;
45 int columns = 3;
46
47 if (ctx.repo->enable_log_filecount)
48 columns++;
49 if (ctx.repo->enable_log_linecount)
50 columns++;
51
Lars Hjemli78af25c2008-07-27 11:54:06 +020052
53 while (h && *h) {
54 while (h && *h == ' ')
55 h++;
Lukas Fleischer977a3ad2013-03-05 16:48:27 +010056 if (!*h)
57 break;
Lars Hjemli78af25c2008-07-27 11:54:06 +020058 t = h;
59 while (t && *t && *t != ' ')
60 t++;
61 c = *t;
62 *t = 0;
Lukas Fleischer977a3ad2013-03-05 16:48:27 +010063 if (urls++ == 0) {
64 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
65 htmlf("<tr><th class='left' colspan='%d'>Clone</th></tr>\n", columns);
66 }
Lars Hjemli78af25c2008-07-27 11:54:06 +020067 print_url(h, suffix);
68 *t = c;
69 h = t;
70 }
71}
72
Lars Hjemlid14c5f62006-12-11 17:04:19 +010073void cgit_print_summary()
Lars Hjemli14180342006-12-11 16:55:07 +010074{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010075 int columns = 3;
76
77 if (ctx.repo->enable_log_filecount)
78 columns++;
79 if (ctx.repo->enable_log_linecount)
80 columns++;
81
Lars Hjemli29154832007-11-11 13:04:28 +010082 html("<table summary='repository info' class='list nowrap'>");
Lars Hjemlib228d4f2008-02-16 13:07:13 +010083 cgit_print_branches(ctx.cfg.summary_branches);
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010084 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
Lars Hjemlib228d4f2008-02-16 13:07:13 +010085 cgit_print_tags(ctx.cfg.summary_tags);
Lars Hjemli5764fe92008-04-14 22:13:38 +020086 if (ctx.cfg.summary_log > 0) {
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010087 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
Lars Hjemli5764fe92008-04-14 22:13:38 +020088 cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL,
Tobias Bieniek792f8132012-10-13 16:10:30 +020089 NULL, NULL, 0, 0, 0);
Lars Hjemli5764fe92008-04-14 22:13:38 +020090 }
Lars Hjemli78af25c2008-07-27 11:54:06 +020091 if (ctx.repo->clone_url)
Lars Hjemlia1429db2011-06-06 20:49:13 +000092 print_urls(expand_macros(ctx.repo->clone_url), NULL);
Lars Hjemli78af25c2008-07-27 11:54:06 +020093 else if (ctx.cfg.clone_prefix)
94 print_urls(ctx.cfg.clone_prefix, ctx.repo->url);
Lars Hjemli23734c52007-01-17 01:10:39 +010095 html("</table>");
Lars Hjemli14180342006-12-11 16:55:07 +010096}
Lars Hjemli90f64ad2008-04-28 23:06:57 +020097
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020098/* The caller must free filename and ref after calling this. */
99void cgit_parse_readme(const char *readme, const char *path, char **filename, char **ref, struct cgit_repo *repo)
Lars Hjemli90f64ad2008-04-28 23:06:57 +0200100{
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200101 const char *slash, *colon;
Jason A. Donenfeldfe36f842013-05-25 19:47:15 +0200102 char *resolved_base, *resolved_full;
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200103
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200104 *filename = NULL;
105 *ref = NULL;
106
107 if (!readme || !(*readme))
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200108 return;
109
Lars Hjemli515edb02010-08-21 15:08:01 +0200110 /* Check if the readme is tracked in the git repo. */
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200111 colon = strchr(readme, ':');
Lars Hjemli515edb02010-08-21 15:08:01 +0200112 if (colon && strlen(colon) > 1) {
Jason A. Donenfeld3cb5d862013-05-25 14:19:10 +0200113 /* If it starts with a colon, we want to use
114 * the default branch */
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200115 if (colon == readme && repo->defbranch)
116 *ref = xstrdup(repo->defbranch);
Jason A. Donenfeld3cb5d862013-05-25 14:19:10 +0200117 else
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200118 *ref = xstrndup(readme, colon - readme);
119 readme = colon + 1;
Lars Hjemli515edb02010-08-21 15:08:01 +0200120 }
121
122 /* Prepend repo path to relative readme path unless tracked. */
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200123 if (!(*ref) && *readme != '/')
124 readme = fmtalloc("%s/%s", repo->path, readme);
Lars Hjemli515edb02010-08-21 15:08:01 +0200125
126 /* If a subpath is specified for the about page, make it relative
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200127 * to the directory containing the configured readme. */
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200128 if (path) {
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200129 slash = strrchr(readme, '/');
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200130 if (!slash) {
Lars Hjemli515edb02010-08-21 15:08:01 +0200131 if (!colon)
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200132 return;
Lars Hjemli515edb02010-08-21 15:08:01 +0200133 slash = colon;
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200134 }
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200135 *filename = xmalloc(slash - readme + 1 + strlen(path) + 1);
136 strncpy(*filename, readme, slash - readme + 1);
Jason A. Donenfeldfe36f842013-05-25 19:47:15 +0200137 if (!(*ref))
138 resolved_base = realpath(*filename, NULL);
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200139 strcpy(*filename + (slash - readme + 1), path);
Jason A. Donenfeldfe36f842013-05-25 19:47:15 +0200140 if (!(*ref))
141 resolved_full = realpath(*filename, NULL);
142 if (!(*ref) && (!resolved_base || !resolved_full || strstr(resolved_full, resolved_base) != resolved_full)) {
143 free(*filename);
144 *filename = NULL;
145 }
146 if (!(*ref)) {
147 free(resolved_base);
148 free(resolved_full);
149 }
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200150 } else
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200151 *filename = xstrdup(readme);
152}
153
154void cgit_print_repo_readme(char *path)
155{
156 char *filename, *ref;
157 cgit_parse_readme(ctx.repo->readme, path, &filename, &ref, ctx.repo);
Lars Hjemli515edb02010-08-21 15:08:01 +0200158
Jason A. Donenfeldfe36f842013-05-25 19:47:15 +0200159 if (!filename)
160 return;
161
Lars Hjemli515edb02010-08-21 15:08:01 +0200162 /* Print the calculated readme, either from the git repo or from the
163 * filesystem, while applying the about-filter.
164 */
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200165 html("<div id='summary'>");
Jason A. Donenfeldc0dfaf12013-05-25 14:50:19 +0200166 if (ctx.repo->about_filter) {
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200167 ctx.repo->about_filter->argv[1] = filename;
Lars Hjemli3ec6b302011-06-06 19:29:58 +0000168 cgit_open_filter(ctx.repo->about_filter);
Jason A. Donenfeldc0dfaf12013-05-25 14:50:19 +0200169 }
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200170 if (ref)
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200171 cgit_print_file(filename, ref);
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200172 else
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200173 html_include(filename);
Jason A. Donenfeldc0dfaf12013-05-25 14:50:19 +0200174 if (ctx.repo->about_filter) {
Lars Hjemli537c05f2009-08-09 13:27:21 +0200175 cgit_close_filter(ctx.repo->about_filter);
Jason A. Donenfeldc0dfaf12013-05-25 14:50:19 +0200176 ctx.repo->about_filter->argv[1] = NULL;
177 }
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200178 html("</div>");
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200179 free(filename);
180 free(ref);
Lars Hjemli90f64ad2008-04-28 23:06:57 +0200181}