]> gitweb.ps.run Git - ps-cgit/blob - ui-summary.c
global: make 'char *path' const where possible
[ps-cgit] / ui-summary.c
1 /* ui-summary.c: functions for generating repo summary page
2  *
3  * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4  *
5  * Licensed under GNU General Public License v2
6  *   (see COPYING for full license text)
7  */
8
9 #include "cgit.h"
10 #include "ui-summary.h"
11 #include "html.h"
12 #include "ui-blob.h"
13 #include "ui-log.h"
14 #include "ui-plain.h"
15 #include "ui-refs.h"
16 #include "ui-shared.h"
17
18 static int urls;
19
20 static void print_url(const char *url)
21 {
22         int columns = 3;
23
24         if (ctx.repo->enable_log_filecount)
25                 columns++;
26         if (ctx.repo->enable_log_linecount)
27                 columns++;
28
29         if (urls++ == 0) {
30                 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
31                 htmlf("<tr class='nohover'><th class='left' colspan='%d'>Clone</th></tr>\n", columns);
32         }
33
34         htmlf("<tr><td colspan='%d'><a rel='vcs-git' href='", columns);
35         html_url_path(url);
36         html("' title='");
37         html_attr(ctx.repo->name);
38         html(" Git repository'>");
39         html_txt(url);
40         html("</a></td></tr>\n");
41 }
42
43 void cgit_print_summary(void)
44 {
45         int columns = 3;
46
47         if (ctx.repo->enable_log_filecount)
48                 columns++;
49         if (ctx.repo->enable_log_linecount)
50                 columns++;
51
52         cgit_print_layout_start();
53         html("<table summary='repository info' class='list nowrap'>");
54         cgit_print_branches(ctx.cfg.summary_branches);
55         htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
56         cgit_print_tags(ctx.cfg.summary_tags);
57         if (ctx.cfg.summary_log > 0) {
58                 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
59                 cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL,
60                                NULL, NULL, 0, 0, 0);
61         }
62         urls = 0;
63         cgit_add_clone_urls(print_url);
64         html("</table>");
65         cgit_print_layout_end();
66 }
67
68 /* The caller must free the return value. */
69 static char* append_readme_path(const char *filename, const char *ref, const char *path)
70 {
71         char *file, *base_dir, *full_path, *resolved_base = NULL, *resolved_full = NULL;
72         /* If a subpath is specified for the about page, make it relative
73          * to the directory containing the configured readme. */
74
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;
81                 }
82                 full_path = xstrdup(path);
83         } else
84                 full_path = fmtalloc("%s/%s", base_dir, path);
85
86         if (!ref) {
87                 resolved_base = realpath(base_dir, NULL);
88                 resolved_full = realpath(full_path, NULL);
89                 if (!resolved_base || !resolved_full || !starts_with(resolved_full, resolved_base)) {
90                         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;
100 }
101
102 void cgit_print_repo_readme(const char *path)
103 {
104         char *filename, *ref, *mimetype;
105         int free_filename = 0;
106
107         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
117         cgit_print_layout_start();
118         if (ctx.repo->readme.nr == 0)
119                 goto done;
120
121         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)
128                         goto done;
129         }
130
131         /* Print the calculated readme, either from the git repo or from the
132          * filesystem, while applying the about-filter.
133          */
134         html("<div id='summary'>");
135         cgit_open_filter(ctx.repo->about_filter, filename);
136         if (ref)
137                 cgit_print_file(filename, ref, 1);
138         else
139                 html_include(filename);
140         cgit_close_filter(ctx.repo->about_filter);
141
142         html("</div>");
143         if (free_filename)
144                 free(filename);
145
146 done:
147         cgit_print_layout_end();
148 }