Blame


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