blob: 5598d0808ae066dd7827d0f6b282caae69394630 [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. Donenfelddcbc0432013-05-26 15:20:02 +02004 * Copyright (C) 2010-2013 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"
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020016#include <libgen.h>
Lars Hjemli14180342006-12-11 16:55:07 +010017
Lars Hjemli78af25c2008-07-27 11:54:06 +020018static void print_url(char *base, char *suffix)
19{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010020 int columns = 3;
John Keepingfb3655d2013-04-06 10:28:57 +010021 struct strbuf basebuf = STRBUF_INIT;
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010022
23 if (ctx.repo->enable_log_filecount)
24 columns++;
25 if (ctx.repo->enable_log_linecount)
26 columns++;
27
Lars Hjemli78af25c2008-07-27 11:54:06 +020028 if (!base || !*base)
29 return;
John Keepingfb3655d2013-04-06 10:28:57 +010030 if (suffix && *suffix) {
31 strbuf_addf(&basebuf, "%s/%s", base, suffix);
32 base = basebuf.buf;
33 }
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010034 htmlf("<tr><td colspan='%d'><a href='", columns);
Lars Hjemli0071aa82008-10-05 21:21:42 +020035 html_url_path(base);
Lars Hjemli78af25c2008-07-27 11:54:06 +020036 html("'>");
37 html_txt(base);
38 html("</a></td></tr>\n");
John Keepingfb3655d2013-04-06 10:28:57 +010039 strbuf_release(&basebuf);
Lars Hjemli78af25c2008-07-27 11:54:06 +020040}
41
42static void print_urls(char *txt, char *suffix)
43{
44 char *h = txt, *t, c;
Lukas Fleischer977a3ad2013-03-05 16:48:27 +010045 int urls = 0;
46 int columns = 3;
47
48 if (ctx.repo->enable_log_filecount)
49 columns++;
50 if (ctx.repo->enable_log_linecount)
51 columns++;
52
Lars Hjemli78af25c2008-07-27 11:54:06 +020053
54 while (h && *h) {
55 while (h && *h == ' ')
56 h++;
Lukas Fleischer977a3ad2013-03-05 16:48:27 +010057 if (!*h)
58 break;
Lars Hjemli78af25c2008-07-27 11:54:06 +020059 t = h;
60 while (t && *t && *t != ' ')
61 t++;
62 c = *t;
63 *t = 0;
Lukas Fleischer977a3ad2013-03-05 16:48:27 +010064 if (urls++ == 0) {
65 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
66 htmlf("<tr><th class='left' colspan='%d'>Clone</th></tr>\n", columns);
67 }
Lars Hjemli78af25c2008-07-27 11:54:06 +020068 print_url(h, suffix);
69 *t = c;
70 h = t;
71 }
72}
73
Lars Hjemlid14c5f62006-12-11 17:04:19 +010074void cgit_print_summary()
Lars Hjemli14180342006-12-11 16:55:07 +010075{
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010076 int columns = 3;
77
78 if (ctx.repo->enable_log_filecount)
79 columns++;
80 if (ctx.repo->enable_log_linecount)
81 columns++;
82
Lars Hjemli29154832007-11-11 13:04:28 +010083 html("<table summary='repository info' class='list nowrap'>");
Lars Hjemlib228d4f2008-02-16 13:07:13 +010084 cgit_print_branches(ctx.cfg.summary_branches);
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010085 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
Lars Hjemlib228d4f2008-02-16 13:07:13 +010086 cgit_print_tags(ctx.cfg.summary_tags);
Lars Hjemli5764fe92008-04-14 22:13:38 +020087 if (ctx.cfg.summary_log > 0) {
Lukas Fleischeref8a97d2013-03-05 15:42:14 +010088 htmlf("<tr class='nohover'><td colspan='%d'>&nbsp;</td></tr>", columns);
Lars Hjemli5764fe92008-04-14 22:13:38 +020089 cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL,
Tobias Bieniek792f8132012-10-13 16:10:30 +020090 NULL, NULL, 0, 0, 0);
Lars Hjemli5764fe92008-04-14 22:13:38 +020091 }
Lars Hjemli78af25c2008-07-27 11:54:06 +020092 if (ctx.repo->clone_url)
Lars Hjemlia1429db2011-06-06 20:49:13 +000093 print_urls(expand_macros(ctx.repo->clone_url), NULL);
Lars Hjemli78af25c2008-07-27 11:54:06 +020094 else if (ctx.cfg.clone_prefix)
95 print_urls(ctx.cfg.clone_prefix, ctx.repo->url);
Lars Hjemli23734c52007-01-17 01:10:39 +010096 html("</table>");
Lars Hjemli14180342006-12-11 16:55:07 +010097}
Lars Hjemli90f64ad2008-04-28 23:06:57 +020098
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020099/* The caller must free the return value. */
100static char* append_readme_path(const char *filename, const char *ref, const char *path)
Lars Hjemli90f64ad2008-04-28 23:06:57 +0200101{
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200102 char *file, *base_dir, *full_path, *resolved_base = NULL, *resolved_full = NULL;
Lars Hjemli515edb02010-08-21 15:08:01 +0200103 /* If a subpath is specified for the about page, make it relative
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200104 * to the directory containing the configured readme. */
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200105
106 file = xstrdup(filename);
107 base_dir = dirname(file);
108 if (!strcmp(base_dir, ".") || !strcmp(base_dir, "..")) {
109 if (!ref) {
110 free(file);
111 return NULL;
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200112 }
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200113 full_path = xstrdup(path);
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200114 } else
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200115 full_path = fmtalloc("%s/%s", base_dir, path);
116
117 if (!ref) {
118 resolved_base = realpath(base_dir, NULL);
119 resolved_full = realpath(full_path, NULL);
120 if (!resolved_base || !resolved_full || strncmp(resolved_base, resolved_full, strlen(resolved_base))) {
121 free(full_path);
122 full_path = NULL;
123 }
124 }
125
126 free(file);
127 free(resolved_base);
128 free(resolved_full);
129
130 return full_path;
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200131}
132
133void cgit_print_repo_readme(char *path)
134{
135 char *filename, *ref;
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200136 int free_filename = 0;
Lars Hjemli515edb02010-08-21 15:08:01 +0200137
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200138 if (ctx.repo->readme.nr == 0)
Jason A. Donenfeldfe36f842013-05-25 19:47:15 +0200139 return;
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200140
141 filename = ctx.repo->readme.items[0].string;
142 ref = ctx.repo->readme.items[0].util;
143
144 if (path) {
145 free_filename = 1;
146 filename = append_readme_path(filename, ref, path);
147 if (!filename)
148 return;
149 }
Jason A. Donenfeldfe36f842013-05-25 19:47:15 +0200150
Lars Hjemli515edb02010-08-21 15:08:01 +0200151 /* Print the calculated readme, either from the git repo or from the
152 * filesystem, while applying the about-filter.
153 */
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200154 html("<div id='summary'>");
Jason A. Donenfeldc0dfaf12013-05-25 14:50:19 +0200155 if (ctx.repo->about_filter) {
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200156 ctx.repo->about_filter->argv[1] = filename;
Lars Hjemli3ec6b302011-06-06 19:29:58 +0000157 cgit_open_filter(ctx.repo->about_filter);
Jason A. Donenfeldc0dfaf12013-05-25 14:50:19 +0200158 }
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200159 if (ref)
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200160 cgit_print_file(filename, ref, 1);
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +0200161 else
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +0200162 html_include(filename);
Jason A. Donenfeldc0dfaf12013-05-25 14:50:19 +0200163 if (ctx.repo->about_filter) {
Lars Hjemli537c05f2009-08-09 13:27:21 +0200164 cgit_close_filter(ctx.repo->about_filter);
Jason A. Donenfeldc0dfaf12013-05-25 14:50:19 +0200165 ctx.repo->about_filter->argv[1] = NULL;
166 }
Lars Hjemlie1782ff2009-08-09 11:50:34 +0200167 html("</div>");
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200168 if (free_filename)
169 free(filename);
Lars Hjemli90f64ad2008-04-28 23:06:57 +0200170}