Blame


1 2c251c14 2020-01-15 tracey /*
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>
5 2c251c14 2020-01-15 tracey *
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.
9 2c251c14 2020-01-15 tracey *
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.
17 2c251c14 2020-01-15 tracey */
18 2c251c14 2020-01-15 tracey
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>
22 2c251c14 2020-01-15 tracey
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>
33 2c251c14 2020-01-15 tracey
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>
45 2c251c14 2020-01-15 tracey
46 2c251c14 2020-01-15 tracey #include <kcgi.h>
47 2c251c14 2020-01-15 tracey #include <kcgihtml.h>
48 2c251c14 2020-01-15 tracey
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"
52 2c251c14 2020-01-15 tracey
53 2c251c14 2020-01-15 tracey #ifndef nitems
54 2c251c14 2020-01-15 tracey #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 2c251c14 2020-01-15 tracey #endif
56 2c251c14 2020-01-15 tracey
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 2c251c14 2020-01-15 tracey unsigned int action;
70 2c251c14 2020-01-15 tracey unsigned int page;
71 2c251c14 2020-01-15 tracey unsigned int repos_total;
72 387a29ba 2020-01-15 tracey enum kmime mime;
73 2c251c14 2020-01-15 tracey };
74 2c251c14 2020-01-15 tracey
75 2c251c14 2020-01-15 tracey enum gw_key {
76 2c251c14 2020-01-15 tracey KEY_PATH,
77 2c251c14 2020-01-15 tracey KEY_ACTION,
78 2c251c14 2020-01-15 tracey KEY_COMMIT_ID,
79 2c251c14 2020-01-15 tracey KEY_FILE,
80 2c251c14 2020-01-15 tracey KEY_PAGE,
81 2c251c14 2020-01-15 tracey KEY__MAX
82 2c251c14 2020-01-15 tracey };
83 2c251c14 2020-01-15 tracey
84 2c251c14 2020-01-15 tracey struct gw_dir {
85 2c251c14 2020-01-15 tracey TAILQ_ENTRY(gw_dir) entry;
86 2c251c14 2020-01-15 tracey char *name;
87 2c251c14 2020-01-15 tracey char *owner;
88 2c251c14 2020-01-15 tracey char *description;
89 2c251c14 2020-01-15 tracey char *url;
90 2c251c14 2020-01-15 tracey char *age;
91 2c251c14 2020-01-15 tracey char *path;
92 2c251c14 2020-01-15 tracey };
93 2c251c14 2020-01-15 tracey
94 2c251c14 2020-01-15 tracey enum tmpl {
95 2c251c14 2020-01-15 tracey TEMPL_HEAD,
96 2c251c14 2020-01-15 tracey TEMPL_HEADER,
97 2c251c14 2020-01-15 tracey TEMPL_SITEPATH,
98 2c251c14 2020-01-15 tracey TEMPL_SITEOWNER,
99 2c251c14 2020-01-15 tracey TEMPL_TITLE,
100 2c251c14 2020-01-15 tracey TEMPL_SEARCH,
101 2c251c14 2020-01-15 tracey TEMPL_CONTENT,
102 2c251c14 2020-01-15 tracey TEMPL__MAX
103 2c251c14 2020-01-15 tracey };
104 2c251c14 2020-01-15 tracey
105 387a29ba 2020-01-15 tracey enum ref_tm {
106 387a29ba 2020-01-15 tracey TM_DIFF,
107 387a29ba 2020-01-15 tracey TM_LONG,
108 387a29ba 2020-01-15 tracey };
109 387a29ba 2020-01-15 tracey
110 474370cb 2020-01-15 tracey struct buf {
111 474370cb 2020-01-15 tracey /* buffer handle, buffer size, and data length */
112 474370cb 2020-01-15 tracey u_char *cb_buf;
113 474370cb 2020-01-15 tracey size_t cb_size;
114 474370cb 2020-01-15 tracey size_t cb_len;
115 474370cb 2020-01-15 tracey };
116 474370cb 2020-01-15 tracey
117 2c251c14 2020-01-15 tracey static const char *const templs[TEMPL__MAX] = {
118 2c251c14 2020-01-15 tracey "head",
119 2c251c14 2020-01-15 tracey "header",
120 2c251c14 2020-01-15 tracey "sitepath",
121 2c251c14 2020-01-15 tracey "siteowner",
122 2c251c14 2020-01-15 tracey "title",
123 2c251c14 2020-01-15 tracey "search",
124 2c251c14 2020-01-15 tracey "content",
125 2c251c14 2020-01-15 tracey };
126 2c251c14 2020-01-15 tracey
127 2c251c14 2020-01-15 tracey static const struct kvalid gw_keys[KEY__MAX] = {
128 2c251c14 2020-01-15 tracey { kvalid_stringne, "path" },
129 2c251c14 2020-01-15 tracey { kvalid_stringne, "action" },
130 2c251c14 2020-01-15 tracey { kvalid_stringne, "commit" },
131 2c251c14 2020-01-15 tracey { kvalid_stringne, "file" },
132 2c251c14 2020-01-15 tracey { kvalid_int, "page" },
133 2c251c14 2020-01-15 tracey };
134 2c251c14 2020-01-15 tracey
135 2c251c14 2020-01-15 tracey static struct gw_dir *gw_init_gw_dir(char *);
136 2c251c14 2020-01-15 tracey
137 2c251c14 2020-01-15 tracey static char *gw_get_repo_description(struct trans *,
138 2c251c14 2020-01-15 tracey char *);
139 2c251c14 2020-01-15 tracey static char *gw_get_repo_owner(struct trans *,
140 2c251c14 2020-01-15 tracey char *);
141 474370cb 2020-01-15 tracey static char *gw_get_time_str(time_t, int);
142 2c251c14 2020-01-15 tracey static char *gw_get_repo_age(struct trans *,
143 387a29ba 2020-01-15 tracey char *, char *, int);
144 474370cb 2020-01-15 tracey static char *gw_get_repo_shortlog(struct trans *,
145 474370cb 2020-01-15 tracey const char *);
146 8d4d2453 2020-01-15 tracey static char *gw_get_repo_tags(struct trans *);
147 8d4d2453 2020-01-15 tracey static char *gw_get_repo_heads(struct trans *);
148 2c251c14 2020-01-15 tracey static char *gw_get_clone_url(struct trans *, char *);
149 2c251c14 2020-01-15 tracey static char *gw_get_got_link(struct trans *);
150 2c251c14 2020-01-15 tracey static char *gw_get_site_link(struct trans *);
151 2c251c14 2020-01-15 tracey static char *gw_html_escape(const char *);
152 2c251c14 2020-01-15 tracey
153 2c251c14 2020-01-15 tracey static void gw_display_open(struct trans *, enum khttp,
154 2c251c14 2020-01-15 tracey enum kmime);
155 2c251c14 2020-01-15 tracey static void gw_display_index(struct trans *,
156 2c251c14 2020-01-15 tracey const struct got_error *);
157 2c251c14 2020-01-15 tracey
158 2c251c14 2020-01-15 tracey static int gw_template(size_t, void *);
159 2c251c14 2020-01-15 tracey
160 2c251c14 2020-01-15 tracey static const struct got_error* apply_unveil(const char *, const char *);
161 2c251c14 2020-01-15 tracey static const struct got_error* gw_load_got_paths(struct trans *);
162 2c251c14 2020-01-15 tracey static const struct got_error* gw_load_got_path(struct trans *,
163 2c251c14 2020-01-15 tracey struct gw_dir *);
164 2c251c14 2020-01-15 tracey static const struct got_error* gw_parse_querystring(struct trans *);
165 474370cb 2020-01-15 tracey static const struct got_error* match_logmsg(int *, struct got_object_id *,
166 474370cb 2020-01-15 tracey struct got_commit_object *, regex_t *);
167 2c251c14 2020-01-15 tracey
168 2c251c14 2020-01-15 tracey static const struct got_error* gw_blame(struct trans *);
169 2c251c14 2020-01-15 tracey static const struct got_error* gw_blob(struct trans *);
170 2c251c14 2020-01-15 tracey static const struct got_error* gw_blob_diff(struct trans *);
171 2c251c14 2020-01-15 tracey static const struct got_error* gw_commit(struct trans *);
172 2c251c14 2020-01-15 tracey static const struct got_error* gw_commit_diff(struct trans *);
173 2c251c14 2020-01-15 tracey static const struct got_error* gw_history(struct trans *);
174 2c251c14 2020-01-15 tracey static const struct got_error* gw_index(struct trans *);
175 2c251c14 2020-01-15 tracey static const struct got_error* gw_log(struct trans *);
176 2c251c14 2020-01-15 tracey static const struct got_error* gw_raw(struct trans *);
177 2c251c14 2020-01-15 tracey static const struct got_error* gw_shortlog(struct trans *);
178 2c251c14 2020-01-15 tracey static const struct got_error* gw_snapshot(struct trans *);
179 387a29ba 2020-01-15 tracey static const struct got_error* gw_summary(struct trans *);
180 2c251c14 2020-01-15 tracey static const struct got_error* gw_tree(struct trans *);
181 2c251c14 2020-01-15 tracey
182 2c251c14 2020-01-15 tracey struct gw_query_action {
183 2c251c14 2020-01-15 tracey unsigned int func_id;
184 2c251c14 2020-01-15 tracey const char *func_name;
185 2c251c14 2020-01-15 tracey const struct got_error *(*func_main)(struct trans *);
186 2c251c14 2020-01-15 tracey char *template;
187 2c251c14 2020-01-15 tracey };
188 2c251c14 2020-01-15 tracey
189 2c251c14 2020-01-15 tracey enum gw_query_actions {
190 2c251c14 2020-01-15 tracey GW_BLAME,
191 2c251c14 2020-01-15 tracey GW_BLOB,
192 2c251c14 2020-01-15 tracey GW_BLOBDIFF,
193 2c251c14 2020-01-15 tracey GW_COMMIT,
194 2c251c14 2020-01-15 tracey GW_COMMITDIFF,
195 2c251c14 2020-01-15 tracey GW_ERR,
196 2c251c14 2020-01-15 tracey GW_HISTORY,
197 2c251c14 2020-01-15 tracey GW_INDEX,
198 2c251c14 2020-01-15 tracey GW_LOG,
199 2c251c14 2020-01-15 tracey GW_RAW,
200 2c251c14 2020-01-15 tracey GW_SHORTLOG,
201 2c251c14 2020-01-15 tracey GW_SNAPSHOT,
202 2c251c14 2020-01-15 tracey GW_SUMMARY,
203 2c251c14 2020-01-15 tracey GW_TREE
204 2c251c14 2020-01-15 tracey };
205 2c251c14 2020-01-15 tracey
206 2c251c14 2020-01-15 tracey static struct gw_query_action gw_query_funcs[] = {
207 2c251c14 2020-01-15 tracey { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
208 2c251c14 2020-01-15 tracey { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
209 2c251c14 2020-01-15 tracey { GW_BLOBDIFF, "blobdiff", gw_blob_diff, "gw_tmpl/index.tmpl" },
210 2c251c14 2020-01-15 tracey { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
211 2c251c14 2020-01-15 tracey { GW_COMMITDIFF, "commit_diff", gw_commit_diff, "gw_tmpl/index.tmpl" },
212 387a29ba 2020-01-15 tracey { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
213 2c251c14 2020-01-15 tracey { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
214 2c251c14 2020-01-15 tracey { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
215 2c251c14 2020-01-15 tracey { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
216 2c251c14 2020-01-15 tracey { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
217 2c251c14 2020-01-15 tracey { GW_SHORTLOG, "shortlog", gw_shortlog, "gw_tmpl/index.tmpl" },
218 2c251c14 2020-01-15 tracey { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
219 46b9c89b 2020-01-15 tracey { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
220 2c251c14 2020-01-15 tracey { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
221 2c251c14 2020-01-15 tracey };
222 2c251c14 2020-01-15 tracey
223 2c251c14 2020-01-15 tracey static const struct got_error *
224 2c251c14 2020-01-15 tracey apply_unveil(const char *repo_path, const char *repo_file)
225 2c251c14 2020-01-15 tracey {
226 2c251c14 2020-01-15 tracey const struct got_error *err;
227 2c251c14 2020-01-15 tracey
228 2c251c14 2020-01-15 tracey if (repo_path && repo_file) {
229 2c251c14 2020-01-15 tracey char *full_path;
230 2c251c14 2020-01-15 tracey if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
231 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf unveil");
232 2c251c14 2020-01-15 tracey if (unveil(full_path, "r") != 0)
233 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", full_path);
234 2c251c14 2020-01-15 tracey }
235 2c251c14 2020-01-15 tracey
236 2c251c14 2020-01-15 tracey if (repo_path && unveil(repo_path, "r") != 0)
237 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", repo_path);
238 2c251c14 2020-01-15 tracey
239 2c251c14 2020-01-15 tracey if (unveil("/tmp", "rwc") != 0)
240 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", "/tmp");
241 2c251c14 2020-01-15 tracey
242 2c251c14 2020-01-15 tracey err = got_privsep_unveil_exec_helpers();
243 2c251c14 2020-01-15 tracey if (err != NULL)
244 2c251c14 2020-01-15 tracey return err;
245 2c251c14 2020-01-15 tracey
246 2c251c14 2020-01-15 tracey if (unveil(NULL, NULL) != 0)
247 2c251c14 2020-01-15 tracey return got_error_from_errno("unveil");
248 2c251c14 2020-01-15 tracey
249 2c251c14 2020-01-15 tracey return NULL;
250 2c251c14 2020-01-15 tracey }
251 2c251c14 2020-01-15 tracey
252 2c251c14 2020-01-15 tracey static const struct got_error *
253 2c251c14 2020-01-15 tracey gw_blame(struct trans *gw_trans)
254 2c251c14 2020-01-15 tracey {
255 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
256 2c251c14 2020-01-15 tracey
257 2c251c14 2020-01-15 tracey return error;
258 2c251c14 2020-01-15 tracey }
259 2c251c14 2020-01-15 tracey
260 2c251c14 2020-01-15 tracey static const struct got_error *
261 2c251c14 2020-01-15 tracey gw_blob(struct trans *gw_trans)
262 2c251c14 2020-01-15 tracey {
263 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
264 2c251c14 2020-01-15 tracey
265 2c251c14 2020-01-15 tracey return error;
266 2c251c14 2020-01-15 tracey }
267 2c251c14 2020-01-15 tracey
268 2c251c14 2020-01-15 tracey static const struct got_error *
269 2c251c14 2020-01-15 tracey gw_blob_diff(struct trans *gw_trans)
270 2c251c14 2020-01-15 tracey {
271 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
272 2c251c14 2020-01-15 tracey
273 2c251c14 2020-01-15 tracey return error;
274 2c251c14 2020-01-15 tracey }
275 2c251c14 2020-01-15 tracey
276 2c251c14 2020-01-15 tracey static const struct got_error *
277 2c251c14 2020-01-15 tracey gw_commit(struct trans *gw_trans)
278 2c251c14 2020-01-15 tracey {
279 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
280 2c251c14 2020-01-15 tracey
281 2c251c14 2020-01-15 tracey return error;
282 2c251c14 2020-01-15 tracey }
283 2c251c14 2020-01-15 tracey
284 2c251c14 2020-01-15 tracey static const struct got_error *
285 2c251c14 2020-01-15 tracey gw_commit_diff(struct trans *gw_trans)
286 2204c934 2020-01-15 tracey {
287 2204c934 2020-01-15 tracey const struct got_error *error = NULL;
288 2204c934 2020-01-15 tracey
289 2204c934 2020-01-15 tracey return error;
290 2204c934 2020-01-15 tracey }
291 2204c934 2020-01-15 tracey
292 2204c934 2020-01-15 tracey static const struct got_error *
293 2c251c14 2020-01-15 tracey gw_history(struct trans *gw_trans)
294 2c251c14 2020-01-15 tracey {
295 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
296 2c251c14 2020-01-15 tracey
297 2c251c14 2020-01-15 tracey return error;
298 2c251c14 2020-01-15 tracey }
299 2c251c14 2020-01-15 tracey
300 2c251c14 2020-01-15 tracey static const struct got_error *
301 2c251c14 2020-01-15 tracey gw_index(struct trans *gw_trans)
302 2c251c14 2020-01-15 tracey {
303 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
304 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
305 2c251c14 2020-01-15 tracey char *html, *navs, *next, *prev;
306 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
307 2c251c14 2020-01-15 tracey
308 46b9c89b 2020-01-15 tracey error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
309 46b9c89b 2020-01-15 tracey if (error)
310 46b9c89b 2020-01-15 tracey return error;
311 46b9c89b 2020-01-15 tracey
312 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
313 46b9c89b 2020-01-15 tracey if (error)
314 2c251c14 2020-01-15 tracey return error;
315 2c251c14 2020-01-15 tracey
316 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, index_projects_header);
317 2c251c14 2020-01-15 tracey
318 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
319 2c251c14 2020-01-15 tracey dir_c++;
320 2c251c14 2020-01-15 tracey
321 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
322 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
323 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
324 2c251c14 2020-01-15 tracey prev_disp++;
325 2c251c14 2020-01-15 tracey continue;
326 2c251c14 2020-01-15 tracey }
327 2c251c14 2020-01-15 tracey
328 2c251c14 2020-01-15 tracey prev_disp++;
329 46b9c89b 2020-01-15 tracey if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
330 46b9c89b 2020-01-15 tracey gw_dir->name, gw_dir->name)) == -1)
331 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
332 2c251c14 2020-01-15 tracey
333 46b9c89b 2020-01-15 tracey if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
334 46b9c89b 2020-01-15 tracey gw_dir->description, gw_dir->owner, gw_dir->age,
335 46b9c89b 2020-01-15 tracey navs)) == -1)
336 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
337 2c251c14 2020-01-15 tracey
338 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, html);
339 2c251c14 2020-01-15 tracey
340 2c251c14 2020-01-15 tracey free(navs);
341 2c251c14 2020-01-15 tracey free(html);
342 2c251c14 2020-01-15 tracey
343 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
344 2c251c14 2020-01-15 tracey continue;
345 2c251c14 2020-01-15 tracey
346 2c251c14 2020-01-15 tracey if (next_disp == gw_trans->gw_conf->got_max_repos_display)
347 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, np_wrapper_start);
348 2c251c14 2020-01-15 tracey else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
349 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
350 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
351 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total))
352 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, np_wrapper_start);
353 2c251c14 2020-01-15 tracey
354 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
355 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
356 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
357 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
358 2c251c14 2020-01-15 tracey if ((asprintf(&prev, nav_prev,
359 2c251c14 2020-01-15 tracey gw_trans->page - 1)) == -1)
360 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
361 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, prev);
362 2c251c14 2020-01-15 tracey free(prev);
363 2c251c14 2020-01-15 tracey }
364 2c251c14 2020-01-15 tracey
365 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
366 2c251c14 2020-01-15 tracey
367 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
368 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
369 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
370 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
371 2c251c14 2020-01-15 tracey if ((asprintf(&next, nav_next,
372 2c251c14 2020-01-15 tracey gw_trans->page + 1)) == -1)
373 2c251c14 2020-01-15 tracey return got_error_from_errno("calloc");
374 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, next);
375 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
376 2c251c14 2020-01-15 tracey free(next);
377 2c251c14 2020-01-15 tracey next_disp = 0;
378 2c251c14 2020-01-15 tracey break;
379 2c251c14 2020-01-15 tracey }
380 2c251c14 2020-01-15 tracey
381 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
382 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
383 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
384 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total))
385 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
386 2c251c14 2020-01-15 tracey
387 2c251c14 2020-01-15 tracey next_disp++;
388 2c251c14 2020-01-15 tracey }
389 2c251c14 2020-01-15 tracey return error;
390 2c251c14 2020-01-15 tracey }
391 2c251c14 2020-01-15 tracey
392 2c251c14 2020-01-15 tracey static const struct got_error *
393 2c251c14 2020-01-15 tracey gw_log(struct trans *gw_trans)
394 2c251c14 2020-01-15 tracey {
395 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
396 2c251c14 2020-01-15 tracey
397 2c251c14 2020-01-15 tracey return error;
398 2c251c14 2020-01-15 tracey }
399 2c251c14 2020-01-15 tracey
400 2c251c14 2020-01-15 tracey static const struct got_error *
401 2c251c14 2020-01-15 tracey gw_raw(struct trans *gw_trans)
402 2c251c14 2020-01-15 tracey {
403 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
404 2c251c14 2020-01-15 tracey
405 2c251c14 2020-01-15 tracey return error;
406 2c251c14 2020-01-15 tracey }
407 2c251c14 2020-01-15 tracey
408 2c251c14 2020-01-15 tracey static const struct got_error *
409 2c251c14 2020-01-15 tracey gw_shortlog(struct trans *gw_trans)
410 2c251c14 2020-01-15 tracey {
411 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
412 17a96b9f 2020-01-15 tracey
413 2c251c14 2020-01-15 tracey return error;
414 2c251c14 2020-01-15 tracey }
415 2c251c14 2020-01-15 tracey
416 2c251c14 2020-01-15 tracey static const struct got_error *
417 2c251c14 2020-01-15 tracey gw_snapshot(struct trans *gw_trans)
418 2c251c14 2020-01-15 tracey {
419 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
420 2c251c14 2020-01-15 tracey
421 2c251c14 2020-01-15 tracey return error;
422 2c251c14 2020-01-15 tracey }
423 2c251c14 2020-01-15 tracey
424 2c251c14 2020-01-15 tracey static const struct got_error *
425 387a29ba 2020-01-15 tracey gw_summary(struct trans *gw_trans)
426 387a29ba 2020-01-15 tracey {
427 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
428 46b9c89b 2020-01-15 tracey char *description_html, *repo_owner_html, *repo_age_html,
429 8d4d2453 2020-01-15 tracey *cloneurl_html, *shortlog, *tags, *heads, *shortlog_html,
430 c6b62706 2020-01-15 tracey *tags_html, *heads_html, *age;
431 387a29ba 2020-01-15 tracey
432 46b9c89b 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
433 46b9c89b 2020-01-15 tracey if (error)
434 46b9c89b 2020-01-15 tracey return error;
435 46b9c89b 2020-01-15 tracey
436 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, summary_wrapper);
437 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description) {
438 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->description != NULL &&
439 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
440 46b9c89b 2020-01-15 tracey if ((asprintf(&description_html, description,
441 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->description)) == -1)
442 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
443 46b9c89b 2020-01-15 tracey
444 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, description_html);
445 46b9c89b 2020-01-15 tracey free(description_html);
446 46b9c89b 2020-01-15 tracey }
447 46b9c89b 2020-01-15 tracey }
448 46b9c89b 2020-01-15 tracey
449 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
450 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->owner != NULL &&
451 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
452 46b9c89b 2020-01-15 tracey if ((asprintf(&repo_owner_html, repo_owner,
453 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->owner)) == -1)
454 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
455 46b9c89b 2020-01-15 tracey
456 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, repo_owner_html);
457 46b9c89b 2020-01-15 tracey free(repo_owner_html);
458 46b9c89b 2020-01-15 tracey }
459 46b9c89b 2020-01-15 tracey }
460 46b9c89b 2020-01-15 tracey
461 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
462 c6b62706 2020-01-15 tracey age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
463 c6b62706 2020-01-15 tracey "refs/heads", TM_LONG);
464 c6b62706 2020-01-15 tracey if (age != NULL && (strcmp(age, "") != 0)) {
465 c6b62706 2020-01-15 tracey if ((asprintf(&repo_age_html, last_change, age)) == -1)
466 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
467 46b9c89b 2020-01-15 tracey
468 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, repo_age_html);
469 46b9c89b 2020-01-15 tracey free(repo_age_html);
470 c6b62706 2020-01-15 tracey free(age);
471 46b9c89b 2020-01-15 tracey }
472 46b9c89b 2020-01-15 tracey }
473 46b9c89b 2020-01-15 tracey
474 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl) {
475 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->url != NULL &&
476 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
477 46b9c89b 2020-01-15 tracey if ((asprintf(&cloneurl_html, cloneurl,
478 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->url)) == -1)
479 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
480 46b9c89b 2020-01-15 tracey
481 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, cloneurl_html);
482 46b9c89b 2020-01-15 tracey free(cloneurl_html);
483 46b9c89b 2020-01-15 tracey }
484 46b9c89b 2020-01-15 tracey }
485 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
486 46b9c89b 2020-01-15 tracey
487 474370cb 2020-01-15 tracey shortlog = gw_get_repo_shortlog(gw_trans, NULL);
488 8d4d2453 2020-01-15 tracey tags = gw_get_repo_tags(gw_trans);
489 8d4d2453 2020-01-15 tracey heads = gw_get_repo_heads(gw_trans);
490 387a29ba 2020-01-15 tracey
491 8d4d2453 2020-01-15 tracey if (shortlog != NULL && strcmp(shortlog, "") != 0) {
492 8d4d2453 2020-01-15 tracey if ((asprintf(&shortlog_html, summary_shortlog,
493 8d4d2453 2020-01-15 tracey shortlog)) == -1)
494 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
495 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, shortlog_html);
496 8d4d2453 2020-01-15 tracey free(shortlog_html);
497 8d4d2453 2020-01-15 tracey free(shortlog);
498 8d4d2453 2020-01-15 tracey }
499 8d4d2453 2020-01-15 tracey
500 8d4d2453 2020-01-15 tracey if (tags != NULL && strcmp(tags, "") != 0) {
501 8d4d2453 2020-01-15 tracey if ((asprintf(&tags_html, summary_tags,
502 8d4d2453 2020-01-15 tracey tags)) == -1)
503 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
504 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, tags_html);
505 8d4d2453 2020-01-15 tracey free(tags_html);
506 8d4d2453 2020-01-15 tracey free(tags);
507 8d4d2453 2020-01-15 tracey }
508 8d4d2453 2020-01-15 tracey
509 8d4d2453 2020-01-15 tracey if (heads != NULL && strcmp(heads, "") != 0) {
510 8d4d2453 2020-01-15 tracey if ((asprintf(&heads_html, summary_heads,
511 8d4d2453 2020-01-15 tracey heads)) == -1)
512 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
513 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, heads_html);
514 8d4d2453 2020-01-15 tracey free(heads_html);
515 8d4d2453 2020-01-15 tracey free(heads);
516 8d4d2453 2020-01-15 tracey }
517 2204c934 2020-01-15 tracey
518 2204c934 2020-01-15 tracey return error;
519 2204c934 2020-01-15 tracey }
520 2204c934 2020-01-15 tracey
521 2204c934 2020-01-15 tracey static const struct got_error *
522 2c251c14 2020-01-15 tracey gw_tree(struct trans *gw_trans)
523 2c251c14 2020-01-15 tracey {
524 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
525 2c251c14 2020-01-15 tracey
526 2c251c14 2020-01-15 tracey return error;
527 2c251c14 2020-01-15 tracey }
528 2c251c14 2020-01-15 tracey
529 2c251c14 2020-01-15 tracey static const struct got_error *
530 2c251c14 2020-01-15 tracey gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
531 2c251c14 2020-01-15 tracey {
532 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
533 2c251c14 2020-01-15 tracey DIR *dt;
534 2c251c14 2020-01-15 tracey char *dir_test;
535 2c251c14 2020-01-15 tracey bool opened = false;
536 2c251c14 2020-01-15 tracey
537 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
538 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
539 2c251c14 2020-01-15 tracey GOTWEB_GIT_DIR)) == -1)
540 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
541 2c251c14 2020-01-15 tracey
542 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
543 2c251c14 2020-01-15 tracey if (dt == NULL) {
544 2c251c14 2020-01-15 tracey free(dir_test);
545 2c251c14 2020-01-15 tracey } else {
546 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
547 2c251c14 2020-01-15 tracey opened = true;
548 2c251c14 2020-01-15 tracey goto done;
549 2c251c14 2020-01-15 tracey }
550 2c251c14 2020-01-15 tracey
551 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
552 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
553 2c251c14 2020-01-15 tracey GOTWEB_GOT_DIR)) == -1)
554 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
555 2c251c14 2020-01-15 tracey
556 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
557 2c251c14 2020-01-15 tracey if (dt == NULL)
558 2c251c14 2020-01-15 tracey free(dir_test);
559 2c251c14 2020-01-15 tracey else {
560 2c251c14 2020-01-15 tracey opened = true;
561 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
562 2c251c14 2020-01-15 tracey goto errored;
563 2c251c14 2020-01-15 tracey }
564 2c251c14 2020-01-15 tracey
565 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s",
566 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
567 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
568 2c251c14 2020-01-15 tracey
569 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
570 2c251c14 2020-01-15 tracey
571 2c251c14 2020-01-15 tracey done:
572 2c251c14 2020-01-15 tracey gw_dir->description = gw_get_repo_description(gw_trans,
573 2c251c14 2020-01-15 tracey gw_dir->path);
574 2c251c14 2020-01-15 tracey gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
575 387a29ba 2020-01-15 tracey gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
576 387a29ba 2020-01-15 tracey TM_DIFF);
577 2c251c14 2020-01-15 tracey gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
578 2c251c14 2020-01-15 tracey
579 2c251c14 2020-01-15 tracey errored:
580 2c251c14 2020-01-15 tracey free(dir_test);
581 2c251c14 2020-01-15 tracey if (opened)
582 2c251c14 2020-01-15 tracey closedir(dt);
583 2c251c14 2020-01-15 tracey return error;
584 2c251c14 2020-01-15 tracey }
585 2c251c14 2020-01-15 tracey
586 2c251c14 2020-01-15 tracey static const struct got_error *
587 2c251c14 2020-01-15 tracey gw_load_got_paths(struct trans *gw_trans)
588 2c251c14 2020-01-15 tracey {
589 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
590 2c251c14 2020-01-15 tracey DIR *d;
591 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
592 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
593 2c251c14 2020-01-15 tracey struct stat st;
594 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
595 2c251c14 2020-01-15 tracey
596 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
597 2c251c14 2020-01-15 tracey if (d == NULL) {
598 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
599 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
600 2c251c14 2020-01-15 tracey return error;
601 2c251c14 2020-01-15 tracey }
602 2c251c14 2020-01-15 tracey
603 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
604 2c251c14 2020-01-15 tracey alphasort);
605 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
606 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
607 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
608 2c251c14 2020-01-15 tracey return error;
609 2c251c14 2020-01-15 tracey }
610 2c251c14 2020-01-15 tracey
611 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
612 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
613 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
614 2c251c14 2020-01-15 tracey break; /* account for parent and self */
615 2c251c14 2020-01-15 tracey
616 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
617 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
618 2c251c14 2020-01-15 tracey continue;
619 2c251c14 2020-01-15 tracey
620 2c251c14 2020-01-15 tracey if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
621 2c251c14 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
622 2c251c14 2020-01-15 tracey
623 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
624 2c251c14 2020-01-15 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO)
625 2c251c14 2020-01-15 tracey continue;
626 2c251c14 2020-01-15 tracey else if (error)
627 2c251c14 2020-01-15 tracey return error;
628 2c251c14 2020-01-15 tracey
629 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
630 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
631 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
632 2c251c14 2020-01-15 tracey entry);
633 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
634 2c251c14 2020-01-15 tracey }
635 2c251c14 2020-01-15 tracey }
636 2c251c14 2020-01-15 tracey
637 2c251c14 2020-01-15 tracey closedir(d);
638 2c251c14 2020-01-15 tracey return error;
639 2c251c14 2020-01-15 tracey }
640 2c251c14 2020-01-15 tracey
641 2c251c14 2020-01-15 tracey static const struct got_error *
642 2c251c14 2020-01-15 tracey gw_parse_querystring(struct trans *gw_trans)
643 2c251c14 2020-01-15 tracey {
644 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
645 2c251c14 2020-01-15 tracey struct kpair *p;
646 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
647 2c251c14 2020-01-15 tracey unsigned int i;
648 2c251c14 2020-01-15 tracey
649 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
650 2c251c14 2020-01-15 tracey error = got_error_from_errno("bad parse");
651 2c251c14 2020-01-15 tracey return error;
652 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
653 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
654 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
655 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
656 2c251c14 2020-01-15 tracey
657 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_path, "%s/%s",
658 387a29ba 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
659 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
660 2c251c14 2020-01-15 tracey
661 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
662 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->commit, "%s",
663 2c251c14 2020-01-15 tracey p->parsed.s)) == -1)
664 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
665 2c251c14 2020-01-15 tracey
666 2c251c14 2020-01-15 tracey /* get action and set function */
667 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
668 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
669 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
670 2c251c14 2020-01-15 tracey if (action->func_name == NULL)
671 2c251c14 2020-01-15 tracey continue;
672 2c251c14 2020-01-15 tracey
673 2c251c14 2020-01-15 tracey if (strcmp(action->func_name,
674 2c251c14 2020-01-15 tracey p->parsed.s) == 0) {
675 2c251c14 2020-01-15 tracey gw_trans->action = i;
676 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->action_name,
677 2c251c14 2020-01-15 tracey "%s", action->func_name)) == -1)
678 2c251c14 2020-01-15 tracey return
679 2c251c14 2020-01-15 tracey got_error_from_errno(
680 2c251c14 2020-01-15 tracey "asprintf");
681 2c251c14 2020-01-15 tracey
682 2c251c14 2020-01-15 tracey break;
683 2c251c14 2020-01-15 tracey }
684 2c251c14 2020-01-15 tracey
685 2c251c14 2020-01-15 tracey action = NULL;
686 2c251c14 2020-01-15 tracey }
687 2c251c14 2020-01-15 tracey
688 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
689 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_file, "%s",
690 2c251c14 2020-01-15 tracey p->parsed.s)) == -1)
691 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
692 2c251c14 2020-01-15 tracey
693 2c251c14 2020-01-15 tracey if (action == NULL) {
694 2c251c14 2020-01-15 tracey error = got_error_from_errno("invalid action");
695 2c251c14 2020-01-15 tracey return error;
696 2c251c14 2020-01-15 tracey }
697 46b9c89b 2020-01-15 tracey if ((gw_trans->gw_dir =
698 46b9c89b 2020-01-15 tracey gw_init_gw_dir(gw_trans->repo_name)) == NULL)
699 46b9c89b 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
700 46b9c89b 2020-01-15 tracey
701 46b9c89b 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
702 46b9c89b 2020-01-15 tracey if (error)
703 46b9c89b 2020-01-15 tracey return error;
704 2c251c14 2020-01-15 tracey } else
705 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
706 2c251c14 2020-01-15 tracey
707 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
708 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
709 2c251c14 2020-01-15 tracey
710 2c251c14 2020-01-15 tracey if (gw_trans->action == GW_RAW)
711 387a29ba 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
712 2c251c14 2020-01-15 tracey
713 2c251c14 2020-01-15 tracey return error;
714 2c251c14 2020-01-15 tracey }
715 2c251c14 2020-01-15 tracey
716 2c251c14 2020-01-15 tracey static struct gw_dir *
717 2c251c14 2020-01-15 tracey gw_init_gw_dir(char *dir)
718 2c251c14 2020-01-15 tracey {
719 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
720 2c251c14 2020-01-15 tracey
721 2c251c14 2020-01-15 tracey if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
722 2c251c14 2020-01-15 tracey return NULL;
723 2c251c14 2020-01-15 tracey
724 2c251c14 2020-01-15 tracey if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
725 2c251c14 2020-01-15 tracey return NULL;
726 2c251c14 2020-01-15 tracey
727 2c251c14 2020-01-15 tracey return gw_dir;
728 474370cb 2020-01-15 tracey }
729 474370cb 2020-01-15 tracey
730 474370cb 2020-01-15 tracey static const struct got_error*
731 474370cb 2020-01-15 tracey match_logmsg(int *have_match, struct got_object_id *id,
732 474370cb 2020-01-15 tracey struct got_commit_object *commit, regex_t *regex)
733 474370cb 2020-01-15 tracey {
734 474370cb 2020-01-15 tracey const struct got_error *err = NULL;
735 474370cb 2020-01-15 tracey regmatch_t regmatch;
736 474370cb 2020-01-15 tracey char *id_str = NULL, *logmsg = NULL;
737 474370cb 2020-01-15 tracey
738 474370cb 2020-01-15 tracey *have_match = 0;
739 474370cb 2020-01-15 tracey
740 474370cb 2020-01-15 tracey err = got_object_id_str(&id_str, id);
741 474370cb 2020-01-15 tracey if (err)
742 474370cb 2020-01-15 tracey return err;
743 474370cb 2020-01-15 tracey
744 474370cb 2020-01-15 tracey err = got_object_commit_get_logmsg(&logmsg, commit);
745 474370cb 2020-01-15 tracey if (err)
746 474370cb 2020-01-15 tracey goto done;
747 474370cb 2020-01-15 tracey
748 474370cb 2020-01-15 tracey if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
749 474370cb 2020-01-15 tracey *have_match = 1;
750 474370cb 2020-01-15 tracey done:
751 474370cb 2020-01-15 tracey free(id_str);
752 474370cb 2020-01-15 tracey free(logmsg);
753 474370cb 2020-01-15 tracey return err;
754 2c251c14 2020-01-15 tracey }
755 2c251c14 2020-01-15 tracey
756 2c251c14 2020-01-15 tracey static void
757 2c251c14 2020-01-15 tracey gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
758 2c251c14 2020-01-15 tracey {
759 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
760 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
761 2c251c14 2020-01-15 tracey khttps[code]);
762 387a29ba 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
763 2c251c14 2020-01-15 tracey kmimetypes[mime]);
764 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
765 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
766 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
767 2c251c14 2020-01-15 tracey khttp_body(gw_trans->gw_req);
768 2c251c14 2020-01-15 tracey }
769 2c251c14 2020-01-15 tracey
770 2c251c14 2020-01-15 tracey static void
771 2c251c14 2020-01-15 tracey gw_display_index(struct trans *gw_trans, const struct got_error *err)
772 2c251c14 2020-01-15 tracey {
773 2c251c14 2020-01-15 tracey gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
774 2c251c14 2020-01-15 tracey khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
775 2c251c14 2020-01-15 tracey
776 2c251c14 2020-01-15 tracey if (err)
777 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, err->msg);
778 2c251c14 2020-01-15 tracey else
779 2c251c14 2020-01-15 tracey khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
780 2c251c14 2020-01-15 tracey gw_query_funcs[gw_trans->action].template);
781 2c251c14 2020-01-15 tracey
782 2c251c14 2020-01-15 tracey khtml_close(gw_trans->gw_html_req);
783 2c251c14 2020-01-15 tracey }
784 2c251c14 2020-01-15 tracey
785 2c251c14 2020-01-15 tracey static int
786 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
787 2c251c14 2020-01-15 tracey {
788 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
789 2c251c14 2020-01-15 tracey struct trans *gw_trans = arg;
790 2c251c14 2020-01-15 tracey char *gw_got_link, *gw_site_link;
791 2c251c14 2020-01-15 tracey char *site_owner_name, *site_owner_name_h;
792 2c251c14 2020-01-15 tracey
793 2c251c14 2020-01-15 tracey switch (key) {
794 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
795 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, head);
796 2c251c14 2020-01-15 tracey break;
797 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
798 2c251c14 2020-01-15 tracey gw_got_link = gw_get_got_link(gw_trans);
799 2c251c14 2020-01-15 tracey if (gw_got_link != NULL)
800 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_got_link);
801 2c251c14 2020-01-15 tracey
802 2c251c14 2020-01-15 tracey free(gw_got_link);
803 2c251c14 2020-01-15 tracey break;
804 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
805 2c251c14 2020-01-15 tracey gw_site_link = gw_get_site_link(gw_trans);
806 2c251c14 2020-01-15 tracey if (gw_site_link != NULL)
807 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_site_link);
808 2c251c14 2020-01-15 tracey
809 2c251c14 2020-01-15 tracey free(gw_site_link);
810 2c251c14 2020-01-15 tracey break;
811 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
812 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_name != NULL)
813 2c251c14 2020-01-15 tracey khtml_puts(gw_trans->gw_html_req,
814 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
815 2c251c14 2020-01-15 tracey
816 2c251c14 2020-01-15 tracey break;
817 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
818 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, search);
819 2c251c14 2020-01-15 tracey break;
820 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
821 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
822 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
823 2c251c14 2020-01-15 tracey site_owner_name =
824 2c251c14 2020-01-15 tracey gw_html_escape(gw_trans->gw_conf->got_site_owner);
825 2c251c14 2020-01-15 tracey if ((asprintf(&site_owner_name_h, site_owner,
826 2c251c14 2020-01-15 tracey site_owner_name))
827 2c251c14 2020-01-15 tracey == -1)
828 2c251c14 2020-01-15 tracey return 0;
829 2c251c14 2020-01-15 tracey
830 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, site_owner_name_h);
831 2c251c14 2020-01-15 tracey free(site_owner_name);
832 2c251c14 2020-01-15 tracey free(site_owner_name_h);
833 2c251c14 2020-01-15 tracey }
834 2c251c14 2020-01-15 tracey break;
835 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
836 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
837 2c251c14 2020-01-15 tracey if (error)
838 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
839 2c251c14 2020-01-15 tracey
840 2c251c14 2020-01-15 tracey break;
841 2c251c14 2020-01-15 tracey default:
842 2c251c14 2020-01-15 tracey return 0;
843 2c251c14 2020-01-15 tracey break;
844 2c251c14 2020-01-15 tracey }
845 2c251c14 2020-01-15 tracey return 1;
846 2c251c14 2020-01-15 tracey }
847 2c251c14 2020-01-15 tracey
848 2c251c14 2020-01-15 tracey static char *
849 2c251c14 2020-01-15 tracey gw_get_repo_description(struct trans *gw_trans, char *dir)
850 2c251c14 2020-01-15 tracey {
851 2c251c14 2020-01-15 tracey FILE *f;
852 2c251c14 2020-01-15 tracey char *description = NULL, *d_file = NULL;
853 2c251c14 2020-01-15 tracey unsigned int len;
854 2c251c14 2020-01-15 tracey
855 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description == false)
856 2c251c14 2020-01-15 tracey goto err;
857 2c251c14 2020-01-15 tracey
858 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/description", dir)) == -1)
859 2c251c14 2020-01-15 tracey goto err;
860 2c251c14 2020-01-15 tracey
861 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
862 2c251c14 2020-01-15 tracey goto err;
863 2c251c14 2020-01-15 tracey
864 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
865 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
866 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
867 2c251c14 2020-01-15 tracey if ((description = calloc(len, sizeof(char *))) == NULL)
868 2c251c14 2020-01-15 tracey goto err;
869 2c251c14 2020-01-15 tracey
870 2c251c14 2020-01-15 tracey fread(description, 1, len, f);
871 2c251c14 2020-01-15 tracey fclose(f);
872 2c251c14 2020-01-15 tracey free(d_file);
873 2c251c14 2020-01-15 tracey return description;
874 2c251c14 2020-01-15 tracey err:
875 2c251c14 2020-01-15 tracey if ((asprintf(&description, "%s", "")) == -1)
876 2c251c14 2020-01-15 tracey return NULL;
877 2c251c14 2020-01-15 tracey
878 2c251c14 2020-01-15 tracey return description;
879 2c251c14 2020-01-15 tracey }
880 2c251c14 2020-01-15 tracey
881 2c251c14 2020-01-15 tracey static char *
882 474370cb 2020-01-15 tracey gw_get_time_str(time_t committer_time, int ref_tm)
883 474370cb 2020-01-15 tracey {
884 474370cb 2020-01-15 tracey struct tm tm;
885 474370cb 2020-01-15 tracey time_t diff_time;
886 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
887 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
888 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
889 474370cb 2020-01-15 tracey char *now = "right now";
890 474370cb 2020-01-15 tracey char *repo_age, *s;
891 474370cb 2020-01-15 tracey char datebuf[BUFFER_SIZE];
892 474370cb 2020-01-15 tracey
893 474370cb 2020-01-15 tracey switch (ref_tm) {
894 474370cb 2020-01-15 tracey case TM_DIFF:
895 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
896 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
897 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
898 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 365), years)) == -1)
899 474370cb 2020-01-15 tracey return NULL;
900 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
901 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
902 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
903 474370cb 2020-01-15 tracey months)) == -1)
904 474370cb 2020-01-15 tracey return NULL;
905 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
906 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
907 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
908 474370cb 2020-01-15 tracey return NULL;
909 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
910 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
911 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24), days)) == -1)
912 474370cb 2020-01-15 tracey return NULL;
913 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
914 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
915 474370cb 2020-01-15 tracey (diff_time / 60 / 60), hours)) == -1)
916 474370cb 2020-01-15 tracey return NULL;
917 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
918 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
919 474370cb 2020-01-15 tracey minutes)) == -1)
920 474370cb 2020-01-15 tracey return NULL;
921 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
922 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", diff_time,
923 474370cb 2020-01-15 tracey seconds)) == -1)
924 474370cb 2020-01-15 tracey return NULL;
925 474370cb 2020-01-15 tracey } else {
926 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s", now)) == -1)
927 474370cb 2020-01-15 tracey return NULL;
928 474370cb 2020-01-15 tracey }
929 474370cb 2020-01-15 tracey break;
930 474370cb 2020-01-15 tracey case TM_LONG:
931 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
932 474370cb 2020-01-15 tracey return NULL;
933 474370cb 2020-01-15 tracey
934 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
935 474370cb 2020-01-15 tracey if (s == NULL)
936 474370cb 2020-01-15 tracey return NULL;
937 474370cb 2020-01-15 tracey
938 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
939 474370cb 2020-01-15 tracey return NULL;
940 474370cb 2020-01-15 tracey break;
941 474370cb 2020-01-15 tracey }
942 474370cb 2020-01-15 tracey return repo_age;
943 474370cb 2020-01-15 tracey }
944 474370cb 2020-01-15 tracey
945 474370cb 2020-01-15 tracey static char *
946 387a29ba 2020-01-15 tracey gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
947 2c251c14 2020-01-15 tracey {
948 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
949 2c251c14 2020-01-15 tracey struct got_object_id *id = NULL;
950 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
951 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
952 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
953 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
954 2c251c14 2020-01-15 tracey struct got_reference *head_ref;
955 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
956 474370cb 2020-01-15 tracey char *repo_age = NULL;
957 387a29ba 2020-01-15 tracey
958 387a29ba 2020-01-15 tracey if (repo_ref == NULL)
959 387a29ba 2020-01-15 tracey return NULL;
960 2c251c14 2020-01-15 tracey
961 2c251c14 2020-01-15 tracey SIMPLEQ_INIT(&refs);
962 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age == false) {
963 2c251c14 2020-01-15 tracey asprintf(&repo_age, "");
964 2c251c14 2020-01-15 tracey return repo_age;
965 2c251c14 2020-01-15 tracey }
966 2c251c14 2020-01-15 tracey error = got_repo_open(&repo, dir, NULL);
967 2c251c14 2020-01-15 tracey if (error != NULL)
968 2c251c14 2020-01-15 tracey goto err;
969 2c251c14 2020-01-15 tracey
970 387a29ba 2020-01-15 tracey error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
971 2c251c14 2020-01-15 tracey NULL);
972 2c251c14 2020-01-15 tracey if (error != NULL)
973 2c251c14 2020-01-15 tracey goto err;
974 2c251c14 2020-01-15 tracey
975 2c251c14 2020-01-15 tracey const char *refname;
976 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
977 2c251c14 2020-01-15 tracey refname = got_ref_get_name(re->ref);
978 2c251c14 2020-01-15 tracey error = got_ref_open(&head_ref, repo, refname, 0);
979 2c251c14 2020-01-15 tracey if (error != NULL)
980 2c251c14 2020-01-15 tracey goto err;
981 2c251c14 2020-01-15 tracey
982 2c251c14 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
983 2c251c14 2020-01-15 tracey got_ref_close(head_ref);
984 2c251c14 2020-01-15 tracey if (error != NULL)
985 2c251c14 2020-01-15 tracey goto err;
986 2c251c14 2020-01-15 tracey
987 387a29ba 2020-01-15 tracey /* here is what breaks tags, so adjust */
988 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
989 2c251c14 2020-01-15 tracey if (error != NULL)
990 2c251c14 2020-01-15 tracey goto err;
991 2c251c14 2020-01-15 tracey
992 2c251c14 2020-01-15 tracey committer_time =
993 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
994 2c251c14 2020-01-15 tracey
995 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
996 2c251c14 2020-01-15 tracey cmp_time = committer_time;
997 2c251c14 2020-01-15 tracey }
998 2c251c14 2020-01-15 tracey
999 474370cb 2020-01-15 tracey if (cmp_time != 0) {
1000 2c251c14 2020-01-15 tracey committer_time = cmp_time;
1001 474370cb 2020-01-15 tracey repo_age = gw_get_time_str(committer_time, ref_tm);
1002 474370cb 2020-01-15 tracey } else
1003 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "")) == -1)
1004 474370cb 2020-01-15 tracey return NULL;
1005 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
1006 2c251c14 2020-01-15 tracey free(id);
1007 2c251c14 2020-01-15 tracey return repo_age;
1008 2c251c14 2020-01-15 tracey err:
1009 2c251c14 2020-01-15 tracey if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1010 2c251c14 2020-01-15 tracey return NULL;
1011 2c251c14 2020-01-15 tracey
1012 2c251c14 2020-01-15 tracey return repo_age;
1013 2c251c14 2020-01-15 tracey }
1014 2c251c14 2020-01-15 tracey
1015 2c251c14 2020-01-15 tracey static char *
1016 2c251c14 2020-01-15 tracey gw_get_repo_owner(struct trans *gw_trans, char *dir)
1017 2c251c14 2020-01-15 tracey {
1018 2c251c14 2020-01-15 tracey FILE *f;
1019 2c251c14 2020-01-15 tracey char *owner = NULL, *d_file = NULL;
1020 2c251c14 2020-01-15 tracey char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1021 2c251c14 2020-01-15 tracey char *comp, *pos, *buf;
1022 2c251c14 2020-01-15 tracey unsigned int i;
1023 2c251c14 2020-01-15 tracey
1024 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner == false)
1025 2c251c14 2020-01-15 tracey goto err;
1026 2c251c14 2020-01-15 tracey
1027 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/config", dir)) == -1)
1028 2c251c14 2020-01-15 tracey goto err;
1029 2c251c14 2020-01-15 tracey
1030 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1031 2c251c14 2020-01-15 tracey goto err;
1032 2c251c14 2020-01-15 tracey
1033 2c251c14 2020-01-15 tracey if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1034 2c251c14 2020-01-15 tracey goto err;
1035 2c251c14 2020-01-15 tracey
1036 2c251c14 2020-01-15 tracey while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
1037 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gotweb)) != NULL)
1038 2c251c14 2020-01-15 tracey break;
1039 2c251c14 2020-01-15 tracey
1040 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gitweb)) != NULL)
1041 2c251c14 2020-01-15 tracey break;
1042 2c251c14 2020-01-15 tracey }
1043 2c251c14 2020-01-15 tracey
1044 2c251c14 2020-01-15 tracey if (pos == NULL)
1045 2c251c14 2020-01-15 tracey goto err;
1046 2c251c14 2020-01-15 tracey
1047 2c251c14 2020-01-15 tracey do {
1048 2c251c14 2020-01-15 tracey fgets(buf, BUFFER_SIZE, f);
1049 2c251c14 2020-01-15 tracey } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1050 2c251c14 2020-01-15 tracey
1051 2c251c14 2020-01-15 tracey if (comp == NULL)
1052 2c251c14 2020-01-15 tracey goto err;
1053 2c251c14 2020-01-15 tracey
1054 2c251c14 2020-01-15 tracey if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1055 2c251c14 2020-01-15 tracey goto err;
1056 2c251c14 2020-01-15 tracey
1057 2c251c14 2020-01-15 tracey for (i = 0; i < 2; i++) {
1058 2c251c14 2020-01-15 tracey owner = strsep(&buf, "\"");
1059 2c251c14 2020-01-15 tracey }
1060 2c251c14 2020-01-15 tracey
1061 2c251c14 2020-01-15 tracey if (owner == NULL)
1062 2c251c14 2020-01-15 tracey goto err;
1063 2c251c14 2020-01-15 tracey
1064 2c251c14 2020-01-15 tracey fclose(f);
1065 2c251c14 2020-01-15 tracey free(d_file);
1066 2c251c14 2020-01-15 tracey return owner;
1067 2c251c14 2020-01-15 tracey err:
1068 2c251c14 2020-01-15 tracey if ((asprintf(&owner, "%s", "")) == -1)
1069 2c251c14 2020-01-15 tracey return NULL;
1070 2c251c14 2020-01-15 tracey
1071 2c251c14 2020-01-15 tracey return owner;
1072 2c251c14 2020-01-15 tracey }
1073 2c251c14 2020-01-15 tracey
1074 2c251c14 2020-01-15 tracey static char *
1075 2c251c14 2020-01-15 tracey gw_get_clone_url(struct trans *gw_trans, char *dir)
1076 2c251c14 2020-01-15 tracey {
1077 2c251c14 2020-01-15 tracey FILE *f;
1078 2c251c14 2020-01-15 tracey char *url = NULL, *d_file = NULL;
1079 2c251c14 2020-01-15 tracey unsigned int len;
1080 2c251c14 2020-01-15 tracey
1081 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1082 2c251c14 2020-01-15 tracey return NULL;
1083 2c251c14 2020-01-15 tracey
1084 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1085 2c251c14 2020-01-15 tracey return NULL;
1086 2c251c14 2020-01-15 tracey
1087 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1088 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1089 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1090 2c251c14 2020-01-15 tracey
1091 2c251c14 2020-01-15 tracey if ((url = calloc(len, sizeof(char *))) == NULL)
1092 2c251c14 2020-01-15 tracey return NULL;
1093 2c251c14 2020-01-15 tracey
1094 2c251c14 2020-01-15 tracey fread(url, 1, len, f);
1095 2c251c14 2020-01-15 tracey fclose(f);
1096 2c251c14 2020-01-15 tracey free(d_file);
1097 2c251c14 2020-01-15 tracey return url;
1098 8d4d2453 2020-01-15 tracey }
1099 8d4d2453 2020-01-15 tracey
1100 8d4d2453 2020-01-15 tracey static char *
1101 474370cb 2020-01-15 tracey gw_get_repo_shortlog(struct trans *gw_trans, const char *search_pattern)
1102 8d4d2453 2020-01-15 tracey {
1103 c6b62706 2020-01-15 tracey const struct got_error *error;
1104 c6b62706 2020-01-15 tracey struct got_repository *repo = NULL;
1105 c6b62706 2020-01-15 tracey struct got_reflist_head refs;
1106 474370cb 2020-01-15 tracey struct got_reflist_entry *re;
1107 c6b62706 2020-01-15 tracey struct got_commit_object *commit = NULL;
1108 c6b62706 2020-01-15 tracey struct got_object_id *id = NULL;
1109 474370cb 2020-01-15 tracey struct got_commit_graph *graph = NULL;
1110 474370cb 2020-01-15 tracey char *start_commit = NULL, *shortlog = NULL, *id_str = NULL,
1111 474370cb 2020-01-15 tracey *path = NULL, *in_repo_path = NULL, *commit_row = NULL,
1112 474370cb 2020-01-15 tracey *commit_age = NULL, *commit_author = NULL, *commit_log = NULL,
1113 474370cb 2020-01-15 tracey *shortlog_navs_html = NULL;
1114 474370cb 2020-01-15 tracey regex_t regex;
1115 2570478e 2020-01-15 tracey int have_match, limit = D_MAXSLCOMMDISP;
1116 474370cb 2020-01-15 tracey size_t newsize;
1117 474370cb 2020-01-15 tracey struct buf *diffbuf;
1118 474370cb 2020-01-15 tracey time_t committer_time;
1119 8d4d2453 2020-01-15 tracey
1120 474370cb 2020-01-15 tracey if (search_pattern &&
1121 474370cb 2020-01-15 tracey regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1122 474370cb 2020-01-15 tracey REG_NEWLINE))
1123 474370cb 2020-01-15 tracey return NULL;
1124 474370cb 2020-01-15 tracey
1125 474370cb 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1126 474370cb 2020-01-15 tracey
1127 c6b62706 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1128 c6b62706 2020-01-15 tracey if (error != NULL)
1129 c6b62706 2020-01-15 tracey goto done;
1130 c6b62706 2020-01-15 tracey
1131 474370cb 2020-01-15 tracey error = buf_alloc(&diffbuf, BUFFER_SIZE);
1132 474370cb 2020-01-15 tracey if (error != NULL)
1133 474370cb 2020-01-15 tracey goto done;
1134 474370cb 2020-01-15 tracey
1135 c6b62706 2020-01-15 tracey if (start_commit == NULL) {
1136 c6b62706 2020-01-15 tracey struct got_reference *head_ref;
1137 c6b62706 2020-01-15 tracey error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1138 c6b62706 2020-01-15 tracey if (error != NULL)
1139 c6b62706 2020-01-15 tracey return NULL;
1140 c6b62706 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
1141 c6b62706 2020-01-15 tracey got_ref_close(head_ref);
1142 c6b62706 2020-01-15 tracey if (error != NULL)
1143 c6b62706 2020-01-15 tracey return NULL;
1144 c6b62706 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1145 c6b62706 2020-01-15 tracey } else {
1146 c6b62706 2020-01-15 tracey struct got_reference *ref;
1147 c6b62706 2020-01-15 tracey error = got_ref_open(&ref, repo, start_commit, 0);
1148 c6b62706 2020-01-15 tracey if (error == NULL) {
1149 c6b62706 2020-01-15 tracey int obj_type;
1150 c6b62706 2020-01-15 tracey error = got_ref_resolve(&id, repo, ref);
1151 c6b62706 2020-01-15 tracey got_ref_close(ref);
1152 c6b62706 2020-01-15 tracey if (error != NULL)
1153 c6b62706 2020-01-15 tracey goto done;
1154 c6b62706 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, id);
1155 c6b62706 2020-01-15 tracey if (error != NULL)
1156 c6b62706 2020-01-15 tracey goto done;
1157 c6b62706 2020-01-15 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
1158 c6b62706 2020-01-15 tracey struct got_tag_object *tag;
1159 c6b62706 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
1160 c6b62706 2020-01-15 tracey if (error != NULL)
1161 c6b62706 2020-01-15 tracey goto done;
1162 c6b62706 2020-01-15 tracey if (got_object_tag_get_object_type(tag) !=
1163 c6b62706 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT) {
1164 c6b62706 2020-01-15 tracey got_object_tag_close(tag);
1165 c6b62706 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1166 c6b62706 2020-01-15 tracey goto done;
1167 c6b62706 2020-01-15 tracey }
1168 c6b62706 2020-01-15 tracey free(id);
1169 c6b62706 2020-01-15 tracey id = got_object_id_dup(
1170 c6b62706 2020-01-15 tracey got_object_tag_get_object_id(tag));
1171 c6b62706 2020-01-15 tracey if (id == NULL)
1172 c6b62706 2020-01-15 tracey error = got_error_from_errno(
1173 c6b62706 2020-01-15 tracey "got_object_id_dup");
1174 c6b62706 2020-01-15 tracey got_object_tag_close(tag);
1175 c6b62706 2020-01-15 tracey if (error)
1176 c6b62706 2020-01-15 tracey goto done;
1177 c6b62706 2020-01-15 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1178 c6b62706 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1179 c6b62706 2020-01-15 tracey goto done;
1180 c6b62706 2020-01-15 tracey }
1181 c6b62706 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1182 c6b62706 2020-01-15 tracey if (error != NULL)
1183 c6b62706 2020-01-15 tracey goto done;
1184 c6b62706 2020-01-15 tracey }
1185 c6b62706 2020-01-15 tracey if (commit == NULL) {
1186 c6b62706 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id,
1187 c6b62706 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1188 c6b62706 2020-01-15 tracey if (error != NULL)
1189 c6b62706 2020-01-15 tracey return NULL;
1190 c6b62706 2020-01-15 tracey }
1191 c6b62706 2020-01-15 tracey }
1192 c6b62706 2020-01-15 tracey
1193 c6b62706 2020-01-15 tracey if (error != NULL)
1194 c6b62706 2020-01-15 tracey goto done;
1195 c6b62706 2020-01-15 tracey
1196 474370cb 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1197 474370cb 2020-01-15 tracey if (error != NULL)
1198 474370cb 2020-01-15 tracey goto done;
1199 474370cb 2020-01-15 tracey
1200 474370cb 2020-01-15 tracey if (in_repo_path) {
1201 474370cb 2020-01-15 tracey free(path);
1202 474370cb 2020-01-15 tracey path = in_repo_path;
1203 474370cb 2020-01-15 tracey }
1204 474370cb 2020-01-15 tracey
1205 c6b62706 2020-01-15 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1206 c6b62706 2020-01-15 tracey if (error)
1207 c6b62706 2020-01-15 tracey goto done;
1208 c6b62706 2020-01-15 tracey
1209 474370cb 2020-01-15 tracey error = got_commit_graph_open(&graph, id, path, 0, repo);
1210 474370cb 2020-01-15 tracey if (error)
1211 474370cb 2020-01-15 tracey goto done;
1212 474370cb 2020-01-15 tracey
1213 474370cb 2020-01-15 tracey error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
1214 474370cb 2020-01-15 tracey if (error)
1215 474370cb 2020-01-15 tracey goto done;
1216 474370cb 2020-01-15 tracey
1217 474370cb 2020-01-15 tracey for (;;) {
1218 474370cb 2020-01-15 tracey struct got_commit_object *commit_disp;
1219 474370cb 2020-01-15 tracey
1220 474370cb 2020-01-15 tracey error = got_commit_graph_iter_next(&id, graph);
1221 474370cb 2020-01-15 tracey if (error) {
1222 474370cb 2020-01-15 tracey if (error->code == GOT_ERR_ITER_COMPLETED) {
1223 474370cb 2020-01-15 tracey error = NULL;
1224 474370cb 2020-01-15 tracey break;
1225 474370cb 2020-01-15 tracey }
1226 474370cb 2020-01-15 tracey if (error->code != GOT_ERR_ITER_NEED_MORE)
1227 474370cb 2020-01-15 tracey break;
1228 474370cb 2020-01-15 tracey error = got_commit_graph_fetch_commits(graph, 1, repo,
1229 474370cb 2020-01-15 tracey NULL, NULL);
1230 474370cb 2020-01-15 tracey if (error)
1231 474370cb 2020-01-15 tracey break;
1232 474370cb 2020-01-15 tracey else
1233 474370cb 2020-01-15 tracey continue;
1234 474370cb 2020-01-15 tracey }
1235 474370cb 2020-01-15 tracey if (id == NULL)
1236 474370cb 2020-01-15 tracey break;
1237 474370cb 2020-01-15 tracey
1238 474370cb 2020-01-15 tracey error = got_object_open_as_commit(&commit_disp, repo, id);
1239 474370cb 2020-01-15 tracey if (error)
1240 474370cb 2020-01-15 tracey break;
1241 474370cb 2020-01-15 tracey
1242 474370cb 2020-01-15 tracey if (search_pattern) {
1243 474370cb 2020-01-15 tracey error = match_logmsg(&have_match, id, commit_disp,
1244 474370cb 2020-01-15 tracey &regex);
1245 474370cb 2020-01-15 tracey if (error) {
1246 474370cb 2020-01-15 tracey got_object_commit_close(commit_disp);
1247 474370cb 2020-01-15 tracey break;
1248 474370cb 2020-01-15 tracey }
1249 474370cb 2020-01-15 tracey if (have_match == 0) {
1250 474370cb 2020-01-15 tracey got_object_commit_close(commit_disp);
1251 474370cb 2020-01-15 tracey continue;
1252 474370cb 2020-01-15 tracey }
1253 474370cb 2020-01-15 tracey }
1254 474370cb 2020-01-15 tracey
1255 474370cb 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1256 474370cb 2020-01-15 tracey const char *name;
1257 474370cb 2020-01-15 tracey struct got_tag_object *tag = NULL;
1258 474370cb 2020-01-15 tracey int cmp;
1259 474370cb 2020-01-15 tracey
1260 474370cb 2020-01-15 tracey name = got_ref_get_name(re->ref);
1261 474370cb 2020-01-15 tracey if (strcmp(name, GOT_REF_HEAD) == 0)
1262 474370cb 2020-01-15 tracey continue;
1263 474370cb 2020-01-15 tracey if (strncmp(name, "refs/", 5) == 0)
1264 474370cb 2020-01-15 tracey name += 5;
1265 474370cb 2020-01-15 tracey if (strncmp(name, "got/", 4) == 0)
1266 474370cb 2020-01-15 tracey continue;
1267 474370cb 2020-01-15 tracey if (strncmp(name, "heads/", 6) == 0)
1268 474370cb 2020-01-15 tracey name += 6;
1269 474370cb 2020-01-15 tracey if (strncmp(name, "remotes/", 8) == 0)
1270 474370cb 2020-01-15 tracey name += 8;
1271 474370cb 2020-01-15 tracey if (strncmp(name, "tags/", 5) == 0) {
1272 474370cb 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo,
1273 474370cb 2020-01-15 tracey re->id);
1274 474370cb 2020-01-15 tracey if (error) {
1275 474370cb 2020-01-15 tracey if (error->code != GOT_ERR_OBJ_TYPE)
1276 474370cb 2020-01-15 tracey continue;
1277 474370cb 2020-01-15 tracey /*
1278 474370cb 2020-01-15 tracey * Ref points at something other
1279 474370cb 2020-01-15 tracey * than a tag.
1280 474370cb 2020-01-15 tracey */
1281 474370cb 2020-01-15 tracey error = NULL;
1282 474370cb 2020-01-15 tracey tag = NULL;
1283 474370cb 2020-01-15 tracey }
1284 474370cb 2020-01-15 tracey }
1285 474370cb 2020-01-15 tracey cmp = got_object_id_cmp(tag ?
1286 474370cb 2020-01-15 tracey got_object_tag_get_object_id(tag) : re->id, id);
1287 474370cb 2020-01-15 tracey if (tag)
1288 474370cb 2020-01-15 tracey got_object_tag_close(tag);
1289 474370cb 2020-01-15 tracey if (cmp != 0)
1290 474370cb 2020-01-15 tracey continue;
1291 474370cb 2020-01-15 tracey }
1292 cdb914e5 2020-01-15 tracey
1293 cdb914e5 2020-01-15 tracey got_ref_list_free(&refs);
1294 474370cb 2020-01-15 tracey
1295 474370cb 2020-01-15 tracey /* commit id */
1296 474370cb 2020-01-15 tracey error = got_object_id_str(&id_str, id);
1297 474370cb 2020-01-15 tracey if (error)
1298 474370cb 2020-01-15 tracey break;
1299 474370cb 2020-01-15 tracey
1300 474370cb 2020-01-15 tracey committer_time =
1301 474370cb 2020-01-15 tracey got_object_commit_get_committer_time(commit_disp);
1302 474370cb 2020-01-15 tracey asprintf(&commit_age, "%s", gw_get_time_str(committer_time,
1303 474370cb 2020-01-15 tracey TM_DIFF));
1304 474370cb 2020-01-15 tracey asprintf(&commit_author, "%s",
1305 474370cb 2020-01-15 tracey got_object_commit_get_author(commit_disp));
1306 474370cb 2020-01-15 tracey error = got_object_commit_get_logmsg(&commit_log, commit_disp);
1307 474370cb 2020-01-15 tracey if (error)
1308 474370cb 2020-01-15 tracey commit_log = strdup("");
1309 474370cb 2020-01-15 tracey asprintf(&shortlog_navs_html, shortlog_navs,
1310 474370cb 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name, id_str,
1311 474370cb 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name, id_str);
1312 474370cb 2020-01-15 tracey asprintf(&commit_row, shortlog_row, commit_age, commit_author,
1313 474370cb 2020-01-15 tracey commit_log, shortlog_navs_html);
1314 474370cb 2020-01-15 tracey error = buf_append(&newsize, diffbuf, commit_row,
1315 474370cb 2020-01-15 tracey strlen(commit_row));
1316 474370cb 2020-01-15 tracey
1317 474370cb 2020-01-15 tracey free(commit_age);
1318 474370cb 2020-01-15 tracey free(commit_author);
1319 474370cb 2020-01-15 tracey free(commit_log);
1320 474370cb 2020-01-15 tracey free(shortlog_navs_html);
1321 474370cb 2020-01-15 tracey free(commit_row);
1322 474370cb 2020-01-15 tracey free(id_str);
1323 474370cb 2020-01-15 tracey commit_age = NULL;
1324 474370cb 2020-01-15 tracey commit_author = NULL;
1325 474370cb 2020-01-15 tracey commit_log = NULL;
1326 474370cb 2020-01-15 tracey shortlog_navs_html = NULL;
1327 474370cb 2020-01-15 tracey commit_row = NULL;
1328 474370cb 2020-01-15 tracey id_str = NULL;
1329 474370cb 2020-01-15 tracey
1330 474370cb 2020-01-15 tracey got_object_commit_close(commit_disp);
1331 474370cb 2020-01-15 tracey if (error || (limit && --limit == 0))
1332 474370cb 2020-01-15 tracey break;
1333 474370cb 2020-01-15 tracey }
1334 474370cb 2020-01-15 tracey shortlog = strdup(diffbuf->cb_buf);
1335 c6b62706 2020-01-15 tracey got_object_commit_close(commit);
1336 c6b62706 2020-01-15 tracey
1337 474370cb 2020-01-15 tracey free(path);
1338 474370cb 2020-01-15 tracey free(id);
1339 474370cb 2020-01-15 tracey buf_free(diffbuf);
1340 474370cb 2020-01-15 tracey
1341 474370cb 2020-01-15 tracey if (repo) {
1342 474370cb 2020-01-15 tracey error = got_repo_close(repo);
1343 474370cb 2020-01-15 tracey if (error != NULL)
1344 474370cb 2020-01-15 tracey return NULL;
1345 474370cb 2020-01-15 tracey }
1346 474370cb 2020-01-15 tracey
1347 cdb914e5 2020-01-15 tracey if (search_pattern)
1348 cdb914e5 2020-01-15 tracey regfree(&regex);
1349 8d4d2453 2020-01-15 tracey return shortlog;
1350 c6b62706 2020-01-15 tracey done:
1351 c6b62706 2020-01-15 tracey if (repo)
1352 c6b62706 2020-01-15 tracey got_repo_close(repo);
1353 c6b62706 2020-01-15 tracey got_ref_list_free(&refs);
1354 474370cb 2020-01-15 tracey
1355 474370cb 2020-01-15 tracey if (search_pattern)
1356 474370cb 2020-01-15 tracey regfree(&regex);
1357 474370cb 2020-01-15 tracey got_commit_graph_close(graph);
1358 c6b62706 2020-01-15 tracey return NULL;
1359 2c251c14 2020-01-15 tracey }
1360 2c251c14 2020-01-15 tracey
1361 2c251c14 2020-01-15 tracey static char *
1362 8d4d2453 2020-01-15 tracey gw_get_repo_tags(struct trans *gw_trans)
1363 8d4d2453 2020-01-15 tracey {
1364 8d4d2453 2020-01-15 tracey char *tags = NULL;
1365 8d4d2453 2020-01-15 tracey
1366 8d4d2453 2020-01-15 tracey asprintf(&tags, tags_row, "30 min ago", "1.0.0", "tag 1.0.0", tags_navs);
1367 8d4d2453 2020-01-15 tracey return tags;
1368 8d4d2453 2020-01-15 tracey }
1369 8d4d2453 2020-01-15 tracey
1370 8d4d2453 2020-01-15 tracey static char *
1371 8d4d2453 2020-01-15 tracey gw_get_repo_heads(struct trans *gw_trans)
1372 8d4d2453 2020-01-15 tracey {
1373 8d4d2453 2020-01-15 tracey char *heads = NULL;
1374 8d4d2453 2020-01-15 tracey
1375 8d4d2453 2020-01-15 tracey asprintf(&heads, heads_row, "30 min ago", "master", heads_navs);
1376 8d4d2453 2020-01-15 tracey return heads;
1377 8d4d2453 2020-01-15 tracey }
1378 8d4d2453 2020-01-15 tracey
1379 8d4d2453 2020-01-15 tracey static char *
1380 2c251c14 2020-01-15 tracey gw_get_got_link(struct trans *gw_trans)
1381 2c251c14 2020-01-15 tracey {
1382 2c251c14 2020-01-15 tracey char *link;
1383 2c251c14 2020-01-15 tracey
1384 2c251c14 2020-01-15 tracey if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1385 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_logo)) == -1)
1386 2c251c14 2020-01-15 tracey return NULL;
1387 2c251c14 2020-01-15 tracey
1388 2c251c14 2020-01-15 tracey return link;
1389 2c251c14 2020-01-15 tracey }
1390 2c251c14 2020-01-15 tracey
1391 2c251c14 2020-01-15 tracey static char *
1392 2c251c14 2020-01-15 tracey gw_get_site_link(struct trans *gw_trans)
1393 2c251c14 2020-01-15 tracey {
1394 2c251c14 2020-01-15 tracey char *link, *repo = "", *action = "";
1395 2c251c14 2020-01-15 tracey
1396 2c251c14 2020-01-15 tracey if (gw_trans->repo_name != NULL)
1397 2c251c14 2020-01-15 tracey if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1398 2c251c14 2020-01-15 tracey "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1399 2c251c14 2020-01-15 tracey return NULL;
1400 2c251c14 2020-01-15 tracey
1401 2c251c14 2020-01-15 tracey if (gw_trans->action_name != NULL)
1402 2c251c14 2020-01-15 tracey if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1403 2c251c14 2020-01-15 tracey return NULL;
1404 2c251c14 2020-01-15 tracey
1405 2c251c14 2020-01-15 tracey if ((asprintf(&link, site_link, GOTWEB,
1406 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1407 2c251c14 2020-01-15 tracey return NULL;
1408 2c251c14 2020-01-15 tracey
1409 2c251c14 2020-01-15 tracey return link;
1410 2c251c14 2020-01-15 tracey }
1411 2c251c14 2020-01-15 tracey
1412 2c251c14 2020-01-15 tracey static char *
1413 2c251c14 2020-01-15 tracey gw_html_escape(const char *html)
1414 2c251c14 2020-01-15 tracey {
1415 2c251c14 2020-01-15 tracey char *escaped_str = NULL, *buf;
1416 2c251c14 2020-01-15 tracey char c[1];
1417 2c251c14 2020-01-15 tracey size_t sz, i;
1418 2c251c14 2020-01-15 tracey
1419 2c251c14 2020-01-15 tracey if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1420 2c251c14 2020-01-15 tracey return NULL;
1421 2c251c14 2020-01-15 tracey
1422 2c251c14 2020-01-15 tracey if (html == NULL)
1423 2c251c14 2020-01-15 tracey return NULL;
1424 2c251c14 2020-01-15 tracey else
1425 2c251c14 2020-01-15 tracey if ((sz = strlen(html)) == 0)
1426 2c251c14 2020-01-15 tracey return NULL;
1427 2c251c14 2020-01-15 tracey
1428 2c251c14 2020-01-15 tracey /* only work with BUFFER_SIZE */
1429 2c251c14 2020-01-15 tracey if (BUFFER_SIZE < sz)
1430 2c251c14 2020-01-15 tracey sz = BUFFER_SIZE;
1431 2c251c14 2020-01-15 tracey
1432 2c251c14 2020-01-15 tracey for (i = 0; i < sz; i++) {
1433 2c251c14 2020-01-15 tracey c[0] = html[i];
1434 2c251c14 2020-01-15 tracey switch (c[0]) {
1435 2c251c14 2020-01-15 tracey case ('>'):
1436 2c251c14 2020-01-15 tracey strcat(buf, "&gt;");
1437 2c251c14 2020-01-15 tracey break;
1438 2c251c14 2020-01-15 tracey case ('&'):
1439 2c251c14 2020-01-15 tracey strcat(buf, "&amp;");
1440 2c251c14 2020-01-15 tracey break;
1441 2c251c14 2020-01-15 tracey case ('<'):
1442 2c251c14 2020-01-15 tracey strcat(buf, "&lt;");
1443 2c251c14 2020-01-15 tracey break;
1444 2c251c14 2020-01-15 tracey case ('"'):
1445 2c251c14 2020-01-15 tracey strcat(buf, "&quot;");
1446 2c251c14 2020-01-15 tracey break;
1447 2c251c14 2020-01-15 tracey case ('\''):
1448 2c251c14 2020-01-15 tracey strcat(buf, "&apos;");
1449 2c251c14 2020-01-15 tracey break;
1450 2c251c14 2020-01-15 tracey case ('\n'):
1451 2c251c14 2020-01-15 tracey strcat(buf, "<br />");
1452 2c251c14 2020-01-15 tracey default:
1453 2c251c14 2020-01-15 tracey strcat(buf, &c[0]);
1454 2c251c14 2020-01-15 tracey break;
1455 2c251c14 2020-01-15 tracey }
1456 2c251c14 2020-01-15 tracey }
1457 2c251c14 2020-01-15 tracey asprintf(&escaped_str, "%s", buf);
1458 2c251c14 2020-01-15 tracey free(buf);
1459 2c251c14 2020-01-15 tracey return escaped_str;
1460 2c251c14 2020-01-15 tracey }
1461 2c251c14 2020-01-15 tracey
1462 2c251c14 2020-01-15 tracey int
1463 2c251c14 2020-01-15 tracey main()
1464 2c251c14 2020-01-15 tracey {
1465 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1466 2c251c14 2020-01-15 tracey struct trans *gw_trans;
1467 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
1468 2c251c14 2020-01-15 tracey const char *page = "index";
1469 2c251c14 2020-01-15 tracey bool gw_malloc = true;
1470 2c251c14 2020-01-15 tracey
1471 2c251c14 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1472 2c251c14 2020-01-15 tracey errx(1, "malloc");
1473 2c251c14 2020-01-15 tracey
1474 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1475 2c251c14 2020-01-15 tracey errx(1, "malloc");
1476 2c251c14 2020-01-15 tracey
1477 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1478 2c251c14 2020-01-15 tracey errx(1, "malloc");
1479 2c251c14 2020-01-15 tracey
1480 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1481 2c251c14 2020-01-15 tracey errx(1, "malloc");
1482 2c251c14 2020-01-15 tracey
1483 2c251c14 2020-01-15 tracey if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1484 2c251c14 2020-01-15 tracey &page, 1, 0))
1485 2c251c14 2020-01-15 tracey errx(1, "khttp_parse");
1486 2c251c14 2020-01-15 tracey
1487 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
1488 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
1489 2c251c14 2020-01-15 tracey gw_malloc = false;
1490 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
1491 2c251c14 2020-01-15 tracey goto err;
1492 2c251c14 2020-01-15 tracey }
1493 2c251c14 2020-01-15 tracey
1494 46b9c89b 2020-01-15 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
1495 46b9c89b 2020-01-15 tracey error = got_error_from_errno("pledge");
1496 46b9c89b 2020-01-15 tracey goto err;
1497 46b9c89b 2020-01-15 tracey }
1498 46b9c89b 2020-01-15 tracey
1499 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
1500 2c251c14 2020-01-15 tracey
1501 2c251c14 2020-01-15 tracey gw_trans->page = 0;
1502 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
1503 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
1504 2c251c14 2020-01-15 tracey gw_trans->commit = NULL;
1505 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
1506 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->key = templs;
1507 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1508 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
1509 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
1510 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1511 2c251c14 2020-01-15 tracey
1512 2c251c14 2020-01-15 tracey err:
1513 2c251c14 2020-01-15 tracey if (error) {
1514 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
1515 2c251c14 2020-01-15 tracey gw_trans->action = GW_ERR;
1516 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
1517 2c251c14 2020-01-15 tracey goto done;
1518 2c251c14 2020-01-15 tracey }
1519 2c251c14 2020-01-15 tracey
1520 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
1521 2c251c14 2020-01-15 tracey if (error)
1522 2c251c14 2020-01-15 tracey goto err;
1523 2c251c14 2020-01-15 tracey
1524 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
1525 2c251c14 2020-01-15 tracey
1526 2c251c14 2020-01-15 tracey done:
1527 2c251c14 2020-01-15 tracey if (gw_malloc) {
1528 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
1529 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_www_path);
1530 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
1531 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
1532 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
1533 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
1534 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
1535 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
1536 2c251c14 2020-01-15 tracey free(gw_trans->commit);
1537 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
1538 2c251c14 2020-01-15 tracey free(gw_trans->repo_name);
1539 2c251c14 2020-01-15 tracey free(gw_trans->repo_file);
1540 2c251c14 2020-01-15 tracey free(gw_trans->action_name);
1541 2c251c14 2020-01-15 tracey
1542 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1543 2c251c14 2020-01-15 tracey free(dir->name);
1544 2c251c14 2020-01-15 tracey free(dir->description);
1545 2c251c14 2020-01-15 tracey free(dir->age);
1546 2c251c14 2020-01-15 tracey free(dir->url);
1547 2c251c14 2020-01-15 tracey free(dir->path);
1548 2c251c14 2020-01-15 tracey free(dir);
1549 2c251c14 2020-01-15 tracey }
1550 2c251c14 2020-01-15 tracey
1551 2c251c14 2020-01-15 tracey }
1552 2c251c14 2020-01-15 tracey
1553 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);
1554 2c251c14 2020-01-15 tracey return EXIT_SUCCESS;
1555 2c251c14 2020-01-15 tracey }