diff options
Diffstat (limited to 'ui-summary.c')
-rw-r--r-- | ui-summary.c | 61 |
1 files changed, 31 insertions, 30 deletions
diff --git a/ui-summary.c b/ui-summary.c index ffad4f2..2f8a822 100644 --- a/ui-summary.c +++ b/ui-summary.c | |||
@@ -95,70 +95,71 @@ void cgit_print_summary() | |||
95 | html("</table>"); | 95 | html("</table>"); |
96 | } | 96 | } |
97 | 97 | ||
98 | void cgit_print_repo_readme(char *path) | 98 | /* The caller must free filename and ref after calling this. */ |
99 | void cgit_parse_readme(const char *readme, const char *path, char **filename, char **ref, struct cgit_repo *repo) | ||
99 | { | 100 | { |
100 | char *slash, *tmp, *colon, *ref; | 101 | const char *slash, *colon; |
101 | int free_filename = 0; | ||
102 | 102 | ||
103 | if (!ctx.repo->readme || !(*ctx.repo->readme)) | 103 | *filename = NULL; |
104 | return; | 104 | *ref = NULL; |
105 | 105 | ||
106 | ref = NULL; | 106 | if (!readme || !(*readme)) |
107 | return; | ||
107 | 108 | ||
108 | /* Check if the readme is tracked in the git repo. */ | 109 | /* Check if the readme is tracked in the git repo. */ |
109 | colon = strchr(ctx.repo->readme, ':'); | 110 | colon = strchr(readme, ':'); |
110 | if (colon && strlen(colon) > 1) { | 111 | if (colon && strlen(colon) > 1) { |
111 | *colon = '\0'; | ||
112 | /* If it starts with a colon, we want to use | 112 | /* If it starts with a colon, we want to use |
113 | * the default branch */ | 113 | * the default branch */ |
114 | if (colon == ctx.repo->readme && ctx.repo->defbranch) | 114 | if (colon == readme && repo->defbranch) |
115 | ref = ctx.repo->defbranch; | 115 | *ref = xstrdup(repo->defbranch); |
116 | else | 116 | else |
117 | ref = ctx.repo->readme; | 117 | *ref = xstrndup(readme, colon - readme); |
118 | ctx.repo->readme = colon + 1; | 118 | readme = colon + 1; |
119 | if (!(*ctx.repo->readme)) | ||
120 | return; | ||
121 | } | 119 | } |
122 | 120 | ||
123 | /* Prepend repo path to relative readme path unless tracked. */ | 121 | /* Prepend repo path to relative readme path unless tracked. */ |
124 | if (!ref && *ctx.repo->readme != '/') | 122 | if (!(*ref) && *readme != '/') |
125 | ctx.repo->readme = fmtalloc("%s/%s", ctx.repo->path, | 123 | readme = fmtalloc("%s/%s", repo->path, readme); |
126 | ctx.repo->readme); | ||
127 | 124 | ||
128 | /* If a subpath is specified for the about page, make it relative | 125 | /* If a subpath is specified for the about page, make it relative |
129 | * to the directory containing the configured readme. | 126 | * to the directory containing the configured readme. */ |
130 | */ | ||
131 | if (path) { | 127 | if (path) { |
132 | slash = strrchr(ctx.repo->readme, '/'); | 128 | slash = strrchr(readme, '/'); |
133 | if (!slash) { | 129 | if (!slash) { |
134 | if (!colon) | 130 | if (!colon) |
135 | return; | 131 | return; |
136 | slash = colon; | 132 | slash = colon; |
137 | } | 133 | } |
138 | free_filename = 1; | 134 | *filename = xmalloc(slash - readme + 1 + strlen(path) + 1); |
139 | tmp = xmalloc(slash - ctx.repo->readme + 1 + strlen(path) + 1); | 135 | strncpy(*filename, readme, slash - readme + 1); |
140 | strncpy(tmp, ctx.repo->readme, slash - ctx.repo->readme + 1); | 136 | strcpy(*filename + (slash - readme + 1), path); |
141 | strcpy(tmp + (slash - ctx.repo->readme + 1), path); | ||
142 | } else | 137 | } else |
143 | tmp = ctx.repo->readme; | 138 | *filename = xstrdup(readme); |
139 | } | ||
140 | |||
141 | void cgit_print_repo_readme(char *path) | ||
142 | { | ||
143 | char *filename, *ref; | ||
144 | cgit_parse_readme(ctx.repo->readme, path, &filename, &ref, ctx.repo); | ||
144 | 145 | ||
145 | /* Print the calculated readme, either from the git repo or from the | 146 | /* Print the calculated readme, either from the git repo or from the |
146 | * filesystem, while applying the about-filter. | 147 | * filesystem, while applying the about-filter. |
147 | */ | 148 | */ |
148 | html("<div id='summary'>"); | 149 | html("<div id='summary'>"); |
149 | if (ctx.repo->about_filter) { | 150 | if (ctx.repo->about_filter) { |
150 | ctx.repo->about_filter->argv[1] = tmp; | 151 | ctx.repo->about_filter->argv[1] = filename; |
151 | cgit_open_filter(ctx.repo->about_filter); | 152 | cgit_open_filter(ctx.repo->about_filter); |
152 | } | 153 | } |
153 | if (ref) | 154 | if (ref) |
154 | cgit_print_file(tmp, ref); | 155 | cgit_print_file(filename, ref); |
155 | else | 156 | else |
156 | html_include(tmp); | 157 | html_include(filename); |
157 | if (ctx.repo->about_filter) { | 158 | if (ctx.repo->about_filter) { |
158 | cgit_close_filter(ctx.repo->about_filter); | 159 | cgit_close_filter(ctx.repo->about_filter); |
159 | ctx.repo->about_filter->argv[1] = NULL; | 160 | ctx.repo->about_filter->argv[1] = NULL; |
160 | } | 161 | } |
161 | html("</div>"); | 162 | html("</div>"); |
162 | if (free_filename) | 163 | free(filename); |
163 | free(tmp); | 164 | free(ref); |
164 | } | 165 | } |