2 2c251c14 2020-01-15 tracey * Copyright (c) 2019 Tracey Emery <tracey@traceyemery.net>
3 2c251c14 2020-01-15 tracey * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 2c251c14 2020-01-15 tracey * Copyright (c) 2014, 2015, 2017 Kristaps Dzonsons <kristaps@bsd.lv>
6 2c251c14 2020-01-15 tracey * Permission to use, copy, modify, and distribute this software for any
7 2c251c14 2020-01-15 tracey * purpose with or without fee is hereby granted, provided that the above
8 2c251c14 2020-01-15 tracey * copyright notice and this permission notice appear in all copies.
10 2c251c14 2020-01-15 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 2c251c14 2020-01-15 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 2c251c14 2020-01-15 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 2c251c14 2020-01-15 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 2c251c14 2020-01-15 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 2c251c14 2020-01-15 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 2c251c14 2020-01-15 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 2c251c14 2020-01-15 tracey #include <sys/queue.h>
20 2c251c14 2020-01-15 tracey #include <sys/stat.h>
21 2c251c14 2020-01-15 tracey #include <sys/types.h>
23 2c251c14 2020-01-15 tracey #include <dirent.h>
24 2c251c14 2020-01-15 tracey #include <err.h>
25 474370cb 2020-01-15 tracey #include <regex.h>
26 2c251c14 2020-01-15 tracey #include <stdarg.h>
27 2c251c14 2020-01-15 tracey #include <stdbool.h>
28 2c251c14 2020-01-15 tracey #include <stdint.h>
29 2c251c14 2020-01-15 tracey #include <stdio.h>
30 2c251c14 2020-01-15 tracey #include <stdlib.h>
31 2c251c14 2020-01-15 tracey #include <string.h>
32 2c251c14 2020-01-15 tracey #include <unistd.h>
34 2c251c14 2020-01-15 tracey #include <got_object.h>
35 2c251c14 2020-01-15 tracey #include <got_reference.h>
36 2c251c14 2020-01-15 tracey #include <got_repository.h>
37 2c251c14 2020-01-15 tracey #include <got_path.h>
38 2c251c14 2020-01-15 tracey #include <got_cancel.h>
39 2c251c14 2020-01-15 tracey #include <got_worktree.h>
40 2c251c14 2020-01-15 tracey #include <got_diff.h>
41 2c251c14 2020-01-15 tracey #include <got_commit_graph.h>
42 2c251c14 2020-01-15 tracey #include <got_blame.h>
43 2c251c14 2020-01-15 tracey #include <got_privsep.h>
44 2c251c14 2020-01-15 tracey #include <got_opentemp.h>
46 2c251c14 2020-01-15 tracey #include <kcgi.h>
47 2c251c14 2020-01-15 tracey #include <kcgihtml.h>
49 474370cb 2020-01-15 tracey #include "buf.h"
50 2c251c14 2020-01-15 tracey #include "gotweb.h"
51 2c251c14 2020-01-15 tracey #include "gotweb_ui.h"
53 2c251c14 2020-01-15 tracey #ifndef nitems
54 2c251c14 2020-01-15 tracey #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 2c251c14 2020-01-15 tracey struct trans {
58 2c251c14 2020-01-15 tracey TAILQ_HEAD(dirs, gw_dir) gw_dirs;
59 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir;
60 2c251c14 2020-01-15 tracey struct gotweb_conf *gw_conf;
61 2c251c14 2020-01-15 tracey struct ktemplate *gw_tmpl;
62 2c251c14 2020-01-15 tracey struct khtmlreq *gw_html_req;
63 2c251c14 2020-01-15 tracey struct kreq *gw_req;
64 2c251c14 2020-01-15 tracey char *repo_name;
65 2c251c14 2020-01-15 tracey char *repo_path;
66 2c251c14 2020-01-15 tracey char *commit;
67 2c251c14 2020-01-15 tracey char *repo_file;
68 2c251c14 2020-01-15 tracey char *action_name;
69 8087c3c5 2020-01-15 tracey char *headref;
70 2c251c14 2020-01-15 tracey unsigned int action;
71 2c251c14 2020-01-15 tracey unsigned int page;
72 2c251c14 2020-01-15 tracey unsigned int repos_total;
73 387a29ba 2020-01-15 tracey enum kmime mime;
76 2c251c14 2020-01-15 tracey enum gw_key {
78 2c251c14 2020-01-15 tracey KEY_ACTION,
79 2c251c14 2020-01-15 tracey KEY_COMMIT_ID,
82 8087c3c5 2020-01-15 tracey KEY_HEADREF,
86 2c251c14 2020-01-15 tracey struct gw_dir {
87 2c251c14 2020-01-15 tracey TAILQ_ENTRY(gw_dir) entry;
88 2c251c14 2020-01-15 tracey char *name;
89 2c251c14 2020-01-15 tracey char *owner;
90 2c251c14 2020-01-15 tracey char *description;
91 2c251c14 2020-01-15 tracey char *url;
92 2c251c14 2020-01-15 tracey char *age;
93 2c251c14 2020-01-15 tracey char *path;
96 2c251c14 2020-01-15 tracey enum tmpl {
97 2c251c14 2020-01-15 tracey TEMPL_HEAD,
98 2c251c14 2020-01-15 tracey TEMPL_HEADER,
99 2c251c14 2020-01-15 tracey TEMPL_SITEPATH,
100 2c251c14 2020-01-15 tracey TEMPL_SITEOWNER,
101 2c251c14 2020-01-15 tracey TEMPL_TITLE,
102 2c251c14 2020-01-15 tracey TEMPL_SEARCH,
103 2c251c14 2020-01-15 tracey TEMPL_CONTENT,
104 2c251c14 2020-01-15 tracey TEMPL__MAX
107 387a29ba 2020-01-15 tracey enum ref_tm {
112 8087c3c5 2020-01-15 tracey enum logs {
113 8087c3c5 2020-01-15 tracey LOGBRIEF,
114 8087c3c5 2020-01-15 tracey LOGCOMMIT,
118 87f9ebf5 2020-01-15 tracey LOGBLAME,
122 87f9ebf5 2020-01-15 tracey enum tags {
123 87f9ebf5 2020-01-15 tracey TAGBRIEF,
127 474370cb 2020-01-15 tracey struct buf {
128 474370cb 2020-01-15 tracey /* buffer handle, buffer size, and data length */
129 474370cb 2020-01-15 tracey u_char *cb_buf;
130 474370cb 2020-01-15 tracey size_t cb_size;
131 474370cb 2020-01-15 tracey size_t cb_len;
134 2c251c14 2020-01-15 tracey static const char *const templs[TEMPL__MAX] = {
136 2c251c14 2020-01-15 tracey "header",
137 2c251c14 2020-01-15 tracey "sitepath",
138 2c251c14 2020-01-15 tracey "siteowner",
140 2c251c14 2020-01-15 tracey "search",
141 2c251c14 2020-01-15 tracey "content",
144 2c251c14 2020-01-15 tracey static const struct kvalid gw_keys[KEY__MAX] = {
145 2c251c14 2020-01-15 tracey { kvalid_stringne, "path" },
146 2c251c14 2020-01-15 tracey { kvalid_stringne, "action" },
147 2c251c14 2020-01-15 tracey { kvalid_stringne, "commit" },
148 2c251c14 2020-01-15 tracey { kvalid_stringne, "file" },
149 2c251c14 2020-01-15 tracey { kvalid_int, "page" },
150 8087c3c5 2020-01-15 tracey { kvalid_stringne, "headref" },
153 2c251c14 2020-01-15 tracey static struct gw_dir *gw_init_gw_dir(char *);
155 2c251c14 2020-01-15 tracey static char *gw_get_repo_description(struct trans *,
157 2c251c14 2020-01-15 tracey static char *gw_get_repo_owner(struct trans *,
159 474370cb 2020-01-15 tracey static char *gw_get_time_str(time_t, int);
160 2c251c14 2020-01-15 tracey static char *gw_get_repo_age(struct trans *,
161 387a29ba 2020-01-15 tracey char *, char *, int);
162 4ceb8155 2020-01-15 tracey static char *gw_get_repo_log(struct trans *, const char *,
163 4ceb8155 2020-01-15 tracey char *, int, int);
164 87f9ebf5 2020-01-15 tracey static char *gw_get_repo_tags(struct trans *, int, int);
165 8d4d2453 2020-01-15 tracey static char *gw_get_repo_heads(struct trans *);
166 2c251c14 2020-01-15 tracey static char *gw_get_clone_url(struct trans *, char *);
167 2c251c14 2020-01-15 tracey static char *gw_get_got_link(struct trans *);
168 2c251c14 2020-01-15 tracey static char *gw_get_site_link(struct trans *);
169 2c251c14 2020-01-15 tracey static char *gw_html_escape(const char *);
171 2c251c14 2020-01-15 tracey static void gw_display_open(struct trans *, enum khttp,
172 2c251c14 2020-01-15 tracey enum kmime);
173 2c251c14 2020-01-15 tracey static void gw_display_index(struct trans *,
174 2c251c14 2020-01-15 tracey const struct got_error *);
176 2c251c14 2020-01-15 tracey static int gw_template(size_t, void *);
178 2c251c14 2020-01-15 tracey static const struct got_error* apply_unveil(const char *, const char *);
179 87f9ebf5 2020-01-15 tracey static const struct got_error* cmp_tags(void *, int *,
180 87f9ebf5 2020-01-15 tracey struct got_reference *,
181 87f9ebf5 2020-01-15 tracey struct got_reference *);
182 2c251c14 2020-01-15 tracey static const struct got_error* gw_load_got_paths(struct trans *);
183 2c251c14 2020-01-15 tracey static const struct got_error* gw_load_got_path(struct trans *,
184 2c251c14 2020-01-15 tracey struct gw_dir *);
185 2c251c14 2020-01-15 tracey static const struct got_error* gw_parse_querystring(struct trans *);
186 474370cb 2020-01-15 tracey static const struct got_error* match_logmsg(int *, struct got_object_id *,
187 474370cb 2020-01-15 tracey struct got_commit_object *, regex_t *);
189 2c251c14 2020-01-15 tracey static const struct got_error* gw_blame(struct trans *);
190 2c251c14 2020-01-15 tracey static const struct got_error* gw_blob(struct trans *);
191 8087c3c5 2020-01-15 tracey static const struct got_error* gw_blobdiff(struct trans *);
192 2c251c14 2020-01-15 tracey static const struct got_error* gw_commit(struct trans *);
193 8087c3c5 2020-01-15 tracey static const struct got_error* gw_commitdiff(struct trans *);
194 2c251c14 2020-01-15 tracey static const struct got_error* gw_history(struct trans *);
195 2c251c14 2020-01-15 tracey static const struct got_error* gw_index(struct trans *);
196 2c251c14 2020-01-15 tracey static const struct got_error* gw_log(struct trans *);
197 2c251c14 2020-01-15 tracey static const struct got_error* gw_raw(struct trans *);
198 4ceb8155 2020-01-15 tracey static const struct got_error* gw_logbriefs(struct trans *);
199 2c251c14 2020-01-15 tracey static const struct got_error* gw_snapshot(struct trans *);
200 387a29ba 2020-01-15 tracey static const struct got_error* gw_summary(struct trans *);
201 b772de24 2020-01-15 tracey static const struct got_error* gw_tag(struct trans *);
202 2c251c14 2020-01-15 tracey static const struct got_error* gw_tree(struct trans *);
204 2c251c14 2020-01-15 tracey struct gw_query_action {
205 2c251c14 2020-01-15 tracey unsigned int func_id;
206 2c251c14 2020-01-15 tracey const char *func_name;
207 2c251c14 2020-01-15 tracey const struct got_error *(*func_main)(struct trans *);
208 2c251c14 2020-01-15 tracey char *template;
211 2c251c14 2020-01-15 tracey enum gw_query_actions {
212 2c251c14 2020-01-15 tracey GW_BLAME,
214 2c251c14 2020-01-15 tracey GW_BLOBDIFF,
215 2c251c14 2020-01-15 tracey GW_COMMIT,
216 2c251c14 2020-01-15 tracey GW_COMMITDIFF,
218 2c251c14 2020-01-15 tracey GW_HISTORY,
219 2c251c14 2020-01-15 tracey GW_INDEX,
222 4ceb8155 2020-01-15 tracey GW_LOGBRIEFS,
223 2c251c14 2020-01-15 tracey GW_SNAPSHOT,
224 2c251c14 2020-01-15 tracey GW_SUMMARY,
229 2c251c14 2020-01-15 tracey static struct gw_query_action gw_query_funcs[] = {
230 2c251c14 2020-01-15 tracey { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
231 2c251c14 2020-01-15 tracey { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
232 8087c3c5 2020-01-15 tracey { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
233 2c251c14 2020-01-15 tracey { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
234 8087c3c5 2020-01-15 tracey { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
235 387a29ba 2020-01-15 tracey { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
236 2c251c14 2020-01-15 tracey { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
237 2c251c14 2020-01-15 tracey { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
238 2c251c14 2020-01-15 tracey { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
239 2c251c14 2020-01-15 tracey { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
240 4ceb8155 2020-01-15 tracey { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
241 2c251c14 2020-01-15 tracey { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
242 46b9c89b 2020-01-15 tracey { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
243 b772de24 2020-01-15 tracey { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
244 2c251c14 2020-01-15 tracey { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
247 2c251c14 2020-01-15 tracey static const struct got_error *
248 2c251c14 2020-01-15 tracey apply_unveil(const char *repo_path, const char *repo_file)
250 2c251c14 2020-01-15 tracey const struct got_error *err;
252 2c251c14 2020-01-15 tracey if (repo_path && repo_file) {
253 2c251c14 2020-01-15 tracey char *full_path;
254 2c251c14 2020-01-15 tracey if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
255 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf unveil");
256 2c251c14 2020-01-15 tracey if (unveil(full_path, "r") != 0)
257 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", full_path);
260 2c251c14 2020-01-15 tracey if (repo_path && unveil(repo_path, "r") != 0)
261 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", repo_path);
263 2c251c14 2020-01-15 tracey if (unveil("/tmp", "rwc") != 0)
264 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", "/tmp");
266 2c251c14 2020-01-15 tracey err = got_privsep_unveil_exec_helpers();
267 2c251c14 2020-01-15 tracey if (err != NULL)
268 2c251c14 2020-01-15 tracey return err;
270 2c251c14 2020-01-15 tracey if (unveil(NULL, NULL) != 0)
271 2c251c14 2020-01-15 tracey return got_error_from_errno("unveil");
273 2c251c14 2020-01-15 tracey return NULL;
276 87f9ebf5 2020-01-15 tracey static const struct got_error *
277 87f9ebf5 2020-01-15 tracey cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
278 87f9ebf5 2020-01-15 tracey struct got_reference *ref2)
280 87f9ebf5 2020-01-15 tracey const struct got_error *err = NULL;
281 87f9ebf5 2020-01-15 tracey struct got_repository *repo = arg;
282 87f9ebf5 2020-01-15 tracey struct got_object_id *id1, *id2 = NULL;
283 87f9ebf5 2020-01-15 tracey struct got_tag_object *tag1 = NULL, *tag2 = NULL;
284 87f9ebf5 2020-01-15 tracey time_t time1, time2;
286 87f9ebf5 2020-01-15 tracey *cmp = 0;
288 87f9ebf5 2020-01-15 tracey err = got_ref_resolve(&id1, repo, ref1);
290 87f9ebf5 2020-01-15 tracey return err;
291 87f9ebf5 2020-01-15 tracey err = got_object_open_as_tag(&tag1, repo, id1);
293 87f9ebf5 2020-01-15 tracey goto done;
295 87f9ebf5 2020-01-15 tracey err = got_ref_resolve(&id2, repo, ref2);
297 87f9ebf5 2020-01-15 tracey goto done;
298 87f9ebf5 2020-01-15 tracey err = got_object_open_as_tag(&tag2, repo, id2);
300 87f9ebf5 2020-01-15 tracey goto done;
302 87f9ebf5 2020-01-15 tracey time1 = got_object_tag_get_tagger_time(tag1);
303 87f9ebf5 2020-01-15 tracey time2 = got_object_tag_get_tagger_time(tag2);
305 87f9ebf5 2020-01-15 tracey /* Put latest tags first. */
306 87f9ebf5 2020-01-15 tracey if (time1 < time2)
307 87f9ebf5 2020-01-15 tracey *cmp = 1;
308 87f9ebf5 2020-01-15 tracey else if (time1 > time2)
309 87f9ebf5 2020-01-15 tracey *cmp = -1;
311 87f9ebf5 2020-01-15 tracey err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
313 87f9ebf5 2020-01-15 tracey free(id1);
314 87f9ebf5 2020-01-15 tracey free(id2);
315 87f9ebf5 2020-01-15 tracey if (tag1)
316 87f9ebf5 2020-01-15 tracey got_object_tag_close(tag1);
317 87f9ebf5 2020-01-15 tracey if (tag2)
318 87f9ebf5 2020-01-15 tracey got_object_tag_close(tag2);
319 87f9ebf5 2020-01-15 tracey return err;
322 2c251c14 2020-01-15 tracey static const struct got_error *
323 2c251c14 2020-01-15 tracey gw_blame(struct trans *gw_trans)
325 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
327 2c251c14 2020-01-15 tracey return error;
330 2c251c14 2020-01-15 tracey static const struct got_error *
331 2c251c14 2020-01-15 tracey gw_blob(struct trans *gw_trans)
333 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
335 2c251c14 2020-01-15 tracey return error;
338 2c251c14 2020-01-15 tracey static const struct got_error *
339 8087c3c5 2020-01-15 tracey gw_blobdiff(struct trans *gw_trans)
341 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
343 2c251c14 2020-01-15 tracey return error;
346 2c251c14 2020-01-15 tracey static const struct got_error *
347 2c251c14 2020-01-15 tracey gw_commit(struct trans *gw_trans)
349 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
350 8087c3c5 2020-01-15 tracey char *log, *log_html;
352 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
353 8087c3c5 2020-01-15 tracey if (error)
354 8087c3c5 2020-01-15 tracey return error;
356 8087c3c5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
358 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
359 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, log_commit, log)) == -1)
360 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
361 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
362 8087c3c5 2020-01-15 tracey free(log_html);
363 8087c3c5 2020-01-15 tracey free(log);
365 2c251c14 2020-01-15 tracey return error;
368 2c251c14 2020-01-15 tracey static const struct got_error *
369 8087c3c5 2020-01-15 tracey gw_commitdiff(struct trans *gw_trans)
371 2204c934 2020-01-15 tracey const struct got_error *error = NULL;
372 87f9ebf5 2020-01-15 tracey char *log, *log_html;
374 87f9ebf5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
375 87f9ebf5 2020-01-15 tracey if (error)
376 87f9ebf5 2020-01-15 tracey return error;
378 87f9ebf5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
380 87f9ebf5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
381 87f9ebf5 2020-01-15 tracey if ((asprintf(&log_html, log_diff, log)) == -1)
382 87f9ebf5 2020-01-15 tracey return got_error_from_errno("asprintf");
383 87f9ebf5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
384 87f9ebf5 2020-01-15 tracey free(log_html);
385 87f9ebf5 2020-01-15 tracey free(log);
387 2204c934 2020-01-15 tracey return error;
390 2204c934 2020-01-15 tracey static const struct got_error *
391 2c251c14 2020-01-15 tracey gw_history(struct trans *gw_trans)
393 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
395 2c251c14 2020-01-15 tracey return error;
398 2c251c14 2020-01-15 tracey static const struct got_error *
399 2c251c14 2020-01-15 tracey gw_index(struct trans *gw_trans)
401 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
402 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
403 2c251c14 2020-01-15 tracey char *html, *navs, *next, *prev;
404 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
406 46b9c89b 2020-01-15 tracey error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
407 46b9c89b 2020-01-15 tracey if (error)
408 46b9c89b 2020-01-15 tracey return error;
410 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
411 46b9c89b 2020-01-15 tracey if (error)
412 2c251c14 2020-01-15 tracey return error;
414 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, index_projects_header);
416 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
419 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
420 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
421 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
422 2c251c14 2020-01-15 tracey prev_disp++;
423 2c251c14 2020-01-15 tracey continue;
426 2c251c14 2020-01-15 tracey prev_disp++;
427 46b9c89b 2020-01-15 tracey if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
428 46b9c89b 2020-01-15 tracey gw_dir->name, gw_dir->name)) == -1)
429 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
431 46b9c89b 2020-01-15 tracey if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
432 46b9c89b 2020-01-15 tracey gw_dir->description, gw_dir->owner, gw_dir->age,
433 46b9c89b 2020-01-15 tracey navs)) == -1)
434 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
436 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, html);
438 2c251c14 2020-01-15 tracey free(navs);
439 2c251c14 2020-01-15 tracey free(html);
441 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
442 2c251c14 2020-01-15 tracey continue;
444 2c251c14 2020-01-15 tracey if (next_disp == gw_trans->gw_conf->got_max_repos_display)
445 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, np_wrapper_start);
446 2c251c14 2020-01-15 tracey else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
447 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
448 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
449 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total))
450 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, np_wrapper_start);
452 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
453 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
454 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
455 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
456 2c251c14 2020-01-15 tracey if ((asprintf(&prev, nav_prev,
457 2c251c14 2020-01-15 tracey gw_trans->page - 1)) == -1)
458 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
459 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, prev);
460 2c251c14 2020-01-15 tracey free(prev);
463 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
465 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
466 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
467 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
468 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
469 2c251c14 2020-01-15 tracey if ((asprintf(&next, nav_next,
470 2c251c14 2020-01-15 tracey gw_trans->page + 1)) == -1)
471 2c251c14 2020-01-15 tracey return got_error_from_errno("calloc");
472 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, next);
473 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
474 2c251c14 2020-01-15 tracey free(next);
475 2c251c14 2020-01-15 tracey next_disp = 0;
479 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
480 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
481 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
482 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total))
483 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
485 2c251c14 2020-01-15 tracey next_disp++;
487 2c251c14 2020-01-15 tracey return error;
490 2c251c14 2020-01-15 tracey static const struct got_error *
491 2c251c14 2020-01-15 tracey gw_log(struct trans *gw_trans)
493 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
494 4ceb8155 2020-01-15 tracey char *log, *log_html;
496 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
497 8087c3c5 2020-01-15 tracey if (error)
498 8087c3c5 2020-01-15 tracey return error;
500 87f9ebf5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
501 8087c3c5 2020-01-15 tracey gw_trans->gw_conf->got_max_commits_display, LOGFULL);
503 4ceb8155 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
504 4ceb8155 2020-01-15 tracey if ((asprintf(&log_html, logs, log)) == -1)
505 4ceb8155 2020-01-15 tracey return got_error_from_errno("asprintf");
506 4ceb8155 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
507 4ceb8155 2020-01-15 tracey free(log_html);
508 4ceb8155 2020-01-15 tracey free(log);
510 2c251c14 2020-01-15 tracey return error;
513 2c251c14 2020-01-15 tracey static const struct got_error *
514 2c251c14 2020-01-15 tracey gw_raw(struct trans *gw_trans)
516 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
518 2c251c14 2020-01-15 tracey return error;
521 2c251c14 2020-01-15 tracey static const struct got_error *
522 4ceb8155 2020-01-15 tracey gw_logbriefs(struct trans *gw_trans)
524 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
525 8087c3c5 2020-01-15 tracey char *log, *log_html;
527 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
528 8087c3c5 2020-01-15 tracey if (error)
529 8087c3c5 2020-01-15 tracey return error;
531 87f9ebf5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
532 8087c3c5 2020-01-15 tracey gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
534 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
535 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, summary_logbriefs,
536 8087c3c5 2020-01-15 tracey log)) == -1)
537 9d84e7dd 2020-01-15 tracey return got_error_from_errno("asprintf");
538 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
539 8087c3c5 2020-01-15 tracey free(log_html);
540 8087c3c5 2020-01-15 tracey free(log);
542 2c251c14 2020-01-15 tracey return error;
545 2c251c14 2020-01-15 tracey static const struct got_error *
546 2c251c14 2020-01-15 tracey gw_snapshot(struct trans *gw_trans)
548 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
550 2c251c14 2020-01-15 tracey return error;
553 2c251c14 2020-01-15 tracey static const struct got_error *
554 387a29ba 2020-01-15 tracey gw_summary(struct trans *gw_trans)
556 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
557 46b9c89b 2020-01-15 tracey char *description_html, *repo_owner_html, *repo_age_html,
558 8087c3c5 2020-01-15 tracey *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
559 8087c3c5 2020-01-15 tracey *heads_html, *age;
561 46b9c89b 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
562 46b9c89b 2020-01-15 tracey if (error)
563 46b9c89b 2020-01-15 tracey return error;
565 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, summary_wrapper);
566 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description) {
567 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->description != NULL &&
568 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
569 46b9c89b 2020-01-15 tracey if ((asprintf(&description_html, description,
570 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->description)) == -1)
571 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
573 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, description_html);
574 46b9c89b 2020-01-15 tracey free(description_html);
578 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
579 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->owner != NULL &&
580 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
581 46b9c89b 2020-01-15 tracey if ((asprintf(&repo_owner_html, repo_owner,
582 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->owner)) == -1)
583 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
585 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, repo_owner_html);
586 46b9c89b 2020-01-15 tracey free(repo_owner_html);
590 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
591 c6b62706 2020-01-15 tracey age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
592 c6b62706 2020-01-15 tracey "refs/heads", TM_LONG);
593 c6b62706 2020-01-15 tracey if (age != NULL && (strcmp(age, "") != 0)) {
594 c6b62706 2020-01-15 tracey if ((asprintf(&repo_age_html, last_change, age)) == -1)
595 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
597 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, repo_age_html);
598 46b9c89b 2020-01-15 tracey free(repo_age_html);
599 c6b62706 2020-01-15 tracey free(age);
603 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl) {
604 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->url != NULL &&
605 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
606 46b9c89b 2020-01-15 tracey if ((asprintf(&cloneurl_html, cloneurl,
607 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->url)) == -1)
608 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
610 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, cloneurl_html);
611 46b9c89b 2020-01-15 tracey free(cloneurl_html);
614 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
616 8087c3c5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
617 87f9ebf5 2020-01-15 tracey tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
618 8d4d2453 2020-01-15 tracey heads = gw_get_repo_heads(gw_trans);
620 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
621 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, summary_logbriefs,
622 8087c3c5 2020-01-15 tracey log)) == -1)
623 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
624 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
625 8087c3c5 2020-01-15 tracey free(log_html);
626 8087c3c5 2020-01-15 tracey free(log);
629 8d4d2453 2020-01-15 tracey if (tags != NULL && strcmp(tags, "") != 0) {
630 8d4d2453 2020-01-15 tracey if ((asprintf(&tags_html, summary_tags,
631 8d4d2453 2020-01-15 tracey tags)) == -1)
632 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
633 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, tags_html);
634 8d4d2453 2020-01-15 tracey free(tags_html);
635 8d4d2453 2020-01-15 tracey free(tags);
638 8d4d2453 2020-01-15 tracey if (heads != NULL && strcmp(heads, "") != 0) {
639 8d4d2453 2020-01-15 tracey if ((asprintf(&heads_html, summary_heads,
640 8d4d2453 2020-01-15 tracey heads)) == -1)
641 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
642 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, heads_html);
643 8d4d2453 2020-01-15 tracey free(heads_html);
644 8d4d2453 2020-01-15 tracey free(heads);
646 2204c934 2020-01-15 tracey return error;
649 2204c934 2020-01-15 tracey static const struct got_error *
650 b772de24 2020-01-15 tracey gw_tag(struct trans *gw_trans)
652 b772de24 2020-01-15 tracey const struct got_error *error = NULL;
653 b772de24 2020-01-15 tracey char *log, *log_html;
655 b772de24 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
656 b772de24 2020-01-15 tracey if (error)
657 b772de24 2020-01-15 tracey return error;
659 b772de24 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
661 b772de24 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
662 b772de24 2020-01-15 tracey if ((asprintf(&log_html, log_tree, log)) == -1)
663 b772de24 2020-01-15 tracey return got_error_from_errno("asprintf");
664 b772de24 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
665 b772de24 2020-01-15 tracey free(log_html);
666 b772de24 2020-01-15 tracey free(log);
668 b772de24 2020-01-15 tracey return error;
671 b772de24 2020-01-15 tracey static const struct got_error *
672 2c251c14 2020-01-15 tracey gw_tree(struct trans *gw_trans)
674 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
675 8087c3c5 2020-01-15 tracey char *log, *log_html;
677 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
678 8087c3c5 2020-01-15 tracey if (error)
679 8087c3c5 2020-01-15 tracey return error;
681 8087c3c5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
683 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
684 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, log_tree, log)) == -1)
685 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
686 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
687 8087c3c5 2020-01-15 tracey free(log_html);
688 8087c3c5 2020-01-15 tracey free(log);
690 2c251c14 2020-01-15 tracey return error;
693 2c251c14 2020-01-15 tracey static const struct got_error *
694 2c251c14 2020-01-15 tracey gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
696 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
698 2c251c14 2020-01-15 tracey char *dir_test;
699 4ceb8155 2020-01-15 tracey int opened = 0;
701 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
702 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
703 2c251c14 2020-01-15 tracey GOTWEB_GIT_DIR)) == -1)
704 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
706 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
707 2c251c14 2020-01-15 tracey if (dt == NULL) {
708 2c251c14 2020-01-15 tracey free(dir_test);
710 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
711 4ceb8155 2020-01-15 tracey opened = 1;
712 2c251c14 2020-01-15 tracey goto done;
715 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
716 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
717 2c251c14 2020-01-15 tracey GOTWEB_GOT_DIR)) == -1)
718 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
720 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
721 2c251c14 2020-01-15 tracey if (dt == NULL)
722 2c251c14 2020-01-15 tracey free(dir_test);
724 4ceb8155 2020-01-15 tracey opened = 1;
725 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
726 2c251c14 2020-01-15 tracey goto errored;
729 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s",
730 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
731 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
733 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
736 2c251c14 2020-01-15 tracey gw_dir->description = gw_get_repo_description(gw_trans,
737 2c251c14 2020-01-15 tracey gw_dir->path);
738 2c251c14 2020-01-15 tracey gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
739 387a29ba 2020-01-15 tracey gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
740 387a29ba 2020-01-15 tracey TM_DIFF);
741 2c251c14 2020-01-15 tracey gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
744 2c251c14 2020-01-15 tracey free(dir_test);
745 2c251c14 2020-01-15 tracey if (opened)
746 2c251c14 2020-01-15 tracey closedir(dt);
747 2c251c14 2020-01-15 tracey return error;
750 2c251c14 2020-01-15 tracey static const struct got_error *
751 2c251c14 2020-01-15 tracey gw_load_got_paths(struct trans *gw_trans)
753 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
755 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
756 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
757 2c251c14 2020-01-15 tracey struct stat st;
758 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
760 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
761 2c251c14 2020-01-15 tracey if (d == NULL) {
762 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
763 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
764 2c251c14 2020-01-15 tracey return error;
767 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
768 2c251c14 2020-01-15 tracey alphasort);
769 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
770 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
771 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
772 2c251c14 2020-01-15 tracey return error;
775 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
776 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
777 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
778 2c251c14 2020-01-15 tracey break; /* account for parent and self */
780 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
781 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
782 2c251c14 2020-01-15 tracey continue;
784 2c251c14 2020-01-15 tracey if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
785 2c251c14 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
787 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
788 2c251c14 2020-01-15 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO)
789 2c251c14 2020-01-15 tracey continue;
790 2c251c14 2020-01-15 tracey else if (error)
791 2c251c14 2020-01-15 tracey return error;
793 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
794 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
795 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
797 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
801 2c251c14 2020-01-15 tracey closedir(d);
802 2c251c14 2020-01-15 tracey return error;
805 2c251c14 2020-01-15 tracey static const struct got_error *
806 2c251c14 2020-01-15 tracey gw_parse_querystring(struct trans *gw_trans)
808 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
809 2c251c14 2020-01-15 tracey struct kpair *p;
810 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
811 2c251c14 2020-01-15 tracey unsigned int i;
813 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
814 2c251c14 2020-01-15 tracey error = got_error_from_errno("bad parse");
815 2c251c14 2020-01-15 tracey return error;
816 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
817 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
818 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
819 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
821 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_path, "%s/%s",
822 387a29ba 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
823 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
825 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
826 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->commit, "%s",
827 2c251c14 2020-01-15 tracey p->parsed.s)) == -1)
828 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
830 2c251c14 2020-01-15 tracey /* get action and set function */
831 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
832 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
833 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
834 2c251c14 2020-01-15 tracey if (action->func_name == NULL)
835 2c251c14 2020-01-15 tracey continue;
837 2c251c14 2020-01-15 tracey if (strcmp(action->func_name,
838 2c251c14 2020-01-15 tracey p->parsed.s) == 0) {
839 2c251c14 2020-01-15 tracey gw_trans->action = i;
840 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->action_name,
841 2c251c14 2020-01-15 tracey "%s", action->func_name)) == -1)
843 b772de24 2020-01-15 tracey got_error_from_errno(
844 2c251c14 2020-01-15 tracey "asprintf");
849 2c251c14 2020-01-15 tracey action = NULL;
852 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
853 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_file, "%s",
854 8087c3c5 2020-01-15 tracey p->parsed.s)) == -1)
855 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
857 8087c3c5 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
858 8087c3c5 2020-01-15 tracey if ((asprintf(&gw_trans->headref, "%s",
859 2c251c14 2020-01-15 tracey p->parsed.s)) == -1)
860 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
862 2c251c14 2020-01-15 tracey if (action == NULL) {
863 2c251c14 2020-01-15 tracey error = got_error_from_errno("invalid action");
864 2c251c14 2020-01-15 tracey return error;
866 46b9c89b 2020-01-15 tracey if ((gw_trans->gw_dir =
867 46b9c89b 2020-01-15 tracey gw_init_gw_dir(gw_trans->repo_name)) == NULL)
868 46b9c89b 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
870 46b9c89b 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
871 46b9c89b 2020-01-15 tracey if (error)
872 46b9c89b 2020-01-15 tracey return error;
874 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
876 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
877 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
879 2c251c14 2020-01-15 tracey if (gw_trans->action == GW_RAW)
880 387a29ba 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
882 2c251c14 2020-01-15 tracey return error;
885 2c251c14 2020-01-15 tracey static struct gw_dir *
886 2c251c14 2020-01-15 tracey gw_init_gw_dir(char *dir)
888 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
890 2c251c14 2020-01-15 tracey if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
891 2c251c14 2020-01-15 tracey return NULL;
893 2c251c14 2020-01-15 tracey if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
894 2c251c14 2020-01-15 tracey return NULL;
896 2c251c14 2020-01-15 tracey return gw_dir;
899 474370cb 2020-01-15 tracey static const struct got_error*
900 474370cb 2020-01-15 tracey match_logmsg(int *have_match, struct got_object_id *id,
901 474370cb 2020-01-15 tracey struct got_commit_object *commit, regex_t *regex)
903 474370cb 2020-01-15 tracey const struct got_error *err = NULL;
904 474370cb 2020-01-15 tracey regmatch_t regmatch;
905 474370cb 2020-01-15 tracey char *id_str = NULL, *logmsg = NULL;
907 474370cb 2020-01-15 tracey *have_match = 0;
909 474370cb 2020-01-15 tracey err = got_object_id_str(&id_str, id);
911 474370cb 2020-01-15 tracey return err;
913 474370cb 2020-01-15 tracey err = got_object_commit_get_logmsg(&logmsg, commit);
915 474370cb 2020-01-15 tracey goto done;
917 474370cb 2020-01-15 tracey if (regexec(regex, logmsg, 1, ®match, 0) == 0)
918 474370cb 2020-01-15 tracey *have_match = 1;
920 474370cb 2020-01-15 tracey free(id_str);
921 474370cb 2020-01-15 tracey free(logmsg);
922 474370cb 2020-01-15 tracey return err;
925 2c251c14 2020-01-15 tracey static void
926 2c251c14 2020-01-15 tracey gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
928 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
929 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
930 2c251c14 2020-01-15 tracey khttps[code]);
931 387a29ba 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
932 2c251c14 2020-01-15 tracey kmimetypes[mime]);
933 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
934 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
935 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
936 2c251c14 2020-01-15 tracey khttp_body(gw_trans->gw_req);
939 2c251c14 2020-01-15 tracey static void
940 2c251c14 2020-01-15 tracey gw_display_index(struct trans *gw_trans, const struct got_error *err)
942 2c251c14 2020-01-15 tracey gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
943 2c251c14 2020-01-15 tracey khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
946 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, err->msg);
948 2c251c14 2020-01-15 tracey khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
949 2c251c14 2020-01-15 tracey gw_query_funcs[gw_trans->action].template);
951 2c251c14 2020-01-15 tracey khtml_close(gw_trans->gw_html_req);
954 2c251c14 2020-01-15 tracey static int
955 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
957 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
958 2c251c14 2020-01-15 tracey struct trans *gw_trans = arg;
959 2c251c14 2020-01-15 tracey char *gw_got_link, *gw_site_link;
960 2c251c14 2020-01-15 tracey char *site_owner_name, *site_owner_name_h;
962 2c251c14 2020-01-15 tracey switch (key) {
963 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
964 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, head);
966 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
967 2c251c14 2020-01-15 tracey gw_got_link = gw_get_got_link(gw_trans);
968 2c251c14 2020-01-15 tracey if (gw_got_link != NULL)
969 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_got_link);
971 2c251c14 2020-01-15 tracey free(gw_got_link);
973 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
974 2c251c14 2020-01-15 tracey gw_site_link = gw_get_site_link(gw_trans);
975 2c251c14 2020-01-15 tracey if (gw_site_link != NULL)
976 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_site_link);
978 2c251c14 2020-01-15 tracey free(gw_site_link);
980 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
981 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_name != NULL)
982 2c251c14 2020-01-15 tracey khtml_puts(gw_trans->gw_html_req,
983 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
986 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
987 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, search);
989 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
990 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
991 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
992 2c251c14 2020-01-15 tracey site_owner_name =
993 2c251c14 2020-01-15 tracey gw_html_escape(gw_trans->gw_conf->got_site_owner);
994 2c251c14 2020-01-15 tracey if ((asprintf(&site_owner_name_h, site_owner,
995 2c251c14 2020-01-15 tracey site_owner_name))
997 2c251c14 2020-01-15 tracey return 0;
999 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, site_owner_name_h);
1000 2c251c14 2020-01-15 tracey free(site_owner_name);
1001 2c251c14 2020-01-15 tracey free(site_owner_name_h);
1004 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
1005 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1006 2c251c14 2020-01-15 tracey if (error)
1007 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
1010 2c251c14 2020-01-15 tracey default:
1011 2c251c14 2020-01-15 tracey return 0;
1014 2c251c14 2020-01-15 tracey return 1;
1017 2c251c14 2020-01-15 tracey static char *
1018 2c251c14 2020-01-15 tracey gw_get_repo_description(struct trans *gw_trans, char *dir)
1020 2c251c14 2020-01-15 tracey FILE *f;
1021 2c251c14 2020-01-15 tracey char *description = NULL, *d_file = NULL;
1022 2c251c14 2020-01-15 tracey unsigned int len;
1024 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description == false)
1025 2c251c14 2020-01-15 tracey goto err;
1027 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/description", dir)) == -1)
1028 2c251c14 2020-01-15 tracey goto err;
1030 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1031 2c251c14 2020-01-15 tracey goto err;
1033 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1034 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1035 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1036 2c251c14 2020-01-15 tracey if ((description = calloc(len, sizeof(char *))) == NULL)
1037 2c251c14 2020-01-15 tracey goto err;
1039 2c251c14 2020-01-15 tracey fread(description, 1, len, f);
1040 2c251c14 2020-01-15 tracey fclose(f);
1041 2c251c14 2020-01-15 tracey free(d_file);
1042 2c251c14 2020-01-15 tracey return description;
1044 2c251c14 2020-01-15 tracey if ((asprintf(&description, "%s", "")) == -1)
1045 2c251c14 2020-01-15 tracey return NULL;
1047 2c251c14 2020-01-15 tracey return description;
1050 2c251c14 2020-01-15 tracey static char *
1051 474370cb 2020-01-15 tracey gw_get_time_str(time_t committer_time, int ref_tm)
1053 474370cb 2020-01-15 tracey struct tm tm;
1054 474370cb 2020-01-15 tracey time_t diff_time;
1055 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
1056 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1057 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
1058 474370cb 2020-01-15 tracey char *now = "right now";
1059 474370cb 2020-01-15 tracey char *repo_age, *s;
1060 6c6c85af 2020-01-15 tracey char datebuf[29];
1062 474370cb 2020-01-15 tracey switch (ref_tm) {
1063 474370cb 2020-01-15 tracey case TM_DIFF:
1064 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
1065 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1066 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1067 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1068 474370cb 2020-01-15 tracey return NULL;
1069 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1070 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1071 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1072 474370cb 2020-01-15 tracey months)) == -1)
1073 474370cb 2020-01-15 tracey return NULL;
1074 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1075 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1076 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1077 474370cb 2020-01-15 tracey return NULL;
1078 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1079 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1080 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24), days)) == -1)
1081 474370cb 2020-01-15 tracey return NULL;
1082 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
1083 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1084 474370cb 2020-01-15 tracey (diff_time / 60 / 60), hours)) == -1)
1085 474370cb 2020-01-15 tracey return NULL;
1086 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
1087 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1088 474370cb 2020-01-15 tracey minutes)) == -1)
1089 474370cb 2020-01-15 tracey return NULL;
1090 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
1091 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", diff_time,
1092 474370cb 2020-01-15 tracey seconds)) == -1)
1093 474370cb 2020-01-15 tracey return NULL;
1094 474370cb 2020-01-15 tracey } else {
1095 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s", now)) == -1)
1096 474370cb 2020-01-15 tracey return NULL;
1099 474370cb 2020-01-15 tracey case TM_LONG:
1100 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1101 474370cb 2020-01-15 tracey return NULL;
1103 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
1104 474370cb 2020-01-15 tracey if (s == NULL)
1105 474370cb 2020-01-15 tracey return NULL;
1107 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1108 474370cb 2020-01-15 tracey return NULL;
1111 474370cb 2020-01-15 tracey return repo_age;
1114 474370cb 2020-01-15 tracey static char *
1115 387a29ba 2020-01-15 tracey gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1117 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1118 2c251c14 2020-01-15 tracey struct got_object_id *id = NULL;
1119 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
1120 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
1121 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
1122 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
1123 2c251c14 2020-01-15 tracey struct got_reference *head_ref;
1124 87f9ebf5 2020-01-15 tracey int is_head = 0;
1125 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
1126 87f9ebf5 2020-01-15 tracey const char *refname;
1127 474370cb 2020-01-15 tracey char *repo_age = NULL;
1129 387a29ba 2020-01-15 tracey if (repo_ref == NULL)
1130 387a29ba 2020-01-15 tracey return NULL;
1132 87f9ebf5 2020-01-15 tracey if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1133 87f9ebf5 2020-01-15 tracey is_head = 1;
1135 2c251c14 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1136 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age == false) {
1137 8087c3c5 2020-01-15 tracey if ((asprintf(&repo_age, "")) == -1)
1138 8087c3c5 2020-01-15 tracey return NULL;
1139 2c251c14 2020-01-15 tracey return repo_age;
1142 2c251c14 2020-01-15 tracey error = got_repo_open(&repo, dir, NULL);
1143 2c251c14 2020-01-15 tracey if (error != NULL)
1144 2c251c14 2020-01-15 tracey goto err;
1146 87f9ebf5 2020-01-15 tracey if (is_head)
1147 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads",
1148 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1150 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, repo_ref,
1151 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1152 2c251c14 2020-01-15 tracey if (error != NULL)
1153 2c251c14 2020-01-15 tracey goto err;
1155 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1156 87f9ebf5 2020-01-15 tracey if (is_head)
1157 87f9ebf5 2020-01-15 tracey refname = strdup(repo_ref);
1159 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1160 2c251c14 2020-01-15 tracey error = got_ref_open(&head_ref, repo, refname, 0);
1161 2c251c14 2020-01-15 tracey if (error != NULL)
1162 2c251c14 2020-01-15 tracey goto err;
1164 2c251c14 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
1165 2c251c14 2020-01-15 tracey got_ref_close(head_ref);
1166 2c251c14 2020-01-15 tracey if (error != NULL)
1167 2c251c14 2020-01-15 tracey goto err;
1169 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1170 2c251c14 2020-01-15 tracey if (error != NULL)
1171 2c251c14 2020-01-15 tracey goto err;
1173 2c251c14 2020-01-15 tracey committer_time =
1174 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
1176 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
1177 2c251c14 2020-01-15 tracey cmp_time = committer_time;
1180 474370cb 2020-01-15 tracey if (cmp_time != 0) {
1181 2c251c14 2020-01-15 tracey committer_time = cmp_time;
1182 474370cb 2020-01-15 tracey repo_age = gw_get_time_str(committer_time, ref_tm);
1184 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "")) == -1)
1185 474370cb 2020-01-15 tracey return NULL;
1186 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
1187 2c251c14 2020-01-15 tracey free(id);
1188 2c251c14 2020-01-15 tracey return repo_age;
1190 2c251c14 2020-01-15 tracey if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1191 2c251c14 2020-01-15 tracey return NULL;
1193 2c251c14 2020-01-15 tracey return repo_age;
1196 2c251c14 2020-01-15 tracey static char *
1197 2c251c14 2020-01-15 tracey gw_get_repo_owner(struct trans *gw_trans, char *dir)
1199 2c251c14 2020-01-15 tracey FILE *f;
1200 2c251c14 2020-01-15 tracey char *owner = NULL, *d_file = NULL;
1201 2c251c14 2020-01-15 tracey char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1202 2c251c14 2020-01-15 tracey char *comp, *pos, *buf;
1203 2c251c14 2020-01-15 tracey unsigned int i;
1205 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner == false)
1206 2c251c14 2020-01-15 tracey goto err;
1208 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/config", dir)) == -1)
1209 2c251c14 2020-01-15 tracey goto err;
1211 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1212 2c251c14 2020-01-15 tracey goto err;
1214 6c6c85af 2020-01-15 tracey if ((buf = calloc(128, sizeof(char *))) == NULL)
1215 2c251c14 2020-01-15 tracey goto err;
1217 6c6c85af 2020-01-15 tracey while ((fgets(buf, 128, f)) != NULL) {
1218 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gotweb)) != NULL)
1221 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gitweb)) != NULL)
1225 2c251c14 2020-01-15 tracey if (pos == NULL)
1226 2c251c14 2020-01-15 tracey goto err;
1229 6c6c85af 2020-01-15 tracey fgets(buf, 128, f);
1230 2c251c14 2020-01-15 tracey } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1232 2c251c14 2020-01-15 tracey if (comp == NULL)
1233 2c251c14 2020-01-15 tracey goto err;
1235 2c251c14 2020-01-15 tracey if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1236 2c251c14 2020-01-15 tracey goto err;
1238 2c251c14 2020-01-15 tracey for (i = 0; i < 2; i++) {
1239 2c251c14 2020-01-15 tracey owner = strsep(&buf, "\"");
1242 2c251c14 2020-01-15 tracey if (owner == NULL)
1243 2c251c14 2020-01-15 tracey goto err;
1245 2c251c14 2020-01-15 tracey fclose(f);
1246 2c251c14 2020-01-15 tracey free(d_file);
1247 2c251c14 2020-01-15 tracey return owner;
1249 2c251c14 2020-01-15 tracey if ((asprintf(&owner, "%s", "")) == -1)
1250 2c251c14 2020-01-15 tracey return NULL;
1252 2c251c14 2020-01-15 tracey return owner;
1255 2c251c14 2020-01-15 tracey static char *
1256 2c251c14 2020-01-15 tracey gw_get_clone_url(struct trans *gw_trans, char *dir)
1258 2c251c14 2020-01-15 tracey FILE *f;
1259 2c251c14 2020-01-15 tracey char *url = NULL, *d_file = NULL;
1260 2c251c14 2020-01-15 tracey unsigned int len;
1262 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1263 2c251c14 2020-01-15 tracey return NULL;
1265 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1266 2c251c14 2020-01-15 tracey return NULL;
1268 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1269 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1270 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1272 2c251c14 2020-01-15 tracey if ((url = calloc(len, sizeof(char *))) == NULL)
1273 2c251c14 2020-01-15 tracey return NULL;
1275 2c251c14 2020-01-15 tracey fread(url, 1, len, f);
1276 2c251c14 2020-01-15 tracey fclose(f);
1277 2c251c14 2020-01-15 tracey free(d_file);
1278 2c251c14 2020-01-15 tracey return url;
1281 8d4d2453 2020-01-15 tracey static char *
1282 4ceb8155 2020-01-15 tracey gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1283 8087c3c5 2020-01-15 tracey char *start_commit, int limit, int log_type)
1285 c6b62706 2020-01-15 tracey const struct got_error *error;
1286 c6b62706 2020-01-15 tracey struct got_repository *repo = NULL;
1287 c6b62706 2020-01-15 tracey struct got_reflist_head refs;
1288 474370cb 2020-01-15 tracey struct got_reflist_entry *re;
1289 c6b62706 2020-01-15 tracey struct got_commit_object *commit = NULL;
1290 8087c3c5 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1291 8087c3c5 2020-01-15 tracey struct got_object_qid *parent_id;
1292 474370cb 2020-01-15 tracey struct got_commit_graph *graph = NULL;
1293 87f9ebf5 2020-01-15 tracey char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1294 87f9ebf5 2020-01-15 tracey *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1295 87f9ebf5 2020-01-15 tracey *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1296 87f9ebf5 2020-01-15 tracey *commit_commit_disp = NULL, *commit_age_diff = NULL,
1297 87f9ebf5 2020-01-15 tracey *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1298 87f9ebf5 2020-01-15 tracey *commit_age_long_disp = NULL, *commit_author = NULL,
1299 87f9ebf5 2020-01-15 tracey *commit_author_disp = NULL, *commit_committer = NULL,
1300 87f9ebf5 2020-01-15 tracey *commit_committer_disp = NULL, *commit_log = NULL,
1301 87f9ebf5 2020-01-15 tracey *commit_log_disp = NULL, *commit_parent = NULL,
1302 87f9ebf5 2020-01-15 tracey *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1303 87f9ebf5 2020-01-15 tracey *log_tree_html = NULL, *log_commit_html = NULL,
1304 87f9ebf5 2020-01-15 tracey *log_diff_html = NULL, *commit_tree = NULL,
1305 077f6c5a 2020-01-15 tracey *commit_tree_disp = NULL, *log_tag_html = NULL;
1306 87f9ebf5 2020-01-15 tracey char *commit_log0, *newline;
1307 474370cb 2020-01-15 tracey regex_t regex;
1308 9d84e7dd 2020-01-15 tracey int have_match;
1309 474370cb 2020-01-15 tracey size_t newsize;
1310 8087c3c5 2020-01-15 tracey struct buf *diffbuf = NULL;
1311 474370cb 2020-01-15 tracey time_t committer_time;
1313 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1314 6c6c85af 2020-01-15 tracey if (error != NULL)
1315 6c6c85af 2020-01-15 tracey return NULL;
1317 474370cb 2020-01-15 tracey if (search_pattern &&
1318 474370cb 2020-01-15 tracey regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB |
1319 474370cb 2020-01-15 tracey REG_NEWLINE))
1320 474370cb 2020-01-15 tracey return NULL;
1322 c6b62706 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1323 c6b62706 2020-01-15 tracey if (error != NULL)
1324 6c6c85af 2020-01-15 tracey return NULL;
1326 6c6c85af 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1328 c6b62706 2020-01-15 tracey if (start_commit == NULL) {
1329 c6b62706 2020-01-15 tracey struct got_reference *head_ref;
1330 8087c3c5 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1331 c6b62706 2020-01-15 tracey if (error != NULL)
1332 8087c3c5 2020-01-15 tracey goto done;
1334 8087c3c5 2020-01-15 tracey error = got_ref_resolve(&id1, repo, head_ref);
1335 c6b62706 2020-01-15 tracey got_ref_close(head_ref);
1336 c6b62706 2020-01-15 tracey if (error != NULL)
1337 8087c3c5 2020-01-15 tracey goto done;
1339 8087c3c5 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id1);
1340 c6b62706 2020-01-15 tracey } else {
1341 c6b62706 2020-01-15 tracey struct got_reference *ref;
1342 c6b62706 2020-01-15 tracey error = got_ref_open(&ref, repo, start_commit, 0);
1343 c6b62706 2020-01-15 tracey if (error == NULL) {
1344 c6b62706 2020-01-15 tracey int obj_type;
1345 8087c3c5 2020-01-15 tracey error = got_ref_resolve(&id1, repo, ref);
1346 c6b62706 2020-01-15 tracey got_ref_close(ref);
1347 c6b62706 2020-01-15 tracey if (error != NULL)
1348 c6b62706 2020-01-15 tracey goto done;
1349 8087c3c5 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, id1);
1350 c6b62706 2020-01-15 tracey if (error != NULL)
1351 c6b62706 2020-01-15 tracey goto done;
1352 c6b62706 2020-01-15 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
1353 c6b62706 2020-01-15 tracey struct got_tag_object *tag;
1354 8087c3c5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id1);
1355 c6b62706 2020-01-15 tracey if (error != NULL)
1356 c6b62706 2020-01-15 tracey goto done;
1357 c6b62706 2020-01-15 tracey if (got_object_tag_get_object_type(tag) !=
1358 c6b62706 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT) {
1359 c6b62706 2020-01-15 tracey got_object_tag_close(tag);
1360 c6b62706 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1361 c6b62706 2020-01-15 tracey goto done;
1363 8087c3c5 2020-01-15 tracey free(id1);
1364 8087c3c5 2020-01-15 tracey id1 = got_object_id_dup(
1365 c6b62706 2020-01-15 tracey got_object_tag_get_object_id(tag));
1366 8087c3c5 2020-01-15 tracey if (id1 == NULL)
1367 c6b62706 2020-01-15 tracey error = got_error_from_errno(
1368 c6b62706 2020-01-15 tracey "got_object_id_dup");
1369 c6b62706 2020-01-15 tracey got_object_tag_close(tag);
1370 c6b62706 2020-01-15 tracey if (error)
1371 c6b62706 2020-01-15 tracey goto done;
1372 c6b62706 2020-01-15 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1373 c6b62706 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1374 c6b62706 2020-01-15 tracey goto done;
1376 8087c3c5 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id1);
1377 c6b62706 2020-01-15 tracey if (error != NULL)
1378 c6b62706 2020-01-15 tracey goto done;
1380 c6b62706 2020-01-15 tracey if (commit == NULL) {
1381 8087c3c5 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id1,
1382 c6b62706 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1383 c6b62706 2020-01-15 tracey if (error != NULL)
1384 8087c3c5 2020-01-15 tracey goto done;
1386 8087c3c5 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id1,
1387 8087c3c5 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1388 8087c3c5 2020-01-15 tracey if (error != NULL)
1389 8087c3c5 2020-01-15 tracey goto done;
1392 c6b62706 2020-01-15 tracey if (error != NULL)
1393 c6b62706 2020-01-15 tracey goto done;
1395 474370cb 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1396 474370cb 2020-01-15 tracey if (error != NULL)
1397 474370cb 2020-01-15 tracey goto done;
1399 474370cb 2020-01-15 tracey if (in_repo_path) {
1400 474370cb 2020-01-15 tracey free(path);
1401 474370cb 2020-01-15 tracey path = in_repo_path;
1404 c6b62706 2020-01-15 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1405 c6b62706 2020-01-15 tracey if (error)
1406 c6b62706 2020-01-15 tracey goto done;
1408 8087c3c5 2020-01-15 tracey error = got_commit_graph_open(&graph, id1, path, 0, repo);
1409 474370cb 2020-01-15 tracey if (error)
1410 474370cb 2020-01-15 tracey goto done;
1412 8087c3c5 2020-01-15 tracey error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1413 474370cb 2020-01-15 tracey if (error)
1414 474370cb 2020-01-15 tracey goto done;
1416 474370cb 2020-01-15 tracey for (;;) {
1417 8087c3c5 2020-01-15 tracey error = got_commit_graph_iter_next(&id1, graph);
1418 474370cb 2020-01-15 tracey if (error) {
1419 474370cb 2020-01-15 tracey if (error->code == GOT_ERR_ITER_COMPLETED) {
1420 474370cb 2020-01-15 tracey error = NULL;
1423 474370cb 2020-01-15 tracey if (error->code != GOT_ERR_ITER_NEED_MORE)
1425 474370cb 2020-01-15 tracey error = got_commit_graph_fetch_commits(graph, 1, repo,
1426 474370cb 2020-01-15 tracey NULL, NULL);
1427 474370cb 2020-01-15 tracey if (error)
1430 474370cb 2020-01-15 tracey continue;
1432 8087c3c5 2020-01-15 tracey if (id1 == NULL)
1435 8087c3c5 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id1);
1436 474370cb 2020-01-15 tracey if (error)
1439 474370cb 2020-01-15 tracey if (search_pattern) {
1440 8087c3c5 2020-01-15 tracey error = match_logmsg(&have_match, id1, commit,
1441 474370cb 2020-01-15 tracey ®ex);
1442 474370cb 2020-01-15 tracey if (error) {
1443 8087c3c5 2020-01-15 tracey got_object_commit_close(commit);
1446 474370cb 2020-01-15 tracey if (have_match == 0) {
1447 8087c3c5 2020-01-15 tracey got_object_commit_close(commit);
1448 474370cb 2020-01-15 tracey continue;
1452 474370cb 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1453 8087c3c5 2020-01-15 tracey char *s;
1454 474370cb 2020-01-15 tracey const char *name;
1455 474370cb 2020-01-15 tracey struct got_tag_object *tag = NULL;
1456 474370cb 2020-01-15 tracey int cmp;
1458 474370cb 2020-01-15 tracey name = got_ref_get_name(re->ref);
1459 474370cb 2020-01-15 tracey if (strcmp(name, GOT_REF_HEAD) == 0)
1460 474370cb 2020-01-15 tracey continue;
1461 474370cb 2020-01-15 tracey if (strncmp(name, "refs/", 5) == 0)
1462 474370cb 2020-01-15 tracey name += 5;
1463 474370cb 2020-01-15 tracey if (strncmp(name, "got/", 4) == 0)
1464 474370cb 2020-01-15 tracey continue;
1465 474370cb 2020-01-15 tracey if (strncmp(name, "heads/", 6) == 0)
1466 474370cb 2020-01-15 tracey name += 6;
1467 474370cb 2020-01-15 tracey if (strncmp(name, "remotes/", 8) == 0)
1468 474370cb 2020-01-15 tracey name += 8;
1469 474370cb 2020-01-15 tracey if (strncmp(name, "tags/", 5) == 0) {
1470 474370cb 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo,
1471 474370cb 2020-01-15 tracey re->id);
1472 474370cb 2020-01-15 tracey if (error) {
1473 474370cb 2020-01-15 tracey if (error->code != GOT_ERR_OBJ_TYPE)
1474 474370cb 2020-01-15 tracey continue;
1476 474370cb 2020-01-15 tracey * Ref points at something other
1477 474370cb 2020-01-15 tracey * than a tag.
1479 474370cb 2020-01-15 tracey error = NULL;
1480 474370cb 2020-01-15 tracey tag = NULL;
1483 474370cb 2020-01-15 tracey cmp = got_object_id_cmp(tag ?
1484 8087c3c5 2020-01-15 tracey got_object_tag_get_object_id(tag) : re->id, id1);
1485 474370cb 2020-01-15 tracey if (tag)
1486 474370cb 2020-01-15 tracey got_object_tag_close(tag);
1487 474370cb 2020-01-15 tracey if (cmp != 0)
1488 474370cb 2020-01-15 tracey continue;
1489 8087c3c5 2020-01-15 tracey s = refs_str;
1490 8087c3c5 2020-01-15 tracey if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1491 8087c3c5 2020-01-15 tracey s ? ", " : "", name)) == -1) {
1492 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1493 8087c3c5 2020-01-15 tracey free(s);
1494 8087c3c5 2020-01-15 tracey goto done;
1496 8087c3c5 2020-01-15 tracey free(s);
1499 8087c3c5 2020-01-15 tracey if (refs_str == NULL)
1500 8087c3c5 2020-01-15 tracey refs_str_disp = strdup("");
1502 8087c3c5 2020-01-15 tracey if ((asprintf(&refs_str_disp, "(%s)",
1503 8087c3c5 2020-01-15 tracey refs_str)) == -1) {
1504 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1505 8087c3c5 2020-01-15 tracey free(refs_str);
1506 8087c3c5 2020-01-15 tracey goto done;
1510 8087c3c5 2020-01-15 tracey error = got_object_id_str(&id_str1, id1);
1511 474370cb 2020-01-15 tracey if (error)
1512 8087c3c5 2020-01-15 tracey goto done;
1514 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&treeid,
1515 87f9ebf5 2020-01-15 tracey got_object_commit_get_tree_id(commit));
1516 87f9ebf5 2020-01-15 tracey if (error)
1517 87f9ebf5 2020-01-15 tracey goto done;
1519 87f9ebf5 2020-01-15 tracey if (gw_trans->action == GW_COMMIT ||
1520 87f9ebf5 2020-01-15 tracey gw_trans->action == GW_COMMITDIFF) {
1521 8087c3c5 2020-01-15 tracey parent_id =
1522 8087c3c5 2020-01-15 tracey SIMPLEQ_FIRST(
1523 8087c3c5 2020-01-15 tracey got_object_commit_get_parent_ids(commit));
1524 8087c3c5 2020-01-15 tracey if (parent_id != NULL) {
1525 8087c3c5 2020-01-15 tracey id2 = got_object_id_dup(parent_id->id);
1526 8087c3c5 2020-01-15 tracey free (parent_id);
1527 8087c3c5 2020-01-15 tracey error = got_object_id_str(&id_str2, id2);
1528 8087c3c5 2020-01-15 tracey if (error)
1529 8087c3c5 2020-01-15 tracey goto done;
1530 87f9ebf5 2020-01-15 tracey free(id2);
1532 8087c3c5 2020-01-15 tracey id_str2 = strdup("/dev/null");
1535 474370cb 2020-01-15 tracey committer_time =
1536 8087c3c5 2020-01-15 tracey got_object_commit_get_committer_time(commit);
1538 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1539 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1540 8087c3c5 2020-01-15 tracey goto done;
1543 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1544 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1545 87f9ebf5 2020-01-15 tracey goto done;
1548 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_tree_disp, commit_tree_html,
1549 87f9ebf5 2020-01-15 tracey treeid)) == -1) {
1550 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1551 87f9ebf5 2020-01-15 tracey goto done;
1554 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1555 8087c3c5 2020-01-15 tracey id_str1)) == -1) {
1556 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1557 8087c3c5 2020-01-15 tracey goto done;
1560 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1561 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1562 8087c3c5 2020-01-15 tracey goto done;
1565 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_commit_disp, commit_commit_html,
1566 8087c3c5 2020-01-15 tracey commit_commit, refs_str_disp)) == -1) {
1567 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1568 8087c3c5 2020-01-15 tracey goto done;
1571 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_long, "%s",
1572 8087c3c5 2020-01-15 tracey gw_get_time_str(committer_time, TM_LONG))) == -1) {
1573 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1574 8087c3c5 2020-01-15 tracey goto done;
1577 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_long_disp, commit_age_html,
1578 8087c3c5 2020-01-15 tracey commit_age_long)) == -1) {
1579 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1580 8087c3c5 2020-01-15 tracey goto done;
1583 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_diff, "%s",
1584 8087c3c5 2020-01-15 tracey gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1585 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1586 8087c3c5 2020-01-15 tracey goto done;
1589 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_diff_disp, commit_age_html,
1590 8087c3c5 2020-01-15 tracey commit_age_diff)) == -1) {
1591 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1592 8087c3c5 2020-01-15 tracey goto done;
1595 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_author, "%s",
1596 8087c3c5 2020-01-15 tracey got_object_commit_get_author(commit))) == -1) {
1597 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1598 8087c3c5 2020-01-15 tracey goto done;
1601 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_author_disp, commit_author_html,
1602 8087c3c5 2020-01-15 tracey gw_html_escape(commit_author))) == -1) {
1603 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1604 8087c3c5 2020-01-15 tracey goto done;
1607 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_committer, "%s",
1608 8087c3c5 2020-01-15 tracey got_object_commit_get_committer(commit))) == -1) {
1609 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1610 8087c3c5 2020-01-15 tracey goto done;
1613 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_committer_disp, commit_committer_html,
1614 8087c3c5 2020-01-15 tracey gw_html_escape(commit_committer))) == -1) {
1615 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1616 8087c3c5 2020-01-15 tracey goto done;
1619 8087c3c5 2020-01-15 tracey if (strcmp(commit_author, commit_committer) == 0) {
1620 8087c3c5 2020-01-15 tracey free(commit_committer_disp);
1621 8087c3c5 2020-01-15 tracey commit_committer_disp = strdup("");
1624 8087c3c5 2020-01-15 tracey error = got_object_commit_get_logmsg(&commit_log0, commit);
1625 8087c3c5 2020-01-15 tracey if (error)
1626 8087c3c5 2020-01-15 tracey goto done;
1628 87f9ebf5 2020-01-15 tracey commit_log = commit_log0;
1629 87f9ebf5 2020-01-15 tracey while (*commit_log == '\n')
1630 87f9ebf5 2020-01-15 tracey commit_log++;
1632 8087c3c5 2020-01-15 tracey switch(log_type) {
1633 8087c3c5 2020-01-15 tracey case (LOGBRIEF):
1634 8087c3c5 2020-01-15 tracey newline = strchr(commit_log, '\n');
1635 8087c3c5 2020-01-15 tracey if (newline)
1636 8087c3c5 2020-01-15 tracey *newline = '\0';
1638 8087c3c5 2020-01-15 tracey if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1639 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1, gw_trans->repo_name,
1640 8087c3c5 2020-01-15 tracey id_str1, gw_trans->repo_name, id_str1,
1641 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1)) == -1) {
1642 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1643 8087c3c5 2020-01-15 tracey goto done;
1646 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_row, logbriefs_row,
1647 8087c3c5 2020-01-15 tracey commit_age_diff, commit_author, commit_log,
1648 8087c3c5 2020-01-15 tracey logbriefs_navs_html)) == -1) {
1649 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1650 8087c3c5 2020-01-15 tracey goto done;
1653 8087c3c5 2020-01-15 tracey free(logbriefs_navs_html);
1654 8087c3c5 2020-01-15 tracey logbriefs_navs_html = NULL;
1656 8087c3c5 2020-01-15 tracey case (LOGFULL):
1657 8087c3c5 2020-01-15 tracey if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1658 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1, gw_trans->repo_name,
1659 8087c3c5 2020-01-15 tracey id_str1, gw_trans->repo_name, id_str1,
1660 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1)) == -1) {
1661 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1662 8087c3c5 2020-01-15 tracey goto done;
1665 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1666 8087c3c5 2020-01-15 tracey commit_author_disp, commit_committer_disp,
1667 8087c3c5 2020-01-15 tracey commit_age_long_disp, gw_html_escape(commit_log),
1668 8087c3c5 2020-01-15 tracey logbriefs_navs_html)) == -1) {
1669 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1670 8087c3c5 2020-01-15 tracey goto done;
1673 8087c3c5 2020-01-15 tracey free(logbriefs_navs_html);
1674 8087c3c5 2020-01-15 tracey logbriefs_navs_html = NULL;
1676 b772de24 2020-01-15 tracey case (LOGTAG):
1677 077f6c5a 2020-01-15 tracey log_tag_html = strdup("tag log here");
1679 077f6c5a 2020-01-15 tracey if ((asprintf(&commit_row, log_tag_row,
1680 077f6c5a 2020-01-15 tracey gw_html_escape(commit_log), log_tag_html)) == -1) {
1681 b772de24 2020-01-15 tracey error = got_error_from_errno("asprintf");
1682 b772de24 2020-01-15 tracey goto done;
1685 077f6c5a 2020-01-15 tracey free(log_tag_html);
1687 8087c3c5 2020-01-15 tracey case (LOGTREE):
1688 8087c3c5 2020-01-15 tracey log_tree_html = strdup("log tree here");
1690 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_row, log_tree_row,
1691 87f9ebf5 2020-01-15 tracey gw_html_escape(commit_log), log_tree_html)) == -1) {
1692 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1693 8087c3c5 2020-01-15 tracey goto done;
1696 8087c3c5 2020-01-15 tracey free(log_tree_html);
1698 8087c3c5 2020-01-15 tracey case (LOGCOMMIT):
1699 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_log_disp, commit_log_html,
1700 8087c3c5 2020-01-15 tracey gw_html_escape(commit_log))) == -1) {
1701 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1702 8087c3c5 2020-01-15 tracey goto done;
1705 8087c3c5 2020-01-15 tracey log_commit_html = strdup("commit here");
1707 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_row, log_commit_row,
1708 8087c3c5 2020-01-15 tracey commit_diff_disp, commit_commit_disp,
1709 87f9ebf5 2020-01-15 tracey commit_tree_disp, commit_author_disp,
1710 87f9ebf5 2020-01-15 tracey commit_committer_disp, commit_age_long_disp,
1711 87f9ebf5 2020-01-15 tracey commit_log_disp, log_commit_html)) == -1) {
1712 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1713 8087c3c5 2020-01-15 tracey goto done;
1715 8087c3c5 2020-01-15 tracey free(commit_log_disp);
1716 8087c3c5 2020-01-15 tracey free(log_commit_html);
1719 87f9ebf5 2020-01-15 tracey case (LOGDIFF):
1720 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_log_disp, commit_log_html,
1721 87f9ebf5 2020-01-15 tracey gw_html_escape(commit_log))) == -1) {
1722 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1723 87f9ebf5 2020-01-15 tracey goto done;
1726 87f9ebf5 2020-01-15 tracey log_diff_html = strdup("diff here");
1728 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_row, log_diff_row,
1729 87f9ebf5 2020-01-15 tracey commit_diff_disp, commit_commit_disp,
1730 87f9ebf5 2020-01-15 tracey commit_tree_disp, commit_author_disp,
1731 87f9ebf5 2020-01-15 tracey commit_committer_disp, commit_age_long_disp,
1732 87f9ebf5 2020-01-15 tracey commit_log_disp, log_diff_html)) == -1) {
1733 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1734 87f9ebf5 2020-01-15 tracey goto done;
1736 87f9ebf5 2020-01-15 tracey free(commit_log_disp);
1737 87f9ebf5 2020-01-15 tracey free(log_diff_html);
1740 8087c3c5 2020-01-15 tracey default:
1741 8087c3c5 2020-01-15 tracey return NULL;
1744 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, commit_row);
1746 8087c3c5 2020-01-15 tracey free(commit_parent);
1747 8087c3c5 2020-01-15 tracey free(commit_diff_disp);
1748 87f9ebf5 2020-01-15 tracey free(commit_tree_disp);
1749 8087c3c5 2020-01-15 tracey free(commit_age_diff);
1750 8087c3c5 2020-01-15 tracey free(commit_age_diff_disp);
1751 8087c3c5 2020-01-15 tracey free(commit_age_long);
1752 8087c3c5 2020-01-15 tracey free(commit_age_long_disp);
1753 474370cb 2020-01-15 tracey free(commit_author);
1754 8087c3c5 2020-01-15 tracey free(commit_author_disp);
1755 8087c3c5 2020-01-15 tracey free(commit_committer);
1756 8087c3c5 2020-01-15 tracey free(commit_committer_disp);
1757 9d84e7dd 2020-01-15 tracey free(commit_log0);
1758 474370cb 2020-01-15 tracey free(commit_row);
1759 8087c3c5 2020-01-15 tracey free(refs_str_disp);
1760 8087c3c5 2020-01-15 tracey free(refs_str);
1761 8087c3c5 2020-01-15 tracey refs_str = NULL;
1762 8087c3c5 2020-01-15 tracey free(id_str1);
1763 8087c3c5 2020-01-15 tracey id_str1 = NULL;
1764 8087c3c5 2020-01-15 tracey free(id_str2);
1765 8087c3c5 2020-01-15 tracey id_str2 = NULL;
1767 474370cb 2020-01-15 tracey if (error || (limit && --limit == 0))
1771 8087c3c5 2020-01-15 tracey if (error)
1772 8087c3c5 2020-01-15 tracey goto done;
1774 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
1775 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
1776 6c6c85af 2020-01-15 tracey logs = strdup(buf_get(diffbuf));
1779 8087c3c5 2020-01-15 tracey buf_free(diffbuf);
1780 8087c3c5 2020-01-15 tracey if (commit != NULL)
1781 8087c3c5 2020-01-15 tracey got_object_commit_close(commit);
1782 8087c3c5 2020-01-15 tracey if (search_pattern)
1783 8087c3c5 2020-01-15 tracey regfree(®ex);
1784 8087c3c5 2020-01-15 tracey if (graph)
1785 8087c3c5 2020-01-15 tracey got_commit_graph_close(graph);
1786 474370cb 2020-01-15 tracey if (repo) {
1787 474370cb 2020-01-15 tracey error = got_repo_close(repo);
1788 474370cb 2020-01-15 tracey if (error != NULL)
1789 474370cb 2020-01-15 tracey return NULL;
1791 8087c3c5 2020-01-15 tracey if (error) {
1792 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, "Error: ");
1793 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
1794 8087c3c5 2020-01-15 tracey return NULL;
1796 8087c3c5 2020-01-15 tracey return logs;
1799 2c251c14 2020-01-15 tracey static char *
1800 87f9ebf5 2020-01-15 tracey gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
1802 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
1803 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
1804 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
1805 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
1806 87f9ebf5 2020-01-15 tracey char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1807 87f9ebf5 2020-01-15 tracey *age = NULL;
1808 87f9ebf5 2020-01-15 tracey char *newline;
1809 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
1810 87f9ebf5 2020-01-15 tracey size_t newsize;
1812 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1813 87f9ebf5 2020-01-15 tracey if (error != NULL)
1814 6c6c85af 2020-01-15 tracey return NULL;
1815 6c6c85af 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1817 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1818 87f9ebf5 2020-01-15 tracey if (error != NULL)
1819 87f9ebf5 2020-01-15 tracey goto done;
1821 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
1822 87f9ebf5 2020-01-15 tracey if (error)
1823 87f9ebf5 2020-01-15 tracey goto done;
1825 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1826 87f9ebf5 2020-01-15 tracey const char *refname;
1827 87f9ebf5 2020-01-15 tracey char *refstr, *tag_log0, *tag_log, *id_str;
1828 87f9ebf5 2020-01-15 tracey time_t tagger_time;
1829 87f9ebf5 2020-01-15 tracey struct got_object_id *id;
1830 87f9ebf5 2020-01-15 tracey struct got_tag_object *tag;
1832 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1833 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/tags/", 10) != 0)
1834 87f9ebf5 2020-01-15 tracey continue;
1835 87f9ebf5 2020-01-15 tracey refname += 10;
1836 87f9ebf5 2020-01-15 tracey refstr = got_ref_to_str(re->ref);
1837 87f9ebf5 2020-01-15 tracey if (refstr == NULL) {
1838 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
1839 87f9ebf5 2020-01-15 tracey goto done;
1842 87f9ebf5 2020-01-15 tracey error = got_ref_resolve(&id, repo, re->ref);
1843 87f9ebf5 2020-01-15 tracey if (error)
1844 87f9ebf5 2020-01-15 tracey goto done;
1845 87f9ebf5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
1846 87f9ebf5 2020-01-15 tracey free(id);
1847 87f9ebf5 2020-01-15 tracey if (error)
1848 87f9ebf5 2020-01-15 tracey goto done;
1850 87f9ebf5 2020-01-15 tracey tagger_time = got_object_tag_get_tagger_time(tag);
1852 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&id_str,
1853 87f9ebf5 2020-01-15 tracey got_object_tag_get_object_id(tag));
1854 87f9ebf5 2020-01-15 tracey if (error)
1855 87f9ebf5 2020-01-15 tracey goto done;
1857 87f9ebf5 2020-01-15 tracey tag_log0 = strdup(got_object_tag_get_message(tag));
1859 87f9ebf5 2020-01-15 tracey if (tag_log0 == NULL) {
1860 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("strdup");
1861 87f9ebf5 2020-01-15 tracey goto done;
1864 87f9ebf5 2020-01-15 tracey tag_log = tag_log0;
1865 87f9ebf5 2020-01-15 tracey while (*tag_log == '\n')
1866 87f9ebf5 2020-01-15 tracey tag_log++;
1868 87f9ebf5 2020-01-15 tracey switch (tag_type) {
1869 87f9ebf5 2020-01-15 tracey case TAGBRIEF:
1870 87f9ebf5 2020-01-15 tracey newline = strchr(tag_log, '\n');
1871 87f9ebf5 2020-01-15 tracey if (newline)
1872 87f9ebf5 2020-01-15 tracey *newline = '\0';
1874 87f9ebf5 2020-01-15 tracey if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
1875 87f9ebf5 2020-01-15 tracey TM_DIFF))) == -1) {
1876 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1877 87f9ebf5 2020-01-15 tracey goto done;
1880 87f9ebf5 2020-01-15 tracey if ((asprintf(&tags_navs_disp, tags_navs,
1881 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name,
1882 87f9ebf5 2020-01-15 tracey id_str, gw_trans->repo_name, id_str,
1883 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str)) == -1) {
1884 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1885 87f9ebf5 2020-01-15 tracey goto done;
1888 87f9ebf5 2020-01-15 tracey if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
1889 87f9ebf5 2020-01-15 tracey tags_navs_disp)) == -1) {
1890 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1891 87f9ebf5 2020-01-15 tracey goto done;
1894 87f9ebf5 2020-01-15 tracey free(tags_navs_disp);
1896 87f9ebf5 2020-01-15 tracey case TAGFULL:
1898 87f9ebf5 2020-01-15 tracey default:
1902 6c6c85af 2020-01-15 tracey got_object_tag_close(tag);
1904 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tag_row);
1906 6c6c85af 2020-01-15 tracey free(id_str);
1907 6c6c85af 2020-01-15 tracey free(refstr);
1908 6c6c85af 2020-01-15 tracey free(age);
1909 87f9ebf5 2020-01-15 tracey free(tag_log0);
1910 87f9ebf5 2020-01-15 tracey free(tag_row);
1912 87f9ebf5 2020-01-15 tracey if (error || (limit && --limit == 0))
1916 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
1917 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
1918 6c6c85af 2020-01-15 tracey tags = strdup(buf_get(diffbuf));
1921 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
1922 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
1923 87f9ebf5 2020-01-15 tracey if (repo)
1924 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
1925 87f9ebf5 2020-01-15 tracey if (error)
1926 87f9ebf5 2020-01-15 tracey return NULL;
1928 87f9ebf5 2020-01-15 tracey return tags;
1931 8d4d2453 2020-01-15 tracey static char *
1932 8d4d2453 2020-01-15 tracey gw_get_repo_heads(struct trans *gw_trans)
1934 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
1935 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
1936 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
1937 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
1938 87f9ebf5 2020-01-15 tracey char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
1939 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
1940 87f9ebf5 2020-01-15 tracey size_t newsize;
1942 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1943 87f9ebf5 2020-01-15 tracey if (error != NULL)
1944 6c6c85af 2020-01-15 tracey return NULL;
1946 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1947 87f9ebf5 2020-01-15 tracey if (error != NULL)
1948 87f9ebf5 2020-01-15 tracey goto done;
1950 87f9ebf5 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1951 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
1953 87f9ebf5 2020-01-15 tracey if (error)
1954 87f9ebf5 2020-01-15 tracey goto done;
1956 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1957 87f9ebf5 2020-01-15 tracey char *refname;
1959 87f9ebf5 2020-01-15 tracey refname = strdup(got_ref_get_name(re->ref));
1960 87f9ebf5 2020-01-15 tracey if (refname == NULL) {
1961 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
1962 87f9ebf5 2020-01-15 tracey goto done;
1965 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) != 0) {
1966 87f9ebf5 2020-01-15 tracey free(refname);
1967 87f9ebf5 2020-01-15 tracey continue;
1970 87f9ebf5 2020-01-15 tracey age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
1971 87f9ebf5 2020-01-15 tracey TM_DIFF);
1973 87f9ebf5 2020-01-15 tracey if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
1974 87f9ebf5 2020-01-15 tracey refname, gw_trans->repo_name, refname,
1975 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, refname, gw_trans->repo_name,
1976 87f9ebf5 2020-01-15 tracey refname)) == -1) {
1977 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1978 87f9ebf5 2020-01-15 tracey goto done;
1981 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
1982 87f9ebf5 2020-01-15 tracey refname += 11;
1984 87f9ebf5 2020-01-15 tracey if ((asprintf(&head_row, heads_row, age, refname,
1985 87f9ebf5 2020-01-15 tracey head_navs_disp)) == -1) {
1986 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1987 87f9ebf5 2020-01-15 tracey goto done;
1990 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, head_row);
1992 87f9ebf5 2020-01-15 tracey free(head_navs_disp);
1993 87f9ebf5 2020-01-15 tracey free(head_row);
1996 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
1997 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
1998 6c6c85af 2020-01-15 tracey heads = strdup(buf_get(diffbuf));
2001 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
2002 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
2003 87f9ebf5 2020-01-15 tracey if (repo)
2004 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
2005 87f9ebf5 2020-01-15 tracey if (error)
2006 87f9ebf5 2020-01-15 tracey return NULL;
2008 87f9ebf5 2020-01-15 tracey return heads;
2011 8d4d2453 2020-01-15 tracey static char *
2012 2c251c14 2020-01-15 tracey gw_get_got_link(struct trans *gw_trans)
2014 2c251c14 2020-01-15 tracey char *link;
2016 2c251c14 2020-01-15 tracey if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2017 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_logo)) == -1)
2018 2c251c14 2020-01-15 tracey return NULL;
2020 2c251c14 2020-01-15 tracey return link;
2023 2c251c14 2020-01-15 tracey static char *
2024 2c251c14 2020-01-15 tracey gw_get_site_link(struct trans *gw_trans)
2026 2c251c14 2020-01-15 tracey char *link, *repo = "", *action = "";
2028 2c251c14 2020-01-15 tracey if (gw_trans->repo_name != NULL)
2029 2c251c14 2020-01-15 tracey if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2030 2c251c14 2020-01-15 tracey "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2031 2c251c14 2020-01-15 tracey return NULL;
2033 2c251c14 2020-01-15 tracey if (gw_trans->action_name != NULL)
2034 2c251c14 2020-01-15 tracey if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2035 2c251c14 2020-01-15 tracey return NULL;
2037 2c251c14 2020-01-15 tracey if ((asprintf(&link, site_link, GOTWEB,
2038 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2039 2c251c14 2020-01-15 tracey return NULL;
2041 2c251c14 2020-01-15 tracey return link;
2044 2c251c14 2020-01-15 tracey static char *
2045 2c251c14 2020-01-15 tracey gw_html_escape(const char *html)
2047 2c251c14 2020-01-15 tracey char *escaped_str = NULL, *buf;
2048 2c251c14 2020-01-15 tracey char c[1];
2049 6c6c85af 2020-01-15 tracey size_t sz, i, buff_sz = 2048;
2051 6c6c85af 2020-01-15 tracey if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2052 2c251c14 2020-01-15 tracey return NULL;
2054 2c251c14 2020-01-15 tracey if (html == NULL)
2055 2c251c14 2020-01-15 tracey return NULL;
2057 2c251c14 2020-01-15 tracey if ((sz = strlen(html)) == 0)
2058 2c251c14 2020-01-15 tracey return NULL;
2060 6c6c85af 2020-01-15 tracey /* only work with buff_sz */
2061 6c6c85af 2020-01-15 tracey if (buff_sz < sz)
2062 6c6c85af 2020-01-15 tracey sz = buff_sz;
2064 2c251c14 2020-01-15 tracey for (i = 0; i < sz; i++) {
2065 2c251c14 2020-01-15 tracey c[0] = html[i];
2066 2c251c14 2020-01-15 tracey switch (c[0]) {
2067 2c251c14 2020-01-15 tracey case ('>'):
2068 2c251c14 2020-01-15 tracey strcat(buf, ">");
2070 2c251c14 2020-01-15 tracey case ('&'):
2071 2c251c14 2020-01-15 tracey strcat(buf, "&");
2073 2c251c14 2020-01-15 tracey case ('<'):
2074 2c251c14 2020-01-15 tracey strcat(buf, "<");
2076 2c251c14 2020-01-15 tracey case ('"'):
2077 2c251c14 2020-01-15 tracey strcat(buf, """);
2079 2c251c14 2020-01-15 tracey case ('\''):
2080 2c251c14 2020-01-15 tracey strcat(buf, "'");
2082 2c251c14 2020-01-15 tracey case ('\n'):
2083 2c251c14 2020-01-15 tracey strcat(buf, "<br />");
2084 8087c3c5 2020-01-15 tracey case ('|'):
2085 8087c3c5 2020-01-15 tracey strcat(buf, " ");
2086 2c251c14 2020-01-15 tracey default:
2087 2c251c14 2020-01-15 tracey strcat(buf, &c[0]);
2091 2c251c14 2020-01-15 tracey asprintf(&escaped_str, "%s", buf);
2092 2c251c14 2020-01-15 tracey free(buf);
2093 2c251c14 2020-01-15 tracey return escaped_str;
2099 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
2100 2c251c14 2020-01-15 tracey struct trans *gw_trans;
2101 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
2102 2c251c14 2020-01-15 tracey const char *page = "index";
2103 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
2105 2c251c14 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2106 2c251c14 2020-01-15 tracey errx(1, "malloc");
2108 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2109 2c251c14 2020-01-15 tracey errx(1, "malloc");
2111 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2112 2c251c14 2020-01-15 tracey errx(1, "malloc");
2114 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2115 2c251c14 2020-01-15 tracey errx(1, "malloc");
2117 2c251c14 2020-01-15 tracey if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
2118 2c251c14 2020-01-15 tracey &page, 1, 0))
2119 2c251c14 2020-01-15 tracey errx(1, "khttp_parse");
2121 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
2122 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
2123 4ceb8155 2020-01-15 tracey gw_malloc = 0;
2124 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
2125 2c251c14 2020-01-15 tracey goto err;
2128 46b9c89b 2020-01-15 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
2129 46b9c89b 2020-01-15 tracey error = got_error_from_errno("pledge");
2130 46b9c89b 2020-01-15 tracey goto err;
2133 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
2135 2c251c14 2020-01-15 tracey gw_trans->page = 0;
2136 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
2137 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
2138 2c251c14 2020-01-15 tracey gw_trans->commit = NULL;
2139 8087c3c5 2020-01-15 tracey gw_trans->headref = strdup(GOT_REF_HEAD);
2140 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
2141 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->key = templs;
2142 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2143 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
2144 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
2145 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2148 2c251c14 2020-01-15 tracey if (error) {
2149 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
2150 2c251c14 2020-01-15 tracey gw_trans->action = GW_ERR;
2151 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
2152 2c251c14 2020-01-15 tracey goto done;
2155 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
2156 2c251c14 2020-01-15 tracey if (error)
2157 2c251c14 2020-01-15 tracey goto err;
2159 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
2162 2c251c14 2020-01-15 tracey if (gw_malloc) {
2163 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
2164 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_www_path);
2165 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
2166 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
2167 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
2168 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
2169 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
2170 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
2171 2c251c14 2020-01-15 tracey free(gw_trans->commit);
2172 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
2173 2c251c14 2020-01-15 tracey free(gw_trans->repo_name);
2174 2c251c14 2020-01-15 tracey free(gw_trans->repo_file);
2175 2c251c14 2020-01-15 tracey free(gw_trans->action_name);
2176 8087c3c5 2020-01-15 tracey free(gw_trans->headref);
2178 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2179 2c251c14 2020-01-15 tracey free(dir->name);
2180 2c251c14 2020-01-15 tracey free(dir->description);
2181 2c251c14 2020-01-15 tracey free(dir->age);
2182 2c251c14 2020-01-15 tracey free(dir->url);
2183 2c251c14 2020-01-15 tracey free(dir->path);
2184 2c251c14 2020-01-15 tracey free(dir);
2189 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);
2190 2c251c14 2020-01-15 tracey return EXIT_SUCCESS;