blob: b0af0737a14762a808428829342ec42c3e6e743b [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"
Lars Hjemlic5984a92008-03-24 16:38:47 +010012#include "ui-log.h"
13#include "ui-refs.h"
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020014#include "ui-blob.h"
John Keepingbbfa0062014-08-01 22:14:17 +010015#include "ui-shared.h"
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020016#include <libgen.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
Lars Hjemli29154832007-11-11 13:04:28 +010052 html("<table summary='repository info' class='list nowrap'>");
Lars Hjemlib228d4f2008-02-16 13:07:13 +010053 cgit_print_branches(ctx.cfg.summary_branches);
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010054 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
Lars Hjemlib228d4f2008-02-16 13:07:13 +010055 cgit_print_tags(ctx.cfg.summary_tags);
Lars Hjemli5764fe92008-04-14 22:13:38 +020056 if (ctx.cfg.summary_log > 0) {
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010057 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
Lars Hjemli5764fe92008-04-14 22:13:38 +020058 cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL,
Tobias Bieniek792f8132012-10-13 16:10:30 +020059 NULL, NULL, 0, 0, 0);
Lars Hjemli5764fe92008-04-14 22:13:38 +020060 }
John Keepingbbfa0062014-08-01 22:14:17 +010061 urls = 0;
62 cgit_add_clone_urls(print_url);
Lars Hjemli23734c52007-01-17 01:10:39 +010063 html("</table>");
Lars Hjemli14180342006-12-11 16:55:07 +010064}
Lars Hjemli90f64ad2008-04-28 23:06:57 +020065
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020066/* The caller must free the return value. */
67static char* append_readme_path(const char *filename, const char *ref, const char *path)
Lars Hjemli90f64ad2008-04-28 23:06:57 +020068{
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020069 char *file, *base_dir, *full_path, *resolved_base = NULL, *resolved_full = NULL;
Lars Hjemli515edb02010-08-21 15:08:01 +020070 /* If a subpath is specified for the about page, make it relative
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020071 * to the directory containing the configured readme. */
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020072
73 file = xstrdup(filename);
74 base_dir = dirname(file);
75 if (!strcmp(base_dir, ".") || !strcmp(base_dir, "..")) {
76 if (!ref) {
77 free(file);
78 return NULL;
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020079 }
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020080 full_path = xstrdup(path);
Lars Hjemlie1782ff2009-08-09 11:50:34 +020081 } else
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020082 full_path = fmtalloc("%s/%s", base_dir, path);
Christian Hesseb4312822014-04-17 11:55:46 +020083
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020084 if (!ref) {
85 resolved_base = realpath(base_dir, NULL);
86 resolved_full = realpath(full_path, NULL);
Christian Hesse79c985e2014-05-29 17:35:46 +020087 if (!resolved_base || !resolved_full || !starts_with(resolved_full, resolved_base)) {
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020088 free(full_path);
89 full_path = NULL;
90 }
91 }
92
93 free(file);
94 free(resolved_base);
95 free(resolved_full);
96
97 return full_path;
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020098}
99
100void cgit_print_repo_readme(char *path)
101{
102 char *filename, *ref;
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200103 int free_filename = 0;
Lars Hjemli515edb02010-08-21 15:08:01 +0200104
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200105 if (ctx.repo->readme.nr == 0)
Jason A. Donenfeldfe36f842013-05-25 19:47:15 +0200106 return;
Christian Hesseb4312822014-04-17 11:55:46 +0200107
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200108 filename = ctx.repo->readme.items[0].string;
109 ref = ctx.repo->readme.items[0].util;
110
111 if (path) {
112 free_filename = 1;
113 filename = append_readme_path(filename, ref, path);
114 if (!filename)
115 return;
116 }
Jason A. Donenfeldfe36f842013-05-25 19:47:15 +0200117
Lars Hjemli515edb02010-08-21 15:08:01 +0200118 /* Print the calculated readme, either from the git repo or from the
119 * filesystem, while applying the about-filter.
120 */
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200121 html("<div id='summary'>");
Jason A. Donenfeld800380d2014-01-13 03:56:50 +0100122 cgit_open_filter(ctx.repo->about_filter, filename);
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200123 if (ref)
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200124 cgit_print_file(filename, ref, 1);
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200125 else
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200126 html_include(filename);
Jason A. Donenfeld800380d2014-01-13 03:56:50 +0100127 cgit_close_filter(ctx.repo->about_filter);
John Keeping3d8a6502014-01-12 17:13:50 +0000128
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200129 html("</div>");
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200130 if (free_filename)
131 free(filename);
Lars Hjemli90f64ad2008-04-28 23:06:57 +0200132}