]> gitweb.ps.run Git - ps-cgit/blob - ui-summary.c
Simplify commit and tag parsing
[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-log.h"
13 #include "ui-refs.h"
14 #include "ui-blob.h"
15 #include "ui-shared.h"
16 #include <libgen.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><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()
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         html("<table summary='repository info' class='list nowrap'>");
53         cgit_print_branches(ctx.cfg.summary_branches);
54         htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
55         cgit_print_tags(ctx.cfg.summary_tags);
56         if (ctx.cfg.summary_log > 0) {
57                 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
58                 cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL,
59                                NULL, NULL, 0, 0, 0);
60         }
61         urls = 0;
62         cgit_add_clone_urls(print_url);
63         html("</table>");
64 }
65
66 /* The caller must free the return value. */
67 static char* append_readme_path(const char *filename, const char *ref, const char *path)
68 {
69         char *file, *base_dir, *full_path, *resolved_base = NULL, *resolved_full = NULL;
70         /* If a subpath is specified for the about page, make it relative
71          * to the directory containing the configured readme. */
72
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;
79                 }
80                 full_path = xstrdup(path);
81         } else
82                 full_path = fmtalloc("%s/%s", base_dir, path);
83
84         if (!ref) {
85                 resolved_base = realpath(base_dir, NULL);
86                 resolved_full = realpath(full_path, NULL);
87                 if (!resolved_base || !resolved_full || !starts_with(resolved_full, resolved_base)) {
88                         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;
98 }
99
100 void cgit_print_repo_readme(char *path)
101 {
102         char *filename, *ref;
103         int free_filename = 0;
104
105         if (ctx.repo->readme.nr == 0)
106                 return;
107
108         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         }
117
118         /* Print the calculated readme, either from the git repo or from the
119          * filesystem, while applying the about-filter.
120          */
121         html("<div id='summary'>");
122         cgit_open_filter(ctx.repo->about_filter, filename);
123         if (ref)
124                 cgit_print_file(filename, ref, 1);
125         else
126                 html_include(filename);
127         cgit_close_filter(ctx.repo->about_filter);
128
129         html("</div>");
130         if (free_filename)
131                 free(filename);
132 }