Lars Hjemli | 1418034 | 2006-12-11 16:55:07 +0100 | [diff] [blame] | 1 | /* ui-summary.c: functions for generating repo summary page |
| 2 | * |
Lukas Fleischer | f7f26f8 | 2014-01-08 15:10:49 +0100 | [diff] [blame] | 3 | * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com> |
Lars Hjemli | 1418034 | 2006-12-11 16:55:07 +0100 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | #include "cgit.h" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 10 | #include "ui-summary.h" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 11 | #include "html.h" |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 12 | #include "ui-log.h" |
| 13 | #include "ui-refs.h" |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 14 | #include "ui-blob.h" |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 15 | #include <libgen.h> |
Lars Hjemli | 1418034 | 2006-12-11 16:55:07 +0100 | [diff] [blame] | 16 | |
Lars Hjemli | 78af25c | 2008-07-27 11:54:06 +0200 | [diff] [blame] | 17 | static void print_url(char *base, char *suffix) |
| 18 | { |
Lukas Fleischer | ef8a97d | 2013-03-05 15:42:14 +0100 | [diff] [blame] | 19 | int columns = 3; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 20 | struct strbuf basebuf = STRBUF_INIT; |
Lukas Fleischer | ef8a97d | 2013-03-05 15:42:14 +0100 | [diff] [blame] | 21 | |
| 22 | if (ctx.repo->enable_log_filecount) |
| 23 | columns++; |
| 24 | if (ctx.repo->enable_log_linecount) |
| 25 | columns++; |
| 26 | |
Lars Hjemli | 78af25c | 2008-07-27 11:54:06 +0200 | [diff] [blame] | 27 | if (!base || !*base) |
| 28 | return; |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 29 | if (suffix && *suffix) { |
| 30 | strbuf_addf(&basebuf, "%s/%s", base, suffix); |
| 31 | base = basebuf.buf; |
| 32 | } |
Lukas Fleischer | ef8a97d | 2013-03-05 15:42:14 +0100 | [diff] [blame] | 33 | htmlf("<tr><td colspan='%d'><a href='", columns); |
Lars Hjemli | 0071aa8 | 2008-10-05 21:21:42 +0200 | [diff] [blame] | 34 | html_url_path(base); |
Lars Hjemli | 78af25c | 2008-07-27 11:54:06 +0200 | [diff] [blame] | 35 | html("'>"); |
| 36 | html_txt(base); |
| 37 | html("</a></td></tr>\n"); |
John Keeping | fb3655d | 2013-04-06 10:28:57 +0100 | [diff] [blame] | 38 | strbuf_release(&basebuf); |
Lars Hjemli | 78af25c | 2008-07-27 11:54:06 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static void print_urls(char *txt, char *suffix) |
| 42 | { |
| 43 | char *h = txt, *t, c; |
Lukas Fleischer | 977a3ad | 2013-03-05 16:48:27 +0100 | [diff] [blame] | 44 | int urls = 0; |
| 45 | int columns = 3; |
| 46 | |
| 47 | if (ctx.repo->enable_log_filecount) |
| 48 | columns++; |
| 49 | if (ctx.repo->enable_log_linecount) |
| 50 | columns++; |
| 51 | |
Lars Hjemli | 78af25c | 2008-07-27 11:54:06 +0200 | [diff] [blame] | 52 | |
| 53 | while (h && *h) { |
| 54 | while (h && *h == ' ') |
| 55 | h++; |
Lukas Fleischer | 977a3ad | 2013-03-05 16:48:27 +0100 | [diff] [blame] | 56 | if (!*h) |
| 57 | break; |
Lars Hjemli | 78af25c | 2008-07-27 11:54:06 +0200 | [diff] [blame] | 58 | t = h; |
| 59 | while (t && *t && *t != ' ') |
| 60 | t++; |
| 61 | c = *t; |
| 62 | *t = 0; |
Lukas Fleischer | 977a3ad | 2013-03-05 16:48:27 +0100 | [diff] [blame] | 63 | if (urls++ == 0) { |
| 64 | htmlf("<tr class='nohover'><td colspan='%d'> </td></tr>", columns); |
| 65 | htmlf("<tr><th class='left' colspan='%d'>Clone</th></tr>\n", columns); |
| 66 | } |
Lars Hjemli | 78af25c | 2008-07-27 11:54:06 +0200 | [diff] [blame] | 67 | print_url(h, suffix); |
| 68 | *t = c; |
| 69 | h = t; |
| 70 | } |
| 71 | } |
| 72 | |
Lars Hjemli | d14c5f6 | 2006-12-11 17:04:19 +0100 | [diff] [blame] | 73 | void cgit_print_summary() |
Lars Hjemli | 1418034 | 2006-12-11 16:55:07 +0100 | [diff] [blame] | 74 | { |
Lukas Fleischer | ef8a97d | 2013-03-05 15:42:14 +0100 | [diff] [blame] | 75 | int columns = 3; |
| 76 | |
| 77 | if (ctx.repo->enable_log_filecount) |
| 78 | columns++; |
| 79 | if (ctx.repo->enable_log_linecount) |
| 80 | columns++; |
| 81 | |
Lars Hjemli | 2915483 | 2007-11-11 13:04:28 +0100 | [diff] [blame] | 82 | html("<table summary='repository info' class='list nowrap'>"); |
Lars Hjemli | b228d4f | 2008-02-16 13:07:13 +0100 | [diff] [blame] | 83 | cgit_print_branches(ctx.cfg.summary_branches); |
Lukas Fleischer | ef8a97d | 2013-03-05 15:42:14 +0100 | [diff] [blame] | 84 | htmlf("<tr class='nohover'><td colspan='%d'> </td></tr>", columns); |
Lars Hjemli | b228d4f | 2008-02-16 13:07:13 +0100 | [diff] [blame] | 85 | cgit_print_tags(ctx.cfg.summary_tags); |
Lars Hjemli | 5764fe9 | 2008-04-14 22:13:38 +0200 | [diff] [blame] | 86 | if (ctx.cfg.summary_log > 0) { |
Lukas Fleischer | ef8a97d | 2013-03-05 15:42:14 +0100 | [diff] [blame] | 87 | htmlf("<tr class='nohover'><td colspan='%d'> </td></tr>", columns); |
Lars Hjemli | 5764fe9 | 2008-04-14 22:13:38 +0200 | [diff] [blame] | 88 | cgit_print_log(ctx.qry.head, 0, ctx.cfg.summary_log, NULL, |
Tobias Bieniek | 792f813 | 2012-10-13 16:10:30 +0200 | [diff] [blame] | 89 | NULL, NULL, 0, 0, 0); |
Lars Hjemli | 5764fe9 | 2008-04-14 22:13:38 +0200 | [diff] [blame] | 90 | } |
Lars Hjemli | 78af25c | 2008-07-27 11:54:06 +0200 | [diff] [blame] | 91 | if (ctx.repo->clone_url) |
Lars Hjemli | a1429db | 2011-06-06 20:49:13 +0000 | [diff] [blame] | 92 | print_urls(expand_macros(ctx.repo->clone_url), NULL); |
Lars Hjemli | 78af25c | 2008-07-27 11:54:06 +0200 | [diff] [blame] | 93 | else if (ctx.cfg.clone_prefix) |
| 94 | print_urls(ctx.cfg.clone_prefix, ctx.repo->url); |
Lars Hjemli | 23734c5 | 2007-01-17 01:10:39 +0100 | [diff] [blame] | 95 | html("</table>"); |
Lars Hjemli | 1418034 | 2006-12-11 16:55:07 +0100 | [diff] [blame] | 96 | } |
Lars Hjemli | 90f64ad | 2008-04-28 23:06:57 +0200 | [diff] [blame] | 97 | |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 98 | /* The caller must free the return value. */ |
| 99 | static char* append_readme_path(const char *filename, const char *ref, const char *path) |
Lars Hjemli | 90f64ad | 2008-04-28 23:06:57 +0200 | [diff] [blame] | 100 | { |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 101 | char *file, *base_dir, *full_path, *resolved_base = NULL, *resolved_full = NULL; |
Lars Hjemli | 515edb0 | 2010-08-21 15:08:01 +0200 | [diff] [blame] | 102 | /* If a subpath is specified for the about page, make it relative |
Jason A. Donenfeld | cd4c77d | 2013-05-25 16:32:37 +0200 | [diff] [blame] | 103 | * to the directory containing the configured readme. */ |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 104 | |
| 105 | file = xstrdup(filename); |
| 106 | base_dir = dirname(file); |
| 107 | if (!strcmp(base_dir, ".") || !strcmp(base_dir, "..")) { |
| 108 | if (!ref) { |
| 109 | free(file); |
| 110 | return NULL; |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 111 | } |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 112 | full_path = xstrdup(path); |
Lars Hjemli | e1782ff | 2009-08-09 11:50:34 +0200 | [diff] [blame] | 113 | } else |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 114 | full_path = fmtalloc("%s/%s", base_dir, path); |
Christian Hesse | b431282 | 2014-04-17 11:55:46 +0200 | [diff] [blame] | 115 | |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 116 | if (!ref) { |
| 117 | resolved_base = realpath(base_dir, NULL); |
| 118 | resolved_full = realpath(full_path, NULL); |
Christian Hesse | 79c985e | 2014-05-29 17:35:46 +0200 | [diff] [blame^] | 119 | if (!resolved_base || !resolved_full || !starts_with(resolved_full, resolved_base)) { |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 120 | free(full_path); |
| 121 | full_path = NULL; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | free(file); |
| 126 | free(resolved_base); |
| 127 | free(resolved_full); |
| 128 | |
| 129 | return full_path; |
Jason A. Donenfeld | cd4c77d | 2013-05-25 16:32:37 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void cgit_print_repo_readme(char *path) |
| 133 | { |
| 134 | char *filename, *ref; |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 135 | int free_filename = 0; |
Lars Hjemli | 515edb0 | 2010-08-21 15:08:01 +0200 | [diff] [blame] | 136 | |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 137 | if (ctx.repo->readme.nr == 0) |
Jason A. Donenfeld | fe36f84 | 2013-05-25 19:47:15 +0200 | [diff] [blame] | 138 | return; |
Christian Hesse | b431282 | 2014-04-17 11:55:46 +0200 | [diff] [blame] | 139 | |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 140 | filename = ctx.repo->readme.items[0].string; |
| 141 | ref = ctx.repo->readme.items[0].util; |
| 142 | |
| 143 | if (path) { |
| 144 | free_filename = 1; |
| 145 | filename = append_readme_path(filename, ref, path); |
| 146 | if (!filename) |
| 147 | return; |
| 148 | } |
Jason A. Donenfeld | fe36f84 | 2013-05-25 19:47:15 +0200 | [diff] [blame] | 149 | |
Lars Hjemli | 515edb0 | 2010-08-21 15:08:01 +0200 | [diff] [blame] | 150 | /* Print the calculated readme, either from the git repo or from the |
| 151 | * filesystem, while applying the about-filter. |
| 152 | */ |
Lars Hjemli | e1782ff | 2009-08-09 11:50:34 +0200 | [diff] [blame] | 153 | html("<div id='summary'>"); |
Jason A. Donenfeld | 800380d | 2014-01-13 03:56:50 +0100 | [diff] [blame] | 154 | cgit_open_filter(ctx.repo->about_filter, filename); |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 155 | if (ref) |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 156 | cgit_print_file(filename, ref, 1); |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 157 | else |
Jason A. Donenfeld | cd4c77d | 2013-05-25 16:32:37 +0200 | [diff] [blame] | 158 | html_include(filename); |
Jason A. Donenfeld | 800380d | 2014-01-13 03:56:50 +0100 | [diff] [blame] | 159 | cgit_close_filter(ctx.repo->about_filter); |
John Keeping | 3d8a650 | 2014-01-12 17:13:50 +0000 | [diff] [blame] | 160 | |
Lars Hjemli | e1782ff | 2009-08-09 11:50:34 +0200 | [diff] [blame] | 161 | html("</div>"); |
Jason A. Donenfeld | dcbc043 | 2013-05-26 15:20:02 +0200 | [diff] [blame] | 162 | if (free_filename) |
| 163 | free(filename); |
Lars Hjemli | 90f64ad | 2008-04-28 23:06:57 +0200 | [diff] [blame] | 164 | } |