Blame


1 2c251c14 2020-01-15 tracey /*
2 9460dac0 2020-01-15 tracey * Copyright (c) 2019, 2020 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 8087c3c5 2020-01-15 tracey char *headref;
70 2c251c14 2020-01-15 tracey unsigned int action;
71 2c251c14 2020-01-15 tracey unsigned int page;
72 2c251c14 2020-01-15 tracey unsigned int repos_total;
73 387a29ba 2020-01-15 tracey enum kmime mime;
74 2c251c14 2020-01-15 tracey };
75 2c251c14 2020-01-15 tracey
76 2c251c14 2020-01-15 tracey enum gw_key {
77 2c251c14 2020-01-15 tracey KEY_PATH,
78 2c251c14 2020-01-15 tracey KEY_ACTION,
79 2c251c14 2020-01-15 tracey KEY_COMMIT_ID,
80 2c251c14 2020-01-15 tracey KEY_FILE,
81 2c251c14 2020-01-15 tracey KEY_PAGE,
82 8087c3c5 2020-01-15 tracey KEY_HEADREF,
83 2c251c14 2020-01-15 tracey KEY__MAX
84 2c251c14 2020-01-15 tracey };
85 2c251c14 2020-01-15 tracey
86 2c251c14 2020-01-15 tracey struct gw_dir {
87 2c251c14 2020-01-15 tracey TAILQ_ENTRY(gw_dir) entry;
88 2c251c14 2020-01-15 tracey char *name;
89 2c251c14 2020-01-15 tracey char *owner;
90 2c251c14 2020-01-15 tracey char *description;
91 2c251c14 2020-01-15 tracey char *url;
92 2c251c14 2020-01-15 tracey char *age;
93 2c251c14 2020-01-15 tracey char *path;
94 2c251c14 2020-01-15 tracey };
95 2c251c14 2020-01-15 tracey
96 2c251c14 2020-01-15 tracey enum tmpl {
97 2c251c14 2020-01-15 tracey TEMPL_HEAD,
98 2c251c14 2020-01-15 tracey TEMPL_HEADER,
99 2c251c14 2020-01-15 tracey TEMPL_SITEPATH,
100 2c251c14 2020-01-15 tracey TEMPL_SITEOWNER,
101 2c251c14 2020-01-15 tracey TEMPL_TITLE,
102 2c251c14 2020-01-15 tracey TEMPL_SEARCH,
103 2c251c14 2020-01-15 tracey TEMPL_CONTENT,
104 2c251c14 2020-01-15 tracey TEMPL__MAX
105 2c251c14 2020-01-15 tracey };
106 2c251c14 2020-01-15 tracey
107 387a29ba 2020-01-15 tracey enum ref_tm {
108 387a29ba 2020-01-15 tracey TM_DIFF,
109 387a29ba 2020-01-15 tracey TM_LONG,
110 387a29ba 2020-01-15 tracey };
111 387a29ba 2020-01-15 tracey
112 8087c3c5 2020-01-15 tracey enum logs {
113 8087c3c5 2020-01-15 tracey LOGBRIEF,
114 8087c3c5 2020-01-15 tracey LOGCOMMIT,
115 8087c3c5 2020-01-15 tracey LOGFULL,
116 8087c3c5 2020-01-15 tracey LOGTREE,
117 87f9ebf5 2020-01-15 tracey LOGDIFF,
118 87f9ebf5 2020-01-15 tracey LOGBLAME,
119 b772de24 2020-01-15 tracey LOGTAG,
120 8087c3c5 2020-01-15 tracey };
121 8087c3c5 2020-01-15 tracey
122 87f9ebf5 2020-01-15 tracey enum tags {
123 87f9ebf5 2020-01-15 tracey TAGBRIEF,
124 87f9ebf5 2020-01-15 tracey TAGFULL,
125 87f9ebf5 2020-01-15 tracey };
126 87f9ebf5 2020-01-15 tracey
127 474370cb 2020-01-15 tracey struct buf {
128 474370cb 2020-01-15 tracey u_char *cb_buf;
129 474370cb 2020-01-15 tracey size_t cb_size;
130 474370cb 2020-01-15 tracey size_t cb_len;
131 474370cb 2020-01-15 tracey };
132 474370cb 2020-01-15 tracey
133 2c251c14 2020-01-15 tracey static const char *const templs[TEMPL__MAX] = {
134 2c251c14 2020-01-15 tracey "head",
135 2c251c14 2020-01-15 tracey "header",
136 2c251c14 2020-01-15 tracey "sitepath",
137 2c251c14 2020-01-15 tracey "siteowner",
138 2c251c14 2020-01-15 tracey "title",
139 2c251c14 2020-01-15 tracey "search",
140 2c251c14 2020-01-15 tracey "content",
141 2c251c14 2020-01-15 tracey };
142 2c251c14 2020-01-15 tracey
143 2c251c14 2020-01-15 tracey static const struct kvalid gw_keys[KEY__MAX] = {
144 2c251c14 2020-01-15 tracey { kvalid_stringne, "path" },
145 2c251c14 2020-01-15 tracey { kvalid_stringne, "action" },
146 2c251c14 2020-01-15 tracey { kvalid_stringne, "commit" },
147 2c251c14 2020-01-15 tracey { kvalid_stringne, "file" },
148 2c251c14 2020-01-15 tracey { kvalid_int, "page" },
149 8087c3c5 2020-01-15 tracey { kvalid_stringne, "headref" },
150 2c251c14 2020-01-15 tracey };
151 65b95fb2 2020-01-15 tracey
152 65b95fb2 2020-01-15 tracey int gw_get_repo_log_count(struct trans *, char *);
153 2c251c14 2020-01-15 tracey
154 2c251c14 2020-01-15 tracey static struct gw_dir *gw_init_gw_dir(char *);
155 2c251c14 2020-01-15 tracey
156 2c251c14 2020-01-15 tracey static char *gw_get_repo_description(struct trans *,
157 2c251c14 2020-01-15 tracey char *);
158 2c251c14 2020-01-15 tracey static char *gw_get_repo_owner(struct trans *,
159 2c251c14 2020-01-15 tracey char *);
160 474370cb 2020-01-15 tracey static char *gw_get_time_str(time_t, int);
161 2c251c14 2020-01-15 tracey static char *gw_get_repo_age(struct trans *,
162 387a29ba 2020-01-15 tracey char *, char *, int);
163 4ceb8155 2020-01-15 tracey static char *gw_get_repo_log(struct trans *, const char *,
164 4ceb8155 2020-01-15 tracey char *, int, int);
165 87f9ebf5 2020-01-15 tracey static char *gw_get_repo_tags(struct trans *, int, int);
166 8d4d2453 2020-01-15 tracey static char *gw_get_repo_heads(struct trans *);
167 2c251c14 2020-01-15 tracey static char *gw_get_clone_url(struct trans *, char *);
168 2c251c14 2020-01-15 tracey static char *gw_get_got_link(struct trans *);
169 2c251c14 2020-01-15 tracey static char *gw_get_site_link(struct trans *);
170 2c251c14 2020-01-15 tracey static char *gw_html_escape(const char *);
171 2c251c14 2020-01-15 tracey
172 2c251c14 2020-01-15 tracey static void gw_display_open(struct trans *, enum khttp,
173 2c251c14 2020-01-15 tracey enum kmime);
174 2c251c14 2020-01-15 tracey static void gw_display_index(struct trans *,
175 2c251c14 2020-01-15 tracey const struct got_error *);
176 2c251c14 2020-01-15 tracey
177 2c251c14 2020-01-15 tracey static int gw_template(size_t, void *);
178 2c251c14 2020-01-15 tracey
179 2c251c14 2020-01-15 tracey static const struct got_error* apply_unveil(const char *, const char *);
180 87f9ebf5 2020-01-15 tracey static const struct got_error* cmp_tags(void *, int *,
181 87f9ebf5 2020-01-15 tracey struct got_reference *,
182 87f9ebf5 2020-01-15 tracey struct got_reference *);
183 2c251c14 2020-01-15 tracey static const struct got_error* gw_load_got_paths(struct trans *);
184 2c251c14 2020-01-15 tracey static const struct got_error* gw_load_got_path(struct trans *,
185 2c251c14 2020-01-15 tracey struct gw_dir *);
186 2c251c14 2020-01-15 tracey static const struct got_error* gw_parse_querystring(struct trans *);
187 474370cb 2020-01-15 tracey static const struct got_error* match_logmsg(int *, struct got_object_id *,
188 474370cb 2020-01-15 tracey struct got_commit_object *, regex_t *);
189 2c251c14 2020-01-15 tracey
190 2c251c14 2020-01-15 tracey static const struct got_error* gw_blame(struct trans *);
191 2c251c14 2020-01-15 tracey static const struct got_error* gw_blob(struct trans *);
192 8087c3c5 2020-01-15 tracey static const struct got_error* gw_blobdiff(struct trans *);
193 2c251c14 2020-01-15 tracey static const struct got_error* gw_commit(struct trans *);
194 8087c3c5 2020-01-15 tracey static const struct got_error* gw_commitdiff(struct trans *);
195 2c251c14 2020-01-15 tracey static const struct got_error* gw_history(struct trans *);
196 2c251c14 2020-01-15 tracey static const struct got_error* gw_index(struct trans *);
197 2c251c14 2020-01-15 tracey static const struct got_error* gw_log(struct trans *);
198 2c251c14 2020-01-15 tracey static const struct got_error* gw_raw(struct trans *);
199 4ceb8155 2020-01-15 tracey static const struct got_error* gw_logbriefs(struct trans *);
200 2c251c14 2020-01-15 tracey static const struct got_error* gw_snapshot(struct trans *);
201 387a29ba 2020-01-15 tracey static const struct got_error* gw_summary(struct trans *);
202 b772de24 2020-01-15 tracey static const struct got_error* gw_tag(struct trans *);
203 2c251c14 2020-01-15 tracey static const struct got_error* gw_tree(struct trans *);
204 2c251c14 2020-01-15 tracey
205 2c251c14 2020-01-15 tracey struct gw_query_action {
206 2c251c14 2020-01-15 tracey unsigned int func_id;
207 2c251c14 2020-01-15 tracey const char *func_name;
208 2c251c14 2020-01-15 tracey const struct got_error *(*func_main)(struct trans *);
209 2c251c14 2020-01-15 tracey char *template;
210 2c251c14 2020-01-15 tracey };
211 2c251c14 2020-01-15 tracey
212 2c251c14 2020-01-15 tracey enum gw_query_actions {
213 2c251c14 2020-01-15 tracey GW_BLAME,
214 2c251c14 2020-01-15 tracey GW_BLOB,
215 2c251c14 2020-01-15 tracey GW_BLOBDIFF,
216 2c251c14 2020-01-15 tracey GW_COMMIT,
217 2c251c14 2020-01-15 tracey GW_COMMITDIFF,
218 2c251c14 2020-01-15 tracey GW_ERR,
219 2c251c14 2020-01-15 tracey GW_HISTORY,
220 2c251c14 2020-01-15 tracey GW_INDEX,
221 2c251c14 2020-01-15 tracey GW_LOG,
222 2c251c14 2020-01-15 tracey GW_RAW,
223 4ceb8155 2020-01-15 tracey GW_LOGBRIEFS,
224 2c251c14 2020-01-15 tracey GW_SNAPSHOT,
225 2c251c14 2020-01-15 tracey GW_SUMMARY,
226 b772de24 2020-01-15 tracey GW_TAG,
227 b772de24 2020-01-15 tracey GW_TREE,
228 2c251c14 2020-01-15 tracey };
229 2c251c14 2020-01-15 tracey
230 2c251c14 2020-01-15 tracey static struct gw_query_action gw_query_funcs[] = {
231 2c251c14 2020-01-15 tracey { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
232 2c251c14 2020-01-15 tracey { GW_BLOB, "blob", gw_blob, "gw_tmpl/index.tmpl" },
233 8087c3c5 2020-01-15 tracey { GW_BLOBDIFF, "blobdiff", gw_blobdiff, "gw_tmpl/index.tmpl" },
234 2c251c14 2020-01-15 tracey { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
235 8087c3c5 2020-01-15 tracey { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
236 387a29ba 2020-01-15 tracey { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
237 2c251c14 2020-01-15 tracey { GW_HISTORY, "history", gw_history, "gw_tmpl/index.tmpl" },
238 2c251c14 2020-01-15 tracey { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
239 2c251c14 2020-01-15 tracey { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
240 2c251c14 2020-01-15 tracey { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
241 4ceb8155 2020-01-15 tracey { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
242 2c251c14 2020-01-15 tracey { GW_SNAPSHOT, "snapshot", gw_snapshot, "gw_tmpl/index.tmpl" },
243 46b9c89b 2020-01-15 tracey { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
244 b772de24 2020-01-15 tracey { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
245 2c251c14 2020-01-15 tracey { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
246 2c251c14 2020-01-15 tracey };
247 2c251c14 2020-01-15 tracey
248 2c251c14 2020-01-15 tracey static const struct got_error *
249 2c251c14 2020-01-15 tracey apply_unveil(const char *repo_path, const char *repo_file)
250 2c251c14 2020-01-15 tracey {
251 2c251c14 2020-01-15 tracey const struct got_error *err;
252 2c251c14 2020-01-15 tracey
253 2c251c14 2020-01-15 tracey if (repo_path && repo_file) {
254 2c251c14 2020-01-15 tracey char *full_path;
255 2c251c14 2020-01-15 tracey if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
256 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf unveil");
257 2c251c14 2020-01-15 tracey if (unveil(full_path, "r") != 0)
258 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", full_path);
259 2c251c14 2020-01-15 tracey }
260 2c251c14 2020-01-15 tracey
261 2c251c14 2020-01-15 tracey if (repo_path && unveil(repo_path, "r") != 0)
262 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", repo_path);
263 2c251c14 2020-01-15 tracey
264 2c251c14 2020-01-15 tracey if (unveil("/tmp", "rwc") != 0)
265 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", "/tmp");
266 2c251c14 2020-01-15 tracey
267 2c251c14 2020-01-15 tracey err = got_privsep_unveil_exec_helpers();
268 2c251c14 2020-01-15 tracey if (err != NULL)
269 2c251c14 2020-01-15 tracey return err;
270 2c251c14 2020-01-15 tracey
271 2c251c14 2020-01-15 tracey if (unveil(NULL, NULL) != 0)
272 2c251c14 2020-01-15 tracey return got_error_from_errno("unveil");
273 2c251c14 2020-01-15 tracey
274 2c251c14 2020-01-15 tracey return NULL;
275 87f9ebf5 2020-01-15 tracey }
276 87f9ebf5 2020-01-15 tracey
277 87f9ebf5 2020-01-15 tracey static const struct got_error *
278 87f9ebf5 2020-01-15 tracey cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
279 87f9ebf5 2020-01-15 tracey struct got_reference *ref2)
280 87f9ebf5 2020-01-15 tracey {
281 87f9ebf5 2020-01-15 tracey const struct got_error *err = NULL;
282 87f9ebf5 2020-01-15 tracey struct got_repository *repo = arg;
283 87f9ebf5 2020-01-15 tracey struct got_object_id *id1, *id2 = NULL;
284 87f9ebf5 2020-01-15 tracey struct got_tag_object *tag1 = NULL, *tag2 = NULL;
285 87f9ebf5 2020-01-15 tracey time_t time1, time2;
286 87f9ebf5 2020-01-15 tracey
287 87f9ebf5 2020-01-15 tracey *cmp = 0;
288 87f9ebf5 2020-01-15 tracey
289 87f9ebf5 2020-01-15 tracey err = got_ref_resolve(&id1, repo, ref1);
290 87f9ebf5 2020-01-15 tracey if (err)
291 87f9ebf5 2020-01-15 tracey return err;
292 87f9ebf5 2020-01-15 tracey err = got_object_open_as_tag(&tag1, repo, id1);
293 87f9ebf5 2020-01-15 tracey if (err)
294 87f9ebf5 2020-01-15 tracey goto done;
295 87f9ebf5 2020-01-15 tracey
296 87f9ebf5 2020-01-15 tracey err = got_ref_resolve(&id2, repo, ref2);
297 87f9ebf5 2020-01-15 tracey if (err)
298 87f9ebf5 2020-01-15 tracey goto done;
299 87f9ebf5 2020-01-15 tracey err = got_object_open_as_tag(&tag2, repo, id2);
300 87f9ebf5 2020-01-15 tracey if (err)
301 87f9ebf5 2020-01-15 tracey goto done;
302 87f9ebf5 2020-01-15 tracey
303 87f9ebf5 2020-01-15 tracey time1 = got_object_tag_get_tagger_time(tag1);
304 87f9ebf5 2020-01-15 tracey time2 = got_object_tag_get_tagger_time(tag2);
305 87f9ebf5 2020-01-15 tracey
306 87f9ebf5 2020-01-15 tracey /* Put latest tags first. */
307 87f9ebf5 2020-01-15 tracey if (time1 < time2)
308 87f9ebf5 2020-01-15 tracey *cmp = 1;
309 87f9ebf5 2020-01-15 tracey else if (time1 > time2)
310 87f9ebf5 2020-01-15 tracey *cmp = -1;
311 87f9ebf5 2020-01-15 tracey else
312 87f9ebf5 2020-01-15 tracey err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
313 87f9ebf5 2020-01-15 tracey done:
314 87f9ebf5 2020-01-15 tracey free(id1);
315 87f9ebf5 2020-01-15 tracey free(id2);
316 87f9ebf5 2020-01-15 tracey if (tag1)
317 87f9ebf5 2020-01-15 tracey got_object_tag_close(tag1);
318 87f9ebf5 2020-01-15 tracey if (tag2)
319 87f9ebf5 2020-01-15 tracey got_object_tag_close(tag2);
320 87f9ebf5 2020-01-15 tracey return err;
321 2c251c14 2020-01-15 tracey }
322 65b95fb2 2020-01-15 tracey
323 65b95fb2 2020-01-15 tracey int
324 65b95fb2 2020-01-15 tracey gw_get_repo_log_count(struct trans *gw_trans, char *start_commit)
325 65b95fb2 2020-01-15 tracey {
326 65b95fb2 2020-01-15 tracey const struct got_error *error;
327 65b95fb2 2020-01-15 tracey struct got_repository *repo = NULL;
328 65b95fb2 2020-01-15 tracey struct got_reflist_head refs;
329 65b95fb2 2020-01-15 tracey struct got_commit_object *commit = NULL;
330 65b95fb2 2020-01-15 tracey struct got_object_id *id = NULL;
331 65b95fb2 2020-01-15 tracey struct got_commit_graph *graph = NULL;
332 65b95fb2 2020-01-15 tracey char *in_repo_path = NULL, *path = NULL;
333 65b95fb2 2020-01-15 tracey int log_count = 0;
334 65b95fb2 2020-01-15 tracey
335 65b95fb2 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
336 65b95fb2 2020-01-15 tracey if (error != NULL)
337 65b95fb2 2020-01-15 tracey return 0;
338 65b95fb2 2020-01-15 tracey
339 65b95fb2 2020-01-15 tracey SIMPLEQ_INIT(&refs);
340 65b95fb2 2020-01-15 tracey
341 65b95fb2 2020-01-15 tracey if (start_commit == NULL) {
342 65b95fb2 2020-01-15 tracey struct got_reference *head_ref;
343 65b95fb2 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
344 65b95fb2 2020-01-15 tracey if (error != NULL)
345 65b95fb2 2020-01-15 tracey goto done;
346 65b95fb2 2020-01-15 tracey
347 65b95fb2 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
348 65b95fb2 2020-01-15 tracey got_ref_close(head_ref);
349 65b95fb2 2020-01-15 tracey if (error != NULL)
350 65b95fb2 2020-01-15 tracey goto done;
351 65b95fb2 2020-01-15 tracey
352 65b95fb2 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
353 65b95fb2 2020-01-15 tracey } else {
354 65b95fb2 2020-01-15 tracey struct got_reference *ref;
355 65b95fb2 2020-01-15 tracey error = got_ref_open(&ref, repo, start_commit, 0);
356 65b95fb2 2020-01-15 tracey if (error == NULL) {
357 65b95fb2 2020-01-15 tracey int obj_type;
358 65b95fb2 2020-01-15 tracey error = got_ref_resolve(&id, repo, ref);
359 65b95fb2 2020-01-15 tracey got_ref_close(ref);
360 65b95fb2 2020-01-15 tracey if (error != NULL)
361 65b95fb2 2020-01-15 tracey goto done;
362 65b95fb2 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, id);
363 65b95fb2 2020-01-15 tracey if (error != NULL)
364 65b95fb2 2020-01-15 tracey goto done;
365 65b95fb2 2020-01-15 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
366 65b95fb2 2020-01-15 tracey struct got_tag_object *tag;
367 65b95fb2 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
368 65b95fb2 2020-01-15 tracey if (error != NULL)
369 65b95fb2 2020-01-15 tracey goto done;
370 65b95fb2 2020-01-15 tracey if (got_object_tag_get_object_type(tag) !=
371 65b95fb2 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT) {
372 65b95fb2 2020-01-15 tracey got_object_tag_close(tag);
373 65b95fb2 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
374 65b95fb2 2020-01-15 tracey goto done;
375 65b95fb2 2020-01-15 tracey }
376 65b95fb2 2020-01-15 tracey free(id);
377 65b95fb2 2020-01-15 tracey id = got_object_id_dup(
378 65b95fb2 2020-01-15 tracey got_object_tag_get_object_id(tag));
379 65b95fb2 2020-01-15 tracey if (id == NULL)
380 65b95fb2 2020-01-15 tracey error = got_error_from_errno(
381 65b95fb2 2020-01-15 tracey "got_object_id_dup");
382 65b95fb2 2020-01-15 tracey got_object_tag_close(tag);
383 65b95fb2 2020-01-15 tracey if (error)
384 65b95fb2 2020-01-15 tracey goto done;
385 65b95fb2 2020-01-15 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
386 65b95fb2 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
387 65b95fb2 2020-01-15 tracey goto done;
388 65b95fb2 2020-01-15 tracey }
389 65b95fb2 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
390 65b95fb2 2020-01-15 tracey if (error != NULL)
391 65b95fb2 2020-01-15 tracey goto done;
392 65b95fb2 2020-01-15 tracey }
393 65b95fb2 2020-01-15 tracey if (commit == NULL) {
394 65b95fb2 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id,
395 65b95fb2 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
396 65b95fb2 2020-01-15 tracey if (error != NULL)
397 65b95fb2 2020-01-15 tracey goto done;
398 65b95fb2 2020-01-15 tracey }
399 65b95fb2 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id,
400 65b95fb2 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
401 65b95fb2 2020-01-15 tracey if (error != NULL)
402 65b95fb2 2020-01-15 tracey goto done;
403 65b95fb2 2020-01-15 tracey }
404 2c251c14 2020-01-15 tracey
405 65b95fb2 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
406 65b95fb2 2020-01-15 tracey if (error != NULL)
407 65b95fb2 2020-01-15 tracey goto done;
408 65b95fb2 2020-01-15 tracey
409 65b95fb2 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
410 65b95fb2 2020-01-15 tracey if (error != NULL)
411 65b95fb2 2020-01-15 tracey goto done;
412 65b95fb2 2020-01-15 tracey
413 65b95fb2 2020-01-15 tracey if (in_repo_path) {
414 65b95fb2 2020-01-15 tracey free(path);
415 65b95fb2 2020-01-15 tracey path = in_repo_path;
416 65b95fb2 2020-01-15 tracey }
417 65b95fb2 2020-01-15 tracey
418 65b95fb2 2020-01-15 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
419 65b95fb2 2020-01-15 tracey if (error)
420 65b95fb2 2020-01-15 tracey goto done;
421 65b95fb2 2020-01-15 tracey
422 65b95fb2 2020-01-15 tracey error = got_commit_graph_open(&graph, path, 0);
423 65b95fb2 2020-01-15 tracey if (error)
424 65b95fb2 2020-01-15 tracey goto done;
425 65b95fb2 2020-01-15 tracey
426 65b95fb2 2020-01-15 tracey error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
427 65b95fb2 2020-01-15 tracey if (error)
428 65b95fb2 2020-01-15 tracey goto done;
429 65b95fb2 2020-01-15 tracey
430 65b95fb2 2020-01-15 tracey for (;;) {
431 65b95fb2 2020-01-15 tracey error = got_commit_graph_iter_next(&id, graph, repo, NULL,
432 65b95fb2 2020-01-15 tracey NULL);
433 65b95fb2 2020-01-15 tracey if (error) {
434 65b95fb2 2020-01-15 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
435 65b95fb2 2020-01-15 tracey error = NULL;
436 65b95fb2 2020-01-15 tracey break;
437 65b95fb2 2020-01-15 tracey }
438 65b95fb2 2020-01-15 tracey if (id == NULL)
439 65b95fb2 2020-01-15 tracey break;
440 65b95fb2 2020-01-15 tracey
441 65b95fb2 2020-01-15 tracey if (error)
442 65b95fb2 2020-01-15 tracey break;
443 65b95fb2 2020-01-15 tracey log_count++;
444 65b95fb2 2020-01-15 tracey }
445 65b95fb2 2020-01-15 tracey done:
446 65b95fb2 2020-01-15 tracey if (graph)
447 65b95fb2 2020-01-15 tracey got_commit_graph_close(graph);
448 65b95fb2 2020-01-15 tracey if (repo) {
449 65b95fb2 2020-01-15 tracey error = got_repo_close(repo);
450 65b95fb2 2020-01-15 tracey if (error != NULL)
451 65b95fb2 2020-01-15 tracey return 0;
452 65b95fb2 2020-01-15 tracey }
453 65b95fb2 2020-01-15 tracey if (error) {
454 65b95fb2 2020-01-15 tracey khttp_puts(gw_trans->gw_req, "Error: ");
455 65b95fb2 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
456 65b95fb2 2020-01-15 tracey return 0;
457 65b95fb2 2020-01-15 tracey } else
458 65b95fb2 2020-01-15 tracey return log_count;
459 65b95fb2 2020-01-15 tracey }
460 65b95fb2 2020-01-15 tracey
461 2c251c14 2020-01-15 tracey static const struct got_error *
462 2c251c14 2020-01-15 tracey gw_blame(struct trans *gw_trans)
463 2c251c14 2020-01-15 tracey {
464 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
465 2c251c14 2020-01-15 tracey
466 2c251c14 2020-01-15 tracey return error;
467 2c251c14 2020-01-15 tracey }
468 2c251c14 2020-01-15 tracey
469 2c251c14 2020-01-15 tracey static const struct got_error *
470 2c251c14 2020-01-15 tracey gw_blob(struct trans *gw_trans)
471 2c251c14 2020-01-15 tracey {
472 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
473 2c251c14 2020-01-15 tracey
474 2c251c14 2020-01-15 tracey return error;
475 2c251c14 2020-01-15 tracey }
476 2c251c14 2020-01-15 tracey
477 2c251c14 2020-01-15 tracey static const struct got_error *
478 8087c3c5 2020-01-15 tracey gw_blobdiff(struct trans *gw_trans)
479 2c251c14 2020-01-15 tracey {
480 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
481 2c251c14 2020-01-15 tracey
482 2c251c14 2020-01-15 tracey return error;
483 2c251c14 2020-01-15 tracey }
484 2c251c14 2020-01-15 tracey
485 2c251c14 2020-01-15 tracey static const struct got_error *
486 2c251c14 2020-01-15 tracey gw_commit(struct trans *gw_trans)
487 2c251c14 2020-01-15 tracey {
488 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
489 8087c3c5 2020-01-15 tracey char *log, *log_html;
490 2c251c14 2020-01-15 tracey
491 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
492 8087c3c5 2020-01-15 tracey if (error)
493 8087c3c5 2020-01-15 tracey return error;
494 8087c3c5 2020-01-15 tracey
495 8087c3c5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
496 8087c3c5 2020-01-15 tracey
497 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
498 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, log_commit, log)) == -1)
499 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
500 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
501 8087c3c5 2020-01-15 tracey free(log_html);
502 8087c3c5 2020-01-15 tracey free(log);
503 8087c3c5 2020-01-15 tracey }
504 2c251c14 2020-01-15 tracey return error;
505 2c251c14 2020-01-15 tracey }
506 2c251c14 2020-01-15 tracey
507 2c251c14 2020-01-15 tracey static const struct got_error *
508 8087c3c5 2020-01-15 tracey gw_commitdiff(struct trans *gw_trans)
509 2204c934 2020-01-15 tracey {
510 2204c934 2020-01-15 tracey const struct got_error *error = NULL;
511 87f9ebf5 2020-01-15 tracey char *log, *log_html;
512 87f9ebf5 2020-01-15 tracey
513 87f9ebf5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
514 87f9ebf5 2020-01-15 tracey if (error)
515 87f9ebf5 2020-01-15 tracey return error;
516 2204c934 2020-01-15 tracey
517 87f9ebf5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
518 87f9ebf5 2020-01-15 tracey
519 87f9ebf5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
520 87f9ebf5 2020-01-15 tracey if ((asprintf(&log_html, log_diff, log)) == -1)
521 87f9ebf5 2020-01-15 tracey return got_error_from_errno("asprintf");
522 87f9ebf5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
523 87f9ebf5 2020-01-15 tracey free(log_html);
524 87f9ebf5 2020-01-15 tracey free(log);
525 87f9ebf5 2020-01-15 tracey }
526 2204c934 2020-01-15 tracey return error;
527 2204c934 2020-01-15 tracey }
528 2204c934 2020-01-15 tracey
529 2204c934 2020-01-15 tracey static const struct got_error *
530 2c251c14 2020-01-15 tracey gw_history(struct trans *gw_trans)
531 2c251c14 2020-01-15 tracey {
532 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
533 2c251c14 2020-01-15 tracey
534 2c251c14 2020-01-15 tracey return error;
535 2c251c14 2020-01-15 tracey }
536 2c251c14 2020-01-15 tracey
537 2c251c14 2020-01-15 tracey static const struct got_error *
538 2c251c14 2020-01-15 tracey gw_index(struct trans *gw_trans)
539 2c251c14 2020-01-15 tracey {
540 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
541 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
542 2c251c14 2020-01-15 tracey char *html, *navs, *next, *prev;
543 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
544 2c251c14 2020-01-15 tracey
545 46b9c89b 2020-01-15 tracey error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
546 46b9c89b 2020-01-15 tracey if (error)
547 46b9c89b 2020-01-15 tracey return error;
548 46b9c89b 2020-01-15 tracey
549 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
550 46b9c89b 2020-01-15 tracey if (error)
551 2c251c14 2020-01-15 tracey return error;
552 2c251c14 2020-01-15 tracey
553 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, index_projects_header);
554 2c251c14 2020-01-15 tracey
555 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
556 2c251c14 2020-01-15 tracey dir_c++;
557 2c251c14 2020-01-15 tracey
558 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
559 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
560 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
561 2c251c14 2020-01-15 tracey prev_disp++;
562 2c251c14 2020-01-15 tracey continue;
563 2c251c14 2020-01-15 tracey }
564 2c251c14 2020-01-15 tracey
565 2c251c14 2020-01-15 tracey prev_disp++;
566 46b9c89b 2020-01-15 tracey if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
567 46b9c89b 2020-01-15 tracey gw_dir->name, gw_dir->name)) == -1)
568 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
569 2c251c14 2020-01-15 tracey
570 46b9c89b 2020-01-15 tracey if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
571 46b9c89b 2020-01-15 tracey gw_dir->description, gw_dir->owner, gw_dir->age,
572 46b9c89b 2020-01-15 tracey navs)) == -1)
573 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
574 2c251c14 2020-01-15 tracey
575 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, html);
576 2c251c14 2020-01-15 tracey
577 2c251c14 2020-01-15 tracey free(navs);
578 2c251c14 2020-01-15 tracey free(html);
579 2c251c14 2020-01-15 tracey
580 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
581 2c251c14 2020-01-15 tracey continue;
582 2c251c14 2020-01-15 tracey
583 2c251c14 2020-01-15 tracey if (next_disp == gw_trans->gw_conf->got_max_repos_display)
584 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, np_wrapper_start);
585 2c251c14 2020-01-15 tracey else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
586 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
587 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
588 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total))
589 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, np_wrapper_start);
590 2c251c14 2020-01-15 tracey
591 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
592 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
593 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
594 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
595 2c251c14 2020-01-15 tracey if ((asprintf(&prev, nav_prev,
596 2c251c14 2020-01-15 tracey gw_trans->page - 1)) == -1)
597 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
598 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, prev);
599 2c251c14 2020-01-15 tracey free(prev);
600 2c251c14 2020-01-15 tracey }
601 2c251c14 2020-01-15 tracey
602 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
603 2c251c14 2020-01-15 tracey
604 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
605 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
606 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
607 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
608 2c251c14 2020-01-15 tracey if ((asprintf(&next, nav_next,
609 2c251c14 2020-01-15 tracey gw_trans->page + 1)) == -1)
610 2c251c14 2020-01-15 tracey return got_error_from_errno("calloc");
611 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, next);
612 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
613 2c251c14 2020-01-15 tracey free(next);
614 2c251c14 2020-01-15 tracey next_disp = 0;
615 2c251c14 2020-01-15 tracey break;
616 2c251c14 2020-01-15 tracey }
617 2c251c14 2020-01-15 tracey
618 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
619 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
620 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
621 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total))
622 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
623 2c251c14 2020-01-15 tracey
624 2c251c14 2020-01-15 tracey next_disp++;
625 2c251c14 2020-01-15 tracey }
626 2c251c14 2020-01-15 tracey return error;
627 2c251c14 2020-01-15 tracey }
628 2c251c14 2020-01-15 tracey
629 2c251c14 2020-01-15 tracey static const struct got_error *
630 2c251c14 2020-01-15 tracey gw_log(struct trans *gw_trans)
631 2c251c14 2020-01-15 tracey {
632 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
633 4ceb8155 2020-01-15 tracey char *log, *log_html;
634 8087c3c5 2020-01-15 tracey
635 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
636 8087c3c5 2020-01-15 tracey if (error)
637 8087c3c5 2020-01-15 tracey return error;
638 2c251c14 2020-01-15 tracey
639 87f9ebf5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
640 8087c3c5 2020-01-15 tracey gw_trans->gw_conf->got_max_commits_display, LOGFULL);
641 4ceb8155 2020-01-15 tracey
642 4ceb8155 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
643 4ceb8155 2020-01-15 tracey if ((asprintf(&log_html, logs, log)) == -1)
644 4ceb8155 2020-01-15 tracey return got_error_from_errno("asprintf");
645 4ceb8155 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
646 4ceb8155 2020-01-15 tracey free(log_html);
647 4ceb8155 2020-01-15 tracey free(log);
648 4ceb8155 2020-01-15 tracey }
649 2c251c14 2020-01-15 tracey return error;
650 2c251c14 2020-01-15 tracey }
651 2c251c14 2020-01-15 tracey
652 2c251c14 2020-01-15 tracey static const struct got_error *
653 2c251c14 2020-01-15 tracey gw_raw(struct trans *gw_trans)
654 2c251c14 2020-01-15 tracey {
655 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
656 2c251c14 2020-01-15 tracey
657 2c251c14 2020-01-15 tracey return error;
658 2c251c14 2020-01-15 tracey }
659 2c251c14 2020-01-15 tracey
660 2c251c14 2020-01-15 tracey static const struct got_error *
661 4ceb8155 2020-01-15 tracey gw_logbriefs(struct trans *gw_trans)
662 2c251c14 2020-01-15 tracey {
663 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
664 8087c3c5 2020-01-15 tracey char *log, *log_html;
665 17a96b9f 2020-01-15 tracey
666 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
667 8087c3c5 2020-01-15 tracey if (error)
668 8087c3c5 2020-01-15 tracey return error;
669 9d84e7dd 2020-01-15 tracey
670 87f9ebf5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
671 8087c3c5 2020-01-15 tracey gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
672 8087c3c5 2020-01-15 tracey
673 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
674 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, summary_logbriefs,
675 8087c3c5 2020-01-15 tracey log)) == -1)
676 9d84e7dd 2020-01-15 tracey return got_error_from_errno("asprintf");
677 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
678 8087c3c5 2020-01-15 tracey free(log_html);
679 8087c3c5 2020-01-15 tracey free(log);
680 9d84e7dd 2020-01-15 tracey }
681 2c251c14 2020-01-15 tracey return error;
682 2c251c14 2020-01-15 tracey }
683 2c251c14 2020-01-15 tracey
684 2c251c14 2020-01-15 tracey static const struct got_error *
685 2c251c14 2020-01-15 tracey gw_snapshot(struct trans *gw_trans)
686 2c251c14 2020-01-15 tracey {
687 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
688 2c251c14 2020-01-15 tracey
689 2c251c14 2020-01-15 tracey return error;
690 2c251c14 2020-01-15 tracey }
691 2c251c14 2020-01-15 tracey
692 2c251c14 2020-01-15 tracey static const struct got_error *
693 387a29ba 2020-01-15 tracey gw_summary(struct trans *gw_trans)
694 387a29ba 2020-01-15 tracey {
695 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
696 46b9c89b 2020-01-15 tracey char *description_html, *repo_owner_html, *repo_age_html,
697 8087c3c5 2020-01-15 tracey *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
698 8087c3c5 2020-01-15 tracey *heads_html, *age;
699 387a29ba 2020-01-15 tracey
700 46b9c89b 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
701 46b9c89b 2020-01-15 tracey if (error)
702 46b9c89b 2020-01-15 tracey return error;
703 46b9c89b 2020-01-15 tracey
704 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, summary_wrapper);
705 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description) {
706 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->description != NULL &&
707 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
708 46b9c89b 2020-01-15 tracey if ((asprintf(&description_html, description,
709 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->description)) == -1)
710 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
711 46b9c89b 2020-01-15 tracey
712 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, description_html);
713 46b9c89b 2020-01-15 tracey free(description_html);
714 46b9c89b 2020-01-15 tracey }
715 46b9c89b 2020-01-15 tracey }
716 46b9c89b 2020-01-15 tracey
717 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
718 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->owner != NULL &&
719 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
720 46b9c89b 2020-01-15 tracey if ((asprintf(&repo_owner_html, repo_owner,
721 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->owner)) == -1)
722 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
723 46b9c89b 2020-01-15 tracey
724 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, repo_owner_html);
725 46b9c89b 2020-01-15 tracey free(repo_owner_html);
726 46b9c89b 2020-01-15 tracey }
727 46b9c89b 2020-01-15 tracey }
728 46b9c89b 2020-01-15 tracey
729 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
730 c6b62706 2020-01-15 tracey age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
731 c6b62706 2020-01-15 tracey "refs/heads", TM_LONG);
732 c6b62706 2020-01-15 tracey if (age != NULL && (strcmp(age, "") != 0)) {
733 c6b62706 2020-01-15 tracey if ((asprintf(&repo_age_html, last_change, age)) == -1)
734 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
735 46b9c89b 2020-01-15 tracey
736 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, repo_age_html);
737 46b9c89b 2020-01-15 tracey free(repo_age_html);
738 c6b62706 2020-01-15 tracey free(age);
739 46b9c89b 2020-01-15 tracey }
740 46b9c89b 2020-01-15 tracey }
741 46b9c89b 2020-01-15 tracey
742 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl) {
743 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->url != NULL &&
744 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
745 46b9c89b 2020-01-15 tracey if ((asprintf(&cloneurl_html, cloneurl,
746 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->url)) == -1)
747 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
748 46b9c89b 2020-01-15 tracey
749 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, cloneurl_html);
750 46b9c89b 2020-01-15 tracey free(cloneurl_html);
751 46b9c89b 2020-01-15 tracey }
752 46b9c89b 2020-01-15 tracey }
753 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
754 46b9c89b 2020-01-15 tracey
755 8087c3c5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
756 87f9ebf5 2020-01-15 tracey tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
757 8d4d2453 2020-01-15 tracey heads = gw_get_repo_heads(gw_trans);
758 387a29ba 2020-01-15 tracey
759 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
760 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, summary_logbriefs,
761 8087c3c5 2020-01-15 tracey log)) == -1)
762 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
763 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
764 8087c3c5 2020-01-15 tracey free(log_html);
765 8087c3c5 2020-01-15 tracey free(log);
766 8d4d2453 2020-01-15 tracey }
767 8d4d2453 2020-01-15 tracey
768 8d4d2453 2020-01-15 tracey if (tags != NULL && strcmp(tags, "") != 0) {
769 8d4d2453 2020-01-15 tracey if ((asprintf(&tags_html, summary_tags,
770 8d4d2453 2020-01-15 tracey tags)) == -1)
771 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
772 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, tags_html);
773 8d4d2453 2020-01-15 tracey free(tags_html);
774 8d4d2453 2020-01-15 tracey free(tags);
775 8d4d2453 2020-01-15 tracey }
776 8d4d2453 2020-01-15 tracey
777 8d4d2453 2020-01-15 tracey if (heads != NULL && strcmp(heads, "") != 0) {
778 8d4d2453 2020-01-15 tracey if ((asprintf(&heads_html, summary_heads,
779 8d4d2453 2020-01-15 tracey heads)) == -1)
780 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
781 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, heads_html);
782 8d4d2453 2020-01-15 tracey free(heads_html);
783 8d4d2453 2020-01-15 tracey free(heads);
784 8d4d2453 2020-01-15 tracey }
785 2204c934 2020-01-15 tracey return error;
786 2204c934 2020-01-15 tracey }
787 2204c934 2020-01-15 tracey
788 2204c934 2020-01-15 tracey static const struct got_error *
789 b772de24 2020-01-15 tracey gw_tag(struct trans *gw_trans)
790 b772de24 2020-01-15 tracey {
791 b772de24 2020-01-15 tracey const struct got_error *error = NULL;
792 b772de24 2020-01-15 tracey char *log, *log_html;
793 b772de24 2020-01-15 tracey
794 b772de24 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
795 b772de24 2020-01-15 tracey if (error)
796 b772de24 2020-01-15 tracey return error;
797 b772de24 2020-01-15 tracey
798 b772de24 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
799 b772de24 2020-01-15 tracey
800 b772de24 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
801 9460dac0 2020-01-15 tracey if ((asprintf(&log_html, log_tag, log)) == -1)
802 b772de24 2020-01-15 tracey return got_error_from_errno("asprintf");
803 b772de24 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
804 b772de24 2020-01-15 tracey free(log_html);
805 b772de24 2020-01-15 tracey free(log);
806 b772de24 2020-01-15 tracey }
807 b772de24 2020-01-15 tracey return error;
808 b772de24 2020-01-15 tracey }
809 b772de24 2020-01-15 tracey
810 b772de24 2020-01-15 tracey static const struct got_error *
811 2c251c14 2020-01-15 tracey gw_tree(struct trans *gw_trans)
812 2c251c14 2020-01-15 tracey {
813 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
814 8087c3c5 2020-01-15 tracey char *log, *log_html;
815 8087c3c5 2020-01-15 tracey
816 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
817 8087c3c5 2020-01-15 tracey if (error)
818 8087c3c5 2020-01-15 tracey return error;
819 8087c3c5 2020-01-15 tracey
820 8087c3c5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
821 2c251c14 2020-01-15 tracey
822 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
823 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, log_tree, log)) == -1)
824 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
825 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
826 8087c3c5 2020-01-15 tracey free(log_html);
827 8087c3c5 2020-01-15 tracey free(log);
828 8087c3c5 2020-01-15 tracey }
829 2c251c14 2020-01-15 tracey return error;
830 2c251c14 2020-01-15 tracey }
831 2c251c14 2020-01-15 tracey
832 2c251c14 2020-01-15 tracey static const struct got_error *
833 2c251c14 2020-01-15 tracey gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
834 2c251c14 2020-01-15 tracey {
835 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
836 2c251c14 2020-01-15 tracey DIR *dt;
837 2c251c14 2020-01-15 tracey char *dir_test;
838 4ceb8155 2020-01-15 tracey int opened = 0;
839 2c251c14 2020-01-15 tracey
840 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
841 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
842 2c251c14 2020-01-15 tracey GOTWEB_GIT_DIR)) == -1)
843 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
844 2c251c14 2020-01-15 tracey
845 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
846 2c251c14 2020-01-15 tracey if (dt == NULL) {
847 2c251c14 2020-01-15 tracey free(dir_test);
848 2c251c14 2020-01-15 tracey } else {
849 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
850 4ceb8155 2020-01-15 tracey opened = 1;
851 2c251c14 2020-01-15 tracey goto done;
852 2c251c14 2020-01-15 tracey }
853 2c251c14 2020-01-15 tracey
854 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
855 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
856 2c251c14 2020-01-15 tracey GOTWEB_GOT_DIR)) == -1)
857 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
858 2c251c14 2020-01-15 tracey
859 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
860 2c251c14 2020-01-15 tracey if (dt == NULL)
861 2c251c14 2020-01-15 tracey free(dir_test);
862 2c251c14 2020-01-15 tracey else {
863 4ceb8155 2020-01-15 tracey opened = 1;
864 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
865 2c251c14 2020-01-15 tracey goto errored;
866 2c251c14 2020-01-15 tracey }
867 2c251c14 2020-01-15 tracey
868 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s",
869 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
870 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
871 2c251c14 2020-01-15 tracey
872 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
873 2c251c14 2020-01-15 tracey
874 2c251c14 2020-01-15 tracey done:
875 2c251c14 2020-01-15 tracey gw_dir->description = gw_get_repo_description(gw_trans,
876 2c251c14 2020-01-15 tracey gw_dir->path);
877 2c251c14 2020-01-15 tracey gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
878 387a29ba 2020-01-15 tracey gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
879 387a29ba 2020-01-15 tracey TM_DIFF);
880 2c251c14 2020-01-15 tracey gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
881 2c251c14 2020-01-15 tracey
882 2c251c14 2020-01-15 tracey errored:
883 2c251c14 2020-01-15 tracey free(dir_test);
884 2c251c14 2020-01-15 tracey if (opened)
885 2c251c14 2020-01-15 tracey closedir(dt);
886 2c251c14 2020-01-15 tracey return error;
887 2c251c14 2020-01-15 tracey }
888 2c251c14 2020-01-15 tracey
889 2c251c14 2020-01-15 tracey static const struct got_error *
890 2c251c14 2020-01-15 tracey gw_load_got_paths(struct trans *gw_trans)
891 2c251c14 2020-01-15 tracey {
892 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
893 2c251c14 2020-01-15 tracey DIR *d;
894 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
895 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
896 2c251c14 2020-01-15 tracey struct stat st;
897 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
898 2c251c14 2020-01-15 tracey
899 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
900 2c251c14 2020-01-15 tracey if (d == NULL) {
901 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
902 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
903 2c251c14 2020-01-15 tracey return error;
904 2c251c14 2020-01-15 tracey }
905 2c251c14 2020-01-15 tracey
906 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
907 2c251c14 2020-01-15 tracey alphasort);
908 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
909 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
910 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
911 2c251c14 2020-01-15 tracey return error;
912 2c251c14 2020-01-15 tracey }
913 2c251c14 2020-01-15 tracey
914 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
915 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
916 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
917 2c251c14 2020-01-15 tracey break; /* account for parent and self */
918 2c251c14 2020-01-15 tracey
919 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
920 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
921 2c251c14 2020-01-15 tracey continue;
922 2c251c14 2020-01-15 tracey
923 2c251c14 2020-01-15 tracey if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
924 2c251c14 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
925 2c251c14 2020-01-15 tracey
926 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
927 2c251c14 2020-01-15 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO)
928 2c251c14 2020-01-15 tracey continue;
929 2c251c14 2020-01-15 tracey else if (error)
930 2c251c14 2020-01-15 tracey return error;
931 2c251c14 2020-01-15 tracey
932 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
933 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
934 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
935 2c251c14 2020-01-15 tracey entry);
936 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
937 2c251c14 2020-01-15 tracey }
938 2c251c14 2020-01-15 tracey }
939 2c251c14 2020-01-15 tracey
940 2c251c14 2020-01-15 tracey closedir(d);
941 2c251c14 2020-01-15 tracey return error;
942 2c251c14 2020-01-15 tracey }
943 2c251c14 2020-01-15 tracey
944 2c251c14 2020-01-15 tracey static const struct got_error *
945 2c251c14 2020-01-15 tracey gw_parse_querystring(struct trans *gw_trans)
946 2c251c14 2020-01-15 tracey {
947 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
948 2c251c14 2020-01-15 tracey struct kpair *p;
949 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
950 2c251c14 2020-01-15 tracey unsigned int i;
951 2c251c14 2020-01-15 tracey
952 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
953 2c251c14 2020-01-15 tracey error = got_error_from_errno("bad parse");
954 2c251c14 2020-01-15 tracey return error;
955 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
956 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
957 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
958 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
959 2c251c14 2020-01-15 tracey
960 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_path, "%s/%s",
961 387a29ba 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
962 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
963 2c251c14 2020-01-15 tracey
964 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
965 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->commit, "%s",
966 2c251c14 2020-01-15 tracey p->parsed.s)) == -1)
967 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
968 2c251c14 2020-01-15 tracey
969 2c251c14 2020-01-15 tracey /* get action and set function */
970 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
971 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
972 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
973 2c251c14 2020-01-15 tracey if (action->func_name == NULL)
974 2c251c14 2020-01-15 tracey continue;
975 077f6c5a 2020-01-15 tracey
976 2c251c14 2020-01-15 tracey if (strcmp(action->func_name,
977 2c251c14 2020-01-15 tracey p->parsed.s) == 0) {
978 2c251c14 2020-01-15 tracey gw_trans->action = i;
979 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->action_name,
980 2c251c14 2020-01-15 tracey "%s", action->func_name)) == -1)
981 2c251c14 2020-01-15 tracey return
982 b772de24 2020-01-15 tracey got_error_from_errno(
983 2c251c14 2020-01-15 tracey "asprintf");
984 2c251c14 2020-01-15 tracey
985 2c251c14 2020-01-15 tracey break;
986 2c251c14 2020-01-15 tracey }
987 2c251c14 2020-01-15 tracey
988 2c251c14 2020-01-15 tracey action = NULL;
989 2c251c14 2020-01-15 tracey }
990 2c251c14 2020-01-15 tracey
991 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
992 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_file, "%s",
993 8087c3c5 2020-01-15 tracey p->parsed.s)) == -1)
994 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
995 8087c3c5 2020-01-15 tracey
996 8087c3c5 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
997 8087c3c5 2020-01-15 tracey if ((asprintf(&gw_trans->headref, "%s",
998 2c251c14 2020-01-15 tracey p->parsed.s)) == -1)
999 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1000 2c251c14 2020-01-15 tracey
1001 2c251c14 2020-01-15 tracey if (action == NULL) {
1002 2c251c14 2020-01-15 tracey error = got_error_from_errno("invalid action");
1003 2c251c14 2020-01-15 tracey return error;
1004 2c251c14 2020-01-15 tracey }
1005 46b9c89b 2020-01-15 tracey if ((gw_trans->gw_dir =
1006 46b9c89b 2020-01-15 tracey gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1007 46b9c89b 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1008 46b9c89b 2020-01-15 tracey
1009 46b9c89b 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1010 46b9c89b 2020-01-15 tracey if (error)
1011 46b9c89b 2020-01-15 tracey return error;
1012 2c251c14 2020-01-15 tracey } else
1013 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
1014 2c251c14 2020-01-15 tracey
1015 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1016 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
1017 2c251c14 2020-01-15 tracey
1018 2c251c14 2020-01-15 tracey if (gw_trans->action == GW_RAW)
1019 387a29ba 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
1020 2c251c14 2020-01-15 tracey
1021 2c251c14 2020-01-15 tracey return error;
1022 2c251c14 2020-01-15 tracey }
1023 2c251c14 2020-01-15 tracey
1024 2c251c14 2020-01-15 tracey static struct gw_dir *
1025 2c251c14 2020-01-15 tracey gw_init_gw_dir(char *dir)
1026 2c251c14 2020-01-15 tracey {
1027 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1028 2c251c14 2020-01-15 tracey
1029 2c251c14 2020-01-15 tracey if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1030 2c251c14 2020-01-15 tracey return NULL;
1031 2c251c14 2020-01-15 tracey
1032 2c251c14 2020-01-15 tracey if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1033 2c251c14 2020-01-15 tracey return NULL;
1034 2c251c14 2020-01-15 tracey
1035 2c251c14 2020-01-15 tracey return gw_dir;
1036 474370cb 2020-01-15 tracey }
1037 474370cb 2020-01-15 tracey
1038 474370cb 2020-01-15 tracey static const struct got_error*
1039 474370cb 2020-01-15 tracey match_logmsg(int *have_match, struct got_object_id *id,
1040 474370cb 2020-01-15 tracey struct got_commit_object *commit, regex_t *regex)
1041 474370cb 2020-01-15 tracey {
1042 474370cb 2020-01-15 tracey const struct got_error *err = NULL;
1043 474370cb 2020-01-15 tracey regmatch_t regmatch;
1044 474370cb 2020-01-15 tracey char *id_str = NULL, *logmsg = NULL;
1045 474370cb 2020-01-15 tracey
1046 474370cb 2020-01-15 tracey *have_match = 0;
1047 474370cb 2020-01-15 tracey
1048 474370cb 2020-01-15 tracey err = got_object_id_str(&id_str, id);
1049 474370cb 2020-01-15 tracey if (err)
1050 474370cb 2020-01-15 tracey return err;
1051 474370cb 2020-01-15 tracey
1052 474370cb 2020-01-15 tracey err = got_object_commit_get_logmsg(&logmsg, commit);
1053 474370cb 2020-01-15 tracey if (err)
1054 474370cb 2020-01-15 tracey goto done;
1055 474370cb 2020-01-15 tracey
1056 474370cb 2020-01-15 tracey if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1057 474370cb 2020-01-15 tracey *have_match = 1;
1058 474370cb 2020-01-15 tracey done:
1059 474370cb 2020-01-15 tracey free(id_str);
1060 474370cb 2020-01-15 tracey free(logmsg);
1061 474370cb 2020-01-15 tracey return err;
1062 2c251c14 2020-01-15 tracey }
1063 2c251c14 2020-01-15 tracey
1064 2c251c14 2020-01-15 tracey static void
1065 2c251c14 2020-01-15 tracey gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
1066 2c251c14 2020-01-15 tracey {
1067 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1068 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1069 2c251c14 2020-01-15 tracey khttps[code]);
1070 387a29ba 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1071 2c251c14 2020-01-15 tracey kmimetypes[mime]);
1072 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1073 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1074 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1075 2c251c14 2020-01-15 tracey khttp_body(gw_trans->gw_req);
1076 2c251c14 2020-01-15 tracey }
1077 2c251c14 2020-01-15 tracey
1078 2c251c14 2020-01-15 tracey static void
1079 2c251c14 2020-01-15 tracey gw_display_index(struct trans *gw_trans, const struct got_error *err)
1080 2c251c14 2020-01-15 tracey {
1081 2c251c14 2020-01-15 tracey gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1082 2c251c14 2020-01-15 tracey khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1083 2c251c14 2020-01-15 tracey
1084 2c251c14 2020-01-15 tracey if (err)
1085 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, err->msg);
1086 2c251c14 2020-01-15 tracey else
1087 2c251c14 2020-01-15 tracey khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1088 2c251c14 2020-01-15 tracey gw_query_funcs[gw_trans->action].template);
1089 2c251c14 2020-01-15 tracey
1090 2c251c14 2020-01-15 tracey khtml_close(gw_trans->gw_html_req);
1091 2c251c14 2020-01-15 tracey }
1092 2c251c14 2020-01-15 tracey
1093 2c251c14 2020-01-15 tracey static int
1094 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
1095 2c251c14 2020-01-15 tracey {
1096 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1097 2c251c14 2020-01-15 tracey struct trans *gw_trans = arg;
1098 2c251c14 2020-01-15 tracey char *gw_got_link, *gw_site_link;
1099 2c251c14 2020-01-15 tracey char *site_owner_name, *site_owner_name_h;
1100 2c251c14 2020-01-15 tracey
1101 2c251c14 2020-01-15 tracey switch (key) {
1102 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
1103 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, head);
1104 2c251c14 2020-01-15 tracey break;
1105 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
1106 2c251c14 2020-01-15 tracey gw_got_link = gw_get_got_link(gw_trans);
1107 2c251c14 2020-01-15 tracey if (gw_got_link != NULL)
1108 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_got_link);
1109 2c251c14 2020-01-15 tracey
1110 2c251c14 2020-01-15 tracey free(gw_got_link);
1111 2c251c14 2020-01-15 tracey break;
1112 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
1113 2c251c14 2020-01-15 tracey gw_site_link = gw_get_site_link(gw_trans);
1114 2c251c14 2020-01-15 tracey if (gw_site_link != NULL)
1115 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_site_link);
1116 2c251c14 2020-01-15 tracey
1117 2c251c14 2020-01-15 tracey free(gw_site_link);
1118 2c251c14 2020-01-15 tracey break;
1119 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
1120 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_name != NULL)
1121 2c251c14 2020-01-15 tracey khtml_puts(gw_trans->gw_html_req,
1122 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
1123 2c251c14 2020-01-15 tracey
1124 2c251c14 2020-01-15 tracey break;
1125 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
1126 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, search);
1127 2c251c14 2020-01-15 tracey break;
1128 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
1129 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
1130 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
1131 2c251c14 2020-01-15 tracey site_owner_name =
1132 2c251c14 2020-01-15 tracey gw_html_escape(gw_trans->gw_conf->got_site_owner);
1133 2c251c14 2020-01-15 tracey if ((asprintf(&site_owner_name_h, site_owner,
1134 2c251c14 2020-01-15 tracey site_owner_name))
1135 2c251c14 2020-01-15 tracey == -1)
1136 2c251c14 2020-01-15 tracey return 0;
1137 2c251c14 2020-01-15 tracey
1138 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, site_owner_name_h);
1139 2c251c14 2020-01-15 tracey free(site_owner_name);
1140 2c251c14 2020-01-15 tracey free(site_owner_name_h);
1141 2c251c14 2020-01-15 tracey }
1142 2c251c14 2020-01-15 tracey break;
1143 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
1144 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1145 2c251c14 2020-01-15 tracey if (error)
1146 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
1147 2c251c14 2020-01-15 tracey
1148 2c251c14 2020-01-15 tracey break;
1149 2c251c14 2020-01-15 tracey default:
1150 2c251c14 2020-01-15 tracey return 0;
1151 2c251c14 2020-01-15 tracey break;
1152 2c251c14 2020-01-15 tracey }
1153 2c251c14 2020-01-15 tracey return 1;
1154 2c251c14 2020-01-15 tracey }
1155 2c251c14 2020-01-15 tracey
1156 2c251c14 2020-01-15 tracey static char *
1157 2c251c14 2020-01-15 tracey gw_get_repo_description(struct trans *gw_trans, char *dir)
1158 2c251c14 2020-01-15 tracey {
1159 2c251c14 2020-01-15 tracey FILE *f;
1160 2c251c14 2020-01-15 tracey char *description = NULL, *d_file = NULL;
1161 2c251c14 2020-01-15 tracey unsigned int len;
1162 2c251c14 2020-01-15 tracey
1163 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description == false)
1164 2c251c14 2020-01-15 tracey goto err;
1165 2c251c14 2020-01-15 tracey
1166 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/description", dir)) == -1)
1167 2c251c14 2020-01-15 tracey goto err;
1168 2c251c14 2020-01-15 tracey
1169 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1170 2c251c14 2020-01-15 tracey goto err;
1171 2c251c14 2020-01-15 tracey
1172 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1173 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1174 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1175 2c251c14 2020-01-15 tracey if ((description = calloc(len, sizeof(char *))) == NULL)
1176 2c251c14 2020-01-15 tracey goto err;
1177 2c251c14 2020-01-15 tracey
1178 2c251c14 2020-01-15 tracey fread(description, 1, len, f);
1179 2c251c14 2020-01-15 tracey fclose(f);
1180 2c251c14 2020-01-15 tracey free(d_file);
1181 2c251c14 2020-01-15 tracey return description;
1182 2c251c14 2020-01-15 tracey err:
1183 2c251c14 2020-01-15 tracey if ((asprintf(&description, "%s", "")) == -1)
1184 2c251c14 2020-01-15 tracey return NULL;
1185 2c251c14 2020-01-15 tracey
1186 2c251c14 2020-01-15 tracey return description;
1187 2c251c14 2020-01-15 tracey }
1188 2c251c14 2020-01-15 tracey
1189 2c251c14 2020-01-15 tracey static char *
1190 474370cb 2020-01-15 tracey gw_get_time_str(time_t committer_time, int ref_tm)
1191 474370cb 2020-01-15 tracey {
1192 474370cb 2020-01-15 tracey struct tm tm;
1193 474370cb 2020-01-15 tracey time_t diff_time;
1194 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
1195 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1196 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
1197 474370cb 2020-01-15 tracey char *now = "right now";
1198 474370cb 2020-01-15 tracey char *repo_age, *s;
1199 6c6c85af 2020-01-15 tracey char datebuf[29];
1200 474370cb 2020-01-15 tracey
1201 474370cb 2020-01-15 tracey switch (ref_tm) {
1202 474370cb 2020-01-15 tracey case TM_DIFF:
1203 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
1204 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1205 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1206 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1207 474370cb 2020-01-15 tracey return NULL;
1208 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1209 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1210 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1211 474370cb 2020-01-15 tracey months)) == -1)
1212 474370cb 2020-01-15 tracey return NULL;
1213 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1214 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1215 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1216 474370cb 2020-01-15 tracey return NULL;
1217 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1218 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1219 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24), days)) == -1)
1220 474370cb 2020-01-15 tracey return NULL;
1221 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
1222 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1223 474370cb 2020-01-15 tracey (diff_time / 60 / 60), hours)) == -1)
1224 474370cb 2020-01-15 tracey return NULL;
1225 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
1226 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1227 474370cb 2020-01-15 tracey minutes)) == -1)
1228 474370cb 2020-01-15 tracey return NULL;
1229 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
1230 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", diff_time,
1231 474370cb 2020-01-15 tracey seconds)) == -1)
1232 474370cb 2020-01-15 tracey return NULL;
1233 474370cb 2020-01-15 tracey } else {
1234 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s", now)) == -1)
1235 474370cb 2020-01-15 tracey return NULL;
1236 474370cb 2020-01-15 tracey }
1237 474370cb 2020-01-15 tracey break;
1238 474370cb 2020-01-15 tracey case TM_LONG:
1239 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1240 474370cb 2020-01-15 tracey return NULL;
1241 474370cb 2020-01-15 tracey
1242 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
1243 474370cb 2020-01-15 tracey if (s == NULL)
1244 474370cb 2020-01-15 tracey return NULL;
1245 474370cb 2020-01-15 tracey
1246 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1247 474370cb 2020-01-15 tracey return NULL;
1248 474370cb 2020-01-15 tracey break;
1249 474370cb 2020-01-15 tracey }
1250 474370cb 2020-01-15 tracey return repo_age;
1251 474370cb 2020-01-15 tracey }
1252 474370cb 2020-01-15 tracey
1253 474370cb 2020-01-15 tracey static char *
1254 387a29ba 2020-01-15 tracey gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1255 2c251c14 2020-01-15 tracey {
1256 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1257 2c251c14 2020-01-15 tracey struct got_object_id *id = NULL;
1258 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
1259 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
1260 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
1261 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
1262 2c251c14 2020-01-15 tracey struct got_reference *head_ref;
1263 87f9ebf5 2020-01-15 tracey int is_head = 0;
1264 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
1265 87f9ebf5 2020-01-15 tracey const char *refname;
1266 474370cb 2020-01-15 tracey char *repo_age = NULL;
1267 387a29ba 2020-01-15 tracey
1268 387a29ba 2020-01-15 tracey if (repo_ref == NULL)
1269 387a29ba 2020-01-15 tracey return NULL;
1270 87f9ebf5 2020-01-15 tracey
1271 87f9ebf5 2020-01-15 tracey if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1272 87f9ebf5 2020-01-15 tracey is_head = 1;
1273 2c251c14 2020-01-15 tracey
1274 2c251c14 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1275 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age == false) {
1276 8087c3c5 2020-01-15 tracey if ((asprintf(&repo_age, "")) == -1)
1277 8087c3c5 2020-01-15 tracey return NULL;
1278 2c251c14 2020-01-15 tracey return repo_age;
1279 2c251c14 2020-01-15 tracey }
1280 87f9ebf5 2020-01-15 tracey
1281 2c251c14 2020-01-15 tracey error = got_repo_open(&repo, dir, NULL);
1282 2c251c14 2020-01-15 tracey if (error != NULL)
1283 2c251c14 2020-01-15 tracey goto err;
1284 2c251c14 2020-01-15 tracey
1285 87f9ebf5 2020-01-15 tracey if (is_head)
1286 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads",
1287 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1288 87f9ebf5 2020-01-15 tracey else
1289 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, repo_ref,
1290 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1291 2c251c14 2020-01-15 tracey if (error != NULL)
1292 2c251c14 2020-01-15 tracey goto err;
1293 2c251c14 2020-01-15 tracey
1294 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1295 87f9ebf5 2020-01-15 tracey if (is_head)
1296 87f9ebf5 2020-01-15 tracey refname = strdup(repo_ref);
1297 87f9ebf5 2020-01-15 tracey else
1298 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1299 2c251c14 2020-01-15 tracey error = got_ref_open(&head_ref, repo, refname, 0);
1300 2c251c14 2020-01-15 tracey if (error != NULL)
1301 2c251c14 2020-01-15 tracey goto err;
1302 2c251c14 2020-01-15 tracey
1303 2c251c14 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
1304 2c251c14 2020-01-15 tracey got_ref_close(head_ref);
1305 2c251c14 2020-01-15 tracey if (error != NULL)
1306 2c251c14 2020-01-15 tracey goto err;
1307 2c251c14 2020-01-15 tracey
1308 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1309 2c251c14 2020-01-15 tracey if (error != NULL)
1310 2c251c14 2020-01-15 tracey goto err;
1311 2c251c14 2020-01-15 tracey
1312 2c251c14 2020-01-15 tracey committer_time =
1313 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
1314 2c251c14 2020-01-15 tracey
1315 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
1316 2c251c14 2020-01-15 tracey cmp_time = committer_time;
1317 2c251c14 2020-01-15 tracey }
1318 2c251c14 2020-01-15 tracey
1319 474370cb 2020-01-15 tracey if (cmp_time != 0) {
1320 2c251c14 2020-01-15 tracey committer_time = cmp_time;
1321 474370cb 2020-01-15 tracey repo_age = gw_get_time_str(committer_time, ref_tm);
1322 474370cb 2020-01-15 tracey } else
1323 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "")) == -1)
1324 474370cb 2020-01-15 tracey return NULL;
1325 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
1326 2c251c14 2020-01-15 tracey free(id);
1327 2c251c14 2020-01-15 tracey return repo_age;
1328 2c251c14 2020-01-15 tracey err:
1329 2c251c14 2020-01-15 tracey if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1330 2c251c14 2020-01-15 tracey return NULL;
1331 2c251c14 2020-01-15 tracey
1332 2c251c14 2020-01-15 tracey return repo_age;
1333 2c251c14 2020-01-15 tracey }
1334 2c251c14 2020-01-15 tracey
1335 2c251c14 2020-01-15 tracey static char *
1336 2c251c14 2020-01-15 tracey gw_get_repo_owner(struct trans *gw_trans, char *dir)
1337 2c251c14 2020-01-15 tracey {
1338 2c251c14 2020-01-15 tracey FILE *f;
1339 2c251c14 2020-01-15 tracey char *owner = NULL, *d_file = NULL;
1340 2c251c14 2020-01-15 tracey char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1341 2c251c14 2020-01-15 tracey char *comp, *pos, *buf;
1342 2c251c14 2020-01-15 tracey unsigned int i;
1343 2c251c14 2020-01-15 tracey
1344 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner == false)
1345 2c251c14 2020-01-15 tracey goto err;
1346 2c251c14 2020-01-15 tracey
1347 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/config", dir)) == -1)
1348 2c251c14 2020-01-15 tracey goto err;
1349 2c251c14 2020-01-15 tracey
1350 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1351 2c251c14 2020-01-15 tracey goto err;
1352 2c251c14 2020-01-15 tracey
1353 6c6c85af 2020-01-15 tracey if ((buf = calloc(128, sizeof(char *))) == NULL)
1354 2c251c14 2020-01-15 tracey goto err;
1355 2c251c14 2020-01-15 tracey
1356 6c6c85af 2020-01-15 tracey while ((fgets(buf, 128, f)) != NULL) {
1357 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gotweb)) != NULL)
1358 2c251c14 2020-01-15 tracey break;
1359 2c251c14 2020-01-15 tracey
1360 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gitweb)) != NULL)
1361 2c251c14 2020-01-15 tracey break;
1362 2c251c14 2020-01-15 tracey }
1363 2c251c14 2020-01-15 tracey
1364 2c251c14 2020-01-15 tracey if (pos == NULL)
1365 2c251c14 2020-01-15 tracey goto err;
1366 2c251c14 2020-01-15 tracey
1367 2c251c14 2020-01-15 tracey do {
1368 6c6c85af 2020-01-15 tracey fgets(buf, 128, f);
1369 2c251c14 2020-01-15 tracey } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1370 2c251c14 2020-01-15 tracey
1371 2c251c14 2020-01-15 tracey if (comp == NULL)
1372 2c251c14 2020-01-15 tracey goto err;
1373 2c251c14 2020-01-15 tracey
1374 2c251c14 2020-01-15 tracey if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1375 2c251c14 2020-01-15 tracey goto err;
1376 2c251c14 2020-01-15 tracey
1377 2c251c14 2020-01-15 tracey for (i = 0; i < 2; i++) {
1378 2c251c14 2020-01-15 tracey owner = strsep(&buf, "\"");
1379 2c251c14 2020-01-15 tracey }
1380 2c251c14 2020-01-15 tracey
1381 2c251c14 2020-01-15 tracey if (owner == NULL)
1382 2c251c14 2020-01-15 tracey goto err;
1383 2c251c14 2020-01-15 tracey
1384 2c251c14 2020-01-15 tracey fclose(f);
1385 2c251c14 2020-01-15 tracey free(d_file);
1386 2c251c14 2020-01-15 tracey return owner;
1387 2c251c14 2020-01-15 tracey err:
1388 2c251c14 2020-01-15 tracey if ((asprintf(&owner, "%s", "")) == -1)
1389 2c251c14 2020-01-15 tracey return NULL;
1390 2c251c14 2020-01-15 tracey
1391 2c251c14 2020-01-15 tracey return owner;
1392 2c251c14 2020-01-15 tracey }
1393 2c251c14 2020-01-15 tracey
1394 2c251c14 2020-01-15 tracey static char *
1395 2c251c14 2020-01-15 tracey gw_get_clone_url(struct trans *gw_trans, char *dir)
1396 2c251c14 2020-01-15 tracey {
1397 2c251c14 2020-01-15 tracey FILE *f;
1398 2c251c14 2020-01-15 tracey char *url = NULL, *d_file = NULL;
1399 2c251c14 2020-01-15 tracey unsigned int len;
1400 2c251c14 2020-01-15 tracey
1401 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1402 2c251c14 2020-01-15 tracey return NULL;
1403 2c251c14 2020-01-15 tracey
1404 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1405 2c251c14 2020-01-15 tracey return NULL;
1406 2c251c14 2020-01-15 tracey
1407 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1408 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1409 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1410 2c251c14 2020-01-15 tracey
1411 2c251c14 2020-01-15 tracey if ((url = calloc(len, sizeof(char *))) == NULL)
1412 2c251c14 2020-01-15 tracey return NULL;
1413 2c251c14 2020-01-15 tracey
1414 2c251c14 2020-01-15 tracey fread(url, 1, len, f);
1415 2c251c14 2020-01-15 tracey fclose(f);
1416 2c251c14 2020-01-15 tracey free(d_file);
1417 2c251c14 2020-01-15 tracey return url;
1418 8d4d2453 2020-01-15 tracey }
1419 8d4d2453 2020-01-15 tracey
1420 8d4d2453 2020-01-15 tracey static char *
1421 4ceb8155 2020-01-15 tracey gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1422 8087c3c5 2020-01-15 tracey char *start_commit, int limit, int log_type)
1423 8d4d2453 2020-01-15 tracey {
1424 c6b62706 2020-01-15 tracey const struct got_error *error;
1425 c6b62706 2020-01-15 tracey struct got_repository *repo = NULL;
1426 c6b62706 2020-01-15 tracey struct got_reflist_head refs;
1427 474370cb 2020-01-15 tracey struct got_reflist_entry *re;
1428 c6b62706 2020-01-15 tracey struct got_commit_object *commit = NULL;
1429 8087c3c5 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1430 8087c3c5 2020-01-15 tracey struct got_object_qid *parent_id;
1431 474370cb 2020-01-15 tracey struct got_commit_graph *graph = NULL;
1432 87f9ebf5 2020-01-15 tracey char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1433 87f9ebf5 2020-01-15 tracey *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1434 87f9ebf5 2020-01-15 tracey *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1435 87f9ebf5 2020-01-15 tracey *commit_commit_disp = NULL, *commit_age_diff = NULL,
1436 87f9ebf5 2020-01-15 tracey *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1437 87f9ebf5 2020-01-15 tracey *commit_age_long_disp = NULL, *commit_author = NULL,
1438 87f9ebf5 2020-01-15 tracey *commit_author_disp = NULL, *commit_committer = NULL,
1439 87f9ebf5 2020-01-15 tracey *commit_committer_disp = NULL, *commit_log = NULL,
1440 87f9ebf5 2020-01-15 tracey *commit_log_disp = NULL, *commit_parent = NULL,
1441 87f9ebf5 2020-01-15 tracey *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1442 87f9ebf5 2020-01-15 tracey *log_tree_html = NULL, *log_commit_html = NULL,
1443 87f9ebf5 2020-01-15 tracey *log_diff_html = NULL, *commit_tree = NULL,
1444 077f6c5a 2020-01-15 tracey *commit_tree_disp = NULL, *log_tag_html = NULL;
1445 87f9ebf5 2020-01-15 tracey char *commit_log0, *newline;
1446 474370cb 2020-01-15 tracey regex_t regex;
1447 65b95fb2 2020-01-15 tracey int have_match, log_count = 0;
1448 474370cb 2020-01-15 tracey size_t newsize;
1449 8087c3c5 2020-01-15 tracey struct buf *diffbuf = NULL;
1450 474370cb 2020-01-15 tracey time_t committer_time;
1451 65b95fb2 2020-01-15 tracey
1452 65b95fb2 2020-01-15 tracey if (gw_trans->action == GW_LOG || gw_trans->action == GW_LOGBRIEFS)
1453 65b95fb2 2020-01-15 tracey log_count = gw_get_repo_log_count(gw_trans, start_commit);
1454 8d4d2453 2020-01-15 tracey
1455 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1456 6c6c85af 2020-01-15 tracey if (error != NULL)
1457 6c6c85af 2020-01-15 tracey return NULL;
1458 6c6c85af 2020-01-15 tracey
1459 474370cb 2020-01-15 tracey if (search_pattern &&
1460 474370cb 2020-01-15 tracey regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1461 474370cb 2020-01-15 tracey REG_NEWLINE))
1462 474370cb 2020-01-15 tracey return NULL;
1463 474370cb 2020-01-15 tracey
1464 c6b62706 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1465 c6b62706 2020-01-15 tracey if (error != NULL)
1466 6c6c85af 2020-01-15 tracey return NULL;
1467 c6b62706 2020-01-15 tracey
1468 6c6c85af 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1469 474370cb 2020-01-15 tracey
1470 c6b62706 2020-01-15 tracey if (start_commit == NULL) {
1471 c6b62706 2020-01-15 tracey struct got_reference *head_ref;
1472 8087c3c5 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1473 c6b62706 2020-01-15 tracey if (error != NULL)
1474 8087c3c5 2020-01-15 tracey goto done;
1475 6c6c85af 2020-01-15 tracey
1476 8087c3c5 2020-01-15 tracey error = got_ref_resolve(&id1, repo, head_ref);
1477 c6b62706 2020-01-15 tracey got_ref_close(head_ref);
1478 c6b62706 2020-01-15 tracey if (error != NULL)
1479 8087c3c5 2020-01-15 tracey goto done;
1480 6c6c85af 2020-01-15 tracey
1481 8087c3c5 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id1);
1482 c6b62706 2020-01-15 tracey } else {
1483 c6b62706 2020-01-15 tracey struct got_reference *ref;
1484 c6b62706 2020-01-15 tracey error = got_ref_open(&ref, repo, start_commit, 0);
1485 c6b62706 2020-01-15 tracey if (error == NULL) {
1486 c6b62706 2020-01-15 tracey int obj_type;
1487 8087c3c5 2020-01-15 tracey error = got_ref_resolve(&id1, repo, ref);
1488 c6b62706 2020-01-15 tracey got_ref_close(ref);
1489 c6b62706 2020-01-15 tracey if (error != NULL)
1490 c6b62706 2020-01-15 tracey goto done;
1491 8087c3c5 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, id1);
1492 c6b62706 2020-01-15 tracey if (error != NULL)
1493 c6b62706 2020-01-15 tracey goto done;
1494 c6b62706 2020-01-15 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
1495 c6b62706 2020-01-15 tracey struct got_tag_object *tag;
1496 8087c3c5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id1);
1497 c6b62706 2020-01-15 tracey if (error != NULL)
1498 c6b62706 2020-01-15 tracey goto done;
1499 c6b62706 2020-01-15 tracey if (got_object_tag_get_object_type(tag) !=
1500 c6b62706 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT) {
1501 c6b62706 2020-01-15 tracey got_object_tag_close(tag);
1502 c6b62706 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1503 c6b62706 2020-01-15 tracey goto done;
1504 c6b62706 2020-01-15 tracey }
1505 8087c3c5 2020-01-15 tracey free(id1);
1506 8087c3c5 2020-01-15 tracey id1 = got_object_id_dup(
1507 c6b62706 2020-01-15 tracey got_object_tag_get_object_id(tag));
1508 8087c3c5 2020-01-15 tracey if (id1 == NULL)
1509 c6b62706 2020-01-15 tracey error = got_error_from_errno(
1510 c6b62706 2020-01-15 tracey "got_object_id_dup");
1511 c6b62706 2020-01-15 tracey got_object_tag_close(tag);
1512 c6b62706 2020-01-15 tracey if (error)
1513 c6b62706 2020-01-15 tracey goto done;
1514 c6b62706 2020-01-15 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1515 c6b62706 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1516 c6b62706 2020-01-15 tracey goto done;
1517 c6b62706 2020-01-15 tracey }
1518 8087c3c5 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id1);
1519 c6b62706 2020-01-15 tracey if (error != NULL)
1520 c6b62706 2020-01-15 tracey goto done;
1521 c6b62706 2020-01-15 tracey }
1522 c6b62706 2020-01-15 tracey if (commit == NULL) {
1523 8087c3c5 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id1,
1524 c6b62706 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1525 c6b62706 2020-01-15 tracey if (error != NULL)
1526 8087c3c5 2020-01-15 tracey goto done;
1527 c6b62706 2020-01-15 tracey }
1528 8087c3c5 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id1,
1529 8087c3c5 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1530 c6b62706 2020-01-15 tracey }
1531 c6b62706 2020-01-15 tracey
1532 c6b62706 2020-01-15 tracey if (error != NULL)
1533 c6b62706 2020-01-15 tracey goto done;
1534 6c6c85af 2020-01-15 tracey
1535 474370cb 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1536 474370cb 2020-01-15 tracey if (error != NULL)
1537 474370cb 2020-01-15 tracey goto done;
1538 474370cb 2020-01-15 tracey
1539 474370cb 2020-01-15 tracey if (in_repo_path) {
1540 474370cb 2020-01-15 tracey free(path);
1541 474370cb 2020-01-15 tracey path = in_repo_path;
1542 474370cb 2020-01-15 tracey }
1543 474370cb 2020-01-15 tracey
1544 c6b62706 2020-01-15 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1545 c6b62706 2020-01-15 tracey if (error)
1546 c6b62706 2020-01-15 tracey goto done;
1547 c6b62706 2020-01-15 tracey
1548 65b95fb2 2020-01-15 tracey error = got_commit_graph_open(&graph, path, 0);
1549 474370cb 2020-01-15 tracey if (error)
1550 474370cb 2020-01-15 tracey goto done;
1551 474370cb 2020-01-15 tracey
1552 8087c3c5 2020-01-15 tracey error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1553 474370cb 2020-01-15 tracey if (error)
1554 474370cb 2020-01-15 tracey goto done;
1555 474370cb 2020-01-15 tracey
1556 474370cb 2020-01-15 tracey for (;;) {
1557 65b95fb2 2020-01-15 tracey error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1558 65b95fb2 2020-01-15 tracey NULL);
1559 474370cb 2020-01-15 tracey if (error) {
1560 65b95fb2 2020-01-15 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
1561 474370cb 2020-01-15 tracey error = NULL;
1562 65b95fb2 2020-01-15 tracey break;
1563 474370cb 2020-01-15 tracey }
1564 8087c3c5 2020-01-15 tracey if (id1 == NULL)
1565 474370cb 2020-01-15 tracey break;
1566 474370cb 2020-01-15 tracey
1567 8087c3c5 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id1);
1568 474370cb 2020-01-15 tracey if (error)
1569 474370cb 2020-01-15 tracey break;
1570 474370cb 2020-01-15 tracey
1571 474370cb 2020-01-15 tracey if (search_pattern) {
1572 8087c3c5 2020-01-15 tracey error = match_logmsg(&have_match, id1, commit,
1573 474370cb 2020-01-15 tracey &regex);
1574 474370cb 2020-01-15 tracey if (error) {
1575 8087c3c5 2020-01-15 tracey got_object_commit_close(commit);
1576 474370cb 2020-01-15 tracey break;
1577 474370cb 2020-01-15 tracey }
1578 474370cb 2020-01-15 tracey if (have_match == 0) {
1579 8087c3c5 2020-01-15 tracey got_object_commit_close(commit);
1580 474370cb 2020-01-15 tracey continue;
1581 474370cb 2020-01-15 tracey }
1582 474370cb 2020-01-15 tracey }
1583 474370cb 2020-01-15 tracey
1584 474370cb 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1585 8087c3c5 2020-01-15 tracey char *s;
1586 474370cb 2020-01-15 tracey const char *name;
1587 474370cb 2020-01-15 tracey struct got_tag_object *tag = NULL;
1588 474370cb 2020-01-15 tracey int cmp;
1589 474370cb 2020-01-15 tracey
1590 474370cb 2020-01-15 tracey name = got_ref_get_name(re->ref);
1591 474370cb 2020-01-15 tracey if (strcmp(name, GOT_REF_HEAD) == 0)
1592 474370cb 2020-01-15 tracey continue;
1593 474370cb 2020-01-15 tracey if (strncmp(name, "refs/", 5) == 0)
1594 474370cb 2020-01-15 tracey name += 5;
1595 474370cb 2020-01-15 tracey if (strncmp(name, "got/", 4) == 0)
1596 474370cb 2020-01-15 tracey continue;
1597 474370cb 2020-01-15 tracey if (strncmp(name, "heads/", 6) == 0)
1598 474370cb 2020-01-15 tracey name += 6;
1599 474370cb 2020-01-15 tracey if (strncmp(name, "remotes/", 8) == 0)
1600 474370cb 2020-01-15 tracey name += 8;
1601 474370cb 2020-01-15 tracey if (strncmp(name, "tags/", 5) == 0) {
1602 474370cb 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo,
1603 474370cb 2020-01-15 tracey re->id);
1604 474370cb 2020-01-15 tracey if (error) {
1605 474370cb 2020-01-15 tracey if (error->code != GOT_ERR_OBJ_TYPE)
1606 474370cb 2020-01-15 tracey continue;
1607 474370cb 2020-01-15 tracey /*
1608 474370cb 2020-01-15 tracey * Ref points at something other
1609 474370cb 2020-01-15 tracey * than a tag.
1610 474370cb 2020-01-15 tracey */
1611 474370cb 2020-01-15 tracey error = NULL;
1612 474370cb 2020-01-15 tracey tag = NULL;
1613 474370cb 2020-01-15 tracey }
1614 474370cb 2020-01-15 tracey }
1615 474370cb 2020-01-15 tracey cmp = got_object_id_cmp(tag ?
1616 8087c3c5 2020-01-15 tracey got_object_tag_get_object_id(tag) : re->id, id1);
1617 474370cb 2020-01-15 tracey if (tag)
1618 474370cb 2020-01-15 tracey got_object_tag_close(tag);
1619 474370cb 2020-01-15 tracey if (cmp != 0)
1620 474370cb 2020-01-15 tracey continue;
1621 8087c3c5 2020-01-15 tracey s = refs_str;
1622 8087c3c5 2020-01-15 tracey if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1623 8087c3c5 2020-01-15 tracey s ? ", " : "", name)) == -1) {
1624 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1625 8087c3c5 2020-01-15 tracey free(s);
1626 8087c3c5 2020-01-15 tracey goto done;
1627 8087c3c5 2020-01-15 tracey }
1628 8087c3c5 2020-01-15 tracey free(s);
1629 474370cb 2020-01-15 tracey }
1630 cdb914e5 2020-01-15 tracey
1631 8087c3c5 2020-01-15 tracey if (refs_str == NULL)
1632 8087c3c5 2020-01-15 tracey refs_str_disp = strdup("");
1633 8087c3c5 2020-01-15 tracey else {
1634 8087c3c5 2020-01-15 tracey if ((asprintf(&refs_str_disp, "(%s)",
1635 8087c3c5 2020-01-15 tracey refs_str)) == -1) {
1636 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1637 8087c3c5 2020-01-15 tracey free(refs_str);
1638 8087c3c5 2020-01-15 tracey goto done;
1639 8087c3c5 2020-01-15 tracey }
1640 8087c3c5 2020-01-15 tracey }
1641 474370cb 2020-01-15 tracey
1642 8087c3c5 2020-01-15 tracey error = got_object_id_str(&id_str1, id1);
1643 474370cb 2020-01-15 tracey if (error)
1644 8087c3c5 2020-01-15 tracey goto done;
1645 474370cb 2020-01-15 tracey
1646 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&treeid,
1647 87f9ebf5 2020-01-15 tracey got_object_commit_get_tree_id(commit));
1648 87f9ebf5 2020-01-15 tracey if (error)
1649 87f9ebf5 2020-01-15 tracey goto done;
1650 87f9ebf5 2020-01-15 tracey
1651 87f9ebf5 2020-01-15 tracey if (gw_trans->action == GW_COMMIT ||
1652 87f9ebf5 2020-01-15 tracey gw_trans->action == GW_COMMITDIFF) {
1653 8087c3c5 2020-01-15 tracey parent_id =
1654 8087c3c5 2020-01-15 tracey SIMPLEQ_FIRST(
1655 8087c3c5 2020-01-15 tracey got_object_commit_get_parent_ids(commit));
1656 8087c3c5 2020-01-15 tracey if (parent_id != NULL) {
1657 8087c3c5 2020-01-15 tracey id2 = got_object_id_dup(parent_id->id);
1658 8087c3c5 2020-01-15 tracey free (parent_id);
1659 8087c3c5 2020-01-15 tracey error = got_object_id_str(&id_str2, id2);
1660 8087c3c5 2020-01-15 tracey if (error)
1661 8087c3c5 2020-01-15 tracey goto done;
1662 87f9ebf5 2020-01-15 tracey free(id2);
1663 8087c3c5 2020-01-15 tracey } else
1664 8087c3c5 2020-01-15 tracey id_str2 = strdup("/dev/null");
1665 8087c3c5 2020-01-15 tracey }
1666 8087c3c5 2020-01-15 tracey
1667 474370cb 2020-01-15 tracey committer_time =
1668 8087c3c5 2020-01-15 tracey got_object_commit_get_committer_time(commit);
1669 4ceb8155 2020-01-15 tracey
1670 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1671 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1672 8087c3c5 2020-01-15 tracey goto done;
1673 8087c3c5 2020-01-15 tracey }
1674 8087c3c5 2020-01-15 tracey
1675 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1676 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1677 87f9ebf5 2020-01-15 tracey goto done;
1678 87f9ebf5 2020-01-15 tracey }
1679 87f9ebf5 2020-01-15 tracey
1680 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_tree_disp, commit_tree_html,
1681 87f9ebf5 2020-01-15 tracey treeid)) == -1) {
1682 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1683 87f9ebf5 2020-01-15 tracey goto done;
1684 87f9ebf5 2020-01-15 tracey }
1685 87f9ebf5 2020-01-15 tracey
1686 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1687 8087c3c5 2020-01-15 tracey id_str1)) == -1) {
1688 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1689 8087c3c5 2020-01-15 tracey goto done;
1690 8087c3c5 2020-01-15 tracey }
1691 8087c3c5 2020-01-15 tracey
1692 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1693 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1694 8087c3c5 2020-01-15 tracey goto done;
1695 8087c3c5 2020-01-15 tracey }
1696 8087c3c5 2020-01-15 tracey
1697 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_commit_disp, commit_commit_html,
1698 8087c3c5 2020-01-15 tracey commit_commit, refs_str_disp)) == -1) {
1699 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1700 8087c3c5 2020-01-15 tracey goto done;
1701 8087c3c5 2020-01-15 tracey }
1702 8087c3c5 2020-01-15 tracey
1703 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_long, "%s",
1704 8087c3c5 2020-01-15 tracey gw_get_time_str(committer_time, TM_LONG))) == -1) {
1705 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1706 8087c3c5 2020-01-15 tracey goto done;
1707 8087c3c5 2020-01-15 tracey }
1708 8087c3c5 2020-01-15 tracey
1709 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_long_disp, commit_age_html,
1710 8087c3c5 2020-01-15 tracey commit_age_long)) == -1) {
1711 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1712 8087c3c5 2020-01-15 tracey goto done;
1713 8087c3c5 2020-01-15 tracey }
1714 8087c3c5 2020-01-15 tracey
1715 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_diff, "%s",
1716 8087c3c5 2020-01-15 tracey gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1717 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1718 8087c3c5 2020-01-15 tracey goto done;
1719 8087c3c5 2020-01-15 tracey }
1720 8087c3c5 2020-01-15 tracey
1721 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_diff_disp, commit_age_html,
1722 8087c3c5 2020-01-15 tracey commit_age_diff)) == -1) {
1723 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1724 8087c3c5 2020-01-15 tracey goto done;
1725 8087c3c5 2020-01-15 tracey }
1726 8087c3c5 2020-01-15 tracey
1727 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_author, "%s",
1728 8087c3c5 2020-01-15 tracey got_object_commit_get_author(commit))) == -1) {
1729 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1730 8087c3c5 2020-01-15 tracey goto done;
1731 8087c3c5 2020-01-15 tracey }
1732 8087c3c5 2020-01-15 tracey
1733 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_author_disp, commit_author_html,
1734 8087c3c5 2020-01-15 tracey gw_html_escape(commit_author))) == -1) {
1735 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1736 8087c3c5 2020-01-15 tracey goto done;
1737 8087c3c5 2020-01-15 tracey }
1738 8087c3c5 2020-01-15 tracey
1739 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_committer, "%s",
1740 8087c3c5 2020-01-15 tracey got_object_commit_get_committer(commit))) == -1) {
1741 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1742 8087c3c5 2020-01-15 tracey goto done;
1743 8087c3c5 2020-01-15 tracey }
1744 8087c3c5 2020-01-15 tracey
1745 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_committer_disp, commit_committer_html,
1746 8087c3c5 2020-01-15 tracey gw_html_escape(commit_committer))) == -1) {
1747 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1748 8087c3c5 2020-01-15 tracey goto done;
1749 8087c3c5 2020-01-15 tracey }
1750 8087c3c5 2020-01-15 tracey
1751 8087c3c5 2020-01-15 tracey if (strcmp(commit_author, commit_committer) == 0) {
1752 8087c3c5 2020-01-15 tracey free(commit_committer_disp);
1753 8087c3c5 2020-01-15 tracey commit_committer_disp = strdup("");
1754 8087c3c5 2020-01-15 tracey }
1755 8087c3c5 2020-01-15 tracey
1756 8087c3c5 2020-01-15 tracey error = got_object_commit_get_logmsg(&commit_log0, commit);
1757 8087c3c5 2020-01-15 tracey if (error)
1758 8087c3c5 2020-01-15 tracey goto done;
1759 87f9ebf5 2020-01-15 tracey
1760 87f9ebf5 2020-01-15 tracey commit_log = commit_log0;
1761 87f9ebf5 2020-01-15 tracey while (*commit_log == '\n')
1762 87f9ebf5 2020-01-15 tracey commit_log++;
1763 8087c3c5 2020-01-15 tracey
1764 8087c3c5 2020-01-15 tracey switch(log_type) {
1765 8087c3c5 2020-01-15 tracey case (LOGBRIEF):
1766 8087c3c5 2020-01-15 tracey newline = strchr(commit_log, '\n');
1767 8087c3c5 2020-01-15 tracey if (newline)
1768 8087c3c5 2020-01-15 tracey *newline = '\0';
1769 8087c3c5 2020-01-15 tracey
1770 8087c3c5 2020-01-15 tracey if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1771 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1, gw_trans->repo_name,
1772 8087c3c5 2020-01-15 tracey id_str1, gw_trans->repo_name, id_str1,
1773 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1)) == -1) {
1774 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1775 8087c3c5 2020-01-15 tracey goto done;
1776 4ceb8155 2020-01-15 tracey }
1777 8087c3c5 2020-01-15 tracey
1778 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_row, logbriefs_row,
1779 8087c3c5 2020-01-15 tracey commit_age_diff, commit_author, commit_log,
1780 8087c3c5 2020-01-15 tracey logbriefs_navs_html)) == -1) {
1781 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1782 8087c3c5 2020-01-15 tracey goto done;
1783 8087c3c5 2020-01-15 tracey }
1784 8087c3c5 2020-01-15 tracey
1785 8087c3c5 2020-01-15 tracey free(logbriefs_navs_html);
1786 8087c3c5 2020-01-15 tracey logbriefs_navs_html = NULL;
1787 8087c3c5 2020-01-15 tracey break;
1788 8087c3c5 2020-01-15 tracey case (LOGFULL):
1789 8087c3c5 2020-01-15 tracey if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1790 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1, gw_trans->repo_name,
1791 8087c3c5 2020-01-15 tracey id_str1, gw_trans->repo_name, id_str1,
1792 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1)) == -1) {
1793 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1794 8087c3c5 2020-01-15 tracey goto done;
1795 8087c3c5 2020-01-15 tracey }
1796 8087c3c5 2020-01-15 tracey
1797 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1798 8087c3c5 2020-01-15 tracey commit_author_disp, commit_committer_disp,
1799 8087c3c5 2020-01-15 tracey commit_age_long_disp, gw_html_escape(commit_log),
1800 8087c3c5 2020-01-15 tracey logbriefs_navs_html)) == -1) {
1801 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1802 8087c3c5 2020-01-15 tracey goto done;
1803 8087c3c5 2020-01-15 tracey }
1804 8087c3c5 2020-01-15 tracey
1805 8087c3c5 2020-01-15 tracey free(logbriefs_navs_html);
1806 8087c3c5 2020-01-15 tracey logbriefs_navs_html = NULL;
1807 8087c3c5 2020-01-15 tracey break;
1808 b772de24 2020-01-15 tracey case (LOGTAG):
1809 077f6c5a 2020-01-15 tracey log_tag_html = strdup("tag log here");
1810 b772de24 2020-01-15 tracey
1811 077f6c5a 2020-01-15 tracey if ((asprintf(&commit_row, log_tag_row,
1812 077f6c5a 2020-01-15 tracey gw_html_escape(commit_log), log_tag_html)) == -1) {
1813 b772de24 2020-01-15 tracey error = got_error_from_errno("asprintf");
1814 b772de24 2020-01-15 tracey goto done;
1815 b772de24 2020-01-15 tracey }
1816 b772de24 2020-01-15 tracey
1817 077f6c5a 2020-01-15 tracey free(log_tag_html);
1818 b772de24 2020-01-15 tracey break;
1819 8087c3c5 2020-01-15 tracey case (LOGTREE):
1820 8087c3c5 2020-01-15 tracey log_tree_html = strdup("log tree here");
1821 8087c3c5 2020-01-15 tracey
1822 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_row, log_tree_row,
1823 87f9ebf5 2020-01-15 tracey gw_html_escape(commit_log), log_tree_html)) == -1) {
1824 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1825 8087c3c5 2020-01-15 tracey goto done;
1826 8087c3c5 2020-01-15 tracey }
1827 8087c3c5 2020-01-15 tracey
1828 8087c3c5 2020-01-15 tracey free(log_tree_html);
1829 8087c3c5 2020-01-15 tracey break;
1830 8087c3c5 2020-01-15 tracey case (LOGCOMMIT):
1831 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_log_disp, commit_log_html,
1832 8087c3c5 2020-01-15 tracey gw_html_escape(commit_log))) == -1) {
1833 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1834 8087c3c5 2020-01-15 tracey goto done;
1835 8087c3c5 2020-01-15 tracey }
1836 8087c3c5 2020-01-15 tracey
1837 8087c3c5 2020-01-15 tracey log_commit_html = strdup("commit here");
1838 8087c3c5 2020-01-15 tracey
1839 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_row, log_commit_row,
1840 8087c3c5 2020-01-15 tracey commit_diff_disp, commit_commit_disp,
1841 87f9ebf5 2020-01-15 tracey commit_tree_disp, commit_author_disp,
1842 87f9ebf5 2020-01-15 tracey commit_committer_disp, commit_age_long_disp,
1843 87f9ebf5 2020-01-15 tracey commit_log_disp, log_commit_html)) == -1) {
1844 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1845 8087c3c5 2020-01-15 tracey goto done;
1846 8087c3c5 2020-01-15 tracey }
1847 8087c3c5 2020-01-15 tracey free(commit_log_disp);
1848 8087c3c5 2020-01-15 tracey free(log_commit_html);
1849 8087c3c5 2020-01-15 tracey
1850 8087c3c5 2020-01-15 tracey break;
1851 87f9ebf5 2020-01-15 tracey case (LOGDIFF):
1852 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_log_disp, commit_log_html,
1853 87f9ebf5 2020-01-15 tracey gw_html_escape(commit_log))) == -1) {
1854 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1855 87f9ebf5 2020-01-15 tracey goto done;
1856 87f9ebf5 2020-01-15 tracey }
1857 87f9ebf5 2020-01-15 tracey
1858 87f9ebf5 2020-01-15 tracey log_diff_html = strdup("diff here");
1859 87f9ebf5 2020-01-15 tracey
1860 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_row, log_diff_row,
1861 87f9ebf5 2020-01-15 tracey commit_diff_disp, commit_commit_disp,
1862 87f9ebf5 2020-01-15 tracey commit_tree_disp, commit_author_disp,
1863 87f9ebf5 2020-01-15 tracey commit_committer_disp, commit_age_long_disp,
1864 87f9ebf5 2020-01-15 tracey commit_log_disp, log_diff_html)) == -1) {
1865 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1866 87f9ebf5 2020-01-15 tracey goto done;
1867 87f9ebf5 2020-01-15 tracey }
1868 87f9ebf5 2020-01-15 tracey free(commit_log_disp);
1869 87f9ebf5 2020-01-15 tracey free(log_diff_html);
1870 87f9ebf5 2020-01-15 tracey
1871 87f9ebf5 2020-01-15 tracey break;
1872 8087c3c5 2020-01-15 tracey default:
1873 8087c3c5 2020-01-15 tracey return NULL;
1874 9d84e7dd 2020-01-15 tracey }
1875 474370cb 2020-01-15 tracey
1876 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, commit_row);
1877 8087c3c5 2020-01-15 tracey
1878 8087c3c5 2020-01-15 tracey free(commit_parent);
1879 8087c3c5 2020-01-15 tracey free(commit_diff_disp);
1880 87f9ebf5 2020-01-15 tracey free(commit_tree_disp);
1881 8087c3c5 2020-01-15 tracey free(commit_age_diff);
1882 8087c3c5 2020-01-15 tracey free(commit_age_diff_disp);
1883 8087c3c5 2020-01-15 tracey free(commit_age_long);
1884 8087c3c5 2020-01-15 tracey free(commit_age_long_disp);
1885 474370cb 2020-01-15 tracey free(commit_author);
1886 8087c3c5 2020-01-15 tracey free(commit_author_disp);
1887 8087c3c5 2020-01-15 tracey free(commit_committer);
1888 8087c3c5 2020-01-15 tracey free(commit_committer_disp);
1889 9d84e7dd 2020-01-15 tracey free(commit_log0);
1890 474370cb 2020-01-15 tracey free(commit_row);
1891 8087c3c5 2020-01-15 tracey free(refs_str_disp);
1892 8087c3c5 2020-01-15 tracey free(refs_str);
1893 8087c3c5 2020-01-15 tracey refs_str = NULL;
1894 8087c3c5 2020-01-15 tracey free(id_str1);
1895 8087c3c5 2020-01-15 tracey id_str1 = NULL;
1896 8087c3c5 2020-01-15 tracey free(id_str2);
1897 8087c3c5 2020-01-15 tracey id_str2 = NULL;
1898 474370cb 2020-01-15 tracey
1899 474370cb 2020-01-15 tracey if (error || (limit && --limit == 0))
1900 474370cb 2020-01-15 tracey break;
1901 474370cb 2020-01-15 tracey }
1902 c6b62706 2020-01-15 tracey
1903 8087c3c5 2020-01-15 tracey if (error)
1904 8087c3c5 2020-01-15 tracey goto done;
1905 474370cb 2020-01-15 tracey
1906 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
1907 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
1908 6c6c85af 2020-01-15 tracey logs = strdup(buf_get(diffbuf));
1909 6c6c85af 2020-01-15 tracey }
1910 8087c3c5 2020-01-15 tracey done:
1911 8087c3c5 2020-01-15 tracey buf_free(diffbuf);
1912 8087c3c5 2020-01-15 tracey if (commit != NULL)
1913 8087c3c5 2020-01-15 tracey got_object_commit_close(commit);
1914 8087c3c5 2020-01-15 tracey if (search_pattern)
1915 8087c3c5 2020-01-15 tracey regfree(&regex);
1916 8087c3c5 2020-01-15 tracey if (graph)
1917 8087c3c5 2020-01-15 tracey got_commit_graph_close(graph);
1918 474370cb 2020-01-15 tracey if (repo) {
1919 474370cb 2020-01-15 tracey error = got_repo_close(repo);
1920 474370cb 2020-01-15 tracey if (error != NULL)
1921 474370cb 2020-01-15 tracey return NULL;
1922 474370cb 2020-01-15 tracey }
1923 8087c3c5 2020-01-15 tracey if (error) {
1924 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, "Error: ");
1925 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
1926 8087c3c5 2020-01-15 tracey return NULL;
1927 8087c3c5 2020-01-15 tracey } else
1928 8087c3c5 2020-01-15 tracey return logs;
1929 2c251c14 2020-01-15 tracey }
1930 2c251c14 2020-01-15 tracey
1931 2c251c14 2020-01-15 tracey static char *
1932 87f9ebf5 2020-01-15 tracey gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
1933 8d4d2453 2020-01-15 tracey {
1934 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
1935 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
1936 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
1937 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
1938 87f9ebf5 2020-01-15 tracey char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
1939 87f9ebf5 2020-01-15 tracey *age = NULL;
1940 87f9ebf5 2020-01-15 tracey char *newline;
1941 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
1942 87f9ebf5 2020-01-15 tracey size_t newsize;
1943 8d4d2453 2020-01-15 tracey
1944 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1945 87f9ebf5 2020-01-15 tracey if (error != NULL)
1946 6c6c85af 2020-01-15 tracey return NULL;
1947 6c6c85af 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1948 87f9ebf5 2020-01-15 tracey
1949 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1950 87f9ebf5 2020-01-15 tracey if (error != NULL)
1951 87f9ebf5 2020-01-15 tracey goto done;
1952 87f9ebf5 2020-01-15 tracey
1953 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
1954 87f9ebf5 2020-01-15 tracey if (error)
1955 87f9ebf5 2020-01-15 tracey goto done;
1956 87f9ebf5 2020-01-15 tracey
1957 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1958 87f9ebf5 2020-01-15 tracey const char *refname;
1959 87f9ebf5 2020-01-15 tracey char *refstr, *tag_log0, *tag_log, *id_str;
1960 87f9ebf5 2020-01-15 tracey time_t tagger_time;
1961 87f9ebf5 2020-01-15 tracey struct got_object_id *id;
1962 87f9ebf5 2020-01-15 tracey struct got_tag_object *tag;
1963 87f9ebf5 2020-01-15 tracey
1964 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1965 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/tags/", 10) != 0)
1966 87f9ebf5 2020-01-15 tracey continue;
1967 87f9ebf5 2020-01-15 tracey refname += 10;
1968 87f9ebf5 2020-01-15 tracey refstr = got_ref_to_str(re->ref);
1969 87f9ebf5 2020-01-15 tracey if (refstr == NULL) {
1970 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
1971 87f9ebf5 2020-01-15 tracey goto done;
1972 87f9ebf5 2020-01-15 tracey }
1973 87f9ebf5 2020-01-15 tracey
1974 87f9ebf5 2020-01-15 tracey error = got_ref_resolve(&id, repo, re->ref);
1975 87f9ebf5 2020-01-15 tracey if (error)
1976 87f9ebf5 2020-01-15 tracey goto done;
1977 87f9ebf5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
1978 87f9ebf5 2020-01-15 tracey free(id);
1979 87f9ebf5 2020-01-15 tracey if (error)
1980 87f9ebf5 2020-01-15 tracey goto done;
1981 87f9ebf5 2020-01-15 tracey
1982 87f9ebf5 2020-01-15 tracey tagger_time = got_object_tag_get_tagger_time(tag);
1983 87f9ebf5 2020-01-15 tracey
1984 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&id_str,
1985 87f9ebf5 2020-01-15 tracey got_object_tag_get_object_id(tag));
1986 87f9ebf5 2020-01-15 tracey if (error)
1987 87f9ebf5 2020-01-15 tracey goto done;
1988 87f9ebf5 2020-01-15 tracey
1989 87f9ebf5 2020-01-15 tracey tag_log0 = strdup(got_object_tag_get_message(tag));
1990 87f9ebf5 2020-01-15 tracey
1991 87f9ebf5 2020-01-15 tracey if (tag_log0 == NULL) {
1992 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("strdup");
1993 87f9ebf5 2020-01-15 tracey goto done;
1994 87f9ebf5 2020-01-15 tracey }
1995 87f9ebf5 2020-01-15 tracey
1996 87f9ebf5 2020-01-15 tracey tag_log = tag_log0;
1997 87f9ebf5 2020-01-15 tracey while (*tag_log == '\n')
1998 87f9ebf5 2020-01-15 tracey tag_log++;
1999 87f9ebf5 2020-01-15 tracey
2000 87f9ebf5 2020-01-15 tracey switch (tag_type) {
2001 87f9ebf5 2020-01-15 tracey case TAGBRIEF:
2002 87f9ebf5 2020-01-15 tracey newline = strchr(tag_log, '\n');
2003 87f9ebf5 2020-01-15 tracey if (newline)
2004 87f9ebf5 2020-01-15 tracey *newline = '\0';
2005 87f9ebf5 2020-01-15 tracey
2006 87f9ebf5 2020-01-15 tracey if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
2007 87f9ebf5 2020-01-15 tracey TM_DIFF))) == -1) {
2008 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2009 87f9ebf5 2020-01-15 tracey goto done;
2010 87f9ebf5 2020-01-15 tracey }
2011 87f9ebf5 2020-01-15 tracey
2012 87f9ebf5 2020-01-15 tracey if ((asprintf(&tags_navs_disp, tags_navs,
2013 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name,
2014 87f9ebf5 2020-01-15 tracey id_str, gw_trans->repo_name, id_str,
2015 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str)) == -1) {
2016 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2017 87f9ebf5 2020-01-15 tracey goto done;
2018 87f9ebf5 2020-01-15 tracey }
2019 87f9ebf5 2020-01-15 tracey
2020 87f9ebf5 2020-01-15 tracey if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
2021 87f9ebf5 2020-01-15 tracey tags_navs_disp)) == -1) {
2022 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2023 87f9ebf5 2020-01-15 tracey goto done;
2024 87f9ebf5 2020-01-15 tracey }
2025 87f9ebf5 2020-01-15 tracey
2026 87f9ebf5 2020-01-15 tracey free(tags_navs_disp);
2027 87f9ebf5 2020-01-15 tracey break;
2028 87f9ebf5 2020-01-15 tracey case TAGFULL:
2029 87f9ebf5 2020-01-15 tracey break;
2030 87f9ebf5 2020-01-15 tracey default:
2031 87f9ebf5 2020-01-15 tracey break;
2032 87f9ebf5 2020-01-15 tracey }
2033 87f9ebf5 2020-01-15 tracey
2034 6c6c85af 2020-01-15 tracey got_object_tag_close(tag);
2035 87f9ebf5 2020-01-15 tracey
2036 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tag_row);
2037 87f9ebf5 2020-01-15 tracey
2038 6c6c85af 2020-01-15 tracey free(id_str);
2039 6c6c85af 2020-01-15 tracey free(refstr);
2040 6c6c85af 2020-01-15 tracey free(age);
2041 87f9ebf5 2020-01-15 tracey free(tag_log0);
2042 87f9ebf5 2020-01-15 tracey free(tag_row);
2043 87f9ebf5 2020-01-15 tracey
2044 87f9ebf5 2020-01-15 tracey if (error || (limit && --limit == 0))
2045 87f9ebf5 2020-01-15 tracey break;
2046 87f9ebf5 2020-01-15 tracey }
2047 6c6c85af 2020-01-15 tracey
2048 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2049 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2050 6c6c85af 2020-01-15 tracey tags = strdup(buf_get(diffbuf));
2051 6c6c85af 2020-01-15 tracey }
2052 87f9ebf5 2020-01-15 tracey done:
2053 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
2054 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
2055 87f9ebf5 2020-01-15 tracey if (repo)
2056 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
2057 87f9ebf5 2020-01-15 tracey if (error)
2058 87f9ebf5 2020-01-15 tracey return NULL;
2059 87f9ebf5 2020-01-15 tracey else
2060 87f9ebf5 2020-01-15 tracey return tags;
2061 8d4d2453 2020-01-15 tracey }
2062 8d4d2453 2020-01-15 tracey
2063 8d4d2453 2020-01-15 tracey static char *
2064 8d4d2453 2020-01-15 tracey gw_get_repo_heads(struct trans *gw_trans)
2065 8d4d2453 2020-01-15 tracey {
2066 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
2067 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
2068 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
2069 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
2070 87f9ebf5 2020-01-15 tracey char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2071 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
2072 87f9ebf5 2020-01-15 tracey size_t newsize;
2073 8d4d2453 2020-01-15 tracey
2074 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2075 87f9ebf5 2020-01-15 tracey if (error != NULL)
2076 6c6c85af 2020-01-15 tracey return NULL;
2077 87f9ebf5 2020-01-15 tracey
2078 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2079 87f9ebf5 2020-01-15 tracey if (error != NULL)
2080 87f9ebf5 2020-01-15 tracey goto done;
2081 87f9ebf5 2020-01-15 tracey
2082 87f9ebf5 2020-01-15 tracey SIMPLEQ_INIT(&refs);
2083 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2084 87f9ebf5 2020-01-15 tracey NULL);
2085 87f9ebf5 2020-01-15 tracey if (error)
2086 87f9ebf5 2020-01-15 tracey goto done;
2087 87f9ebf5 2020-01-15 tracey
2088 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2089 87f9ebf5 2020-01-15 tracey char *refname;
2090 87f9ebf5 2020-01-15 tracey
2091 87f9ebf5 2020-01-15 tracey refname = strdup(got_ref_get_name(re->ref));
2092 87f9ebf5 2020-01-15 tracey if (refname == NULL) {
2093 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
2094 87f9ebf5 2020-01-15 tracey goto done;
2095 87f9ebf5 2020-01-15 tracey }
2096 87f9ebf5 2020-01-15 tracey
2097 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) != 0) {
2098 87f9ebf5 2020-01-15 tracey free(refname);
2099 87f9ebf5 2020-01-15 tracey continue;
2100 87f9ebf5 2020-01-15 tracey }
2101 87f9ebf5 2020-01-15 tracey
2102 87f9ebf5 2020-01-15 tracey age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2103 87f9ebf5 2020-01-15 tracey TM_DIFF);
2104 87f9ebf5 2020-01-15 tracey
2105 87f9ebf5 2020-01-15 tracey if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2106 87f9ebf5 2020-01-15 tracey refname, gw_trans->repo_name, refname,
2107 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, refname, gw_trans->repo_name,
2108 87f9ebf5 2020-01-15 tracey refname)) == -1) {
2109 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2110 87f9ebf5 2020-01-15 tracey goto done;
2111 87f9ebf5 2020-01-15 tracey }
2112 87f9ebf5 2020-01-15 tracey
2113 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
2114 87f9ebf5 2020-01-15 tracey refname += 11;
2115 87f9ebf5 2020-01-15 tracey
2116 87f9ebf5 2020-01-15 tracey if ((asprintf(&head_row, heads_row, age, refname,
2117 87f9ebf5 2020-01-15 tracey head_navs_disp)) == -1) {
2118 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2119 87f9ebf5 2020-01-15 tracey goto done;
2120 87f9ebf5 2020-01-15 tracey }
2121 87f9ebf5 2020-01-15 tracey
2122 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, head_row);
2123 87f9ebf5 2020-01-15 tracey
2124 87f9ebf5 2020-01-15 tracey free(head_navs_disp);
2125 87f9ebf5 2020-01-15 tracey free(head_row);
2126 87f9ebf5 2020-01-15 tracey }
2127 87f9ebf5 2020-01-15 tracey
2128 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2129 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2130 6c6c85af 2020-01-15 tracey heads = strdup(buf_get(diffbuf));
2131 6c6c85af 2020-01-15 tracey }
2132 87f9ebf5 2020-01-15 tracey done:
2133 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
2134 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
2135 87f9ebf5 2020-01-15 tracey if (repo)
2136 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
2137 87f9ebf5 2020-01-15 tracey if (error)
2138 87f9ebf5 2020-01-15 tracey return NULL;
2139 87f9ebf5 2020-01-15 tracey else
2140 87f9ebf5 2020-01-15 tracey return heads;
2141 8d4d2453 2020-01-15 tracey }
2142 8d4d2453 2020-01-15 tracey
2143 8d4d2453 2020-01-15 tracey static char *
2144 2c251c14 2020-01-15 tracey gw_get_got_link(struct trans *gw_trans)
2145 2c251c14 2020-01-15 tracey {
2146 2c251c14 2020-01-15 tracey char *link;
2147 2c251c14 2020-01-15 tracey
2148 2c251c14 2020-01-15 tracey if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2149 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_logo)) == -1)
2150 2c251c14 2020-01-15 tracey return NULL;
2151 2c251c14 2020-01-15 tracey
2152 2c251c14 2020-01-15 tracey return link;
2153 2c251c14 2020-01-15 tracey }
2154 2c251c14 2020-01-15 tracey
2155 2c251c14 2020-01-15 tracey static char *
2156 2c251c14 2020-01-15 tracey gw_get_site_link(struct trans *gw_trans)
2157 2c251c14 2020-01-15 tracey {
2158 2c251c14 2020-01-15 tracey char *link, *repo = "", *action = "";
2159 2c251c14 2020-01-15 tracey
2160 2c251c14 2020-01-15 tracey if (gw_trans->repo_name != NULL)
2161 2c251c14 2020-01-15 tracey if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2162 2c251c14 2020-01-15 tracey "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2163 2c251c14 2020-01-15 tracey return NULL;
2164 2c251c14 2020-01-15 tracey
2165 2c251c14 2020-01-15 tracey if (gw_trans->action_name != NULL)
2166 2c251c14 2020-01-15 tracey if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2167 2c251c14 2020-01-15 tracey return NULL;
2168 2c251c14 2020-01-15 tracey
2169 2c251c14 2020-01-15 tracey if ((asprintf(&link, site_link, GOTWEB,
2170 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2171 2c251c14 2020-01-15 tracey return NULL;
2172 2c251c14 2020-01-15 tracey
2173 2c251c14 2020-01-15 tracey return link;
2174 2c251c14 2020-01-15 tracey }
2175 2c251c14 2020-01-15 tracey
2176 2c251c14 2020-01-15 tracey static char *
2177 2c251c14 2020-01-15 tracey gw_html_escape(const char *html)
2178 2c251c14 2020-01-15 tracey {
2179 2c251c14 2020-01-15 tracey char *escaped_str = NULL, *buf;
2180 2c251c14 2020-01-15 tracey char c[1];
2181 6c6c85af 2020-01-15 tracey size_t sz, i, buff_sz = 2048;
2182 2c251c14 2020-01-15 tracey
2183 6c6c85af 2020-01-15 tracey if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2184 2c251c14 2020-01-15 tracey return NULL;
2185 2c251c14 2020-01-15 tracey
2186 2c251c14 2020-01-15 tracey if (html == NULL)
2187 2c251c14 2020-01-15 tracey return NULL;
2188 2c251c14 2020-01-15 tracey else
2189 2c251c14 2020-01-15 tracey if ((sz = strlen(html)) == 0)
2190 2c251c14 2020-01-15 tracey return NULL;
2191 2c251c14 2020-01-15 tracey
2192 6c6c85af 2020-01-15 tracey /* only work with buff_sz */
2193 6c6c85af 2020-01-15 tracey if (buff_sz < sz)
2194 6c6c85af 2020-01-15 tracey sz = buff_sz;
2195 2c251c14 2020-01-15 tracey
2196 2c251c14 2020-01-15 tracey for (i = 0; i < sz; i++) {
2197 2c251c14 2020-01-15 tracey c[0] = html[i];
2198 2c251c14 2020-01-15 tracey switch (c[0]) {
2199 2c251c14 2020-01-15 tracey case ('>'):
2200 2c251c14 2020-01-15 tracey strcat(buf, "&gt;");
2201 2c251c14 2020-01-15 tracey break;
2202 2c251c14 2020-01-15 tracey case ('&'):
2203 2c251c14 2020-01-15 tracey strcat(buf, "&amp;");
2204 2c251c14 2020-01-15 tracey break;
2205 2c251c14 2020-01-15 tracey case ('<'):
2206 2c251c14 2020-01-15 tracey strcat(buf, "&lt;");
2207 2c251c14 2020-01-15 tracey break;
2208 2c251c14 2020-01-15 tracey case ('"'):
2209 2c251c14 2020-01-15 tracey strcat(buf, "&quot;");
2210 2c251c14 2020-01-15 tracey break;
2211 2c251c14 2020-01-15 tracey case ('\''):
2212 2c251c14 2020-01-15 tracey strcat(buf, "&apos;");
2213 2c251c14 2020-01-15 tracey break;
2214 2c251c14 2020-01-15 tracey case ('\n'):
2215 2c251c14 2020-01-15 tracey strcat(buf, "<br />");
2216 8087c3c5 2020-01-15 tracey case ('|'):
2217 8087c3c5 2020-01-15 tracey strcat(buf, " ");
2218 2c251c14 2020-01-15 tracey default:
2219 2c251c14 2020-01-15 tracey strcat(buf, &c[0]);
2220 2c251c14 2020-01-15 tracey break;
2221 2c251c14 2020-01-15 tracey }
2222 2c251c14 2020-01-15 tracey }
2223 2c251c14 2020-01-15 tracey asprintf(&escaped_str, "%s", buf);
2224 2c251c14 2020-01-15 tracey free(buf);
2225 2c251c14 2020-01-15 tracey return escaped_str;
2226 2c251c14 2020-01-15 tracey }
2227 2c251c14 2020-01-15 tracey
2228 2c251c14 2020-01-15 tracey int
2229 2c251c14 2020-01-15 tracey main()
2230 2c251c14 2020-01-15 tracey {
2231 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
2232 2c251c14 2020-01-15 tracey struct trans *gw_trans;
2233 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
2234 2c251c14 2020-01-15 tracey const char *page = "index";
2235 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
2236 2c251c14 2020-01-15 tracey
2237 2c251c14 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2238 2c251c14 2020-01-15 tracey errx(1, "malloc");
2239 2c251c14 2020-01-15 tracey
2240 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2241 2c251c14 2020-01-15 tracey errx(1, "malloc");
2242 2c251c14 2020-01-15 tracey
2243 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2244 2c251c14 2020-01-15 tracey errx(1, "malloc");
2245 2c251c14 2020-01-15 tracey
2246 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2247 2c251c14 2020-01-15 tracey errx(1, "malloc");
2248 2c251c14 2020-01-15 tracey
2249 2c251c14 2020-01-15 tracey if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__MAX,
2250 2c251c14 2020-01-15 tracey &page, 1, 0))
2251 2c251c14 2020-01-15 tracey errx(1, "khttp_parse");
2252 2c251c14 2020-01-15 tracey
2253 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
2254 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
2255 4ceb8155 2020-01-15 tracey gw_malloc = 0;
2256 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
2257 2c251c14 2020-01-15 tracey goto err;
2258 2c251c14 2020-01-15 tracey }
2259 2c251c14 2020-01-15 tracey
2260 46b9c89b 2020-01-15 tracey if (pledge("stdio rpath proc exec sendfd unveil", NULL) == -1) {
2261 46b9c89b 2020-01-15 tracey error = got_error_from_errno("pledge");
2262 46b9c89b 2020-01-15 tracey goto err;
2263 46b9c89b 2020-01-15 tracey }
2264 46b9c89b 2020-01-15 tracey
2265 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
2266 2c251c14 2020-01-15 tracey
2267 2c251c14 2020-01-15 tracey gw_trans->page = 0;
2268 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
2269 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
2270 2c251c14 2020-01-15 tracey gw_trans->commit = NULL;
2271 8087c3c5 2020-01-15 tracey gw_trans->headref = strdup(GOT_REF_HEAD);
2272 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
2273 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->key = templs;
2274 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2275 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
2276 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
2277 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2278 2c251c14 2020-01-15 tracey
2279 2c251c14 2020-01-15 tracey err:
2280 2c251c14 2020-01-15 tracey if (error) {
2281 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
2282 2c251c14 2020-01-15 tracey gw_trans->action = GW_ERR;
2283 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
2284 2c251c14 2020-01-15 tracey goto done;
2285 2c251c14 2020-01-15 tracey }
2286 2c251c14 2020-01-15 tracey
2287 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
2288 2c251c14 2020-01-15 tracey if (error)
2289 2c251c14 2020-01-15 tracey goto err;
2290 2c251c14 2020-01-15 tracey
2291 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
2292 2c251c14 2020-01-15 tracey
2293 2c251c14 2020-01-15 tracey done:
2294 2c251c14 2020-01-15 tracey if (gw_malloc) {
2295 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
2296 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_www_path);
2297 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
2298 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
2299 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
2300 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
2301 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
2302 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
2303 2c251c14 2020-01-15 tracey free(gw_trans->commit);
2304 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
2305 2c251c14 2020-01-15 tracey free(gw_trans->repo_name);
2306 2c251c14 2020-01-15 tracey free(gw_trans->repo_file);
2307 2c251c14 2020-01-15 tracey free(gw_trans->action_name);
2308 8087c3c5 2020-01-15 tracey free(gw_trans->headref);
2309 2c251c14 2020-01-15 tracey
2310 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2311 2c251c14 2020-01-15 tracey free(dir->name);
2312 2c251c14 2020-01-15 tracey free(dir->description);
2313 2c251c14 2020-01-15 tracey free(dir->age);
2314 2c251c14 2020-01-15 tracey free(dir->url);
2315 2c251c14 2020-01-15 tracey free(dir->path);
2316 2c251c14 2020-01-15 tracey free(dir);
2317 2c251c14 2020-01-15 tracey }
2318 2c251c14 2020-01-15 tracey
2319 2c251c14 2020-01-15 tracey }
2320 2c251c14 2020-01-15 tracey
2321 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);
2322 2c251c14 2020-01-15 tracey return EXIT_SUCCESS;
2323 2c251c14 2020-01-15 tracey }