blob: 8e81ac4e7961558a63ba20ecad55d2e098c3e815 [file] [log] [blame]
Lars Hjemli14180342006-12-11 16:55:07 +01001/* ui-summary.c: functions for generating repo summary page
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemli14180342006-12-11 16:55:07 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-summary.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020012#include "ui-blob.h"
Christian Hesse790d2492015-08-16 14:53:53 +020013#include "ui-log.h"
14#include "ui-plain.h"
15#include "ui-refs.h"
John Keepingbbfa0062014-08-01 22:14:17 +010016#include "ui-shared.h"
Lars Hjemli14180342006-12-11 16:55:07 +010017
John Keepingbbfa0062014-08-01 22:14:17 +010018static int urls;
19
20static void print_url(const char *url)
Lars Hjemli78af25c2008-07-27 11:54:06 +020021{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010022 int columns = 3;
23
24 if (ctx.repo->enable_log_filecount)
25 columns++;
26 if (ctx.repo->enable_log_linecount)
27 columns++;
28
John Keepingbbfa0062014-08-01 22:14:17 +010029 if (urls++ == 0) {
30 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
Jason A. Donenfeldbd248322015-03-05 02:18:42 +010031 htmlf("<tr class='nohover'><th class='left' colspan='%d'>Clone</th></tr>\n", columns);
John Keepingfb3655d2013-04-06 10:28:57 +010032 }
John Keepingbbfa0062014-08-01 22:14:17 +010033
John Keepingd31be4c2014-08-01 22:14:18 +010034 htmlf("<tr><td colspan='%d'><a rel='vcs-git' href='", columns);
John Keepingbbfa0062014-08-01 22:14:17 +010035 html_url_path(url);
John Keepingd31be4c2014-08-01 22:14:18 +010036 html("' title='");
37 html_attr(ctx.repo->name);
38 html(" Git repository'>");
John Keepingbbfa0062014-08-01 22:14:17 +010039 html_txt(url);
Lars Hjemli78af25c2008-07-27 11:54:06 +020040 html("</a></td></tr>\n");
Lars Hjemli78af25c2008-07-27 11:54:06 +020041}
42
John Keepinge3d3fff2015-03-08 16:32:16 +000043void cgit_print_summary(void)
Lars Hjemli14180342006-12-11 16:55:07 +010044{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010045 int columns = 3;
46
47 if (ctx.repo->enable_log_filecount)
48 columns++;
49 if (ctx.repo->enable_log_linecount)
50 columns++;
51
John Keepinga3daa412015-08-14 12:47:18 +010052 cgit_print_layout_start();
Lars Hjemli29154832007-11-11 13:04:28 +010053 html("<table summary='repository info' class='list nowrap'>");
Lars Hjemlib228d4f2008-02-16 13:07:13 +010054 cgit_print_branches(ctx.cfg.summary_branches);
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010055 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
Lars Hjemlib228d4f2008-02-16 13:07:13 +010056 cgit_print_tags(ctx.cfg.summary_tags);
Lars Hjemli5764fe92008-04-14 22:13:38 +020057 if (ctx.cfg.summary_log > 0) {
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010058 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
Lars Hjemli5764fe92008-04-14 22:13:38 +020059 cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL,
Tobias Bieniek792f8132012-10-13 16:10:30 +020060 NULL, NULL, 0, 0, 0);
Lars Hjemli5764fe92008-04-14 22:13:38 +020061 }
John Keepingbbfa0062014-08-01 22:14:17 +010062 urls = 0;
63 cgit_add_clone_urls(print_url);
Lars Hjemli23734c52007-01-17 01:10:39 +010064 html("</table>");
John Keepinga3daa412015-08-14 12:47:18 +010065 cgit_print_layout_end();
Lars Hjemli14180342006-12-11 16:55:07 +010066}
Lars Hjemli90f64ad2008-04-28 23:06:57 +020067
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020068/* The caller must free the return value. */
69static char* append_readme_path(const char *filename, const char *ref, const char *path)
Lars Hjemli90f64ad2008-04-28 23:06:57 +020070{
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020071 char *file, *base_dir, *full_path, *resolved_base = NULL, *resolved_full = NULL;
Lars Hjemli515edb02010-08-21 15:08:01 +020072 /* If a subpath is specified for the about page, make it relative
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020073 * to the directory containing the configured readme. */
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020074
75 file = xstrdup(filename);
76 base_dir = dirname(file);
77 if (!strcmp(base_dir, ".") || !strcmp(base_dir, "..")) {
78 if (!ref) {
79 free(file);
80 return NULL;
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020081 }
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020082 full_path = xstrdup(path);
Lars Hjemlie1782ff2009-08-09 11:50:34 +020083 } else
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020084 full_path = fmtalloc("%s/%s", base_dir, path);
Christian Hesseb4312822014-04-17 11:55:46 +020085
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020086 if (!ref) {
87 resolved_base = realpath(base_dir, NULL);
88 resolved_full = realpath(full_path, NULL);
Christian Hesse79c985e2014-05-29 17:35:46 +020089 if (!resolved_base || !resolved_full || !starts_with(resolved_full, resolved_base)) {
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020090 free(full_path);
91 full_path = NULL;
92 }
93 }
94
95 free(file);
96 free(resolved_base);
97 free(resolved_full);
98
99 return full_path;
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200100}
101
102void cgit_print_repo_readme(char *path)
103{
Christian Hesse790d2492015-08-16 14:53:53 +0200104 char *filename, *ref, *mimetype;
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200105 int free_filename = 0;
Lars Hjemli515edb02010-08-21 15:08:01 +0200106
Christian Hesse790d2492015-08-16 14:53:53 +0200107 mimetype = get_mimetype_for_filename(path);
108 if (mimetype && (!strncmp(mimetype, "image/", 6) || !strncmp(mimetype, "video/", 6))) {
109 ctx.page.mimetype = mimetype;
110 ctx.page.charset = NULL;
111 cgit_print_plain();
112 free(mimetype);
113 return;
114 }
115 free(mimetype);
116
John Keeping51d91762015-08-14 12:47:12 +0100117 cgit_print_layout_start();
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200118 if (ctx.repo->readme.nr == 0)
John Keeping51d91762015-08-14 12:47:12 +0100119 goto done;
Christian Hesseb4312822014-04-17 11:55:46 +0200120
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200121 filename = ctx.repo->readme.items[0].string;
122 ref = ctx.repo->readme.items[0].util;
123
124 if (path) {
125 free_filename = 1;
126 filename = append_readme_path(filename, ref, path);
127 if (!filename)
John Keeping51d91762015-08-14 12:47:12 +0100128 goto done;
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200129 }
Jason A. Donenfeldfe36f842013-05-25 19:47:15 +0200130
Lars Hjemli515edb02010-08-21 15:08:01 +0200131 /* Print the calculated readme, either from the git repo or from the
132 * filesystem, while applying the about-filter.
133 */
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200134 html("<div id='summary'>");
Jason A. Donenfeld800380d2014-01-13 03:56:50 +0100135 cgit_open_filter(ctx.repo->about_filter, filename);
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200136 if (ref)
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200137 cgit_print_file(filename, ref, 1);
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200138 else
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200139 html_include(filename);
Jason A. Donenfeld800380d2014-01-13 03:56:50 +0100140 cgit_close_filter(ctx.repo->about_filter);
John Keeping3d8a6502014-01-12 17:13:50 +0000141
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200142 html("</div>");
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200143 if (free_filename)
144 free(filename);
John Keeping51d91762015-08-14 12:47:12 +0100145
146done:
147 cgit_print_layout_end();
Lars Hjemli90f64ad2008-04-28 23:06:57 +0200148}