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 4ceb8155 2020-01-15 tracey static char *gw_get_repo_log(struct trans *, const char *,
145 4ceb8155 2020-01-15 tracey char *, int, int);
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 4ceb8155 2020-01-15 tracey static const struct got_error* gw_logbriefs(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 4ceb8155 2020-01-15 tracey GW_LOGBRIEFS,
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 4ceb8155 2020-01-15 tracey { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "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 4ceb8155 2020-01-15 tracey char *log, *log_html;
397 2c251c14 2020-01-15 tracey
398 4ceb8155 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, NULL,
399 4ceb8155 2020-01-15 tracey gw_trans->gw_conf->got_max_commits_display, 1);
400 4ceb8155 2020-01-15 tracey
401 4ceb8155 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
402 4ceb8155 2020-01-15 tracey if ((asprintf(&log_html, logs, log)) == -1)
403 4ceb8155 2020-01-15 tracey return got_error_from_errno("asprintf");
404 4ceb8155 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
405 4ceb8155 2020-01-15 tracey free(log_html);
406 4ceb8155 2020-01-15 tracey free(log);
407 4ceb8155 2020-01-15 tracey }
408 2c251c14 2020-01-15 tracey return error;
409 2c251c14 2020-01-15 tracey }
410 2c251c14 2020-01-15 tracey
411 2c251c14 2020-01-15 tracey static const struct got_error *
412 2c251c14 2020-01-15 tracey gw_raw(struct trans *gw_trans)
413 2c251c14 2020-01-15 tracey {
414 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
415 2c251c14 2020-01-15 tracey
416 2c251c14 2020-01-15 tracey return error;
417 2c251c14 2020-01-15 tracey }
418 2c251c14 2020-01-15 tracey
419 2c251c14 2020-01-15 tracey static const struct got_error *
420 4ceb8155 2020-01-15 tracey gw_logbriefs(struct trans *gw_trans)
421 2c251c14 2020-01-15 tracey {
422 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
423 4ceb8155 2020-01-15 tracey char *logbriefs, *logbriefs_html;
424 17a96b9f 2020-01-15 tracey
425 4ceb8155 2020-01-15 tracey logbriefs = gw_get_repo_log(gw_trans, NULL, NULL,
426 4ceb8155 2020-01-15 tracey gw_trans->gw_conf->got_max_commits_display, 0);
427 9d84e7dd 2020-01-15 tracey
428 4ceb8155 2020-01-15 tracey if (logbriefs != NULL && strcmp(logbriefs, "") != 0) {
429 4ceb8155 2020-01-15 tracey if ((asprintf(&logbriefs_html, summary_logbriefs,
430 4ceb8155 2020-01-15 tracey logbriefs)) == -1)
431 9d84e7dd 2020-01-15 tracey return got_error_from_errno("asprintf");
432 4ceb8155 2020-01-15 tracey khttp_puts(gw_trans->gw_req, logbriefs_html);
433 4ceb8155 2020-01-15 tracey free(logbriefs_html);
434 4ceb8155 2020-01-15 tracey free(logbriefs);
435 9d84e7dd 2020-01-15 tracey }
436 2c251c14 2020-01-15 tracey return error;
437 2c251c14 2020-01-15 tracey }
438 2c251c14 2020-01-15 tracey
439 2c251c14 2020-01-15 tracey static const struct got_error *
440 2c251c14 2020-01-15 tracey gw_snapshot(struct trans *gw_trans)
441 2c251c14 2020-01-15 tracey {
442 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
443 2c251c14 2020-01-15 tracey
444 2c251c14 2020-01-15 tracey return error;
445 2c251c14 2020-01-15 tracey }
446 2c251c14 2020-01-15 tracey
447 2c251c14 2020-01-15 tracey static const struct got_error *
448 387a29ba 2020-01-15 tracey gw_summary(struct trans *gw_trans)
449 387a29ba 2020-01-15 tracey {
450 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
451 46b9c89b 2020-01-15 tracey char *description_html, *repo_owner_html, *repo_age_html,
452 4ceb8155 2020-01-15 tracey *cloneurl_html, *logbriefs, *tags, *heads, *logbriefs_html,
453 c6b62706 2020-01-15 tracey *tags_html, *heads_html, *age;
454 387a29ba 2020-01-15 tracey
455 46b9c89b 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
456 46b9c89b 2020-01-15 tracey if (error)
457 46b9c89b 2020-01-15 tracey return error;
458 46b9c89b 2020-01-15 tracey
459 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, summary_wrapper);
460 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description) {
461 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->description != NULL &&
462 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
463 46b9c89b 2020-01-15 tracey if ((asprintf(&description_html, description,
464 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->description)) == -1)
465 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
466 46b9c89b 2020-01-15 tracey
467 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, description_html);
468 46b9c89b 2020-01-15 tracey free(description_html);
469 46b9c89b 2020-01-15 tracey }
470 46b9c89b 2020-01-15 tracey }
471 46b9c89b 2020-01-15 tracey
472 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
473 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->owner != NULL &&
474 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
475 46b9c89b 2020-01-15 tracey if ((asprintf(&repo_owner_html, repo_owner,
476 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->owner)) == -1)
477 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
478 46b9c89b 2020-01-15 tracey
479 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, repo_owner_html);
480 46b9c89b 2020-01-15 tracey free(repo_owner_html);
481 46b9c89b 2020-01-15 tracey }
482 46b9c89b 2020-01-15 tracey }
483 46b9c89b 2020-01-15 tracey
484 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
485 c6b62706 2020-01-15 tracey age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
486 c6b62706 2020-01-15 tracey "refs/heads", TM_LONG);
487 c6b62706 2020-01-15 tracey if (age != NULL && (strcmp(age, "") != 0)) {
488 c6b62706 2020-01-15 tracey if ((asprintf(&repo_age_html, last_change, age)) == -1)
489 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
490 46b9c89b 2020-01-15 tracey
491 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, repo_age_html);
492 46b9c89b 2020-01-15 tracey free(repo_age_html);
493 c6b62706 2020-01-15 tracey free(age);
494 46b9c89b 2020-01-15 tracey }
495 46b9c89b 2020-01-15 tracey }
496 46b9c89b 2020-01-15 tracey
497 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl) {
498 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->url != NULL &&
499 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
500 46b9c89b 2020-01-15 tracey if ((asprintf(&cloneurl_html, cloneurl,
501 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->url)) == -1)
502 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
503 46b9c89b 2020-01-15 tracey
504 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, cloneurl_html);
505 46b9c89b 2020-01-15 tracey free(cloneurl_html);
506 46b9c89b 2020-01-15 tracey }
507 46b9c89b 2020-01-15 tracey }
508 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
509 46b9c89b 2020-01-15 tracey
510 4ceb8155 2020-01-15 tracey logbriefs = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
511 8d4d2453 2020-01-15 tracey tags = gw_get_repo_tags(gw_trans);
512 8d4d2453 2020-01-15 tracey heads = gw_get_repo_heads(gw_trans);
513 387a29ba 2020-01-15 tracey
514 4ceb8155 2020-01-15 tracey if (logbriefs != NULL && strcmp(logbriefs, "") != 0) {
515 4ceb8155 2020-01-15 tracey if ((asprintf(&logbriefs_html, summary_logbriefs,
516 4ceb8155 2020-01-15 tracey logbriefs)) == -1)
517 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
518 4ceb8155 2020-01-15 tracey khttp_puts(gw_trans->gw_req, logbriefs_html);
519 4ceb8155 2020-01-15 tracey free(logbriefs_html);
520 4ceb8155 2020-01-15 tracey free(logbriefs);
521 8d4d2453 2020-01-15 tracey }
522 8d4d2453 2020-01-15 tracey
523 8d4d2453 2020-01-15 tracey if (tags != NULL && strcmp(tags, "") != 0) {
524 8d4d2453 2020-01-15 tracey if ((asprintf(&tags_html, summary_tags,
525 8d4d2453 2020-01-15 tracey tags)) == -1)
526 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
527 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, tags_html);
528 8d4d2453 2020-01-15 tracey free(tags_html);
529 8d4d2453 2020-01-15 tracey free(tags);
530 8d4d2453 2020-01-15 tracey }
531 8d4d2453 2020-01-15 tracey
532 8d4d2453 2020-01-15 tracey if (heads != NULL && strcmp(heads, "") != 0) {
533 8d4d2453 2020-01-15 tracey if ((asprintf(&heads_html, summary_heads,
534 8d4d2453 2020-01-15 tracey heads)) == -1)
535 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
536 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, heads_html);
537 8d4d2453 2020-01-15 tracey free(heads_html);
538 8d4d2453 2020-01-15 tracey free(heads);
539 8d4d2453 2020-01-15 tracey }
540 2204c934 2020-01-15 tracey
541 2204c934 2020-01-15 tracey return error;
542 2204c934 2020-01-15 tracey }
543 2204c934 2020-01-15 tracey
544 2204c934 2020-01-15 tracey static const struct got_error *
545 2c251c14 2020-01-15 tracey gw_tree(struct trans *gw_trans)
546 2c251c14 2020-01-15 tracey {
547 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
548 2c251c14 2020-01-15 tracey
549 2c251c14 2020-01-15 tracey return error;
550 2c251c14 2020-01-15 tracey }
551 2c251c14 2020-01-15 tracey
552 2c251c14 2020-01-15 tracey static const struct got_error *
553 2c251c14 2020-01-15 tracey gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
554 2c251c14 2020-01-15 tracey {
555 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
556 2c251c14 2020-01-15 tracey DIR *dt;
557 2c251c14 2020-01-15 tracey char *dir_test;
558 4ceb8155 2020-01-15 tracey int opened = 0;
559 2c251c14 2020-01-15 tracey
560 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
561 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
562 2c251c14 2020-01-15 tracey GOTWEB_GIT_DIR)) == -1)
563 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
564 2c251c14 2020-01-15 tracey
565 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
566 2c251c14 2020-01-15 tracey if (dt == NULL) {
567 2c251c14 2020-01-15 tracey free(dir_test);
568 2c251c14 2020-01-15 tracey } else {
569 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
570 4ceb8155 2020-01-15 tracey opened = 1;
571 2c251c14 2020-01-15 tracey goto done;
572 2c251c14 2020-01-15 tracey }
573 2c251c14 2020-01-15 tracey
574 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
575 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
576 2c251c14 2020-01-15 tracey GOTWEB_GOT_DIR)) == -1)
577 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
578 2c251c14 2020-01-15 tracey
579 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
580 2c251c14 2020-01-15 tracey if (dt == NULL)
581 2c251c14 2020-01-15 tracey free(dir_test);
582 2c251c14 2020-01-15 tracey else {
583 4ceb8155 2020-01-15 tracey opened = 1;
584 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
585 2c251c14 2020-01-15 tracey goto errored;
586 2c251c14 2020-01-15 tracey }
587 2c251c14 2020-01-15 tracey
588 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s",
589 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
590 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
591 2c251c14 2020-01-15 tracey
592 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
593 2c251c14 2020-01-15 tracey
594 2c251c14 2020-01-15 tracey done:
595 2c251c14 2020-01-15 tracey gw_dir->description = gw_get_repo_description(gw_trans,
596 2c251c14 2020-01-15 tracey gw_dir->path);
597 2c251c14 2020-01-15 tracey gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
598 387a29ba 2020-01-15 tracey gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
599 387a29ba 2020-01-15 tracey TM_DIFF);
600 2c251c14 2020-01-15 tracey gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
601 2c251c14 2020-01-15 tracey
602 2c251c14 2020-01-15 tracey errored:
603 2c251c14 2020-01-15 tracey free(dir_test);
604 2c251c14 2020-01-15 tracey if (opened)
605 2c251c14 2020-01-15 tracey closedir(dt);
606 2c251c14 2020-01-15 tracey return error;
607 2c251c14 2020-01-15 tracey }
608 2c251c14 2020-01-15 tracey
609 2c251c14 2020-01-15 tracey static const struct got_error *
610 2c251c14 2020-01-15 tracey gw_load_got_paths(struct trans *gw_trans)
611 2c251c14 2020-01-15 tracey {
612 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
613 2c251c14 2020-01-15 tracey DIR *d;
614 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
615 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
616 2c251c14 2020-01-15 tracey struct stat st;
617 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
618 2c251c14 2020-01-15 tracey
619 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
620 2c251c14 2020-01-15 tracey if (d == NULL) {
621 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
622 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
623 2c251c14 2020-01-15 tracey return error;
624 2c251c14 2020-01-15 tracey }
625 2c251c14 2020-01-15 tracey
626 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
627 2c251c14 2020-01-15 tracey alphasort);
628 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
629 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
630 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
631 2c251c14 2020-01-15 tracey return error;
632 2c251c14 2020-01-15 tracey }
633 2c251c14 2020-01-15 tracey
634 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
635 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
636 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
637 2c251c14 2020-01-15 tracey break; /* account for parent and self */
638 2c251c14 2020-01-15 tracey
639 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
640 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
641 2c251c14 2020-01-15 tracey continue;
642 2c251c14 2020-01-15 tracey
643 2c251c14 2020-01-15 tracey if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
644 2c251c14 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
645 2c251c14 2020-01-15 tracey
646 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
647 2c251c14 2020-01-15 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO)
648 2c251c14 2020-01-15 tracey continue;
649 2c251c14 2020-01-15 tracey else if (error)
650 2c251c14 2020-01-15 tracey return error;
651 2c251c14 2020-01-15 tracey
652 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
653 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
654 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
655 2c251c14 2020-01-15 tracey entry);
656 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
657 2c251c14 2020-01-15 tracey }
658 2c251c14 2020-01-15 tracey }
659 2c251c14 2020-01-15 tracey
660 2c251c14 2020-01-15 tracey closedir(d);
661 2c251c14 2020-01-15 tracey return error;
662 2c251c14 2020-01-15 tracey }
663 2c251c14 2020-01-15 tracey
664 2c251c14 2020-01-15 tracey static const struct got_error *
665 2c251c14 2020-01-15 tracey gw_parse_querystring(struct trans *gw_trans)
666 2c251c14 2020-01-15 tracey {
667 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
668 2c251c14 2020-01-15 tracey struct kpair *p;
669 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
670 2c251c14 2020-01-15 tracey unsigned int i;
671 2c251c14 2020-01-15 tracey
672 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
673 2c251c14 2020-01-15 tracey error = got_error_from_errno("bad parse");
674 2c251c14 2020-01-15 tracey return error;
675 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
676 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
677 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
678 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
679 2c251c14 2020-01-15 tracey
680 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_path, "%s/%s",
681 387a29ba 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
682 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
683 2c251c14 2020-01-15 tracey
684 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
685 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->commit, "%s",
686 2c251c14 2020-01-15 tracey p->parsed.s)) == -1)
687 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
688 2c251c14 2020-01-15 tracey
689 2c251c14 2020-01-15 tracey /* get action and set function */
690 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
691 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
692 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
693 2c251c14 2020-01-15 tracey if (action->func_name == NULL)
694 2c251c14 2020-01-15 tracey continue;
695 2c251c14 2020-01-15 tracey
696 2c251c14 2020-01-15 tracey if (strcmp(action->func_name,
697 2c251c14 2020-01-15 tracey p->parsed.s) == 0) {
698 2c251c14 2020-01-15 tracey gw_trans->action = i;
699 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->action_name,
700 2c251c14 2020-01-15 tracey "%s", action->func_name)) == -1)
701 2c251c14 2020-01-15 tracey return
702 2c251c14 2020-01-15 tracey got_error_from_errno(
703 2c251c14 2020-01-15 tracey "asprintf");
704 2c251c14 2020-01-15 tracey
705 2c251c14 2020-01-15 tracey break;
706 2c251c14 2020-01-15 tracey }
707 2c251c14 2020-01-15 tracey
708 2c251c14 2020-01-15 tracey action = NULL;
709 2c251c14 2020-01-15 tracey }
710 2c251c14 2020-01-15 tracey
711 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
712 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_file, "%s",
713 2c251c14 2020-01-15 tracey p->parsed.s)) == -1)
714 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
715 2c251c14 2020-01-15 tracey
716 2c251c14 2020-01-15 tracey if (action == NULL) {
717 2c251c14 2020-01-15 tracey error = got_error_from_errno("invalid action");
718 2c251c14 2020-01-15 tracey return error;
719 2c251c14 2020-01-15 tracey }
720 46b9c89b 2020-01-15 tracey if ((gw_trans->gw_dir =
721 46b9c89b 2020-01-15 tracey gw_init_gw_dir(gw_trans->repo_name)) == NULL)
722 46b9c89b 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
723 46b9c89b 2020-01-15 tracey
724 46b9c89b 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
725 46b9c89b 2020-01-15 tracey if (error)
726 46b9c89b 2020-01-15 tracey return error;
727 2c251c14 2020-01-15 tracey } else
728 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
729 2c251c14 2020-01-15 tracey
730 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
731 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
732 2c251c14 2020-01-15 tracey
733 2c251c14 2020-01-15 tracey if (gw_trans->action == GW_RAW)
734 387a29ba 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
735 2c251c14 2020-01-15 tracey
736 2c251c14 2020-01-15 tracey return error;
737 2c251c14 2020-01-15 tracey }
738 2c251c14 2020-01-15 tracey
739 2c251c14 2020-01-15 tracey static struct gw_dir *
740 2c251c14 2020-01-15 tracey gw_init_gw_dir(char *dir)
741 2c251c14 2020-01-15 tracey {
742 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
743 2c251c14 2020-01-15 tracey
744 2c251c14 2020-01-15 tracey if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
745 2c251c14 2020-01-15 tracey return NULL;
746 2c251c14 2020-01-15 tracey
747 2c251c14 2020-01-15 tracey if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
748 2c251c14 2020-01-15 tracey return NULL;
749 2c251c14 2020-01-15 tracey
750 2c251c14 2020-01-15 tracey return gw_dir;
751 474370cb 2020-01-15 tracey }
752 474370cb 2020-01-15 tracey
753 474370cb 2020-01-15 tracey static const struct got_error*
754 474370cb 2020-01-15 tracey match_logmsg(int *have_match, struct got_object_id *id,
755 474370cb 2020-01-15 tracey struct got_commit_object *commit, regex_t *regex)
756 474370cb 2020-01-15 tracey {
757 474370cb 2020-01-15 tracey const struct got_error *err = NULL;
758 474370cb 2020-01-15 tracey regmatch_t regmatch;
759 474370cb 2020-01-15 tracey char *id_str = NULL, *logmsg = NULL;
760 474370cb 2020-01-15 tracey
761 474370cb 2020-01-15 tracey *have_match = 0;
762 474370cb 2020-01-15 tracey
763 474370cb 2020-01-15 tracey err = got_object_id_str(&id_str, id);
764 474370cb 2020-01-15 tracey if (err)
765 474370cb 2020-01-15 tracey return err;
766 474370cb 2020-01-15 tracey
767 474370cb 2020-01-15 tracey err = got_object_commit_get_logmsg(&logmsg, commit);
768 474370cb 2020-01-15 tracey if (err)
769 474370cb 2020-01-15 tracey goto done;
770 474370cb 2020-01-15 tracey
771 474370cb 2020-01-15 tracey if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
772 474370cb 2020-01-15 tracey *have_match = 1;
773 474370cb 2020-01-15 tracey done:
774 474370cb 2020-01-15 tracey free(id_str);
775 474370cb 2020-01-15 tracey free(logmsg);
776 474370cb 2020-01-15 tracey return err;
777 2c251c14 2020-01-15 tracey }
778 2c251c14 2020-01-15 tracey
779 2c251c14 2020-01-15 tracey static void
780 2c251c14 2020-01-15 tracey gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
781 2c251c14 2020-01-15 tracey {
782 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
783 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
784 2c251c14 2020-01-15 tracey khttps[code]);
785 387a29ba 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
786 2c251c14 2020-01-15 tracey kmimetypes[mime]);
787 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
788 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
789 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
790 2c251c14 2020-01-15 tracey khttp_body(gw_trans->gw_req);
791 2c251c14 2020-01-15 tracey }
792 2c251c14 2020-01-15 tracey
793 2c251c14 2020-01-15 tracey static void
794 2c251c14 2020-01-15 tracey gw_display_index(struct trans *gw_trans, const struct got_error *err)
795 2c251c14 2020-01-15 tracey {
796 2c251c14 2020-01-15 tracey gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
797 2c251c14 2020-01-15 tracey khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
798 2c251c14 2020-01-15 tracey
799 2c251c14 2020-01-15 tracey if (err)
800 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, err->msg);
801 2c251c14 2020-01-15 tracey else
802 2c251c14 2020-01-15 tracey khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
803 2c251c14 2020-01-15 tracey gw_query_funcs[gw_trans->action].template);
804 2c251c14 2020-01-15 tracey
805 2c251c14 2020-01-15 tracey khtml_close(gw_trans->gw_html_req);
806 2c251c14 2020-01-15 tracey }
807 2c251c14 2020-01-15 tracey
808 2c251c14 2020-01-15 tracey static int
809 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
810 2c251c14 2020-01-15 tracey {
811 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
812 2c251c14 2020-01-15 tracey struct trans *gw_trans = arg;
813 2c251c14 2020-01-15 tracey char *gw_got_link, *gw_site_link;
814 2c251c14 2020-01-15 tracey char *site_owner_name, *site_owner_name_h;
815 2c251c14 2020-01-15 tracey
816 2c251c14 2020-01-15 tracey switch (key) {
817 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
818 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, head);
819 2c251c14 2020-01-15 tracey break;
820 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
821 2c251c14 2020-01-15 tracey gw_got_link = gw_get_got_link(gw_trans);
822 2c251c14 2020-01-15 tracey if (gw_got_link != NULL)
823 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_got_link);
824 2c251c14 2020-01-15 tracey
825 2c251c14 2020-01-15 tracey free(gw_got_link);
826 2c251c14 2020-01-15 tracey break;
827 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
828 2c251c14 2020-01-15 tracey gw_site_link = gw_get_site_link(gw_trans);
829 2c251c14 2020-01-15 tracey if (gw_site_link != NULL)
830 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_site_link);
831 2c251c14 2020-01-15 tracey
832 2c251c14 2020-01-15 tracey free(gw_site_link);
833 2c251c14 2020-01-15 tracey break;
834 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
835 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_name != NULL)
836 2c251c14 2020-01-15 tracey khtml_puts(gw_trans->gw_html_req,
837 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
838 2c251c14 2020-01-15 tracey
839 2c251c14 2020-01-15 tracey break;
840 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
841 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, search);
842 2c251c14 2020-01-15 tracey break;
843 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
844 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
845 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
846 2c251c14 2020-01-15 tracey site_owner_name =
847 2c251c14 2020-01-15 tracey gw_html_escape(gw_trans->gw_conf->got_site_owner);
848 2c251c14 2020-01-15 tracey if ((asprintf(&site_owner_name_h, site_owner,
849 2c251c14 2020-01-15 tracey site_owner_name))
850 2c251c14 2020-01-15 tracey == -1)
851 2c251c14 2020-01-15 tracey return 0;
852 2c251c14 2020-01-15 tracey
853 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, site_owner_name_h);
854 2c251c14 2020-01-15 tracey free(site_owner_name);
855 2c251c14 2020-01-15 tracey free(site_owner_name_h);
856 2c251c14 2020-01-15 tracey }
857 2c251c14 2020-01-15 tracey break;
858 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
859 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
860 2c251c14 2020-01-15 tracey if (error)
861 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
862 2c251c14 2020-01-15 tracey
863 2c251c14 2020-01-15 tracey break;
864 2c251c14 2020-01-15 tracey default:
865 2c251c14 2020-01-15 tracey return 0;
866 2c251c14 2020-01-15 tracey break;
867 2c251c14 2020-01-15 tracey }
868 2c251c14 2020-01-15 tracey return 1;
869 2c251c14 2020-01-15 tracey }
870 2c251c14 2020-01-15 tracey
871 2c251c14 2020-01-15 tracey static char *
872 2c251c14 2020-01-15 tracey gw_get_repo_description(struct trans *gw_trans, char *dir)
873 2c251c14 2020-01-15 tracey {
874 2c251c14 2020-01-15 tracey FILE *f;
875 2c251c14 2020-01-15 tracey char *description = NULL, *d_file = NULL;
876 2c251c14 2020-01-15 tracey unsigned int len;
877 2c251c14 2020-01-15 tracey
878 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description == false)
879 2c251c14 2020-01-15 tracey goto err;
880 2c251c14 2020-01-15 tracey
881 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/description", dir)) == -1)
882 2c251c14 2020-01-15 tracey goto err;
883 2c251c14 2020-01-15 tracey
884 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
885 2c251c14 2020-01-15 tracey goto err;
886 2c251c14 2020-01-15 tracey
887 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
888 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
889 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
890 2c251c14 2020-01-15 tracey if ((description = calloc(len, sizeof(char *))) == NULL)
891 2c251c14 2020-01-15 tracey goto err;
892 2c251c14 2020-01-15 tracey
893 2c251c14 2020-01-15 tracey fread(description, 1, len, f);
894 2c251c14 2020-01-15 tracey fclose(f);
895 2c251c14 2020-01-15 tracey free(d_file);
896 2c251c14 2020-01-15 tracey return description;
897 2c251c14 2020-01-15 tracey err:
898 2c251c14 2020-01-15 tracey if ((asprintf(&description, "%s", "")) == -1)
899 2c251c14 2020-01-15 tracey return NULL;
900 2c251c14 2020-01-15 tracey
901 2c251c14 2020-01-15 tracey return description;
902 2c251c14 2020-01-15 tracey }
903 2c251c14 2020-01-15 tracey
904 2c251c14 2020-01-15 tracey static char *
905 474370cb 2020-01-15 tracey gw_get_time_str(time_t committer_time, int ref_tm)
906 474370cb 2020-01-15 tracey {
907 474370cb 2020-01-15 tracey struct tm tm;
908 474370cb 2020-01-15 tracey time_t diff_time;
909 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
910 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
911 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
912 474370cb 2020-01-15 tracey char *now = "right now";
913 474370cb 2020-01-15 tracey char *repo_age, *s;
914 474370cb 2020-01-15 tracey char datebuf[BUFFER_SIZE];
915 474370cb 2020-01-15 tracey
916 474370cb 2020-01-15 tracey switch (ref_tm) {
917 474370cb 2020-01-15 tracey case TM_DIFF:
918 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
919 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
920 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
921 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 365), years)) == -1)
922 474370cb 2020-01-15 tracey return NULL;
923 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
924 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
925 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
926 474370cb 2020-01-15 tracey months)) == -1)
927 474370cb 2020-01-15 tracey return NULL;
928 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
929 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
930 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
931 474370cb 2020-01-15 tracey return NULL;
932 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
933 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
934 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24), days)) == -1)
935 474370cb 2020-01-15 tracey return NULL;
936 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
937 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
938 474370cb 2020-01-15 tracey (diff_time / 60 / 60), hours)) == -1)
939 474370cb 2020-01-15 tracey return NULL;
940 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
941 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
942 474370cb 2020-01-15 tracey minutes)) == -1)
943 474370cb 2020-01-15 tracey return NULL;
944 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
945 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", diff_time,
946 474370cb 2020-01-15 tracey seconds)) == -1)
947 474370cb 2020-01-15 tracey return NULL;
948 474370cb 2020-01-15 tracey } else {
949 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s", now)) == -1)
950 474370cb 2020-01-15 tracey return NULL;
951 474370cb 2020-01-15 tracey }
952 474370cb 2020-01-15 tracey break;
953 474370cb 2020-01-15 tracey case TM_LONG:
954 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
955 474370cb 2020-01-15 tracey return NULL;
956 474370cb 2020-01-15 tracey
957 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
958 474370cb 2020-01-15 tracey if (s == NULL)
959 474370cb 2020-01-15 tracey return NULL;
960 474370cb 2020-01-15 tracey
961 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
962 474370cb 2020-01-15 tracey return NULL;
963 474370cb 2020-01-15 tracey break;
964 474370cb 2020-01-15 tracey }
965 474370cb 2020-01-15 tracey return repo_age;
966 474370cb 2020-01-15 tracey }
967 474370cb 2020-01-15 tracey
968 474370cb 2020-01-15 tracey static char *
969 387a29ba 2020-01-15 tracey gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
970 2c251c14 2020-01-15 tracey {
971 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
972 2c251c14 2020-01-15 tracey struct got_object_id *id = NULL;
973 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
974 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
975 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
976 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
977 2c251c14 2020-01-15 tracey struct got_reference *head_ref;
978 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
979 474370cb 2020-01-15 tracey char *repo_age = NULL;
980 387a29ba 2020-01-15 tracey
981 387a29ba 2020-01-15 tracey if (repo_ref == NULL)
982 387a29ba 2020-01-15 tracey return NULL;
983 2c251c14 2020-01-15 tracey
984 2c251c14 2020-01-15 tracey SIMPLEQ_INIT(&refs);
985 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age == false) {
986 2c251c14 2020-01-15 tracey asprintf(&repo_age, "");
987 2c251c14 2020-01-15 tracey return repo_age;
988 2c251c14 2020-01-15 tracey }
989 2c251c14 2020-01-15 tracey error = got_repo_open(&repo, dir, NULL);
990 2c251c14 2020-01-15 tracey if (error != NULL)
991 2c251c14 2020-01-15 tracey goto err;
992 2c251c14 2020-01-15 tracey
993 387a29ba 2020-01-15 tracey error = got_ref_list(&refs, repo, repo_ref, got_ref_cmp_by_name,
994 2c251c14 2020-01-15 tracey NULL);
995 2c251c14 2020-01-15 tracey if (error != NULL)
996 2c251c14 2020-01-15 tracey goto err;
997 2c251c14 2020-01-15 tracey
998 2c251c14 2020-01-15 tracey const char *refname;
999 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1000 2c251c14 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1001 2c251c14 2020-01-15 tracey error = got_ref_open(&head_ref, repo, refname, 0);
1002 2c251c14 2020-01-15 tracey if (error != NULL)
1003 2c251c14 2020-01-15 tracey goto err;
1004 2c251c14 2020-01-15 tracey
1005 2c251c14 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
1006 2c251c14 2020-01-15 tracey got_ref_close(head_ref);
1007 2c251c14 2020-01-15 tracey if (error != NULL)
1008 2c251c14 2020-01-15 tracey goto err;
1009 2c251c14 2020-01-15 tracey
1010 387a29ba 2020-01-15 tracey /* here is what breaks tags, so adjust */
1011 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1012 2c251c14 2020-01-15 tracey if (error != NULL)
1013 2c251c14 2020-01-15 tracey goto err;
1014 2c251c14 2020-01-15 tracey
1015 2c251c14 2020-01-15 tracey committer_time =
1016 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
1017 2c251c14 2020-01-15 tracey
1018 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
1019 2c251c14 2020-01-15 tracey cmp_time = committer_time;
1020 2c251c14 2020-01-15 tracey }
1021 2c251c14 2020-01-15 tracey
1022 474370cb 2020-01-15 tracey if (cmp_time != 0) {
1023 2c251c14 2020-01-15 tracey committer_time = cmp_time;
1024 474370cb 2020-01-15 tracey repo_age = gw_get_time_str(committer_time, ref_tm);
1025 474370cb 2020-01-15 tracey } else
1026 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "")) == -1)
1027 474370cb 2020-01-15 tracey return NULL;
1028 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
1029 2c251c14 2020-01-15 tracey free(id);
1030 2c251c14 2020-01-15 tracey return repo_age;
1031 2c251c14 2020-01-15 tracey err:
1032 2c251c14 2020-01-15 tracey if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1033 2c251c14 2020-01-15 tracey return NULL;
1034 2c251c14 2020-01-15 tracey
1035 2c251c14 2020-01-15 tracey return repo_age;
1036 2c251c14 2020-01-15 tracey }
1037 2c251c14 2020-01-15 tracey
1038 2c251c14 2020-01-15 tracey static char *
1039 2c251c14 2020-01-15 tracey gw_get_repo_owner(struct trans *gw_trans, char *dir)
1040 2c251c14 2020-01-15 tracey {
1041 2c251c14 2020-01-15 tracey FILE *f;
1042 2c251c14 2020-01-15 tracey char *owner = NULL, *d_file = NULL;
1043 2c251c14 2020-01-15 tracey char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1044 2c251c14 2020-01-15 tracey char *comp, *pos, *buf;
1045 2c251c14 2020-01-15 tracey unsigned int i;
1046 2c251c14 2020-01-15 tracey
1047 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner == false)
1048 2c251c14 2020-01-15 tracey goto err;
1049 2c251c14 2020-01-15 tracey
1050 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/config", dir)) == -1)
1051 2c251c14 2020-01-15 tracey goto err;
1052 2c251c14 2020-01-15 tracey
1053 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1054 2c251c14 2020-01-15 tracey goto err;
1055 2c251c14 2020-01-15 tracey
1056 2c251c14 2020-01-15 tracey if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1057 2c251c14 2020-01-15 tracey goto err;
1058 2c251c14 2020-01-15 tracey
1059 2c251c14 2020-01-15 tracey while ((fgets(buf, BUFFER_SIZE, f)) != NULL) {
1060 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gotweb)) != NULL)
1061 2c251c14 2020-01-15 tracey break;
1062 2c251c14 2020-01-15 tracey
1063 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gitweb)) != NULL)
1064 2c251c14 2020-01-15 tracey break;
1065 2c251c14 2020-01-15 tracey }
1066 2c251c14 2020-01-15 tracey
1067 2c251c14 2020-01-15 tracey if (pos == NULL)
1068 2c251c14 2020-01-15 tracey goto err;
1069 2c251c14 2020-01-15 tracey
1070 2c251c14 2020-01-15 tracey do {
1071 2c251c14 2020-01-15 tracey fgets(buf, BUFFER_SIZE, f);
1072 2c251c14 2020-01-15 tracey } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1073 2c251c14 2020-01-15 tracey
1074 2c251c14 2020-01-15 tracey if (comp == NULL)
1075 2c251c14 2020-01-15 tracey goto err;
1076 2c251c14 2020-01-15 tracey
1077 2c251c14 2020-01-15 tracey if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1078 2c251c14 2020-01-15 tracey goto err;
1079 2c251c14 2020-01-15 tracey
1080 2c251c14 2020-01-15 tracey for (i = 0; i < 2; i++) {
1081 2c251c14 2020-01-15 tracey owner = strsep(&buf, "\"");
1082 2c251c14 2020-01-15 tracey }
1083 2c251c14 2020-01-15 tracey
1084 2c251c14 2020-01-15 tracey if (owner == NULL)
1085 2c251c14 2020-01-15 tracey goto err;
1086 2c251c14 2020-01-15 tracey
1087 2c251c14 2020-01-15 tracey fclose(f);
1088 2c251c14 2020-01-15 tracey free(d_file);
1089 2c251c14 2020-01-15 tracey return owner;
1090 2c251c14 2020-01-15 tracey err:
1091 2c251c14 2020-01-15 tracey if ((asprintf(&owner, "%s", "")) == -1)
1092 2c251c14 2020-01-15 tracey return NULL;
1093 2c251c14 2020-01-15 tracey
1094 2c251c14 2020-01-15 tracey return owner;
1095 2c251c14 2020-01-15 tracey }
1096 2c251c14 2020-01-15 tracey
1097 2c251c14 2020-01-15 tracey static char *
1098 2c251c14 2020-01-15 tracey gw_get_clone_url(struct trans *gw_trans, char *dir)
1099 2c251c14 2020-01-15 tracey {
1100 2c251c14 2020-01-15 tracey FILE *f;
1101 2c251c14 2020-01-15 tracey char *url = NULL, *d_file = NULL;
1102 2c251c14 2020-01-15 tracey unsigned int len;
1103 2c251c14 2020-01-15 tracey
1104 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1105 2c251c14 2020-01-15 tracey return NULL;
1106 2c251c14 2020-01-15 tracey
1107 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1108 2c251c14 2020-01-15 tracey return NULL;
1109 2c251c14 2020-01-15 tracey
1110 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1111 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1112 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1113 2c251c14 2020-01-15 tracey
1114 2c251c14 2020-01-15 tracey if ((url = calloc(len, sizeof(char *))) == NULL)
1115 2c251c14 2020-01-15 tracey return NULL;
1116 2c251c14 2020-01-15 tracey
1117 2c251c14 2020-01-15 tracey fread(url, 1, len, f);
1118 2c251c14 2020-01-15 tracey fclose(f);
1119 2c251c14 2020-01-15 tracey free(d_file);
1120 2c251c14 2020-01-15 tracey return url;
1121 8d4d2453 2020-01-15 tracey }
1122 8d4d2453 2020-01-15 tracey
1123 8d4d2453 2020-01-15 tracey static char *
1124 4ceb8155 2020-01-15 tracey gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1125 4ceb8155 2020-01-15 tracey char *start_commit, int limit, int full_log)
1126 8d4d2453 2020-01-15 tracey {
1127 c6b62706 2020-01-15 tracey const struct got_error *error;
1128 c6b62706 2020-01-15 tracey struct got_repository *repo = NULL;
1129 c6b62706 2020-01-15 tracey struct got_reflist_head refs;
1130 474370cb 2020-01-15 tracey struct got_reflist_entry *re;
1131 c6b62706 2020-01-15 tracey struct got_commit_object *commit = NULL;
1132 c6b62706 2020-01-15 tracey struct got_object_id *id = NULL;
1133 474370cb 2020-01-15 tracey struct got_commit_graph *graph = NULL;
1134 4ceb8155 2020-01-15 tracey char *logbriefs = NULL, *id_str = NULL, *path = NULL,
1135 4ceb8155 2020-01-15 tracey *in_repo_path = NULL, *commit_row = NULL, *commit_age = NULL,
1136 4ceb8155 2020-01-15 tracey *commit_author = NULL, *commit_log = NULL, *commit_log0, *newline,
1137 4ceb8155 2020-01-15 tracey *logbriefs_navs_html = NULL;
1138 474370cb 2020-01-15 tracey regex_t regex;
1139 9d84e7dd 2020-01-15 tracey int have_match;
1140 474370cb 2020-01-15 tracey size_t newsize;
1141 474370cb 2020-01-15 tracey struct buf *diffbuf;
1142 474370cb 2020-01-15 tracey time_t committer_time;
1143 8d4d2453 2020-01-15 tracey
1144 474370cb 2020-01-15 tracey if (search_pattern &&
1145 474370cb 2020-01-15 tracey regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1146 474370cb 2020-01-15 tracey REG_NEWLINE))
1147 474370cb 2020-01-15 tracey return NULL;
1148 474370cb 2020-01-15 tracey
1149 474370cb 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1150 474370cb 2020-01-15 tracey
1151 c6b62706 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1152 c6b62706 2020-01-15 tracey if (error != NULL)
1153 c6b62706 2020-01-15 tracey goto done;
1154 c6b62706 2020-01-15 tracey
1155 474370cb 2020-01-15 tracey error = buf_alloc(&diffbuf, BUFFER_SIZE);
1156 474370cb 2020-01-15 tracey if (error != NULL)
1157 474370cb 2020-01-15 tracey goto done;
1158 474370cb 2020-01-15 tracey
1159 c6b62706 2020-01-15 tracey if (start_commit == NULL) {
1160 c6b62706 2020-01-15 tracey struct got_reference *head_ref;
1161 c6b62706 2020-01-15 tracey error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1162 c6b62706 2020-01-15 tracey if (error != NULL)
1163 c6b62706 2020-01-15 tracey return NULL;
1164 c6b62706 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
1165 c6b62706 2020-01-15 tracey got_ref_close(head_ref);
1166 c6b62706 2020-01-15 tracey if (error != NULL)
1167 c6b62706 2020-01-15 tracey return NULL;
1168 c6b62706 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1169 c6b62706 2020-01-15 tracey } else {
1170 c6b62706 2020-01-15 tracey struct got_reference *ref;
1171 c6b62706 2020-01-15 tracey error = got_ref_open(&ref, repo, start_commit, 0);
1172 c6b62706 2020-01-15 tracey if (error == NULL) {
1173 c6b62706 2020-01-15 tracey int obj_type;
1174 c6b62706 2020-01-15 tracey error = got_ref_resolve(&id, repo, ref);
1175 c6b62706 2020-01-15 tracey got_ref_close(ref);
1176 c6b62706 2020-01-15 tracey if (error != NULL)
1177 c6b62706 2020-01-15 tracey goto done;
1178 c6b62706 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, id);
1179 c6b62706 2020-01-15 tracey if (error != NULL)
1180 c6b62706 2020-01-15 tracey goto done;
1181 c6b62706 2020-01-15 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
1182 c6b62706 2020-01-15 tracey struct got_tag_object *tag;
1183 c6b62706 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
1184 c6b62706 2020-01-15 tracey if (error != NULL)
1185 c6b62706 2020-01-15 tracey goto done;
1186 c6b62706 2020-01-15 tracey if (got_object_tag_get_object_type(tag) !=
1187 c6b62706 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT) {
1188 c6b62706 2020-01-15 tracey got_object_tag_close(tag);
1189 c6b62706 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1190 c6b62706 2020-01-15 tracey goto done;
1191 c6b62706 2020-01-15 tracey }
1192 c6b62706 2020-01-15 tracey free(id);
1193 c6b62706 2020-01-15 tracey id = got_object_id_dup(
1194 c6b62706 2020-01-15 tracey got_object_tag_get_object_id(tag));
1195 c6b62706 2020-01-15 tracey if (id == NULL)
1196 c6b62706 2020-01-15 tracey error = got_error_from_errno(
1197 c6b62706 2020-01-15 tracey "got_object_id_dup");
1198 c6b62706 2020-01-15 tracey got_object_tag_close(tag);
1199 c6b62706 2020-01-15 tracey if (error)
1200 c6b62706 2020-01-15 tracey goto done;
1201 c6b62706 2020-01-15 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1202 c6b62706 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1203 c6b62706 2020-01-15 tracey goto done;
1204 c6b62706 2020-01-15 tracey }
1205 c6b62706 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1206 c6b62706 2020-01-15 tracey if (error != NULL)
1207 c6b62706 2020-01-15 tracey goto done;
1208 c6b62706 2020-01-15 tracey }
1209 c6b62706 2020-01-15 tracey if (commit == NULL) {
1210 c6b62706 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id,
1211 c6b62706 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1212 c6b62706 2020-01-15 tracey if (error != NULL)
1213 c6b62706 2020-01-15 tracey return NULL;
1214 c6b62706 2020-01-15 tracey }
1215 c6b62706 2020-01-15 tracey }
1216 c6b62706 2020-01-15 tracey
1217 c6b62706 2020-01-15 tracey if (error != NULL)
1218 c6b62706 2020-01-15 tracey goto done;
1219 c6b62706 2020-01-15 tracey
1220 474370cb 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1221 474370cb 2020-01-15 tracey if (error != NULL)
1222 474370cb 2020-01-15 tracey goto done;
1223 474370cb 2020-01-15 tracey
1224 474370cb 2020-01-15 tracey if (in_repo_path) {
1225 474370cb 2020-01-15 tracey free(path);
1226 474370cb 2020-01-15 tracey path = in_repo_path;
1227 474370cb 2020-01-15 tracey }
1228 474370cb 2020-01-15 tracey
1229 c6b62706 2020-01-15 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1230 c6b62706 2020-01-15 tracey if (error)
1231 c6b62706 2020-01-15 tracey goto done;
1232 c6b62706 2020-01-15 tracey
1233 474370cb 2020-01-15 tracey error = got_commit_graph_open(&graph, id, path, 0, repo);
1234 474370cb 2020-01-15 tracey if (error)
1235 474370cb 2020-01-15 tracey goto done;
1236 474370cb 2020-01-15 tracey
1237 474370cb 2020-01-15 tracey error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
1238 474370cb 2020-01-15 tracey if (error)
1239 474370cb 2020-01-15 tracey goto done;
1240 474370cb 2020-01-15 tracey
1241 474370cb 2020-01-15 tracey for (;;) {
1242 474370cb 2020-01-15 tracey struct got_commit_object *commit_disp;
1243 474370cb 2020-01-15 tracey
1244 474370cb 2020-01-15 tracey error = got_commit_graph_iter_next(&id, graph);
1245 474370cb 2020-01-15 tracey if (error) {
1246 474370cb 2020-01-15 tracey if (error->code == GOT_ERR_ITER_COMPLETED) {
1247 474370cb 2020-01-15 tracey error = NULL;
1248 474370cb 2020-01-15 tracey break;
1249 474370cb 2020-01-15 tracey }
1250 474370cb 2020-01-15 tracey if (error->code != GOT_ERR_ITER_NEED_MORE)
1251 474370cb 2020-01-15 tracey break;
1252 474370cb 2020-01-15 tracey error = got_commit_graph_fetch_commits(graph, 1, repo,
1253 474370cb 2020-01-15 tracey NULL, NULL);
1254 474370cb 2020-01-15 tracey if (error)
1255 474370cb 2020-01-15 tracey break;
1256 474370cb 2020-01-15 tracey else
1257 474370cb 2020-01-15 tracey continue;
1258 474370cb 2020-01-15 tracey }
1259 474370cb 2020-01-15 tracey if (id == NULL)
1260 474370cb 2020-01-15 tracey break;
1261 474370cb 2020-01-15 tracey
1262 474370cb 2020-01-15 tracey error = got_object_open_as_commit(&commit_disp, repo, id);
1263 474370cb 2020-01-15 tracey if (error)
1264 474370cb 2020-01-15 tracey break;
1265 474370cb 2020-01-15 tracey
1266 474370cb 2020-01-15 tracey if (search_pattern) {
1267 474370cb 2020-01-15 tracey error = match_logmsg(&have_match, id, commit_disp,
1268 474370cb 2020-01-15 tracey &regex);
1269 474370cb 2020-01-15 tracey if (error) {
1270 474370cb 2020-01-15 tracey got_object_commit_close(commit_disp);
1271 474370cb 2020-01-15 tracey break;
1272 474370cb 2020-01-15 tracey }
1273 474370cb 2020-01-15 tracey if (have_match == 0) {
1274 474370cb 2020-01-15 tracey got_object_commit_close(commit_disp);
1275 474370cb 2020-01-15 tracey continue;
1276 474370cb 2020-01-15 tracey }
1277 474370cb 2020-01-15 tracey }
1278 474370cb 2020-01-15 tracey
1279 474370cb 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1280 474370cb 2020-01-15 tracey const char *name;
1281 474370cb 2020-01-15 tracey struct got_tag_object *tag = NULL;
1282 474370cb 2020-01-15 tracey int cmp;
1283 474370cb 2020-01-15 tracey
1284 474370cb 2020-01-15 tracey name = got_ref_get_name(re->ref);
1285 474370cb 2020-01-15 tracey if (strcmp(name, GOT_REF_HEAD) == 0)
1286 474370cb 2020-01-15 tracey continue;
1287 474370cb 2020-01-15 tracey if (strncmp(name, "refs/", 5) == 0)
1288 474370cb 2020-01-15 tracey name += 5;
1289 474370cb 2020-01-15 tracey if (strncmp(name, "got/", 4) == 0)
1290 474370cb 2020-01-15 tracey continue;
1291 474370cb 2020-01-15 tracey if (strncmp(name, "heads/", 6) == 0)
1292 474370cb 2020-01-15 tracey name += 6;
1293 474370cb 2020-01-15 tracey if (strncmp(name, "remotes/", 8) == 0)
1294 474370cb 2020-01-15 tracey name += 8;
1295 474370cb 2020-01-15 tracey if (strncmp(name, "tags/", 5) == 0) {
1296 474370cb 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo,
1297 474370cb 2020-01-15 tracey re->id);
1298 474370cb 2020-01-15 tracey if (error) {
1299 474370cb 2020-01-15 tracey if (error->code != GOT_ERR_OBJ_TYPE)
1300 474370cb 2020-01-15 tracey continue;
1301 474370cb 2020-01-15 tracey /*
1302 474370cb 2020-01-15 tracey * Ref points at something other
1303 474370cb 2020-01-15 tracey * than a tag.
1304 474370cb 2020-01-15 tracey */
1305 474370cb 2020-01-15 tracey error = NULL;
1306 474370cb 2020-01-15 tracey tag = NULL;
1307 474370cb 2020-01-15 tracey }
1308 474370cb 2020-01-15 tracey }
1309 474370cb 2020-01-15 tracey cmp = got_object_id_cmp(tag ?
1310 474370cb 2020-01-15 tracey got_object_tag_get_object_id(tag) : re->id, id);
1311 474370cb 2020-01-15 tracey if (tag)
1312 474370cb 2020-01-15 tracey got_object_tag_close(tag);
1313 474370cb 2020-01-15 tracey if (cmp != 0)
1314 474370cb 2020-01-15 tracey continue;
1315 474370cb 2020-01-15 tracey }
1316 cdb914e5 2020-01-15 tracey
1317 cdb914e5 2020-01-15 tracey got_ref_list_free(&refs);
1318 474370cb 2020-01-15 tracey
1319 474370cb 2020-01-15 tracey /* commit id */
1320 474370cb 2020-01-15 tracey error = got_object_id_str(&id_str, id);
1321 474370cb 2020-01-15 tracey if (error)
1322 474370cb 2020-01-15 tracey break;
1323 474370cb 2020-01-15 tracey
1324 474370cb 2020-01-15 tracey committer_time =
1325 474370cb 2020-01-15 tracey got_object_commit_get_committer_time(commit_disp);
1326 4ceb8155 2020-01-15 tracey
1327 4ceb8155 2020-01-15 tracey if (full_log) {
1328 4ceb8155 2020-01-15 tracey
1329 4ceb8155 2020-01-15 tracey asprintf(&commit_age, "%s",
1330 4ceb8155 2020-01-15 tracey gw_get_time_str(committer_time, TM_LONG));
1331 4ceb8155 2020-01-15 tracey asprintf(&commit_author, "%s",
1332 4ceb8155 2020-01-15 tracey got_object_commit_get_author(commit_disp));
1333 4ceb8155 2020-01-15 tracey error = got_object_commit_get_logmsg(&commit_log0,
1334 4ceb8155 2020-01-15 tracey commit_disp);
1335 4ceb8155 2020-01-15 tracey if (error)
1336 4ceb8155 2020-01-15 tracey commit_log = strdup("");
1337 4ceb8155 2020-01-15 tracey else
1338 4ceb8155 2020-01-15 tracey commit_log = gw_html_escape(commit_log0);
1339 4ceb8155 2020-01-15 tracey asprintf(&logbriefs_navs_html, logbriefs_navs,
1340 4ceb8155 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name,
1341 4ceb8155 2020-01-15 tracey id_str, gw_trans->repo_name, id_str,
1342 4ceb8155 2020-01-15 tracey gw_trans->repo_name, id_str);
1343 4ceb8155 2020-01-15 tracey asprintf(&commit_row, logs_row, id_str,
1344 4ceb8155 2020-01-15 tracey gw_html_escape(commit_author), commit_age,
1345 4ceb8155 2020-01-15 tracey commit_log, logbriefs_navs_html);
1346 4ceb8155 2020-01-15 tracey error = buf_append(&newsize, diffbuf, commit_row,
1347 4ceb8155 2020-01-15 tracey strlen(commit_row));
1348 4ceb8155 2020-01-15 tracey } else {
1349 4ceb8155 2020-01-15 tracey asprintf(&commit_age, "%s",
1350 4ceb8155 2020-01-15 tracey gw_get_time_str(committer_time, TM_DIFF));
1351 4ceb8155 2020-01-15 tracey asprintf(&commit_author, "%s",
1352 4ceb8155 2020-01-15 tracey got_object_commit_get_author(commit_disp));
1353 4ceb8155 2020-01-15 tracey error = got_object_commit_get_logmsg(&commit_log0,
1354 4ceb8155 2020-01-15 tracey commit_disp);
1355 4ceb8155 2020-01-15 tracey if (error)
1356 4ceb8155 2020-01-15 tracey commit_log = strdup("");
1357 4ceb8155 2020-01-15 tracey else {
1358 4ceb8155 2020-01-15 tracey commit_log = commit_log0;
1359 4ceb8155 2020-01-15 tracey while (*commit_log == '\n')
1360 4ceb8155 2020-01-15 tracey commit_log++;
1361 4ceb8155 2020-01-15 tracey newline = strchr(commit_log, '\n');
1362 4ceb8155 2020-01-15 tracey if (newline)
1363 4ceb8155 2020-01-15 tracey *newline = '\0';
1364 4ceb8155 2020-01-15 tracey }
1365 4ceb8155 2020-01-15 tracey asprintf(&logbriefs_navs_html, logbriefs_navs,
1366 4ceb8155 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name,
1367 4ceb8155 2020-01-15 tracey id_str, gw_trans->repo_name, id_str,
1368 4ceb8155 2020-01-15 tracey gw_trans->repo_name, id_str);
1369 4ceb8155 2020-01-15 tracey asprintf(&commit_row, logbriefs_row, commit_age,
1370 4ceb8155 2020-01-15 tracey commit_author, commit_log, logbriefs_navs_html);
1371 4ceb8155 2020-01-15 tracey error = buf_append(&newsize, diffbuf, commit_row,
1372 4ceb8155 2020-01-15 tracey strlen(commit_row));
1373 9d84e7dd 2020-01-15 tracey }
1374 474370cb 2020-01-15 tracey
1375 474370cb 2020-01-15 tracey free(commit_age);
1376 474370cb 2020-01-15 tracey free(commit_author);
1377 9d84e7dd 2020-01-15 tracey free(commit_log0);
1378 4ceb8155 2020-01-15 tracey free(logbriefs_navs_html);
1379 474370cb 2020-01-15 tracey free(commit_row);
1380 474370cb 2020-01-15 tracey free(id_str);
1381 474370cb 2020-01-15 tracey commit_age = NULL;
1382 474370cb 2020-01-15 tracey commit_author = NULL;
1383 474370cb 2020-01-15 tracey commit_log = NULL;
1384 4ceb8155 2020-01-15 tracey logbriefs_navs_html = NULL;
1385 474370cb 2020-01-15 tracey commit_row = NULL;
1386 474370cb 2020-01-15 tracey id_str = NULL;
1387 474370cb 2020-01-15 tracey
1388 474370cb 2020-01-15 tracey got_object_commit_close(commit_disp);
1389 474370cb 2020-01-15 tracey if (error || (limit && --limit == 0))
1390 474370cb 2020-01-15 tracey break;
1391 474370cb 2020-01-15 tracey }
1392 4ceb8155 2020-01-15 tracey logbriefs = strdup(diffbuf->cb_buf);
1393 c6b62706 2020-01-15 tracey got_object_commit_close(commit);
1394 c6b62706 2020-01-15 tracey
1395 474370cb 2020-01-15 tracey free(path);
1396 474370cb 2020-01-15 tracey free(id);
1397 474370cb 2020-01-15 tracey buf_free(diffbuf);
1398 474370cb 2020-01-15 tracey
1399 474370cb 2020-01-15 tracey if (repo) {
1400 474370cb 2020-01-15 tracey error = got_repo_close(repo);
1401 474370cb 2020-01-15 tracey if (error != NULL)
1402 474370cb 2020-01-15 tracey return NULL;
1403 474370cb 2020-01-15 tracey }
1404 474370cb 2020-01-15 tracey
1405 cdb914e5 2020-01-15 tracey if (search_pattern)
1406 cdb914e5 2020-01-15 tracey regfree(&regex);
1407 4ceb8155 2020-01-15 tracey return logbriefs;
1408 c6b62706 2020-01-15 tracey done:
1409 c6b62706 2020-01-15 tracey if (repo)
1410 c6b62706 2020-01-15 tracey got_repo_close(repo);
1411 c6b62706 2020-01-15 tracey got_ref_list_free(&refs);
1412 474370cb 2020-01-15 tracey
1413 474370cb 2020-01-15 tracey if (search_pattern)
1414 474370cb 2020-01-15 tracey regfree(&regex);
1415 474370cb 2020-01-15 tracey got_commit_graph_close(graph);
1416 c6b62706 2020-01-15 tracey return NULL;
1417 2c251c14 2020-01-15 tracey }
1418 2c251c14 2020-01-15 tracey
1419 2c251c14 2020-01-15 tracey static char *
1420 8d4d2453 2020-01-15 tracey gw_get_repo_tags(struct trans *gw_trans)
1421 8d4d2453 2020-01-15 tracey {
1422 8d4d2453 2020-01-15 tracey char *tags = NULL;
1423 8d4d2453 2020-01-15 tracey
1424 4ceb8155 2020-01-15 tracey asprintf(&tags, tags_row, "30 min ago", "1.0.0", "tag 1.0.0",
1425 4ceb8155 2020-01-15 tracey tags_navs);
1426 8d4d2453 2020-01-15 tracey return tags;
1427 8d4d2453 2020-01-15 tracey }
1428 8d4d2453 2020-01-15 tracey
1429 8d4d2453 2020-01-15 tracey static char *
1430 8d4d2453 2020-01-15 tracey gw_get_repo_heads(struct trans *gw_trans)
1431 8d4d2453 2020-01-15 tracey {
1432 8d4d2453 2020-01-15 tracey char *heads = NULL;
1433 8d4d2453 2020-01-15 tracey
1434 8d4d2453 2020-01-15 tracey asprintf(&heads, heads_row, "30 min ago", "master", heads_navs);
1435 8d4d2453 2020-01-15 tracey return heads;
1436 8d4d2453 2020-01-15 tracey }
1437 8d4d2453 2020-01-15 tracey
1438 8d4d2453 2020-01-15 tracey static char *
1439 2c251c14 2020-01-15 tracey gw_get_got_link(struct trans *gw_trans)
1440 2c251c14 2020-01-15 tracey {
1441 2c251c14 2020-01-15 tracey char *link;
1442 2c251c14 2020-01-15 tracey
1443 2c251c14 2020-01-15 tracey if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
1444 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_logo)) == -1)
1445 2c251c14 2020-01-15 tracey return NULL;
1446 2c251c14 2020-01-15 tracey
1447 2c251c14 2020-01-15 tracey return link;
1448 2c251c14 2020-01-15 tracey }
1449 2c251c14 2020-01-15 tracey
1450 2c251c14 2020-01-15 tracey static char *
1451 2c251c14 2020-01-15 tracey gw_get_site_link(struct trans *gw_trans)
1452 2c251c14 2020-01-15 tracey {
1453 2c251c14 2020-01-15 tracey char *link, *repo = "", *action = "";
1454 2c251c14 2020-01-15 tracey
1455 2c251c14 2020-01-15 tracey if (gw_trans->repo_name != NULL)
1456 2c251c14 2020-01-15 tracey if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
1457 2c251c14 2020-01-15 tracey "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
1458 2c251c14 2020-01-15 tracey return NULL;
1459 2c251c14 2020-01-15 tracey
1460 2c251c14 2020-01-15 tracey if (gw_trans->action_name != NULL)
1461 2c251c14 2020-01-15 tracey if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
1462 2c251c14 2020-01-15 tracey return NULL;
1463 2c251c14 2020-01-15 tracey
1464 2c251c14 2020-01-15 tracey if ((asprintf(&link, site_link, GOTWEB,
1465 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_link, repo, action)) == -1)
1466 2c251c14 2020-01-15 tracey return NULL;
1467 2c251c14 2020-01-15 tracey
1468 2c251c14 2020-01-15 tracey return link;
1469 2c251c14 2020-01-15 tracey }
1470 2c251c14 2020-01-15 tracey
1471 2c251c14 2020-01-15 tracey static char *
1472 2c251c14 2020-01-15 tracey gw_html_escape(const char *html)
1473 2c251c14 2020-01-15 tracey {
1474 2c251c14 2020-01-15 tracey char *escaped_str = NULL, *buf;
1475 2c251c14 2020-01-15 tracey char c[1];
1476 2c251c14 2020-01-15 tracey size_t sz, i;
1477 2c251c14 2020-01-15 tracey
1478 2c251c14 2020-01-15 tracey if ((buf = calloc(BUFFER_SIZE, sizeof(char *))) == NULL)
1479 2c251c14 2020-01-15 tracey return NULL;
1480 2c251c14 2020-01-15 tracey
1481 2c251c14 2020-01-15 tracey if (html == NULL)
1482 2c251c14 2020-01-15 tracey return NULL;
1483 2c251c14 2020-01-15 tracey else
1484 2c251c14 2020-01-15 tracey if ((sz = strlen(html)) == 0)
1485 2c251c14 2020-01-15 tracey return NULL;
1486 2c251c14 2020-01-15 tracey
1487 2c251c14 2020-01-15 tracey /* only work with BUFFER_SIZE */
1488 2c251c14 2020-01-15 tracey if (BUFFER_SIZE < sz)
1489 2c251c14 2020-01-15 tracey sz = BUFFER_SIZE;
1490 2c251c14 2020-01-15 tracey
1491 2c251c14 2020-01-15 tracey for (i = 0; i < sz; i++) {
1492 2c251c14 2020-01-15 tracey c[0] = html[i];
1493 2c251c14 2020-01-15 tracey switch (c[0]) {
1494 2c251c14 2020-01-15 tracey case ('>'):
1495 2c251c14 2020-01-15 tracey strcat(buf, "&gt;");
1496 2c251c14 2020-01-15 tracey break;
1497 2c251c14 2020-01-15 tracey case ('&'):
1498 2c251c14 2020-01-15 tracey strcat(buf, "&amp;");
1499 2c251c14 2020-01-15 tracey break;
1500 2c251c14 2020-01-15 tracey case ('<'):
1501 2c251c14 2020-01-15 tracey strcat(buf, "&lt;");
1502 2c251c14 2020-01-15 tracey break;
1503 2c251c14 2020-01-15 tracey case ('"'):
1504 2c251c14 2020-01-15 tracey strcat(buf, "&quot;");
1505 2c251c14 2020-01-15 tracey break;
1506 2c251c14 2020-01-15 tracey case ('\''):
1507 2c251c14 2020-01-15 tracey strcat(buf, "&apos;");
1508 2c251c14 2020-01-15 tracey break;
1509 2c251c14 2020-01-15 tracey case ('\n'):
1510 2c251c14 2020-01-15 tracey strcat(buf, "<br />");
1511 2c251c14 2020-01-15 tracey default:
1512 2c251c14 2020-01-15 tracey strcat(buf, &c[0]);
1513 2c251c14 2020-01-15 tracey break;
1514 2c251c14 2020-01-15 tracey }
1515 2c251c14 2020-01-15 tracey }
1516 2c251c14 2020-01-15 tracey asprintf(&escaped_str, "%s", buf);
1517 2c251c14 2020-01-15 tracey free(buf);
1518 2c251c14 2020-01-15 tracey return escaped_str;
1519 2c251c14 2020-01-15 tracey }
1520 2c251c14 2020-01-15 tracey
1521 2c251c14 2020-01-15 tracey int
1522 2c251c14 2020-01-15 tracey main()
1523 2c251c14 2020-01-15 tracey {
1524 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1525 2c251c14 2020-01-15 tracey struct trans *gw_trans;
1526 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
1527 2c251c14 2020-01-15 tracey const char *page = "index";
1528 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
1529 2c251c14 2020-01-15 tracey
1530 2c251c14 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
1531 2c251c14 2020-01-15 tracey errx(1, "malloc");
1532 2c251c14 2020-01-15 tracey
1533 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
1534 2c251c14 2020-01-15 tracey errx(1, "malloc");
1535 2c251c14 2020-01-15 tracey
1536 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
1537 2c251c14 2020-01-15 tracey errx(1, "malloc");
1538 2c251c14 2020-01-15 tracey
1539 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
1540 2c251c14 2020-01-15 tracey errx(1, "malloc");
1541 2c251c14 2020-01-15 tracey
1542 2c251c14 2020-01-15 tracey if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
1543 2c251c14 2020-01-15 tracey &page, 1, 0))
1544 2c251c14 2020-01-15 tracey errx(1, "khttp_parse");
1545 2c251c14 2020-01-15 tracey
1546 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
1547 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
1548 4ceb8155 2020-01-15 tracey gw_malloc = 0;
1549 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
1550 2c251c14 2020-01-15 tracey goto err;
1551 2c251c14 2020-01-15 tracey }
1552 2c251c14 2020-01-15 tracey
1553 46b9c89b 2020-01-15 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
1554 46b9c89b 2020-01-15 tracey error = got_error_from_errno("pledge");
1555 46b9c89b 2020-01-15 tracey goto err;
1556 46b9c89b 2020-01-15 tracey }
1557 46b9c89b 2020-01-15 tracey
1558 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
1559 2c251c14 2020-01-15 tracey
1560 2c251c14 2020-01-15 tracey gw_trans->page = 0;
1561 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
1562 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
1563 2c251c14 2020-01-15 tracey gw_trans->commit = NULL;
1564 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
1565 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->key = templs;
1566 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
1567 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
1568 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
1569 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
1570 2c251c14 2020-01-15 tracey
1571 2c251c14 2020-01-15 tracey err:
1572 2c251c14 2020-01-15 tracey if (error) {
1573 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
1574 2c251c14 2020-01-15 tracey gw_trans->action = GW_ERR;
1575 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
1576 2c251c14 2020-01-15 tracey goto done;
1577 2c251c14 2020-01-15 tracey }
1578 2c251c14 2020-01-15 tracey
1579 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
1580 2c251c14 2020-01-15 tracey if (error)
1581 2c251c14 2020-01-15 tracey goto err;
1582 2c251c14 2020-01-15 tracey
1583 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
1584 2c251c14 2020-01-15 tracey
1585 2c251c14 2020-01-15 tracey done:
1586 2c251c14 2020-01-15 tracey if (gw_malloc) {
1587 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
1588 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_www_path);
1589 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
1590 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
1591 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
1592 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
1593 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
1594 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
1595 2c251c14 2020-01-15 tracey free(gw_trans->commit);
1596 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
1597 2c251c14 2020-01-15 tracey free(gw_trans->repo_name);
1598 2c251c14 2020-01-15 tracey free(gw_trans->repo_file);
1599 2c251c14 2020-01-15 tracey free(gw_trans->action_name);
1600 2c251c14 2020-01-15 tracey
1601 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
1602 2c251c14 2020-01-15 tracey free(dir->name);
1603 2c251c14 2020-01-15 tracey free(dir->description);
1604 2c251c14 2020-01-15 tracey free(dir->age);
1605 2c251c14 2020-01-15 tracey free(dir->url);
1606 2c251c14 2020-01-15 tracey free(dir->path);
1607 2c251c14 2020-01-15 tracey free(dir);
1608 2c251c14 2020-01-15 tracey }
1609 2c251c14 2020-01-15 tracey
1610 2c251c14 2020-01-15 tracey }
1611 2c251c14 2020-01-15 tracey
1612 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);
1613 2c251c14 2020-01-15 tracey return EXIT_SUCCESS;
1614 2c251c14 2020-01-15 tracey }