Blame


1 2c251c14 2020-01-15 tracey /*
2 9460dac0 2020-01-15 tracey * Copyright (c) 2019, 2020 Tracey Emery <tracey@traceyemery.net>
3 2c251c14 2020-01-15 tracey * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 2c251c14 2020-01-15 tracey *
5 2c251c14 2020-01-15 tracey * Permission to use, copy, modify, and distribute this software for any
6 2c251c14 2020-01-15 tracey * purpose with or without fee is hereby granted, provided that the above
7 2c251c14 2020-01-15 tracey * copyright notice and this permission notice appear in all copies.
8 2c251c14 2020-01-15 tracey *
9 2c251c14 2020-01-15 tracey * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 2c251c14 2020-01-15 tracey * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 2c251c14 2020-01-15 tracey * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 2c251c14 2020-01-15 tracey * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 2c251c14 2020-01-15 tracey * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 2c251c14 2020-01-15 tracey * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 2c251c14 2020-01-15 tracey * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 2c251c14 2020-01-15 tracey */
17 2c251c14 2020-01-15 tracey
18 2c251c14 2020-01-15 tracey #include <sys/queue.h>
19 2c251c14 2020-01-15 tracey #include <sys/stat.h>
20 2c251c14 2020-01-15 tracey #include <sys/types.h>
21 2c251c14 2020-01-15 tracey
22 2c251c14 2020-01-15 tracey #include <dirent.h>
23 2c251c14 2020-01-15 tracey #include <err.h>
24 474370cb 2020-01-15 tracey #include <regex.h>
25 2c251c14 2020-01-15 tracey #include <stdarg.h>
26 2c251c14 2020-01-15 tracey #include <stdbool.h>
27 2c251c14 2020-01-15 tracey #include <stdint.h>
28 2c251c14 2020-01-15 tracey #include <stdio.h>
29 2c251c14 2020-01-15 tracey #include <stdlib.h>
30 2c251c14 2020-01-15 tracey #include <string.h>
31 2c251c14 2020-01-15 tracey #include <unistd.h>
32 2c251c14 2020-01-15 tracey
33 2c251c14 2020-01-15 tracey #include <got_object.h>
34 2c251c14 2020-01-15 tracey #include <got_reference.h>
35 2c251c14 2020-01-15 tracey #include <got_repository.h>
36 2c251c14 2020-01-15 tracey #include <got_path.h>
37 2c251c14 2020-01-15 tracey #include <got_cancel.h>
38 2c251c14 2020-01-15 tracey #include <got_worktree.h>
39 2c251c14 2020-01-15 tracey #include <got_diff.h>
40 2c251c14 2020-01-15 tracey #include <got_commit_graph.h>
41 2c251c14 2020-01-15 tracey #include <got_blame.h>
42 2c251c14 2020-01-15 tracey #include <got_privsep.h>
43 2c251c14 2020-01-15 tracey #include <got_opentemp.h>
44 2c251c14 2020-01-15 tracey
45 2c251c14 2020-01-15 tracey #include <kcgi.h>
46 2c251c14 2020-01-15 tracey #include <kcgihtml.h>
47 2c251c14 2020-01-15 tracey
48 474370cb 2020-01-15 tracey #include "buf.h"
49 2c251c14 2020-01-15 tracey #include "gotweb.h"
50 2c251c14 2020-01-15 tracey #include "gotweb_ui.h"
51 2c251c14 2020-01-15 tracey
52 2c251c14 2020-01-15 tracey #ifndef nitems
53 2c251c14 2020-01-15 tracey #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 2c251c14 2020-01-15 tracey #endif
55 2c251c14 2020-01-15 tracey
56 2c251c14 2020-01-15 tracey struct trans {
57 2c251c14 2020-01-15 tracey TAILQ_HEAD(dirs, gw_dir) gw_dirs;
58 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir;
59 2c251c14 2020-01-15 tracey struct gotweb_conf *gw_conf;
60 2c251c14 2020-01-15 tracey struct ktemplate *gw_tmpl;
61 2c251c14 2020-01-15 tracey struct khtmlreq *gw_html_req;
62 2c251c14 2020-01-15 tracey struct kreq *gw_req;
63 2c251c14 2020-01-15 tracey char *repo_name;
64 2c251c14 2020-01-15 tracey char *repo_path;
65 2c251c14 2020-01-15 tracey char *commit;
66 2c251c14 2020-01-15 tracey char *repo_file;
67 ec46ccd7 2020-01-15 tracey char *repo_folder;
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_ACTION,
78 2c251c14 2020-01-15 tracey KEY_COMMIT_ID,
79 2c251c14 2020-01-15 tracey KEY_FILE,
80 ec46ccd7 2020-01-15 tracey KEY_FOLDER,
81 8087c3c5 2020-01-15 tracey KEY_HEADREF,
82 ec46ccd7 2020-01-15 tracey KEY_PAGE,
83 ec46ccd7 2020-01-15 tracey KEY_PATH,
84 ec46ccd7 2020-01-15 tracey KEY__ZMAX
85 2c251c14 2020-01-15 tracey };
86 2c251c14 2020-01-15 tracey
87 2c251c14 2020-01-15 tracey struct gw_dir {
88 2c251c14 2020-01-15 tracey TAILQ_ENTRY(gw_dir) entry;
89 2c251c14 2020-01-15 tracey char *name;
90 2c251c14 2020-01-15 tracey char *owner;
91 2c251c14 2020-01-15 tracey char *description;
92 2c251c14 2020-01-15 tracey char *url;
93 2c251c14 2020-01-15 tracey char *age;
94 2c251c14 2020-01-15 tracey char *path;
95 2c251c14 2020-01-15 tracey };
96 2c251c14 2020-01-15 tracey
97 2c251c14 2020-01-15 tracey enum tmpl {
98 2c251c14 2020-01-15 tracey TEMPL_HEAD,
99 2c251c14 2020-01-15 tracey TEMPL_HEADER,
100 2c251c14 2020-01-15 tracey TEMPL_SITEPATH,
101 2c251c14 2020-01-15 tracey TEMPL_SITEOWNER,
102 2c251c14 2020-01-15 tracey TEMPL_TITLE,
103 2c251c14 2020-01-15 tracey TEMPL_SEARCH,
104 2c251c14 2020-01-15 tracey TEMPL_CONTENT,
105 2c251c14 2020-01-15 tracey TEMPL__MAX
106 2c251c14 2020-01-15 tracey };
107 2c251c14 2020-01-15 tracey
108 387a29ba 2020-01-15 tracey enum ref_tm {
109 387a29ba 2020-01-15 tracey TM_DIFF,
110 387a29ba 2020-01-15 tracey TM_LONG,
111 387a29ba 2020-01-15 tracey };
112 387a29ba 2020-01-15 tracey
113 8087c3c5 2020-01-15 tracey enum logs {
114 8087c3c5 2020-01-15 tracey LOGBRIEF,
115 8087c3c5 2020-01-15 tracey LOGCOMMIT,
116 8087c3c5 2020-01-15 tracey LOGFULL,
117 8087c3c5 2020-01-15 tracey LOGTREE,
118 87f9ebf5 2020-01-15 tracey LOGDIFF,
119 87f9ebf5 2020-01-15 tracey LOGBLAME,
120 b772de24 2020-01-15 tracey LOGTAG,
121 8087c3c5 2020-01-15 tracey };
122 8087c3c5 2020-01-15 tracey
123 87f9ebf5 2020-01-15 tracey enum tags {
124 87f9ebf5 2020-01-15 tracey TAGBRIEF,
125 87f9ebf5 2020-01-15 tracey TAGFULL,
126 87f9ebf5 2020-01-15 tracey };
127 87f9ebf5 2020-01-15 tracey
128 474370cb 2020-01-15 tracey struct buf {
129 474370cb 2020-01-15 tracey u_char *cb_buf;
130 474370cb 2020-01-15 tracey size_t cb_size;
131 474370cb 2020-01-15 tracey size_t cb_len;
132 474370cb 2020-01-15 tracey };
133 474370cb 2020-01-15 tracey
134 2c251c14 2020-01-15 tracey static const char *const templs[TEMPL__MAX] = {
135 2c251c14 2020-01-15 tracey "head",
136 2c251c14 2020-01-15 tracey "header",
137 2c251c14 2020-01-15 tracey "sitepath",
138 2c251c14 2020-01-15 tracey "siteowner",
139 2c251c14 2020-01-15 tracey "title",
140 2c251c14 2020-01-15 tracey "search",
141 2c251c14 2020-01-15 tracey "content",
142 2c251c14 2020-01-15 tracey };
143 2c251c14 2020-01-15 tracey
144 ec46ccd7 2020-01-15 tracey static const struct kvalid gw_keys[KEY__ZMAX] = {
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 ec46ccd7 2020-01-15 tracey { kvalid_stringne, "folder" },
149 8087c3c5 2020-01-15 tracey { kvalid_stringne, "headref" },
150 ec46ccd7 2020-01-15 tracey { kvalid_int, "page" },
151 ec46ccd7 2020-01-15 tracey { kvalid_stringne, "path" },
152 2c251c14 2020-01-15 tracey };
153 65b95fb2 2020-01-15 tracey
154 65b95fb2 2020-01-15 tracey int gw_get_repo_log_count(struct trans *, char *);
155 2c251c14 2020-01-15 tracey
156 2c251c14 2020-01-15 tracey static struct gw_dir *gw_init_gw_dir(char *);
157 2c251c14 2020-01-15 tracey
158 2c251c14 2020-01-15 tracey static char *gw_get_repo_description(struct trans *,
159 2c251c14 2020-01-15 tracey char *);
160 2c251c14 2020-01-15 tracey static char *gw_get_repo_owner(struct trans *,
161 2c251c14 2020-01-15 tracey char *);
162 474370cb 2020-01-15 tracey static char *gw_get_time_str(time_t, int);
163 2c251c14 2020-01-15 tracey static char *gw_get_repo_age(struct trans *,
164 387a29ba 2020-01-15 tracey char *, char *, int);
165 4ceb8155 2020-01-15 tracey static char *gw_get_repo_log(struct trans *, const char *,
166 4ceb8155 2020-01-15 tracey char *, int, int);
167 ec46ccd7 2020-01-15 tracey static char *gw_get_file_blame(struct trans *, char *);
168 bcbc97d8 2020-01-15 tracey static char *gw_get_repo_tree(struct trans *, char *);
169 ec46ccd7 2020-01-15 tracey static char *gw_get_repo_diff(struct trans *, char *,
170 ec46ccd7 2020-01-15 tracey char *);
171 87f9ebf5 2020-01-15 tracey static char *gw_get_repo_tags(struct trans *, int, int);
172 8d4d2453 2020-01-15 tracey static char *gw_get_repo_heads(struct trans *);
173 2c251c14 2020-01-15 tracey static char *gw_get_clone_url(struct trans *, char *);
174 2c251c14 2020-01-15 tracey static char *gw_get_got_link(struct trans *);
175 2c251c14 2020-01-15 tracey static char *gw_get_site_link(struct trans *);
176 2c251c14 2020-01-15 tracey static char *gw_html_escape(const char *);
177 ec46ccd7 2020-01-15 tracey static char *color_diff_line(char *);
178 2c251c14 2020-01-15 tracey
179 2c251c14 2020-01-15 tracey static void gw_display_open(struct trans *, enum khttp,
180 2c251c14 2020-01-15 tracey enum kmime);
181 2c251c14 2020-01-15 tracey static void gw_display_index(struct trans *,
182 2c251c14 2020-01-15 tracey const struct got_error *);
183 2c251c14 2020-01-15 tracey
184 2c251c14 2020-01-15 tracey static int gw_template(size_t, void *);
185 2c251c14 2020-01-15 tracey
186 2c251c14 2020-01-15 tracey static const struct got_error* apply_unveil(const char *, const char *);
187 87f9ebf5 2020-01-15 tracey static const struct got_error* cmp_tags(void *, int *,
188 87f9ebf5 2020-01-15 tracey struct got_reference *,
189 87f9ebf5 2020-01-15 tracey struct got_reference *);
190 bcbc97d8 2020-01-15 tracey static const struct got_error* resolve_commit_arg(struct got_object_id **,
191 bcbc97d8 2020-01-15 tracey const char *, struct got_repository *);
192 ec46ccd7 2020-01-15 tracey static const struct got_error* match_object_id(struct got_object_id **,
193 ec46ccd7 2020-01-15 tracey char **, const char *r, int, int,
194 ec46ccd7 2020-01-15 tracey struct got_repository *);
195 ec46ccd7 2020-01-15 tracey static const struct got_error* blame_cb(void *, int, int,
196 ec46ccd7 2020-01-15 tracey struct got_object_id *);
197 2c251c14 2020-01-15 tracey static const struct got_error* gw_load_got_paths(struct trans *);
198 2c251c14 2020-01-15 tracey static const struct got_error* gw_load_got_path(struct trans *,
199 2c251c14 2020-01-15 tracey struct gw_dir *);
200 2c251c14 2020-01-15 tracey static const struct got_error* gw_parse_querystring(struct trans *);
201 474370cb 2020-01-15 tracey static const struct got_error* match_logmsg(int *, struct got_object_id *,
202 474370cb 2020-01-15 tracey struct got_commit_object *, regex_t *);
203 2c251c14 2020-01-15 tracey
204 2c251c14 2020-01-15 tracey static const struct got_error* gw_blame(struct trans *);
205 2c251c14 2020-01-15 tracey static const struct got_error* gw_commit(struct trans *);
206 8087c3c5 2020-01-15 tracey static const struct got_error* gw_commitdiff(struct trans *);
207 2c251c14 2020-01-15 tracey static const struct got_error* gw_index(struct trans *);
208 2c251c14 2020-01-15 tracey static const struct got_error* gw_log(struct trans *);
209 2c251c14 2020-01-15 tracey static const struct got_error* gw_raw(struct trans *);
210 4ceb8155 2020-01-15 tracey static const struct got_error* gw_logbriefs(struct trans *);
211 387a29ba 2020-01-15 tracey static const struct got_error* gw_summary(struct trans *);
212 b772de24 2020-01-15 tracey static const struct got_error* gw_tag(struct trans *);
213 2c251c14 2020-01-15 tracey static const struct got_error* gw_tree(struct trans *);
214 2c251c14 2020-01-15 tracey
215 2c251c14 2020-01-15 tracey struct gw_query_action {
216 2c251c14 2020-01-15 tracey unsigned int func_id;
217 2c251c14 2020-01-15 tracey const char *func_name;
218 2c251c14 2020-01-15 tracey const struct got_error *(*func_main)(struct trans *);
219 2c251c14 2020-01-15 tracey char *template;
220 2c251c14 2020-01-15 tracey };
221 2c251c14 2020-01-15 tracey
222 2c251c14 2020-01-15 tracey enum gw_query_actions {
223 2c251c14 2020-01-15 tracey GW_BLAME,
224 2c251c14 2020-01-15 tracey GW_COMMIT,
225 2c251c14 2020-01-15 tracey GW_COMMITDIFF,
226 2c251c14 2020-01-15 tracey GW_ERR,
227 2c251c14 2020-01-15 tracey GW_INDEX,
228 2c251c14 2020-01-15 tracey GW_LOG,
229 2c251c14 2020-01-15 tracey GW_RAW,
230 4ceb8155 2020-01-15 tracey GW_LOGBRIEFS,
231 2c251c14 2020-01-15 tracey GW_SUMMARY,
232 b772de24 2020-01-15 tracey GW_TAG,
233 b772de24 2020-01-15 tracey GW_TREE,
234 2c251c14 2020-01-15 tracey };
235 2c251c14 2020-01-15 tracey
236 2c251c14 2020-01-15 tracey static struct gw_query_action gw_query_funcs[] = {
237 2c251c14 2020-01-15 tracey { GW_BLAME, "blame", gw_blame, "gw_tmpl/index.tmpl" },
238 2c251c14 2020-01-15 tracey { GW_COMMIT, "commit", gw_commit, "gw_tmpl/index.tmpl" },
239 8087c3c5 2020-01-15 tracey { GW_COMMITDIFF, "commitdiff", gw_commitdiff, "gw_tmpl/index.tmpl" },
240 387a29ba 2020-01-15 tracey { GW_ERR, NULL, NULL, "gw_tmpl/index.tmpl" },
241 2c251c14 2020-01-15 tracey { GW_INDEX, "index", gw_index, "gw_tmpl/index.tmpl" },
242 2c251c14 2020-01-15 tracey { GW_LOG, "log", gw_log, "gw_tmpl/index.tmpl" },
243 2c251c14 2020-01-15 tracey { GW_RAW, "raw", gw_raw, "gw_tmpl/index.tmpl" },
244 4ceb8155 2020-01-15 tracey { GW_LOGBRIEFS, "logbriefs", gw_logbriefs, "gw_tmpl/index.tmpl" },
245 46b9c89b 2020-01-15 tracey { GW_SUMMARY, "summary", gw_summary, "gw_tmpl/index.tmpl" },
246 b772de24 2020-01-15 tracey { GW_TAG, "tag", gw_tag, "gw_tmpl/index.tmpl" },
247 2c251c14 2020-01-15 tracey { GW_TREE, "tree", gw_tree, "gw_tmpl/index.tmpl" },
248 2c251c14 2020-01-15 tracey };
249 2c251c14 2020-01-15 tracey
250 2c251c14 2020-01-15 tracey static const struct got_error *
251 2c251c14 2020-01-15 tracey apply_unveil(const char *repo_path, const char *repo_file)
252 2c251c14 2020-01-15 tracey {
253 2c251c14 2020-01-15 tracey const struct got_error *err;
254 2c251c14 2020-01-15 tracey
255 2c251c14 2020-01-15 tracey if (repo_path && repo_file) {
256 2c251c14 2020-01-15 tracey char *full_path;
257 2c251c14 2020-01-15 tracey if ((asprintf(&full_path, "%s/%s", repo_path, repo_file)) == -1)
258 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf unveil");
259 2c251c14 2020-01-15 tracey if (unveil(full_path, "r") != 0)
260 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", full_path);
261 2c251c14 2020-01-15 tracey }
262 2c251c14 2020-01-15 tracey
263 2c251c14 2020-01-15 tracey if (repo_path && unveil(repo_path, "r") != 0)
264 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", repo_path);
265 2c251c14 2020-01-15 tracey
266 2c251c14 2020-01-15 tracey if (unveil("/tmp", "rwc") != 0)
267 2c251c14 2020-01-15 tracey return got_error_from_errno2("unveil", "/tmp");
268 2c251c14 2020-01-15 tracey
269 2c251c14 2020-01-15 tracey err = got_privsep_unveil_exec_helpers();
270 2c251c14 2020-01-15 tracey if (err != NULL)
271 2c251c14 2020-01-15 tracey return err;
272 2c251c14 2020-01-15 tracey
273 2c251c14 2020-01-15 tracey if (unveil(NULL, NULL) != 0)
274 2c251c14 2020-01-15 tracey return got_error_from_errno("unveil");
275 2c251c14 2020-01-15 tracey
276 2c251c14 2020-01-15 tracey return NULL;
277 87f9ebf5 2020-01-15 tracey }
278 87f9ebf5 2020-01-15 tracey
279 87f9ebf5 2020-01-15 tracey static const struct got_error *
280 87f9ebf5 2020-01-15 tracey cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
281 87f9ebf5 2020-01-15 tracey struct got_reference *ref2)
282 87f9ebf5 2020-01-15 tracey {
283 87f9ebf5 2020-01-15 tracey const struct got_error *err = NULL;
284 87f9ebf5 2020-01-15 tracey struct got_repository *repo = arg;
285 87f9ebf5 2020-01-15 tracey struct got_object_id *id1, *id2 = NULL;
286 87f9ebf5 2020-01-15 tracey struct got_tag_object *tag1 = NULL, *tag2 = NULL;
287 87f9ebf5 2020-01-15 tracey time_t time1, time2;
288 87f9ebf5 2020-01-15 tracey
289 87f9ebf5 2020-01-15 tracey *cmp = 0;
290 87f9ebf5 2020-01-15 tracey
291 87f9ebf5 2020-01-15 tracey err = got_ref_resolve(&id1, repo, ref1);
292 87f9ebf5 2020-01-15 tracey if (err)
293 87f9ebf5 2020-01-15 tracey return err;
294 87f9ebf5 2020-01-15 tracey err = got_object_open_as_tag(&tag1, repo, id1);
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 err = got_ref_resolve(&id2, repo, ref2);
299 87f9ebf5 2020-01-15 tracey if (err)
300 87f9ebf5 2020-01-15 tracey goto done;
301 87f9ebf5 2020-01-15 tracey err = got_object_open_as_tag(&tag2, repo, id2);
302 87f9ebf5 2020-01-15 tracey if (err)
303 87f9ebf5 2020-01-15 tracey goto done;
304 87f9ebf5 2020-01-15 tracey
305 87f9ebf5 2020-01-15 tracey time1 = got_object_tag_get_tagger_time(tag1);
306 87f9ebf5 2020-01-15 tracey time2 = got_object_tag_get_tagger_time(tag2);
307 87f9ebf5 2020-01-15 tracey
308 87f9ebf5 2020-01-15 tracey /* Put latest tags first. */
309 87f9ebf5 2020-01-15 tracey if (time1 < time2)
310 87f9ebf5 2020-01-15 tracey *cmp = 1;
311 87f9ebf5 2020-01-15 tracey else if (time1 > time2)
312 87f9ebf5 2020-01-15 tracey *cmp = -1;
313 87f9ebf5 2020-01-15 tracey else
314 87f9ebf5 2020-01-15 tracey err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
315 87f9ebf5 2020-01-15 tracey done:
316 87f9ebf5 2020-01-15 tracey free(id1);
317 87f9ebf5 2020-01-15 tracey free(id2);
318 87f9ebf5 2020-01-15 tracey if (tag1)
319 87f9ebf5 2020-01-15 tracey got_object_tag_close(tag1);
320 87f9ebf5 2020-01-15 tracey if (tag2)
321 87f9ebf5 2020-01-15 tracey got_object_tag_close(tag2);
322 87f9ebf5 2020-01-15 tracey return err;
323 2c251c14 2020-01-15 tracey }
324 bcbc97d8 2020-01-15 tracey
325 bcbc97d8 2020-01-15 tracey static const struct got_error *
326 bcbc97d8 2020-01-15 tracey resolve_commit_arg(struct got_object_id **commit_id,
327 bcbc97d8 2020-01-15 tracey const char *commit_id_arg, struct got_repository *repo)
328 bcbc97d8 2020-01-15 tracey {
329 bcbc97d8 2020-01-15 tracey const struct got_error *err;
330 bcbc97d8 2020-01-15 tracey struct got_reference *ref;
331 bcbc97d8 2020-01-15 tracey struct got_tag_object *tag;
332 bcbc97d8 2020-01-15 tracey
333 bcbc97d8 2020-01-15 tracey err = got_repo_object_match_tag(&tag, commit_id_arg,
334 bcbc97d8 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT, repo);
335 bcbc97d8 2020-01-15 tracey if (err == NULL) {
336 bcbc97d8 2020-01-15 tracey *commit_id = got_object_id_dup(
337 bcbc97d8 2020-01-15 tracey got_object_tag_get_object_id(tag));
338 bcbc97d8 2020-01-15 tracey if (*commit_id == NULL)
339 bcbc97d8 2020-01-15 tracey err = got_error_from_errno("got_object_id_dup");
340 bcbc97d8 2020-01-15 tracey got_object_tag_close(tag);
341 bcbc97d8 2020-01-15 tracey return err;
342 bcbc97d8 2020-01-15 tracey } else if (err->code != GOT_ERR_NO_OBJ)
343 bcbc97d8 2020-01-15 tracey return err;
344 65b95fb2 2020-01-15 tracey
345 bcbc97d8 2020-01-15 tracey err = got_ref_open(&ref, repo, commit_id_arg, 0);
346 bcbc97d8 2020-01-15 tracey if (err == NULL) {
347 bcbc97d8 2020-01-15 tracey err = got_ref_resolve(commit_id, repo, ref);
348 bcbc97d8 2020-01-15 tracey got_ref_close(ref);
349 bcbc97d8 2020-01-15 tracey } else {
350 bcbc97d8 2020-01-15 tracey if (err->code != GOT_ERR_NOT_REF)
351 bcbc97d8 2020-01-15 tracey return err;
352 bcbc97d8 2020-01-15 tracey err = got_repo_match_object_id_prefix(commit_id,
353 bcbc97d8 2020-01-15 tracey commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
354 bcbc97d8 2020-01-15 tracey }
355 bcbc97d8 2020-01-15 tracey return err;
356 bcbc97d8 2020-01-15 tracey }
357 bcbc97d8 2020-01-15 tracey
358 ec46ccd7 2020-01-15 tracey static const struct got_error *
359 ec46ccd7 2020-01-15 tracey match_object_id(struct got_object_id **id, char **label,
360 ec46ccd7 2020-01-15 tracey const char *id_str, int obj_type, int resolve_tags,
361 ec46ccd7 2020-01-15 tracey struct got_repository *repo)
362 ec46ccd7 2020-01-15 tracey {
363 ec46ccd7 2020-01-15 tracey const struct got_error *err;
364 ec46ccd7 2020-01-15 tracey struct got_tag_object *tag;
365 ec46ccd7 2020-01-15 tracey struct got_reference *ref = NULL;
366 ec46ccd7 2020-01-15 tracey
367 ec46ccd7 2020-01-15 tracey *id = NULL;
368 ec46ccd7 2020-01-15 tracey *label = NULL;
369 ec46ccd7 2020-01-15 tracey
370 ec46ccd7 2020-01-15 tracey if (resolve_tags) {
371 ec46ccd7 2020-01-15 tracey err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
372 ec46ccd7 2020-01-15 tracey repo);
373 ec46ccd7 2020-01-15 tracey if (err == NULL) {
374 ec46ccd7 2020-01-15 tracey *id = got_object_id_dup(
375 ec46ccd7 2020-01-15 tracey got_object_tag_get_object_id(tag));
376 ec46ccd7 2020-01-15 tracey if (*id == NULL)
377 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("got_object_id_dup");
378 ec46ccd7 2020-01-15 tracey else if (asprintf(label, "refs/tags/%s",
379 ec46ccd7 2020-01-15 tracey got_object_tag_get_name(tag)) == -1) {
380 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("asprintf");
381 ec46ccd7 2020-01-15 tracey free(*id);
382 ec46ccd7 2020-01-15 tracey *id = NULL;
383 ec46ccd7 2020-01-15 tracey }
384 ec46ccd7 2020-01-15 tracey got_object_tag_close(tag);
385 ec46ccd7 2020-01-15 tracey return err;
386 ec46ccd7 2020-01-15 tracey } else if (err->code != GOT_ERR_NO_OBJ)
387 ec46ccd7 2020-01-15 tracey return err;
388 ec46ccd7 2020-01-15 tracey }
389 ec46ccd7 2020-01-15 tracey
390 ec46ccd7 2020-01-15 tracey err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
391 ec46ccd7 2020-01-15 tracey if (err) {
392 ec46ccd7 2020-01-15 tracey if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
393 ec46ccd7 2020-01-15 tracey return err;
394 ec46ccd7 2020-01-15 tracey err = got_ref_open(&ref, repo, id_str, 0);
395 ec46ccd7 2020-01-15 tracey if (err != NULL)
396 ec46ccd7 2020-01-15 tracey goto done;
397 ec46ccd7 2020-01-15 tracey *label = strdup(got_ref_get_name(ref));
398 ec46ccd7 2020-01-15 tracey if (*label == NULL) {
399 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("strdup");
400 ec46ccd7 2020-01-15 tracey goto done;
401 ec46ccd7 2020-01-15 tracey }
402 ec46ccd7 2020-01-15 tracey err = got_ref_resolve(id, repo, ref);
403 ec46ccd7 2020-01-15 tracey } else {
404 ec46ccd7 2020-01-15 tracey err = got_object_id_str(label, *id);
405 ec46ccd7 2020-01-15 tracey if (*label == NULL) {
406 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("strdup");
407 ec46ccd7 2020-01-15 tracey goto done;
408 ec46ccd7 2020-01-15 tracey }
409 ec46ccd7 2020-01-15 tracey }
410 ec46ccd7 2020-01-15 tracey done:
411 ec46ccd7 2020-01-15 tracey if (ref)
412 ec46ccd7 2020-01-15 tracey got_ref_close(ref);
413 ec46ccd7 2020-01-15 tracey return err;
414 ec46ccd7 2020-01-15 tracey }
415 ec46ccd7 2020-01-15 tracey
416 65b95fb2 2020-01-15 tracey int
417 65b95fb2 2020-01-15 tracey gw_get_repo_log_count(struct trans *gw_trans, char *start_commit)
418 65b95fb2 2020-01-15 tracey {
419 65b95fb2 2020-01-15 tracey const struct got_error *error;
420 65b95fb2 2020-01-15 tracey struct got_repository *repo = NULL;
421 65b95fb2 2020-01-15 tracey struct got_reflist_head refs;
422 65b95fb2 2020-01-15 tracey struct got_commit_object *commit = NULL;
423 65b95fb2 2020-01-15 tracey struct got_object_id *id = NULL;
424 65b95fb2 2020-01-15 tracey struct got_commit_graph *graph = NULL;
425 65b95fb2 2020-01-15 tracey char *in_repo_path = NULL, *path = NULL;
426 65b95fb2 2020-01-15 tracey int log_count = 0;
427 65b95fb2 2020-01-15 tracey
428 65b95fb2 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
429 ec46ccd7 2020-01-15 tracey if (error)
430 65b95fb2 2020-01-15 tracey return 0;
431 65b95fb2 2020-01-15 tracey
432 65b95fb2 2020-01-15 tracey SIMPLEQ_INIT(&refs);
433 65b95fb2 2020-01-15 tracey
434 65b95fb2 2020-01-15 tracey if (start_commit == NULL) {
435 65b95fb2 2020-01-15 tracey struct got_reference *head_ref;
436 65b95fb2 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
437 ec46ccd7 2020-01-15 tracey if (error)
438 65b95fb2 2020-01-15 tracey goto done;
439 65b95fb2 2020-01-15 tracey
440 65b95fb2 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
441 65b95fb2 2020-01-15 tracey got_ref_close(head_ref);
442 ec46ccd7 2020-01-15 tracey if (error)
443 65b95fb2 2020-01-15 tracey goto done;
444 65b95fb2 2020-01-15 tracey
445 65b95fb2 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
446 65b95fb2 2020-01-15 tracey } else {
447 65b95fb2 2020-01-15 tracey struct got_reference *ref;
448 65b95fb2 2020-01-15 tracey error = got_ref_open(&ref, repo, start_commit, 0);
449 65b95fb2 2020-01-15 tracey if (error == NULL) {
450 65b95fb2 2020-01-15 tracey int obj_type;
451 65b95fb2 2020-01-15 tracey error = got_ref_resolve(&id, repo, ref);
452 65b95fb2 2020-01-15 tracey got_ref_close(ref);
453 ec46ccd7 2020-01-15 tracey if (error)
454 65b95fb2 2020-01-15 tracey goto done;
455 65b95fb2 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, id);
456 ec46ccd7 2020-01-15 tracey if (error)
457 65b95fb2 2020-01-15 tracey goto done;
458 65b95fb2 2020-01-15 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
459 65b95fb2 2020-01-15 tracey struct got_tag_object *tag;
460 65b95fb2 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
461 ec46ccd7 2020-01-15 tracey if (error)
462 65b95fb2 2020-01-15 tracey goto done;
463 65b95fb2 2020-01-15 tracey if (got_object_tag_get_object_type(tag) !=
464 65b95fb2 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT) {
465 65b95fb2 2020-01-15 tracey got_object_tag_close(tag);
466 65b95fb2 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
467 65b95fb2 2020-01-15 tracey goto done;
468 65b95fb2 2020-01-15 tracey }
469 65b95fb2 2020-01-15 tracey free(id);
470 65b95fb2 2020-01-15 tracey id = got_object_id_dup(
471 65b95fb2 2020-01-15 tracey got_object_tag_get_object_id(tag));
472 65b95fb2 2020-01-15 tracey if (id == NULL)
473 65b95fb2 2020-01-15 tracey error = got_error_from_errno(
474 65b95fb2 2020-01-15 tracey "got_object_id_dup");
475 65b95fb2 2020-01-15 tracey got_object_tag_close(tag);
476 65b95fb2 2020-01-15 tracey if (error)
477 65b95fb2 2020-01-15 tracey goto done;
478 65b95fb2 2020-01-15 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
479 65b95fb2 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
480 65b95fb2 2020-01-15 tracey goto done;
481 65b95fb2 2020-01-15 tracey }
482 65b95fb2 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
483 ec46ccd7 2020-01-15 tracey if (error)
484 65b95fb2 2020-01-15 tracey goto done;
485 65b95fb2 2020-01-15 tracey }
486 65b95fb2 2020-01-15 tracey if (commit == NULL) {
487 65b95fb2 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id,
488 65b95fb2 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
489 ec46ccd7 2020-01-15 tracey if (error)
490 65b95fb2 2020-01-15 tracey goto done;
491 65b95fb2 2020-01-15 tracey }
492 65b95fb2 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id,
493 65b95fb2 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
494 ec46ccd7 2020-01-15 tracey if (error)
495 65b95fb2 2020-01-15 tracey goto done;
496 65b95fb2 2020-01-15 tracey }
497 2c251c14 2020-01-15 tracey
498 65b95fb2 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
499 ec46ccd7 2020-01-15 tracey if (error)
500 65b95fb2 2020-01-15 tracey goto done;
501 65b95fb2 2020-01-15 tracey
502 65b95fb2 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
503 ec46ccd7 2020-01-15 tracey if (error)
504 65b95fb2 2020-01-15 tracey goto done;
505 65b95fb2 2020-01-15 tracey
506 65b95fb2 2020-01-15 tracey if (in_repo_path) {
507 65b95fb2 2020-01-15 tracey free(path);
508 65b95fb2 2020-01-15 tracey path = in_repo_path;
509 65b95fb2 2020-01-15 tracey }
510 65b95fb2 2020-01-15 tracey
511 65b95fb2 2020-01-15 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
512 65b95fb2 2020-01-15 tracey if (error)
513 65b95fb2 2020-01-15 tracey goto done;
514 65b95fb2 2020-01-15 tracey
515 65b95fb2 2020-01-15 tracey error = got_commit_graph_open(&graph, path, 0);
516 65b95fb2 2020-01-15 tracey if (error)
517 65b95fb2 2020-01-15 tracey goto done;
518 65b95fb2 2020-01-15 tracey
519 65b95fb2 2020-01-15 tracey error = got_commit_graph_iter_start(graph, id, repo, NULL, NULL);
520 65b95fb2 2020-01-15 tracey if (error)
521 65b95fb2 2020-01-15 tracey goto done;
522 65b95fb2 2020-01-15 tracey
523 65b95fb2 2020-01-15 tracey for (;;) {
524 65b95fb2 2020-01-15 tracey error = got_commit_graph_iter_next(&id, graph, repo, NULL,
525 65b95fb2 2020-01-15 tracey NULL);
526 65b95fb2 2020-01-15 tracey if (error) {
527 65b95fb2 2020-01-15 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
528 65b95fb2 2020-01-15 tracey error = NULL;
529 65b95fb2 2020-01-15 tracey break;
530 65b95fb2 2020-01-15 tracey }
531 65b95fb2 2020-01-15 tracey if (id == NULL)
532 65b95fb2 2020-01-15 tracey break;
533 65b95fb2 2020-01-15 tracey
534 65b95fb2 2020-01-15 tracey if (error)
535 65b95fb2 2020-01-15 tracey break;
536 65b95fb2 2020-01-15 tracey log_count++;
537 65b95fb2 2020-01-15 tracey }
538 65b95fb2 2020-01-15 tracey done:
539 2e676fc5 2020-01-15 tracey free(in_repo_path);
540 65b95fb2 2020-01-15 tracey if (graph)
541 65b95fb2 2020-01-15 tracey got_commit_graph_close(graph);
542 65b95fb2 2020-01-15 tracey if (repo) {
543 65b95fb2 2020-01-15 tracey error = got_repo_close(repo);
544 ec46ccd7 2020-01-15 tracey if (error)
545 65b95fb2 2020-01-15 tracey return 0;
546 65b95fb2 2020-01-15 tracey }
547 65b95fb2 2020-01-15 tracey if (error) {
548 65b95fb2 2020-01-15 tracey khttp_puts(gw_trans->gw_req, "Error: ");
549 65b95fb2 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
550 65b95fb2 2020-01-15 tracey return 0;
551 65b95fb2 2020-01-15 tracey } else
552 65b95fb2 2020-01-15 tracey return log_count;
553 65b95fb2 2020-01-15 tracey }
554 65b95fb2 2020-01-15 tracey
555 2c251c14 2020-01-15 tracey static const struct got_error *
556 2c251c14 2020-01-15 tracey gw_blame(struct trans *gw_trans)
557 2c251c14 2020-01-15 tracey {
558 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
559 2c251c14 2020-01-15 tracey
560 ec46ccd7 2020-01-15 tracey char *log, *log_html;
561 ec46ccd7 2020-01-15 tracey
562 ec46ccd7 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
563 ec46ccd7 2020-01-15 tracey if (error)
564 ec46ccd7 2020-01-15 tracey return error;
565 ec46ccd7 2020-01-15 tracey
566 ec46ccd7 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGBLAME);
567 ec46ccd7 2020-01-15 tracey
568 ec46ccd7 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
569 2e676fc5 2020-01-15 tracey if ((asprintf(&log_html, log_blame, log)) == -1)
570 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
571 ec46ccd7 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
572 ec46ccd7 2020-01-15 tracey free(log_html);
573 ec46ccd7 2020-01-15 tracey free(log);
574 ec46ccd7 2020-01-15 tracey }
575 2c251c14 2020-01-15 tracey return error;
576 2c251c14 2020-01-15 tracey }
577 2c251c14 2020-01-15 tracey
578 2c251c14 2020-01-15 tracey static const struct got_error *
579 2c251c14 2020-01-15 tracey gw_commit(struct trans *gw_trans)
580 2c251c14 2020-01-15 tracey {
581 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
582 8087c3c5 2020-01-15 tracey char *log, *log_html;
583 2c251c14 2020-01-15 tracey
584 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
585 8087c3c5 2020-01-15 tracey if (error)
586 8087c3c5 2020-01-15 tracey return error;
587 8087c3c5 2020-01-15 tracey
588 8087c3c5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGCOMMIT);
589 8087c3c5 2020-01-15 tracey
590 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
591 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, log_commit, log)) == -1)
592 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
593 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
594 8087c3c5 2020-01-15 tracey free(log_html);
595 8087c3c5 2020-01-15 tracey free(log);
596 8087c3c5 2020-01-15 tracey }
597 2c251c14 2020-01-15 tracey return error;
598 2c251c14 2020-01-15 tracey }
599 2c251c14 2020-01-15 tracey
600 2c251c14 2020-01-15 tracey static const struct got_error *
601 8087c3c5 2020-01-15 tracey gw_commitdiff(struct trans *gw_trans)
602 2204c934 2020-01-15 tracey {
603 2204c934 2020-01-15 tracey const struct got_error *error = NULL;
604 87f9ebf5 2020-01-15 tracey char *log, *log_html;
605 87f9ebf5 2020-01-15 tracey
606 87f9ebf5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
607 87f9ebf5 2020-01-15 tracey if (error)
608 87f9ebf5 2020-01-15 tracey return error;
609 2204c934 2020-01-15 tracey
610 87f9ebf5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGDIFF);
611 87f9ebf5 2020-01-15 tracey
612 87f9ebf5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
613 87f9ebf5 2020-01-15 tracey if ((asprintf(&log_html, log_diff, log)) == -1)
614 87f9ebf5 2020-01-15 tracey return got_error_from_errno("asprintf");
615 87f9ebf5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
616 87f9ebf5 2020-01-15 tracey free(log_html);
617 87f9ebf5 2020-01-15 tracey free(log);
618 87f9ebf5 2020-01-15 tracey }
619 2204c934 2020-01-15 tracey return error;
620 2204c934 2020-01-15 tracey }
621 2204c934 2020-01-15 tracey
622 2204c934 2020-01-15 tracey static const struct got_error *
623 2c251c14 2020-01-15 tracey gw_index(struct trans *gw_trans)
624 2c251c14 2020-01-15 tracey {
625 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
626 46b9c89b 2020-01-15 tracey struct gw_dir *gw_dir = NULL;
627 2c251c14 2020-01-15 tracey char *html, *navs, *next, *prev;
628 387a29ba 2020-01-15 tracey unsigned int prev_disp = 0, next_disp = 1, dir_c = 0;
629 2c251c14 2020-01-15 tracey
630 46b9c89b 2020-01-15 tracey error = apply_unveil(gw_trans->gw_conf->got_repos_path, NULL);
631 46b9c89b 2020-01-15 tracey if (error)
632 46b9c89b 2020-01-15 tracey return error;
633 46b9c89b 2020-01-15 tracey
634 2c251c14 2020-01-15 tracey error = gw_load_got_paths(gw_trans);
635 46b9c89b 2020-01-15 tracey if (error)
636 2c251c14 2020-01-15 tracey return error;
637 2c251c14 2020-01-15 tracey
638 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, index_projects_header);
639 2c251c14 2020-01-15 tracey
640 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry)
641 2c251c14 2020-01-15 tracey dir_c++;
642 2c251c14 2020-01-15 tracey
643 46b9c89b 2020-01-15 tracey TAILQ_FOREACH(gw_dir, &gw_trans->gw_dirs, entry) {
644 2c251c14 2020-01-15 tracey if (gw_trans->page > 0 && (gw_trans->page *
645 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) > prev_disp) {
646 2c251c14 2020-01-15 tracey prev_disp++;
647 2c251c14 2020-01-15 tracey continue;
648 2c251c14 2020-01-15 tracey }
649 2c251c14 2020-01-15 tracey
650 2c251c14 2020-01-15 tracey prev_disp++;
651 46b9c89b 2020-01-15 tracey if((asprintf(&navs, index_navs, gw_dir->name, gw_dir->name,
652 46b9c89b 2020-01-15 tracey gw_dir->name, gw_dir->name)) == -1)
653 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
654 2c251c14 2020-01-15 tracey
655 46b9c89b 2020-01-15 tracey if ((asprintf(&html, index_projects, gw_dir->name, gw_dir->name,
656 46b9c89b 2020-01-15 tracey gw_dir->description, gw_dir->owner, gw_dir->age,
657 46b9c89b 2020-01-15 tracey navs)) == -1)
658 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
659 2c251c14 2020-01-15 tracey
660 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, html);
661 2c251c14 2020-01-15 tracey
662 2c251c14 2020-01-15 tracey free(navs);
663 2c251c14 2020-01-15 tracey free(html);
664 2c251c14 2020-01-15 tracey
665 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display == 0)
666 2c251c14 2020-01-15 tracey continue;
667 2c251c14 2020-01-15 tracey
668 2c251c14 2020-01-15 tracey if (next_disp == gw_trans->gw_conf->got_max_repos_display)
669 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, np_wrapper_start);
670 2c251c14 2020-01-15 tracey else if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
671 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
672 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
673 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total))
674 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, np_wrapper_start);
675 2c251c14 2020-01-15 tracey
676 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
677 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
678 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
679 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total)) {
680 2c251c14 2020-01-15 tracey if ((asprintf(&prev, nav_prev,
681 2c251c14 2020-01-15 tracey gw_trans->page - 1)) == -1)
682 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
683 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, prev);
684 2c251c14 2020-01-15 tracey free(prev);
685 2c251c14 2020-01-15 tracey }
686 2c251c14 2020-01-15 tracey
687 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
688 2c251c14 2020-01-15 tracey
689 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos_display > 0 &&
690 2c251c14 2020-01-15 tracey next_disp == gw_trans->gw_conf->got_max_repos_display &&
691 2c251c14 2020-01-15 tracey dir_c != (gw_trans->page + 1) *
692 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_max_repos_display) {
693 2c251c14 2020-01-15 tracey if ((asprintf(&next, nav_next,
694 2c251c14 2020-01-15 tracey gw_trans->page + 1)) == -1)
695 2c251c14 2020-01-15 tracey return got_error_from_errno("calloc");
696 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, next);
697 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
698 2c251c14 2020-01-15 tracey free(next);
699 2c251c14 2020-01-15 tracey next_disp = 0;
700 2c251c14 2020-01-15 tracey break;
701 2c251c14 2020-01-15 tracey }
702 2c251c14 2020-01-15 tracey
703 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf->got_max_repos_display > 0) &&
704 2c251c14 2020-01-15 tracey (gw_trans->page > 0) &&
705 2c251c14 2020-01-15 tracey (next_disp == gw_trans->gw_conf->got_max_repos_display ||
706 2c251c14 2020-01-15 tracey prev_disp == gw_trans->repos_total))
707 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
708 2c251c14 2020-01-15 tracey
709 2c251c14 2020-01-15 tracey next_disp++;
710 2c251c14 2020-01-15 tracey }
711 2c251c14 2020-01-15 tracey return error;
712 2c251c14 2020-01-15 tracey }
713 2c251c14 2020-01-15 tracey
714 2c251c14 2020-01-15 tracey static const struct got_error *
715 2c251c14 2020-01-15 tracey gw_log(struct trans *gw_trans)
716 2c251c14 2020-01-15 tracey {
717 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
718 4ceb8155 2020-01-15 tracey char *log, *log_html;
719 8087c3c5 2020-01-15 tracey
720 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
721 8087c3c5 2020-01-15 tracey if (error)
722 8087c3c5 2020-01-15 tracey return error;
723 2c251c14 2020-01-15 tracey
724 87f9ebf5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
725 8087c3c5 2020-01-15 tracey gw_trans->gw_conf->got_max_commits_display, LOGFULL);
726 4ceb8155 2020-01-15 tracey
727 4ceb8155 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
728 4ceb8155 2020-01-15 tracey if ((asprintf(&log_html, logs, log)) == -1)
729 4ceb8155 2020-01-15 tracey return got_error_from_errno("asprintf");
730 4ceb8155 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
731 4ceb8155 2020-01-15 tracey free(log_html);
732 4ceb8155 2020-01-15 tracey free(log);
733 4ceb8155 2020-01-15 tracey }
734 2c251c14 2020-01-15 tracey return error;
735 2c251c14 2020-01-15 tracey }
736 2c251c14 2020-01-15 tracey
737 2c251c14 2020-01-15 tracey static const struct got_error *
738 2c251c14 2020-01-15 tracey gw_raw(struct trans *gw_trans)
739 2c251c14 2020-01-15 tracey {
740 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
741 2c251c14 2020-01-15 tracey
742 2c251c14 2020-01-15 tracey return error;
743 2c251c14 2020-01-15 tracey }
744 2c251c14 2020-01-15 tracey
745 2c251c14 2020-01-15 tracey static const struct got_error *
746 4ceb8155 2020-01-15 tracey gw_logbriefs(struct trans *gw_trans)
747 2c251c14 2020-01-15 tracey {
748 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
749 8087c3c5 2020-01-15 tracey char *log, *log_html;
750 17a96b9f 2020-01-15 tracey
751 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
752 8087c3c5 2020-01-15 tracey if (error)
753 8087c3c5 2020-01-15 tracey return error;
754 9d84e7dd 2020-01-15 tracey
755 87f9ebf5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit,
756 8087c3c5 2020-01-15 tracey gw_trans->gw_conf->got_max_commits_display, LOGBRIEF);
757 8087c3c5 2020-01-15 tracey
758 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
759 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, summary_logbriefs,
760 8087c3c5 2020-01-15 tracey log)) == -1)
761 9d84e7dd 2020-01-15 tracey return got_error_from_errno("asprintf");
762 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
763 8087c3c5 2020-01-15 tracey free(log_html);
764 8087c3c5 2020-01-15 tracey free(log);
765 9d84e7dd 2020-01-15 tracey }
766 2c251c14 2020-01-15 tracey return error;
767 2c251c14 2020-01-15 tracey }
768 2c251c14 2020-01-15 tracey
769 2c251c14 2020-01-15 tracey static const struct got_error *
770 387a29ba 2020-01-15 tracey gw_summary(struct trans *gw_trans)
771 387a29ba 2020-01-15 tracey {
772 387a29ba 2020-01-15 tracey const struct got_error *error = NULL;
773 46b9c89b 2020-01-15 tracey char *description_html, *repo_owner_html, *repo_age_html,
774 8087c3c5 2020-01-15 tracey *cloneurl_html, *log, *log_html, *tags, *heads, *tags_html,
775 8087c3c5 2020-01-15 tracey *heads_html, *age;
776 387a29ba 2020-01-15 tracey
777 46b9c89b 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
778 46b9c89b 2020-01-15 tracey if (error)
779 46b9c89b 2020-01-15 tracey return error;
780 46b9c89b 2020-01-15 tracey
781 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, summary_wrapper);
782 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description) {
783 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->description != NULL &&
784 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->description, "") != 0)) {
785 46b9c89b 2020-01-15 tracey if ((asprintf(&description_html, description,
786 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->description)) == -1)
787 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
788 46b9c89b 2020-01-15 tracey
789 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, description_html);
790 46b9c89b 2020-01-15 tracey free(description_html);
791 46b9c89b 2020-01-15 tracey }
792 46b9c89b 2020-01-15 tracey }
793 46b9c89b 2020-01-15 tracey
794 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner) {
795 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->owner != NULL &&
796 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->owner, "") != 0)) {
797 46b9c89b 2020-01-15 tracey if ((asprintf(&repo_owner_html, repo_owner,
798 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->owner)) == -1)
799 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
800 46b9c89b 2020-01-15 tracey
801 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, repo_owner_html);
802 46b9c89b 2020-01-15 tracey free(repo_owner_html);
803 46b9c89b 2020-01-15 tracey }
804 46b9c89b 2020-01-15 tracey }
805 46b9c89b 2020-01-15 tracey
806 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age) {
807 c6b62706 2020-01-15 tracey age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path,
808 c6b62706 2020-01-15 tracey "refs/heads", TM_LONG);
809 c6b62706 2020-01-15 tracey if (age != NULL && (strcmp(age, "") != 0)) {
810 c6b62706 2020-01-15 tracey if ((asprintf(&repo_age_html, last_change, age)) == -1)
811 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
812 46b9c89b 2020-01-15 tracey
813 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, repo_age_html);
814 46b9c89b 2020-01-15 tracey free(repo_age_html);
815 c6b62706 2020-01-15 tracey free(age);
816 46b9c89b 2020-01-15 tracey }
817 46b9c89b 2020-01-15 tracey }
818 46b9c89b 2020-01-15 tracey
819 46b9c89b 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_cloneurl) {
820 46b9c89b 2020-01-15 tracey if (gw_trans->gw_dir->url != NULL &&
821 46b9c89b 2020-01-15 tracey (strcmp(gw_trans->gw_dir->url, "") != 0)) {
822 46b9c89b 2020-01-15 tracey if ((asprintf(&cloneurl_html, cloneurl,
823 46b9c89b 2020-01-15 tracey gw_trans->gw_dir->url)) == -1)
824 46b9c89b 2020-01-15 tracey return got_error_from_errno("asprintf");
825 46b9c89b 2020-01-15 tracey
826 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, cloneurl_html);
827 46b9c89b 2020-01-15 tracey free(cloneurl_html);
828 46b9c89b 2020-01-15 tracey }
829 46b9c89b 2020-01-15 tracey }
830 46b9c89b 2020-01-15 tracey khttp_puts(gw_trans->gw_req, div_end);
831 46b9c89b 2020-01-15 tracey
832 8087c3c5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, NULL, D_MAXSLCOMMDISP, 0);
833 87f9ebf5 2020-01-15 tracey tags = gw_get_repo_tags(gw_trans, D_MAXSLCOMMDISP, TAGBRIEF);
834 8d4d2453 2020-01-15 tracey heads = gw_get_repo_heads(gw_trans);
835 387a29ba 2020-01-15 tracey
836 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
837 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, summary_logbriefs,
838 8087c3c5 2020-01-15 tracey log)) == -1)
839 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
840 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
841 8087c3c5 2020-01-15 tracey free(log_html);
842 8087c3c5 2020-01-15 tracey free(log);
843 8d4d2453 2020-01-15 tracey }
844 8d4d2453 2020-01-15 tracey
845 8d4d2453 2020-01-15 tracey if (tags != NULL && strcmp(tags, "") != 0) {
846 8d4d2453 2020-01-15 tracey if ((asprintf(&tags_html, summary_tags,
847 8d4d2453 2020-01-15 tracey tags)) == -1)
848 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
849 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, tags_html);
850 8d4d2453 2020-01-15 tracey free(tags_html);
851 8d4d2453 2020-01-15 tracey free(tags);
852 8d4d2453 2020-01-15 tracey }
853 8d4d2453 2020-01-15 tracey
854 8d4d2453 2020-01-15 tracey if (heads != NULL && strcmp(heads, "") != 0) {
855 8d4d2453 2020-01-15 tracey if ((asprintf(&heads_html, summary_heads,
856 8d4d2453 2020-01-15 tracey heads)) == -1)
857 8d4d2453 2020-01-15 tracey return got_error_from_errno("asprintf");
858 8d4d2453 2020-01-15 tracey khttp_puts(gw_trans->gw_req, heads_html);
859 8d4d2453 2020-01-15 tracey free(heads_html);
860 8d4d2453 2020-01-15 tracey free(heads);
861 8d4d2453 2020-01-15 tracey }
862 2204c934 2020-01-15 tracey return error;
863 2204c934 2020-01-15 tracey }
864 2204c934 2020-01-15 tracey
865 2204c934 2020-01-15 tracey static const struct got_error *
866 b772de24 2020-01-15 tracey gw_tag(struct trans *gw_trans)
867 b772de24 2020-01-15 tracey {
868 b772de24 2020-01-15 tracey const struct got_error *error = NULL;
869 b772de24 2020-01-15 tracey char *log, *log_html;
870 b772de24 2020-01-15 tracey
871 b772de24 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
872 b772de24 2020-01-15 tracey if (error)
873 b772de24 2020-01-15 tracey return error;
874 b772de24 2020-01-15 tracey
875 b772de24 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTAG);
876 b772de24 2020-01-15 tracey
877 b772de24 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
878 9460dac0 2020-01-15 tracey if ((asprintf(&log_html, log_tag, log)) == -1)
879 b772de24 2020-01-15 tracey return got_error_from_errno("asprintf");
880 b772de24 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
881 b772de24 2020-01-15 tracey free(log_html);
882 b772de24 2020-01-15 tracey free(log);
883 b772de24 2020-01-15 tracey }
884 b772de24 2020-01-15 tracey return error;
885 b772de24 2020-01-15 tracey }
886 b772de24 2020-01-15 tracey
887 b772de24 2020-01-15 tracey static const struct got_error *
888 2c251c14 2020-01-15 tracey gw_tree(struct trans *gw_trans)
889 2c251c14 2020-01-15 tracey {
890 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
891 8087c3c5 2020-01-15 tracey char *log, *log_html;
892 8087c3c5 2020-01-15 tracey
893 8087c3c5 2020-01-15 tracey error = apply_unveil(gw_trans->gw_dir->path, NULL);
894 8087c3c5 2020-01-15 tracey if (error)
895 8087c3c5 2020-01-15 tracey return error;
896 8087c3c5 2020-01-15 tracey
897 8087c3c5 2020-01-15 tracey log = gw_get_repo_log(gw_trans, NULL, gw_trans->commit, 1, LOGTREE);
898 2c251c14 2020-01-15 tracey
899 8087c3c5 2020-01-15 tracey if (log != NULL && strcmp(log, "") != 0) {
900 8087c3c5 2020-01-15 tracey if ((asprintf(&log_html, log_tree, log)) == -1)
901 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
902 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, log_html);
903 8087c3c5 2020-01-15 tracey free(log_html);
904 8087c3c5 2020-01-15 tracey free(log);
905 8087c3c5 2020-01-15 tracey }
906 2c251c14 2020-01-15 tracey return error;
907 2c251c14 2020-01-15 tracey }
908 2c251c14 2020-01-15 tracey
909 2c251c14 2020-01-15 tracey static const struct got_error *
910 2c251c14 2020-01-15 tracey gw_load_got_path(struct trans *gw_trans, struct gw_dir *gw_dir)
911 2c251c14 2020-01-15 tracey {
912 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
913 2c251c14 2020-01-15 tracey DIR *dt;
914 2c251c14 2020-01-15 tracey char *dir_test;
915 4ceb8155 2020-01-15 tracey int opened = 0;
916 2c251c14 2020-01-15 tracey
917 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
918 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
919 2c251c14 2020-01-15 tracey GOTWEB_GIT_DIR)) == -1)
920 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
921 2c251c14 2020-01-15 tracey
922 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
923 2c251c14 2020-01-15 tracey if (dt == NULL) {
924 2c251c14 2020-01-15 tracey free(dir_test);
925 2c251c14 2020-01-15 tracey } else {
926 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
927 4ceb8155 2020-01-15 tracey opened = 1;
928 2c251c14 2020-01-15 tracey goto done;
929 2c251c14 2020-01-15 tracey }
930 2c251c14 2020-01-15 tracey
931 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s/%s",
932 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name,
933 2c251c14 2020-01-15 tracey GOTWEB_GOT_DIR)) == -1)
934 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
935 2c251c14 2020-01-15 tracey
936 2c251c14 2020-01-15 tracey dt = opendir(dir_test);
937 2c251c14 2020-01-15 tracey if (dt == NULL)
938 2c251c14 2020-01-15 tracey free(dir_test);
939 2c251c14 2020-01-15 tracey else {
940 4ceb8155 2020-01-15 tracey opened = 1;
941 2c251c14 2020-01-15 tracey error = got_error(GOT_ERR_NOT_GIT_REPO);
942 2c251c14 2020-01-15 tracey goto errored;
943 2c251c14 2020-01-15 tracey }
944 2c251c14 2020-01-15 tracey
945 2c251c14 2020-01-15 tracey if ((asprintf(&dir_test, "%s/%s",
946 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, gw_dir->name)) == -1)
947 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
948 2c251c14 2020-01-15 tracey
949 2c251c14 2020-01-15 tracey gw_dir->path = strdup(dir_test);
950 2c251c14 2020-01-15 tracey
951 2c251c14 2020-01-15 tracey done:
952 2c251c14 2020-01-15 tracey gw_dir->description = gw_get_repo_description(gw_trans,
953 2c251c14 2020-01-15 tracey gw_dir->path);
954 2c251c14 2020-01-15 tracey gw_dir->owner = gw_get_repo_owner(gw_trans, gw_dir->path);
955 387a29ba 2020-01-15 tracey gw_dir->age = gw_get_repo_age(gw_trans, gw_dir->path, "refs/heads",
956 387a29ba 2020-01-15 tracey TM_DIFF);
957 2c251c14 2020-01-15 tracey gw_dir->url = gw_get_clone_url(gw_trans, gw_dir->path);
958 2c251c14 2020-01-15 tracey
959 2c251c14 2020-01-15 tracey errored:
960 2c251c14 2020-01-15 tracey free(dir_test);
961 2c251c14 2020-01-15 tracey if (opened)
962 2c251c14 2020-01-15 tracey closedir(dt);
963 2c251c14 2020-01-15 tracey return error;
964 2c251c14 2020-01-15 tracey }
965 2c251c14 2020-01-15 tracey
966 2c251c14 2020-01-15 tracey static const struct got_error *
967 2c251c14 2020-01-15 tracey gw_load_got_paths(struct trans *gw_trans)
968 2c251c14 2020-01-15 tracey {
969 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
970 2c251c14 2020-01-15 tracey DIR *d;
971 2c251c14 2020-01-15 tracey struct dirent **sd_dent;
972 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
973 2c251c14 2020-01-15 tracey struct stat st;
974 2c251c14 2020-01-15 tracey unsigned int d_cnt, d_i;
975 2c251c14 2020-01-15 tracey
976 2c251c14 2020-01-15 tracey d = opendir(gw_trans->gw_conf->got_repos_path);
977 2c251c14 2020-01-15 tracey if (d == NULL) {
978 2c251c14 2020-01-15 tracey error = got_error_from_errno2("opendir",
979 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
980 2c251c14 2020-01-15 tracey return error;
981 2c251c14 2020-01-15 tracey }
982 2c251c14 2020-01-15 tracey
983 2c251c14 2020-01-15 tracey d_cnt = scandir(gw_trans->gw_conf->got_repos_path, &sd_dent, NULL,
984 2c251c14 2020-01-15 tracey alphasort);
985 2c251c14 2020-01-15 tracey if (d_cnt == -1) {
986 2c251c14 2020-01-15 tracey error = got_error_from_errno2("scandir",
987 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_repos_path);
988 2c251c14 2020-01-15 tracey return error;
989 2c251c14 2020-01-15 tracey }
990 2c251c14 2020-01-15 tracey
991 2c251c14 2020-01-15 tracey for (d_i = 0; d_i < d_cnt; d_i++) {
992 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_max_repos > 0 &&
993 2c251c14 2020-01-15 tracey (d_i - 2) == gw_trans->gw_conf->got_max_repos)
994 2c251c14 2020-01-15 tracey break; /* account for parent and self */
995 2c251c14 2020-01-15 tracey
996 2c251c14 2020-01-15 tracey if (strcmp(sd_dent[d_i]->d_name, ".") == 0 ||
997 2c251c14 2020-01-15 tracey strcmp(sd_dent[d_i]->d_name, "..") == 0)
998 2c251c14 2020-01-15 tracey continue;
999 2c251c14 2020-01-15 tracey
1000 2c251c14 2020-01-15 tracey if ((gw_dir = gw_init_gw_dir(sd_dent[d_i]->d_name)) == NULL)
1001 2c251c14 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1002 2c251c14 2020-01-15 tracey
1003 2c251c14 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_dir);
1004 2c251c14 2020-01-15 tracey if (error && error->code == GOT_ERR_NOT_GIT_REPO)
1005 2c251c14 2020-01-15 tracey continue;
1006 2c251c14 2020-01-15 tracey else if (error)
1007 2c251c14 2020-01-15 tracey return error;
1008 2c251c14 2020-01-15 tracey
1009 2c251c14 2020-01-15 tracey if (lstat(gw_dir->path, &st) == 0 && S_ISDIR(st.st_mode) &&
1010 2c251c14 2020-01-15 tracey !got_path_dir_is_empty(gw_dir->path)) {
1011 2c251c14 2020-01-15 tracey TAILQ_INSERT_TAIL(&gw_trans->gw_dirs, gw_dir,
1012 2c251c14 2020-01-15 tracey entry);
1013 2c251c14 2020-01-15 tracey gw_trans->repos_total++;
1014 2c251c14 2020-01-15 tracey }
1015 2c251c14 2020-01-15 tracey }
1016 2c251c14 2020-01-15 tracey
1017 2c251c14 2020-01-15 tracey closedir(d);
1018 2c251c14 2020-01-15 tracey return error;
1019 2c251c14 2020-01-15 tracey }
1020 2c251c14 2020-01-15 tracey
1021 2c251c14 2020-01-15 tracey static const struct got_error *
1022 2c251c14 2020-01-15 tracey gw_parse_querystring(struct trans *gw_trans)
1023 2c251c14 2020-01-15 tracey {
1024 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1025 2c251c14 2020-01-15 tracey struct kpair *p;
1026 2c251c14 2020-01-15 tracey struct gw_query_action *action = NULL;
1027 2c251c14 2020-01-15 tracey unsigned int i;
1028 2c251c14 2020-01-15 tracey
1029 2c251c14 2020-01-15 tracey if (gw_trans->gw_req->fieldnmap[0]) {
1030 2c251c14 2020-01-15 tracey error = got_error_from_errno("bad parse");
1031 2c251c14 2020-01-15 tracey return error;
1032 2c251c14 2020-01-15 tracey } else if ((p = gw_trans->gw_req->fieldmap[KEY_PATH])) {
1033 2c251c14 2020-01-15 tracey /* define gw_trans->repo_path */
1034 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_name, "%s", p->parsed.s)) == -1)
1035 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1036 2c251c14 2020-01-15 tracey
1037 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_path, "%s/%s",
1038 387a29ba 2020-01-15 tracey gw_trans->gw_conf->got_repos_path, p->parsed.s)) == -1)
1039 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1040 2c251c14 2020-01-15 tracey
1041 2c251c14 2020-01-15 tracey /* get action and set function */
1042 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_ACTION]))
1043 2c251c14 2020-01-15 tracey for (i = 0; i < nitems(gw_query_funcs); i++) {
1044 2c251c14 2020-01-15 tracey action = &gw_query_funcs[i];
1045 2c251c14 2020-01-15 tracey if (action->func_name == NULL)
1046 2c251c14 2020-01-15 tracey continue;
1047 077f6c5a 2020-01-15 tracey
1048 2c251c14 2020-01-15 tracey if (strcmp(action->func_name,
1049 2c251c14 2020-01-15 tracey p->parsed.s) == 0) {
1050 2c251c14 2020-01-15 tracey gw_trans->action = i;
1051 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->action_name,
1052 2c251c14 2020-01-15 tracey "%s", action->func_name)) == -1)
1053 2c251c14 2020-01-15 tracey return
1054 b772de24 2020-01-15 tracey got_error_from_errno(
1055 2c251c14 2020-01-15 tracey "asprintf");
1056 2c251c14 2020-01-15 tracey
1057 2c251c14 2020-01-15 tracey break;
1058 2c251c14 2020-01-15 tracey }
1059 2c251c14 2020-01-15 tracey
1060 2c251c14 2020-01-15 tracey action = NULL;
1061 2c251c14 2020-01-15 tracey }
1062 ec46ccd7 2020-01-15 tracey
1063 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_COMMIT_ID]))
1064 ec46ccd7 2020-01-15 tracey if ((asprintf(&gw_trans->commit, "%s",
1065 ec46ccd7 2020-01-15 tracey p->parsed.s)) == -1)
1066 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1067 2c251c14 2020-01-15 tracey
1068 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FILE]))
1069 2c251c14 2020-01-15 tracey if ((asprintf(&gw_trans->repo_file, "%s",
1070 8087c3c5 2020-01-15 tracey p->parsed.s)) == -1)
1071 8087c3c5 2020-01-15 tracey return got_error_from_errno("asprintf");
1072 8087c3c5 2020-01-15 tracey
1073 ec46ccd7 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_FOLDER]))
1074 ec46ccd7 2020-01-15 tracey if ((asprintf(&gw_trans->repo_folder, "%s",
1075 ec46ccd7 2020-01-15 tracey p->parsed.s)) == -1)
1076 ec46ccd7 2020-01-15 tracey return got_error_from_errno("asprintf");
1077 ec46ccd7 2020-01-15 tracey
1078 8087c3c5 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_HEADREF]))
1079 8087c3c5 2020-01-15 tracey if ((asprintf(&gw_trans->headref, "%s",
1080 2c251c14 2020-01-15 tracey p->parsed.s)) == -1)
1081 2c251c14 2020-01-15 tracey return got_error_from_errno("asprintf");
1082 2c251c14 2020-01-15 tracey
1083 2c251c14 2020-01-15 tracey if (action == NULL) {
1084 2c251c14 2020-01-15 tracey error = got_error_from_errno("invalid action");
1085 2c251c14 2020-01-15 tracey return error;
1086 2c251c14 2020-01-15 tracey }
1087 46b9c89b 2020-01-15 tracey if ((gw_trans->gw_dir =
1088 46b9c89b 2020-01-15 tracey gw_init_gw_dir(gw_trans->repo_name)) == NULL)
1089 46b9c89b 2020-01-15 tracey return got_error_from_errno("gw_dir malloc");
1090 46b9c89b 2020-01-15 tracey
1091 46b9c89b 2020-01-15 tracey error = gw_load_got_path(gw_trans, gw_trans->gw_dir);
1092 46b9c89b 2020-01-15 tracey if (error)
1093 46b9c89b 2020-01-15 tracey return error;
1094 2c251c14 2020-01-15 tracey } else
1095 2c251c14 2020-01-15 tracey gw_trans->action = GW_INDEX;
1096 2c251c14 2020-01-15 tracey
1097 2c251c14 2020-01-15 tracey if ((p = gw_trans->gw_req->fieldmap[KEY_PAGE]))
1098 2c251c14 2020-01-15 tracey gw_trans->page = p->parsed.i;
1099 2c251c14 2020-01-15 tracey
1100 2c251c14 2020-01-15 tracey if (gw_trans->action == GW_RAW)
1101 387a29ba 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
1102 2c251c14 2020-01-15 tracey
1103 2c251c14 2020-01-15 tracey return error;
1104 2c251c14 2020-01-15 tracey }
1105 2c251c14 2020-01-15 tracey
1106 2c251c14 2020-01-15 tracey static struct gw_dir *
1107 2c251c14 2020-01-15 tracey gw_init_gw_dir(char *dir)
1108 2c251c14 2020-01-15 tracey {
1109 2c251c14 2020-01-15 tracey struct gw_dir *gw_dir;
1110 2c251c14 2020-01-15 tracey
1111 2c251c14 2020-01-15 tracey if ((gw_dir = malloc(sizeof(*gw_dir))) == NULL)
1112 2c251c14 2020-01-15 tracey return NULL;
1113 2c251c14 2020-01-15 tracey
1114 2c251c14 2020-01-15 tracey if ((asprintf(&gw_dir->name, "%s", dir)) == -1)
1115 2c251c14 2020-01-15 tracey return NULL;
1116 2c251c14 2020-01-15 tracey
1117 2c251c14 2020-01-15 tracey return gw_dir;
1118 474370cb 2020-01-15 tracey }
1119 474370cb 2020-01-15 tracey
1120 474370cb 2020-01-15 tracey static const struct got_error*
1121 474370cb 2020-01-15 tracey match_logmsg(int *have_match, struct got_object_id *id,
1122 474370cb 2020-01-15 tracey struct got_commit_object *commit, regex_t *regex)
1123 474370cb 2020-01-15 tracey {
1124 474370cb 2020-01-15 tracey const struct got_error *err = NULL;
1125 474370cb 2020-01-15 tracey regmatch_t regmatch;
1126 474370cb 2020-01-15 tracey char *id_str = NULL, *logmsg = NULL;
1127 474370cb 2020-01-15 tracey
1128 474370cb 2020-01-15 tracey *have_match = 0;
1129 474370cb 2020-01-15 tracey
1130 474370cb 2020-01-15 tracey err = got_object_id_str(&id_str, id);
1131 474370cb 2020-01-15 tracey if (err)
1132 474370cb 2020-01-15 tracey return err;
1133 474370cb 2020-01-15 tracey
1134 474370cb 2020-01-15 tracey err = got_object_commit_get_logmsg(&logmsg, commit);
1135 474370cb 2020-01-15 tracey if (err)
1136 474370cb 2020-01-15 tracey goto done;
1137 474370cb 2020-01-15 tracey
1138 474370cb 2020-01-15 tracey if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1139 474370cb 2020-01-15 tracey *have_match = 1;
1140 474370cb 2020-01-15 tracey done:
1141 474370cb 2020-01-15 tracey free(id_str);
1142 474370cb 2020-01-15 tracey free(logmsg);
1143 474370cb 2020-01-15 tracey return err;
1144 2c251c14 2020-01-15 tracey }
1145 2c251c14 2020-01-15 tracey
1146 2c251c14 2020-01-15 tracey static void
1147 2c251c14 2020-01-15 tracey gw_display_open(struct trans *gw_trans, enum khttp code, enum kmime mime)
1148 2c251c14 2020-01-15 tracey {
1149 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_ALLOW], "GET");
1150 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_STATUS], "%s",
1151 2c251c14 2020-01-15 tracey khttps[code]);
1152 387a29ba 2020-01-15 tracey khttp_head(gw_trans->gw_req, kresps[KRESP_CONTENT_TYPE], "%s",
1153 2c251c14 2020-01-15 tracey kmimetypes[mime]);
1154 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-Content-Type-Options", "nosniff");
1155 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-Frame-Options", "DENY");
1156 2c251c14 2020-01-15 tracey khttp_head(gw_trans->gw_req, "X-XSS-Protection", "1; mode=block");
1157 2c251c14 2020-01-15 tracey khttp_body(gw_trans->gw_req);
1158 2c251c14 2020-01-15 tracey }
1159 2c251c14 2020-01-15 tracey
1160 2c251c14 2020-01-15 tracey static void
1161 2c251c14 2020-01-15 tracey gw_display_index(struct trans *gw_trans, const struct got_error *err)
1162 2c251c14 2020-01-15 tracey {
1163 2c251c14 2020-01-15 tracey gw_display_open(gw_trans, KHTTP_200, gw_trans->mime);
1164 2c251c14 2020-01-15 tracey khtml_open(gw_trans->gw_html_req, gw_trans->gw_req, 0);
1165 2c251c14 2020-01-15 tracey
1166 2c251c14 2020-01-15 tracey if (err)
1167 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, err->msg);
1168 2c251c14 2020-01-15 tracey else
1169 2c251c14 2020-01-15 tracey khttp_template(gw_trans->gw_req, gw_trans->gw_tmpl,
1170 2c251c14 2020-01-15 tracey gw_query_funcs[gw_trans->action].template);
1171 2c251c14 2020-01-15 tracey
1172 2c251c14 2020-01-15 tracey khtml_close(gw_trans->gw_html_req);
1173 2c251c14 2020-01-15 tracey }
1174 2c251c14 2020-01-15 tracey
1175 2c251c14 2020-01-15 tracey static int
1176 2c251c14 2020-01-15 tracey gw_template(size_t key, void *arg)
1177 2c251c14 2020-01-15 tracey {
1178 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1179 2c251c14 2020-01-15 tracey struct trans *gw_trans = arg;
1180 2c251c14 2020-01-15 tracey char *gw_got_link, *gw_site_link;
1181 2c251c14 2020-01-15 tracey char *site_owner_name, *site_owner_name_h;
1182 2c251c14 2020-01-15 tracey
1183 2c251c14 2020-01-15 tracey switch (key) {
1184 2c251c14 2020-01-15 tracey case (TEMPL_HEAD):
1185 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, head);
1186 2c251c14 2020-01-15 tracey break;
1187 2c251c14 2020-01-15 tracey case(TEMPL_HEADER):
1188 2c251c14 2020-01-15 tracey gw_got_link = gw_get_got_link(gw_trans);
1189 2c251c14 2020-01-15 tracey if (gw_got_link != NULL)
1190 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_got_link);
1191 2c251c14 2020-01-15 tracey
1192 2c251c14 2020-01-15 tracey free(gw_got_link);
1193 2c251c14 2020-01-15 tracey break;
1194 2c251c14 2020-01-15 tracey case (TEMPL_SITEPATH):
1195 2c251c14 2020-01-15 tracey gw_site_link = gw_get_site_link(gw_trans);
1196 2c251c14 2020-01-15 tracey if (gw_site_link != NULL)
1197 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, gw_site_link);
1198 2c251c14 2020-01-15 tracey
1199 2c251c14 2020-01-15 tracey free(gw_site_link);
1200 2c251c14 2020-01-15 tracey break;
1201 2c251c14 2020-01-15 tracey case(TEMPL_TITLE):
1202 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_name != NULL)
1203 2c251c14 2020-01-15 tracey khtml_puts(gw_trans->gw_html_req,
1204 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_name);
1205 2c251c14 2020-01-15 tracey
1206 2c251c14 2020-01-15 tracey break;
1207 2c251c14 2020-01-15 tracey case (TEMPL_SEARCH):
1208 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, search);
1209 2c251c14 2020-01-15 tracey break;
1210 2c251c14 2020-01-15 tracey case(TEMPL_SITEOWNER):
1211 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_site_owner != NULL &&
1212 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_show_site_owner) {
1213 2c251c14 2020-01-15 tracey site_owner_name =
1214 2c251c14 2020-01-15 tracey gw_html_escape(gw_trans->gw_conf->got_site_owner);
1215 2c251c14 2020-01-15 tracey if ((asprintf(&site_owner_name_h, site_owner,
1216 2c251c14 2020-01-15 tracey site_owner_name))
1217 2c251c14 2020-01-15 tracey == -1)
1218 2c251c14 2020-01-15 tracey return 0;
1219 2c251c14 2020-01-15 tracey
1220 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, site_owner_name_h);
1221 2c251c14 2020-01-15 tracey free(site_owner_name);
1222 2c251c14 2020-01-15 tracey free(site_owner_name_h);
1223 2c251c14 2020-01-15 tracey }
1224 2c251c14 2020-01-15 tracey break;
1225 2c251c14 2020-01-15 tracey case(TEMPL_CONTENT):
1226 2c251c14 2020-01-15 tracey error = gw_query_funcs[gw_trans->action].func_main(gw_trans);
1227 2c251c14 2020-01-15 tracey if (error)
1228 2c251c14 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
1229 2c251c14 2020-01-15 tracey
1230 2c251c14 2020-01-15 tracey break;
1231 2c251c14 2020-01-15 tracey default:
1232 2c251c14 2020-01-15 tracey return 0;
1233 2c251c14 2020-01-15 tracey break;
1234 2c251c14 2020-01-15 tracey }
1235 2c251c14 2020-01-15 tracey return 1;
1236 2c251c14 2020-01-15 tracey }
1237 2c251c14 2020-01-15 tracey
1238 2c251c14 2020-01-15 tracey static char *
1239 2c251c14 2020-01-15 tracey gw_get_repo_description(struct trans *gw_trans, char *dir)
1240 2c251c14 2020-01-15 tracey {
1241 2c251c14 2020-01-15 tracey FILE *f;
1242 2c251c14 2020-01-15 tracey char *description = NULL, *d_file = NULL;
1243 2c251c14 2020-01-15 tracey unsigned int len;
1244 2c251c14 2020-01-15 tracey
1245 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_description == false)
1246 2c251c14 2020-01-15 tracey goto err;
1247 2c251c14 2020-01-15 tracey
1248 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/description", dir)) == -1)
1249 2c251c14 2020-01-15 tracey goto err;
1250 2c251c14 2020-01-15 tracey
1251 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1252 2c251c14 2020-01-15 tracey goto err;
1253 2c251c14 2020-01-15 tracey
1254 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1255 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1256 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1257 2c251c14 2020-01-15 tracey if ((description = calloc(len, sizeof(char *))) == NULL)
1258 2c251c14 2020-01-15 tracey goto err;
1259 2c251c14 2020-01-15 tracey
1260 2c251c14 2020-01-15 tracey fread(description, 1, len, f);
1261 2c251c14 2020-01-15 tracey fclose(f);
1262 2c251c14 2020-01-15 tracey free(d_file);
1263 2c251c14 2020-01-15 tracey return description;
1264 2c251c14 2020-01-15 tracey err:
1265 2c251c14 2020-01-15 tracey if ((asprintf(&description, "%s", "")) == -1)
1266 2c251c14 2020-01-15 tracey return NULL;
1267 2c251c14 2020-01-15 tracey
1268 2c251c14 2020-01-15 tracey return description;
1269 2c251c14 2020-01-15 tracey }
1270 2c251c14 2020-01-15 tracey
1271 2c251c14 2020-01-15 tracey static char *
1272 474370cb 2020-01-15 tracey gw_get_time_str(time_t committer_time, int ref_tm)
1273 474370cb 2020-01-15 tracey {
1274 474370cb 2020-01-15 tracey struct tm tm;
1275 474370cb 2020-01-15 tracey time_t diff_time;
1276 474370cb 2020-01-15 tracey char *years = "years ago", *months = "months ago";
1277 474370cb 2020-01-15 tracey char *weeks = "weeks ago", *days = "days ago", *hours = "hours ago";
1278 474370cb 2020-01-15 tracey char *minutes = "minutes ago", *seconds = "seconds ago";
1279 474370cb 2020-01-15 tracey char *now = "right now";
1280 474370cb 2020-01-15 tracey char *repo_age, *s;
1281 6c6c85af 2020-01-15 tracey char datebuf[29];
1282 474370cb 2020-01-15 tracey
1283 474370cb 2020-01-15 tracey switch (ref_tm) {
1284 474370cb 2020-01-15 tracey case TM_DIFF:
1285 474370cb 2020-01-15 tracey diff_time = time(NULL) - committer_time;
1286 474370cb 2020-01-15 tracey if (diff_time > 60 * 60 * 24 * 365 * 2) {
1287 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1288 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 365), years)) == -1)
1289 474370cb 2020-01-15 tracey return NULL;
1290 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * (365 / 12) * 2) {
1291 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1292 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / (365 / 12)),
1293 474370cb 2020-01-15 tracey months)) == -1)
1294 474370cb 2020-01-15 tracey return NULL;
1295 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 7 * 2) {
1296 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1297 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24 / 7), weeks)) == -1)
1298 474370cb 2020-01-15 tracey return NULL;
1299 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 24 * 2) {
1300 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1301 474370cb 2020-01-15 tracey (diff_time / 60 / 60 / 24), days)) == -1)
1302 474370cb 2020-01-15 tracey return NULL;
1303 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 60 * 2) {
1304 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s",
1305 474370cb 2020-01-15 tracey (diff_time / 60 / 60), hours)) == -1)
1306 474370cb 2020-01-15 tracey return NULL;
1307 474370cb 2020-01-15 tracey } else if (diff_time > 60 * 2) {
1308 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", (diff_time / 60),
1309 474370cb 2020-01-15 tracey minutes)) == -1)
1310 474370cb 2020-01-15 tracey return NULL;
1311 474370cb 2020-01-15 tracey } else if (diff_time > 2) {
1312 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%lld %s", diff_time,
1313 474370cb 2020-01-15 tracey seconds)) == -1)
1314 474370cb 2020-01-15 tracey return NULL;
1315 474370cb 2020-01-15 tracey } else {
1316 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s", now)) == -1)
1317 474370cb 2020-01-15 tracey return NULL;
1318 474370cb 2020-01-15 tracey }
1319 474370cb 2020-01-15 tracey break;
1320 474370cb 2020-01-15 tracey case TM_LONG:
1321 474370cb 2020-01-15 tracey if (gmtime_r(&committer_time, &tm) == NULL)
1322 474370cb 2020-01-15 tracey return NULL;
1323 474370cb 2020-01-15 tracey
1324 474370cb 2020-01-15 tracey s = asctime_r(&tm, datebuf);
1325 474370cb 2020-01-15 tracey if (s == NULL)
1326 474370cb 2020-01-15 tracey return NULL;
1327 474370cb 2020-01-15 tracey
1328 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "%s UTC", datebuf)) == -1)
1329 474370cb 2020-01-15 tracey return NULL;
1330 474370cb 2020-01-15 tracey break;
1331 474370cb 2020-01-15 tracey }
1332 474370cb 2020-01-15 tracey return repo_age;
1333 474370cb 2020-01-15 tracey }
1334 474370cb 2020-01-15 tracey
1335 474370cb 2020-01-15 tracey static char *
1336 387a29ba 2020-01-15 tracey gw_get_repo_age(struct trans *gw_trans, char *dir, char *repo_ref, int ref_tm)
1337 2c251c14 2020-01-15 tracey {
1338 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
1339 2c251c14 2020-01-15 tracey struct got_object_id *id = NULL;
1340 2c251c14 2020-01-15 tracey struct got_repository *repo = NULL;
1341 2c251c14 2020-01-15 tracey struct got_commit_object *commit = NULL;
1342 2c251c14 2020-01-15 tracey struct got_reflist_head refs;
1343 2c251c14 2020-01-15 tracey struct got_reflist_entry *re;
1344 2c251c14 2020-01-15 tracey struct got_reference *head_ref;
1345 87f9ebf5 2020-01-15 tracey int is_head = 0;
1346 474370cb 2020-01-15 tracey time_t committer_time = 0, cmp_time = 0;
1347 87f9ebf5 2020-01-15 tracey const char *refname;
1348 474370cb 2020-01-15 tracey char *repo_age = NULL;
1349 387a29ba 2020-01-15 tracey
1350 387a29ba 2020-01-15 tracey if (repo_ref == NULL)
1351 387a29ba 2020-01-15 tracey return NULL;
1352 87f9ebf5 2020-01-15 tracey
1353 87f9ebf5 2020-01-15 tracey if (strncmp(repo_ref, "refs/heads/", 11) == 0)
1354 87f9ebf5 2020-01-15 tracey is_head = 1;
1355 2c251c14 2020-01-15 tracey
1356 2c251c14 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1357 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_age == false) {
1358 8087c3c5 2020-01-15 tracey if ((asprintf(&repo_age, "")) == -1)
1359 8087c3c5 2020-01-15 tracey return NULL;
1360 2c251c14 2020-01-15 tracey return repo_age;
1361 2c251c14 2020-01-15 tracey }
1362 87f9ebf5 2020-01-15 tracey
1363 2c251c14 2020-01-15 tracey error = got_repo_open(&repo, dir, NULL);
1364 ec46ccd7 2020-01-15 tracey if (error)
1365 2c251c14 2020-01-15 tracey goto err;
1366 2c251c14 2020-01-15 tracey
1367 87f9ebf5 2020-01-15 tracey if (is_head)
1368 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads",
1369 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1370 87f9ebf5 2020-01-15 tracey else
1371 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, repo_ref,
1372 87f9ebf5 2020-01-15 tracey got_ref_cmp_by_name, NULL);
1373 ec46ccd7 2020-01-15 tracey if (error)
1374 2c251c14 2020-01-15 tracey goto err;
1375 2c251c14 2020-01-15 tracey
1376 2c251c14 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1377 87f9ebf5 2020-01-15 tracey if (is_head)
1378 87f9ebf5 2020-01-15 tracey refname = strdup(repo_ref);
1379 87f9ebf5 2020-01-15 tracey else
1380 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
1381 2c251c14 2020-01-15 tracey error = got_ref_open(&head_ref, repo, refname, 0);
1382 ec46ccd7 2020-01-15 tracey if (error)
1383 2c251c14 2020-01-15 tracey goto err;
1384 2c251c14 2020-01-15 tracey
1385 2c251c14 2020-01-15 tracey error = got_ref_resolve(&id, repo, head_ref);
1386 2c251c14 2020-01-15 tracey got_ref_close(head_ref);
1387 ec46ccd7 2020-01-15 tracey if (error)
1388 2c251c14 2020-01-15 tracey goto err;
1389 2c251c14 2020-01-15 tracey
1390 2c251c14 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id);
1391 ec46ccd7 2020-01-15 tracey if (error)
1392 2c251c14 2020-01-15 tracey goto err;
1393 2c251c14 2020-01-15 tracey
1394 2c251c14 2020-01-15 tracey committer_time =
1395 2c251c14 2020-01-15 tracey got_object_commit_get_committer_time(commit);
1396 2c251c14 2020-01-15 tracey
1397 387a29ba 2020-01-15 tracey if (cmp_time < committer_time)
1398 2c251c14 2020-01-15 tracey cmp_time = committer_time;
1399 2c251c14 2020-01-15 tracey }
1400 2c251c14 2020-01-15 tracey
1401 474370cb 2020-01-15 tracey if (cmp_time != 0) {
1402 2c251c14 2020-01-15 tracey committer_time = cmp_time;
1403 474370cb 2020-01-15 tracey repo_age = gw_get_time_str(committer_time, ref_tm);
1404 474370cb 2020-01-15 tracey } else
1405 474370cb 2020-01-15 tracey if ((asprintf(&repo_age, "")) == -1)
1406 474370cb 2020-01-15 tracey return NULL;
1407 2c251c14 2020-01-15 tracey got_ref_list_free(&refs);
1408 2c251c14 2020-01-15 tracey free(id);
1409 2c251c14 2020-01-15 tracey return repo_age;
1410 2c251c14 2020-01-15 tracey err:
1411 2c251c14 2020-01-15 tracey if ((asprintf(&repo_age, "%s", error->msg)) == -1)
1412 2c251c14 2020-01-15 tracey return NULL;
1413 2c251c14 2020-01-15 tracey
1414 2c251c14 2020-01-15 tracey return repo_age;
1415 ec46ccd7 2020-01-15 tracey }
1416 ec46ccd7 2020-01-15 tracey
1417 ec46ccd7 2020-01-15 tracey static char *
1418 ec46ccd7 2020-01-15 tracey gw_get_repo_diff(struct trans *gw_trans, char *id_str1, char *id_str2)
1419 ec46ccd7 2020-01-15 tracey {
1420 ec46ccd7 2020-01-15 tracey const struct got_error *error;
1421 ec46ccd7 2020-01-15 tracey FILE *f = NULL;
1422 ec46ccd7 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1423 ec46ccd7 2020-01-15 tracey struct got_repository *repo = NULL;
1424 ec46ccd7 2020-01-15 tracey struct buf *diffbuf = NULL;
1425 ec46ccd7 2020-01-15 tracey char *label1 = NULL, *label2 = NULL, *diff_html = NULL, *buf = NULL,
1426 ec46ccd7 2020-01-15 tracey *buf_color = NULL;
1427 ec46ccd7 2020-01-15 tracey int type1, type2;
1428 ec46ccd7 2020-01-15 tracey size_t newsize;
1429 ec46ccd7 2020-01-15 tracey
1430 ec46ccd7 2020-01-15 tracey f = got_opentemp();
1431 ec46ccd7 2020-01-15 tracey if (f == NULL)
1432 ec46ccd7 2020-01-15 tracey return NULL;
1433 ec46ccd7 2020-01-15 tracey
1434 ec46ccd7 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1435 ec46ccd7 2020-01-15 tracey if (error)
1436 ec46ccd7 2020-01-15 tracey return NULL;
1437 ec46ccd7 2020-01-15 tracey
1438 ec46ccd7 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1439 ec46ccd7 2020-01-15 tracey if (error)
1440 ec46ccd7 2020-01-15 tracey goto done;
1441 ec46ccd7 2020-01-15 tracey
1442 ec46ccd7 2020-01-15 tracey error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
1443 ec46ccd7 2020-01-15 tracey repo);
1444 ec46ccd7 2020-01-15 tracey if (error)
1445 ec46ccd7 2020-01-15 tracey goto done;
1446 ec46ccd7 2020-01-15 tracey
1447 ec46ccd7 2020-01-15 tracey if (id_str2) {
1448 ec46ccd7 2020-01-15 tracey error = match_object_id(&id2, &label2, id_str2,
1449 ec46ccd7 2020-01-15 tracey GOT_OBJ_TYPE_ANY, 1, repo);
1450 ec46ccd7 2020-01-15 tracey if (error)
1451 ec46ccd7 2020-01-15 tracey goto done;
1452 ec46ccd7 2020-01-15 tracey
1453 ec46ccd7 2020-01-15 tracey error = got_object_get_type(&type2, repo, id2);
1454 ec46ccd7 2020-01-15 tracey if (error)
1455 ec46ccd7 2020-01-15 tracey goto done;
1456 ec46ccd7 2020-01-15 tracey }
1457 ec46ccd7 2020-01-15 tracey
1458 ec46ccd7 2020-01-15 tracey error = got_object_get_type(&type1, repo, id1);
1459 ec46ccd7 2020-01-15 tracey if (error)
1460 ec46ccd7 2020-01-15 tracey goto done;
1461 ec46ccd7 2020-01-15 tracey
1462 ec46ccd7 2020-01-15 tracey if (id_str2 && type1 != type2) {
1463 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1464 ec46ccd7 2020-01-15 tracey goto done;
1465 ec46ccd7 2020-01-15 tracey }
1466 ec46ccd7 2020-01-15 tracey
1467 ec46ccd7 2020-01-15 tracey switch (type1) {
1468 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_BLOB:
1469 ec46ccd7 2020-01-15 tracey error = got_diff_objects_as_blobs(id2, id1, NULL, NULL, 3, 0,
1470 ec46ccd7 2020-01-15 tracey repo, f);
1471 ec46ccd7 2020-01-15 tracey break;
1472 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_TREE:
1473 ec46ccd7 2020-01-15 tracey error = got_diff_objects_as_trees(id2, id1, "", "", 3, 0, repo,
1474 ec46ccd7 2020-01-15 tracey f);
1475 ec46ccd7 2020-01-15 tracey break;
1476 ec46ccd7 2020-01-15 tracey case GOT_OBJ_TYPE_COMMIT:
1477 ec46ccd7 2020-01-15 tracey error = got_diff_objects_as_commits(id2, id1, 3, 0, repo, f);
1478 ec46ccd7 2020-01-15 tracey break;
1479 ec46ccd7 2020-01-15 tracey default:
1480 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1481 ec46ccd7 2020-01-15 tracey }
1482 ec46ccd7 2020-01-15 tracey
1483 ec46ccd7 2020-01-15 tracey if ((buf = calloc(128, sizeof(char *))) == NULL)
1484 ec46ccd7 2020-01-15 tracey goto done;
1485 ec46ccd7 2020-01-15 tracey
1486 ec46ccd7 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1487 ec46ccd7 2020-01-15 tracey
1488 ec46ccd7 2020-01-15 tracey while ((fgets(buf, 128, f)) != NULL) {
1489 ec46ccd7 2020-01-15 tracey buf_color = color_diff_line(buf);
1490 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, buf_color);
1491 ec46ccd7 2020-01-15 tracey if (error)
1492 ec46ccd7 2020-01-15 tracey return NULL;
1493 ec46ccd7 2020-01-15 tracey
1494 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, div_end);
1495 ec46ccd7 2020-01-15 tracey if (error)
1496 ec46ccd7 2020-01-15 tracey return NULL;
1497 ec46ccd7 2020-01-15 tracey }
1498 ec46ccd7 2020-01-15 tracey
1499 ec46ccd7 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
1500 ec46ccd7 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
1501 ec46ccd7 2020-01-15 tracey diff_html = strdup(buf_get(diffbuf));
1502 ec46ccd7 2020-01-15 tracey }
1503 ec46ccd7 2020-01-15 tracey done:
1504 ec46ccd7 2020-01-15 tracey fclose(f);
1505 ec46ccd7 2020-01-15 tracey free(buf_color);
1506 ec46ccd7 2020-01-15 tracey free(buf);
1507 ec46ccd7 2020-01-15 tracey free(diffbuf);
1508 ec46ccd7 2020-01-15 tracey free(label1);
1509 ec46ccd7 2020-01-15 tracey free(label2);
1510 ec46ccd7 2020-01-15 tracey free(id1);
1511 ec46ccd7 2020-01-15 tracey free(id2);
1512 ec46ccd7 2020-01-15 tracey if (repo)
1513 ec46ccd7 2020-01-15 tracey got_repo_close(repo);
1514 ec46ccd7 2020-01-15 tracey
1515 ec46ccd7 2020-01-15 tracey if (error)
1516 ec46ccd7 2020-01-15 tracey return NULL;
1517 ec46ccd7 2020-01-15 tracey else
1518 ec46ccd7 2020-01-15 tracey return diff_html;
1519 2c251c14 2020-01-15 tracey }
1520 2c251c14 2020-01-15 tracey
1521 2c251c14 2020-01-15 tracey static char *
1522 2c251c14 2020-01-15 tracey gw_get_repo_owner(struct trans *gw_trans, char *dir)
1523 2c251c14 2020-01-15 tracey {
1524 2c251c14 2020-01-15 tracey FILE *f;
1525 2c251c14 2020-01-15 tracey char *owner = NULL, *d_file = NULL;
1526 2c251c14 2020-01-15 tracey char *gotweb = "[gotweb]", *gitweb = "[gitweb]", *gw_owner = "owner";
1527 2c251c14 2020-01-15 tracey char *comp, *pos, *buf;
1528 2c251c14 2020-01-15 tracey unsigned int i;
1529 2c251c14 2020-01-15 tracey
1530 2c251c14 2020-01-15 tracey if (gw_trans->gw_conf->got_show_repo_owner == false)
1531 2c251c14 2020-01-15 tracey goto err;
1532 2c251c14 2020-01-15 tracey
1533 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/config", dir)) == -1)
1534 2c251c14 2020-01-15 tracey goto err;
1535 2c251c14 2020-01-15 tracey
1536 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1537 2c251c14 2020-01-15 tracey goto err;
1538 2c251c14 2020-01-15 tracey
1539 6c6c85af 2020-01-15 tracey if ((buf = calloc(128, sizeof(char *))) == NULL)
1540 2c251c14 2020-01-15 tracey goto err;
1541 2c251c14 2020-01-15 tracey
1542 6c6c85af 2020-01-15 tracey while ((fgets(buf, 128, f)) != NULL) {
1543 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gotweb)) != NULL)
1544 2c251c14 2020-01-15 tracey break;
1545 2c251c14 2020-01-15 tracey
1546 2c251c14 2020-01-15 tracey if ((pos = strstr(buf, gitweb)) != NULL)
1547 2c251c14 2020-01-15 tracey break;
1548 2c251c14 2020-01-15 tracey }
1549 2c251c14 2020-01-15 tracey
1550 2c251c14 2020-01-15 tracey if (pos == NULL)
1551 2c251c14 2020-01-15 tracey goto err;
1552 2c251c14 2020-01-15 tracey
1553 2c251c14 2020-01-15 tracey do {
1554 6c6c85af 2020-01-15 tracey fgets(buf, 128, f);
1555 2c251c14 2020-01-15 tracey } while ((comp = strcasestr(buf, gw_owner)) == NULL);
1556 2c251c14 2020-01-15 tracey
1557 2c251c14 2020-01-15 tracey if (comp == NULL)
1558 2c251c14 2020-01-15 tracey goto err;
1559 2c251c14 2020-01-15 tracey
1560 2c251c14 2020-01-15 tracey if (strncmp(gw_owner, comp, strlen(gw_owner)) != 0)
1561 2c251c14 2020-01-15 tracey goto err;
1562 2c251c14 2020-01-15 tracey
1563 2c251c14 2020-01-15 tracey for (i = 0; i < 2; i++) {
1564 2c251c14 2020-01-15 tracey owner = strsep(&buf, "\"");
1565 2c251c14 2020-01-15 tracey }
1566 2c251c14 2020-01-15 tracey
1567 2c251c14 2020-01-15 tracey if (owner == NULL)
1568 2c251c14 2020-01-15 tracey goto err;
1569 2c251c14 2020-01-15 tracey
1570 2c251c14 2020-01-15 tracey fclose(f);
1571 2c251c14 2020-01-15 tracey free(d_file);
1572 2c251c14 2020-01-15 tracey return owner;
1573 2c251c14 2020-01-15 tracey err:
1574 2c251c14 2020-01-15 tracey if ((asprintf(&owner, "%s", "")) == -1)
1575 2c251c14 2020-01-15 tracey return NULL;
1576 2c251c14 2020-01-15 tracey
1577 2c251c14 2020-01-15 tracey return owner;
1578 2c251c14 2020-01-15 tracey }
1579 2c251c14 2020-01-15 tracey
1580 2c251c14 2020-01-15 tracey static char *
1581 2c251c14 2020-01-15 tracey gw_get_clone_url(struct trans *gw_trans, char *dir)
1582 2c251c14 2020-01-15 tracey {
1583 2c251c14 2020-01-15 tracey FILE *f;
1584 2c251c14 2020-01-15 tracey char *url = NULL, *d_file = NULL;
1585 2c251c14 2020-01-15 tracey unsigned int len;
1586 2c251c14 2020-01-15 tracey
1587 2c251c14 2020-01-15 tracey if ((asprintf(&d_file, "%s/cloneurl", dir)) == -1)
1588 2c251c14 2020-01-15 tracey return NULL;
1589 2c251c14 2020-01-15 tracey
1590 2c251c14 2020-01-15 tracey if ((f = fopen(d_file, "r")) == NULL)
1591 2c251c14 2020-01-15 tracey return NULL;
1592 2c251c14 2020-01-15 tracey
1593 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_END);
1594 2c251c14 2020-01-15 tracey len = ftell(f) + 1;
1595 2c251c14 2020-01-15 tracey fseek(f, 0, SEEK_SET);
1596 2c251c14 2020-01-15 tracey
1597 2c251c14 2020-01-15 tracey if ((url = calloc(len, sizeof(char *))) == NULL)
1598 2c251c14 2020-01-15 tracey return NULL;
1599 2c251c14 2020-01-15 tracey
1600 2c251c14 2020-01-15 tracey fread(url, 1, len, f);
1601 2c251c14 2020-01-15 tracey fclose(f);
1602 2c251c14 2020-01-15 tracey free(d_file);
1603 2c251c14 2020-01-15 tracey return url;
1604 8d4d2453 2020-01-15 tracey }
1605 8d4d2453 2020-01-15 tracey
1606 8d4d2453 2020-01-15 tracey static char *
1607 4ceb8155 2020-01-15 tracey gw_get_repo_log(struct trans *gw_trans, const char *search_pattern,
1608 8087c3c5 2020-01-15 tracey char *start_commit, int limit, int log_type)
1609 8d4d2453 2020-01-15 tracey {
1610 c6b62706 2020-01-15 tracey const struct got_error *error;
1611 c6b62706 2020-01-15 tracey struct got_repository *repo = NULL;
1612 c6b62706 2020-01-15 tracey struct got_reflist_head refs;
1613 474370cb 2020-01-15 tracey struct got_reflist_entry *re;
1614 c6b62706 2020-01-15 tracey struct got_commit_object *commit = NULL;
1615 8087c3c5 2020-01-15 tracey struct got_object_id *id1 = NULL, *id2 = NULL;
1616 8087c3c5 2020-01-15 tracey struct got_object_qid *parent_id;
1617 474370cb 2020-01-15 tracey struct got_commit_graph *graph = NULL;
1618 87f9ebf5 2020-01-15 tracey char *logs = NULL, *id_str1 = NULL, *id_str2 = NULL, *path = NULL,
1619 87f9ebf5 2020-01-15 tracey *in_repo_path = NULL, *refs_str = NULL, *refs_str_disp = NULL,
1620 87f9ebf5 2020-01-15 tracey *treeid = NULL, *commit_row = NULL, *commit_commit = NULL,
1621 87f9ebf5 2020-01-15 tracey *commit_commit_disp = NULL, *commit_age_diff = NULL,
1622 87f9ebf5 2020-01-15 tracey *commit_age_diff_disp = NULL, *commit_age_long = NULL,
1623 87f9ebf5 2020-01-15 tracey *commit_age_long_disp = NULL, *commit_author = NULL,
1624 87f9ebf5 2020-01-15 tracey *commit_author_disp = NULL, *commit_committer = NULL,
1625 87f9ebf5 2020-01-15 tracey *commit_committer_disp = NULL, *commit_log = NULL,
1626 87f9ebf5 2020-01-15 tracey *commit_log_disp = NULL, *commit_parent = NULL,
1627 87f9ebf5 2020-01-15 tracey *commit_diff_disp = NULL, *logbriefs_navs_html = NULL,
1628 87f9ebf5 2020-01-15 tracey *log_tree_html = NULL, *log_commit_html = NULL,
1629 87f9ebf5 2020-01-15 tracey *log_diff_html = NULL, *commit_tree = NULL,
1630 ec46ccd7 2020-01-15 tracey *commit_tree_disp = NULL, *log_tag_html = NULL,
1631 ec46ccd7 2020-01-15 tracey *log_blame_html = NULL;
1632 87f9ebf5 2020-01-15 tracey char *commit_log0, *newline;
1633 474370cb 2020-01-15 tracey regex_t regex;
1634 ec46ccd7 2020-01-15 tracey int have_match, log_count = 0, has_parent = 1;
1635 474370cb 2020-01-15 tracey size_t newsize;
1636 8087c3c5 2020-01-15 tracey struct buf *diffbuf = NULL;
1637 474370cb 2020-01-15 tracey time_t committer_time;
1638 65b95fb2 2020-01-15 tracey
1639 65b95fb2 2020-01-15 tracey if (gw_trans->action == GW_LOG || gw_trans->action == GW_LOGBRIEFS)
1640 65b95fb2 2020-01-15 tracey log_count = gw_get_repo_log_count(gw_trans, start_commit);
1641 8d4d2453 2020-01-15 tracey
1642 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
1643 ec46ccd7 2020-01-15 tracey if (error)
1644 6c6c85af 2020-01-15 tracey return NULL;
1645 6c6c85af 2020-01-15 tracey
1646 474370cb 2020-01-15 tracey if (search_pattern &&
1647 474370cb 2020-01-15 tracey regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB |
1648 474370cb 2020-01-15 tracey REG_NEWLINE))
1649 474370cb 2020-01-15 tracey return NULL;
1650 474370cb 2020-01-15 tracey
1651 c6b62706 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
1652 ec46ccd7 2020-01-15 tracey if (error)
1653 6c6c85af 2020-01-15 tracey return NULL;
1654 c6b62706 2020-01-15 tracey
1655 6c6c85af 2020-01-15 tracey SIMPLEQ_INIT(&refs);
1656 474370cb 2020-01-15 tracey
1657 c6b62706 2020-01-15 tracey if (start_commit == NULL) {
1658 c6b62706 2020-01-15 tracey struct got_reference *head_ref;
1659 8087c3c5 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
1660 ec46ccd7 2020-01-15 tracey if (error)
1661 8087c3c5 2020-01-15 tracey goto done;
1662 6c6c85af 2020-01-15 tracey
1663 8087c3c5 2020-01-15 tracey error = got_ref_resolve(&id1, repo, head_ref);
1664 c6b62706 2020-01-15 tracey got_ref_close(head_ref);
1665 ec46ccd7 2020-01-15 tracey if (error)
1666 8087c3c5 2020-01-15 tracey goto done;
1667 6c6c85af 2020-01-15 tracey
1668 8087c3c5 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id1);
1669 c6b62706 2020-01-15 tracey } else {
1670 c6b62706 2020-01-15 tracey struct got_reference *ref;
1671 c6b62706 2020-01-15 tracey error = got_ref_open(&ref, repo, start_commit, 0);
1672 c6b62706 2020-01-15 tracey if (error == NULL) {
1673 c6b62706 2020-01-15 tracey int obj_type;
1674 8087c3c5 2020-01-15 tracey error = got_ref_resolve(&id1, repo, ref);
1675 c6b62706 2020-01-15 tracey got_ref_close(ref);
1676 ec46ccd7 2020-01-15 tracey if (error)
1677 c6b62706 2020-01-15 tracey goto done;
1678 8087c3c5 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, id1);
1679 ec46ccd7 2020-01-15 tracey if (error)
1680 c6b62706 2020-01-15 tracey goto done;
1681 c6b62706 2020-01-15 tracey if (obj_type == GOT_OBJ_TYPE_TAG) {
1682 c6b62706 2020-01-15 tracey struct got_tag_object *tag;
1683 8087c3c5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id1);
1684 ec46ccd7 2020-01-15 tracey if (error)
1685 c6b62706 2020-01-15 tracey goto done;
1686 c6b62706 2020-01-15 tracey if (got_object_tag_get_object_type(tag) !=
1687 c6b62706 2020-01-15 tracey GOT_OBJ_TYPE_COMMIT) {
1688 c6b62706 2020-01-15 tracey got_object_tag_close(tag);
1689 c6b62706 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1690 c6b62706 2020-01-15 tracey goto done;
1691 c6b62706 2020-01-15 tracey }
1692 8087c3c5 2020-01-15 tracey free(id1);
1693 8087c3c5 2020-01-15 tracey id1 = got_object_id_dup(
1694 c6b62706 2020-01-15 tracey got_object_tag_get_object_id(tag));
1695 8087c3c5 2020-01-15 tracey if (id1 == NULL)
1696 c6b62706 2020-01-15 tracey error = got_error_from_errno(
1697 c6b62706 2020-01-15 tracey "got_object_id_dup");
1698 c6b62706 2020-01-15 tracey got_object_tag_close(tag);
1699 c6b62706 2020-01-15 tracey if (error)
1700 c6b62706 2020-01-15 tracey goto done;
1701 c6b62706 2020-01-15 tracey } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1702 c6b62706 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
1703 c6b62706 2020-01-15 tracey goto done;
1704 c6b62706 2020-01-15 tracey }
1705 8087c3c5 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id1);
1706 ec46ccd7 2020-01-15 tracey if (error)
1707 c6b62706 2020-01-15 tracey goto done;
1708 c6b62706 2020-01-15 tracey }
1709 c6b62706 2020-01-15 tracey if (commit == NULL) {
1710 8087c3c5 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id1,
1711 c6b62706 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1712 ec46ccd7 2020-01-15 tracey if (error)
1713 8087c3c5 2020-01-15 tracey goto done;
1714 c6b62706 2020-01-15 tracey }
1715 8087c3c5 2020-01-15 tracey error = got_repo_match_object_id_prefix(&id1,
1716 8087c3c5 2020-01-15 tracey start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1717 c6b62706 2020-01-15 tracey }
1718 c6b62706 2020-01-15 tracey
1719 ec46ccd7 2020-01-15 tracey if (error)
1720 c6b62706 2020-01-15 tracey goto done;
1721 6c6c85af 2020-01-15 tracey
1722 474370cb 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
1723 ec46ccd7 2020-01-15 tracey if (error)
1724 474370cb 2020-01-15 tracey goto done;
1725 474370cb 2020-01-15 tracey
1726 474370cb 2020-01-15 tracey if (in_repo_path) {
1727 474370cb 2020-01-15 tracey free(path);
1728 474370cb 2020-01-15 tracey path = in_repo_path;
1729 474370cb 2020-01-15 tracey }
1730 474370cb 2020-01-15 tracey
1731 c6b62706 2020-01-15 tracey error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1732 c6b62706 2020-01-15 tracey if (error)
1733 c6b62706 2020-01-15 tracey goto done;
1734 c6b62706 2020-01-15 tracey
1735 65b95fb2 2020-01-15 tracey error = got_commit_graph_open(&graph, path, 0);
1736 474370cb 2020-01-15 tracey if (error)
1737 474370cb 2020-01-15 tracey goto done;
1738 474370cb 2020-01-15 tracey
1739 8087c3c5 2020-01-15 tracey error = got_commit_graph_iter_start(graph, id1, repo, NULL, NULL);
1740 474370cb 2020-01-15 tracey if (error)
1741 474370cb 2020-01-15 tracey goto done;
1742 474370cb 2020-01-15 tracey
1743 474370cb 2020-01-15 tracey for (;;) {
1744 65b95fb2 2020-01-15 tracey error = got_commit_graph_iter_next(&id1, graph, repo, NULL,
1745 65b95fb2 2020-01-15 tracey NULL);
1746 474370cb 2020-01-15 tracey if (error) {
1747 65b95fb2 2020-01-15 tracey if (error->code == GOT_ERR_ITER_COMPLETED)
1748 474370cb 2020-01-15 tracey error = NULL;
1749 65b95fb2 2020-01-15 tracey break;
1750 474370cb 2020-01-15 tracey }
1751 8087c3c5 2020-01-15 tracey if (id1 == NULL)
1752 474370cb 2020-01-15 tracey break;
1753 474370cb 2020-01-15 tracey
1754 8087c3c5 2020-01-15 tracey error = got_object_open_as_commit(&commit, repo, id1);
1755 474370cb 2020-01-15 tracey if (error)
1756 474370cb 2020-01-15 tracey break;
1757 474370cb 2020-01-15 tracey
1758 474370cb 2020-01-15 tracey if (search_pattern) {
1759 8087c3c5 2020-01-15 tracey error = match_logmsg(&have_match, id1, commit,
1760 474370cb 2020-01-15 tracey &regex);
1761 474370cb 2020-01-15 tracey if (error) {
1762 8087c3c5 2020-01-15 tracey got_object_commit_close(commit);
1763 474370cb 2020-01-15 tracey break;
1764 474370cb 2020-01-15 tracey }
1765 474370cb 2020-01-15 tracey if (have_match == 0) {
1766 8087c3c5 2020-01-15 tracey got_object_commit_close(commit);
1767 474370cb 2020-01-15 tracey continue;
1768 474370cb 2020-01-15 tracey }
1769 474370cb 2020-01-15 tracey }
1770 474370cb 2020-01-15 tracey
1771 474370cb 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
1772 8087c3c5 2020-01-15 tracey char *s;
1773 474370cb 2020-01-15 tracey const char *name;
1774 474370cb 2020-01-15 tracey struct got_tag_object *tag = NULL;
1775 474370cb 2020-01-15 tracey int cmp;
1776 474370cb 2020-01-15 tracey
1777 474370cb 2020-01-15 tracey name = got_ref_get_name(re->ref);
1778 474370cb 2020-01-15 tracey if (strcmp(name, GOT_REF_HEAD) == 0)
1779 474370cb 2020-01-15 tracey continue;
1780 474370cb 2020-01-15 tracey if (strncmp(name, "refs/", 5) == 0)
1781 474370cb 2020-01-15 tracey name += 5;
1782 474370cb 2020-01-15 tracey if (strncmp(name, "got/", 4) == 0)
1783 474370cb 2020-01-15 tracey continue;
1784 474370cb 2020-01-15 tracey if (strncmp(name, "heads/", 6) == 0)
1785 474370cb 2020-01-15 tracey name += 6;
1786 474370cb 2020-01-15 tracey if (strncmp(name, "remotes/", 8) == 0)
1787 474370cb 2020-01-15 tracey name += 8;
1788 474370cb 2020-01-15 tracey if (strncmp(name, "tags/", 5) == 0) {
1789 474370cb 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo,
1790 474370cb 2020-01-15 tracey re->id);
1791 474370cb 2020-01-15 tracey if (error) {
1792 474370cb 2020-01-15 tracey if (error->code != GOT_ERR_OBJ_TYPE)
1793 474370cb 2020-01-15 tracey continue;
1794 474370cb 2020-01-15 tracey /*
1795 474370cb 2020-01-15 tracey * Ref points at something other
1796 474370cb 2020-01-15 tracey * than a tag.
1797 474370cb 2020-01-15 tracey */
1798 474370cb 2020-01-15 tracey error = NULL;
1799 474370cb 2020-01-15 tracey tag = NULL;
1800 474370cb 2020-01-15 tracey }
1801 474370cb 2020-01-15 tracey }
1802 474370cb 2020-01-15 tracey cmp = got_object_id_cmp(tag ?
1803 8087c3c5 2020-01-15 tracey got_object_tag_get_object_id(tag) : re->id, id1);
1804 474370cb 2020-01-15 tracey if (tag)
1805 474370cb 2020-01-15 tracey got_object_tag_close(tag);
1806 474370cb 2020-01-15 tracey if (cmp != 0)
1807 474370cb 2020-01-15 tracey continue;
1808 8087c3c5 2020-01-15 tracey s = refs_str;
1809 8087c3c5 2020-01-15 tracey if ((asprintf(&refs_str, "%s%s%s", s ? s : "",
1810 8087c3c5 2020-01-15 tracey s ? ", " : "", name)) == -1) {
1811 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1812 8087c3c5 2020-01-15 tracey free(s);
1813 8087c3c5 2020-01-15 tracey goto done;
1814 8087c3c5 2020-01-15 tracey }
1815 8087c3c5 2020-01-15 tracey free(s);
1816 474370cb 2020-01-15 tracey }
1817 cdb914e5 2020-01-15 tracey
1818 8087c3c5 2020-01-15 tracey if (refs_str == NULL)
1819 8087c3c5 2020-01-15 tracey refs_str_disp = strdup("");
1820 8087c3c5 2020-01-15 tracey else {
1821 8087c3c5 2020-01-15 tracey if ((asprintf(&refs_str_disp, "(%s)",
1822 8087c3c5 2020-01-15 tracey refs_str)) == -1) {
1823 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1824 8087c3c5 2020-01-15 tracey free(refs_str);
1825 8087c3c5 2020-01-15 tracey goto done;
1826 8087c3c5 2020-01-15 tracey }
1827 8087c3c5 2020-01-15 tracey }
1828 474370cb 2020-01-15 tracey
1829 8087c3c5 2020-01-15 tracey error = got_object_id_str(&id_str1, id1);
1830 474370cb 2020-01-15 tracey if (error)
1831 8087c3c5 2020-01-15 tracey goto done;
1832 474370cb 2020-01-15 tracey
1833 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&treeid,
1834 87f9ebf5 2020-01-15 tracey got_object_commit_get_tree_id(commit));
1835 87f9ebf5 2020-01-15 tracey if (error)
1836 87f9ebf5 2020-01-15 tracey goto done;
1837 87f9ebf5 2020-01-15 tracey
1838 87f9ebf5 2020-01-15 tracey if (gw_trans->action == GW_COMMIT ||
1839 87f9ebf5 2020-01-15 tracey gw_trans->action == GW_COMMITDIFF) {
1840 8087c3c5 2020-01-15 tracey parent_id =
1841 8087c3c5 2020-01-15 tracey SIMPLEQ_FIRST(
1842 8087c3c5 2020-01-15 tracey got_object_commit_get_parent_ids(commit));
1843 8087c3c5 2020-01-15 tracey if (parent_id != NULL) {
1844 8087c3c5 2020-01-15 tracey id2 = got_object_id_dup(parent_id->id);
1845 8087c3c5 2020-01-15 tracey free (parent_id);
1846 8087c3c5 2020-01-15 tracey error = got_object_id_str(&id_str2, id2);
1847 8087c3c5 2020-01-15 tracey if (error)
1848 8087c3c5 2020-01-15 tracey goto done;
1849 87f9ebf5 2020-01-15 tracey free(id2);
1850 ec46ccd7 2020-01-15 tracey } else {
1851 ec46ccd7 2020-01-15 tracey has_parent = 0;
1852 8087c3c5 2020-01-15 tracey id_str2 = strdup("/dev/null");
1853 ec46ccd7 2020-01-15 tracey }
1854 8087c3c5 2020-01-15 tracey }
1855 8087c3c5 2020-01-15 tracey
1856 474370cb 2020-01-15 tracey committer_time =
1857 8087c3c5 2020-01-15 tracey got_object_commit_get_committer_time(commit);
1858 4ceb8155 2020-01-15 tracey
1859 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_parent, "%s", id_str2)) == -1) {
1860 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1861 8087c3c5 2020-01-15 tracey goto done;
1862 8087c3c5 2020-01-15 tracey }
1863 8087c3c5 2020-01-15 tracey
1864 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_tree, "%s", treeid)) == -1) {
1865 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1866 87f9ebf5 2020-01-15 tracey goto done;
1867 87f9ebf5 2020-01-15 tracey }
1868 87f9ebf5 2020-01-15 tracey
1869 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_tree_disp, commit_tree_html,
1870 87f9ebf5 2020-01-15 tracey treeid)) == -1) {
1871 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1872 87f9ebf5 2020-01-15 tracey goto done;
1873 87f9ebf5 2020-01-15 tracey }
1874 87f9ebf5 2020-01-15 tracey
1875 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_diff_disp, commit_diff_html, id_str2,
1876 8087c3c5 2020-01-15 tracey id_str1)) == -1) {
1877 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1878 8087c3c5 2020-01-15 tracey goto done;
1879 8087c3c5 2020-01-15 tracey }
1880 8087c3c5 2020-01-15 tracey
1881 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_commit, "%s", id_str1)) == -1) {
1882 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1883 8087c3c5 2020-01-15 tracey goto done;
1884 8087c3c5 2020-01-15 tracey }
1885 8087c3c5 2020-01-15 tracey
1886 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_commit_disp, commit_commit_html,
1887 8087c3c5 2020-01-15 tracey commit_commit, refs_str_disp)) == -1) {
1888 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1889 8087c3c5 2020-01-15 tracey goto done;
1890 8087c3c5 2020-01-15 tracey }
1891 8087c3c5 2020-01-15 tracey
1892 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_long, "%s",
1893 8087c3c5 2020-01-15 tracey gw_get_time_str(committer_time, TM_LONG))) == -1) {
1894 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1895 8087c3c5 2020-01-15 tracey goto done;
1896 8087c3c5 2020-01-15 tracey }
1897 8087c3c5 2020-01-15 tracey
1898 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_long_disp, commit_age_html,
1899 8087c3c5 2020-01-15 tracey commit_age_long)) == -1) {
1900 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1901 8087c3c5 2020-01-15 tracey goto done;
1902 8087c3c5 2020-01-15 tracey }
1903 8087c3c5 2020-01-15 tracey
1904 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_diff, "%s",
1905 8087c3c5 2020-01-15 tracey gw_get_time_str(committer_time, TM_DIFF))) == -1) {
1906 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1907 8087c3c5 2020-01-15 tracey goto done;
1908 8087c3c5 2020-01-15 tracey }
1909 8087c3c5 2020-01-15 tracey
1910 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_age_diff_disp, commit_age_html,
1911 8087c3c5 2020-01-15 tracey commit_age_diff)) == -1) {
1912 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1913 8087c3c5 2020-01-15 tracey goto done;
1914 8087c3c5 2020-01-15 tracey }
1915 8087c3c5 2020-01-15 tracey
1916 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_author, "%s",
1917 8087c3c5 2020-01-15 tracey got_object_commit_get_author(commit))) == -1) {
1918 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1919 8087c3c5 2020-01-15 tracey goto done;
1920 8087c3c5 2020-01-15 tracey }
1921 8087c3c5 2020-01-15 tracey
1922 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_author_disp, commit_author_html,
1923 8087c3c5 2020-01-15 tracey gw_html_escape(commit_author))) == -1) {
1924 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1925 8087c3c5 2020-01-15 tracey goto done;
1926 8087c3c5 2020-01-15 tracey }
1927 8087c3c5 2020-01-15 tracey
1928 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_committer, "%s",
1929 8087c3c5 2020-01-15 tracey got_object_commit_get_committer(commit))) == -1) {
1930 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1931 8087c3c5 2020-01-15 tracey goto done;
1932 8087c3c5 2020-01-15 tracey }
1933 8087c3c5 2020-01-15 tracey
1934 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_committer_disp, commit_committer_html,
1935 8087c3c5 2020-01-15 tracey gw_html_escape(commit_committer))) == -1) {
1936 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1937 8087c3c5 2020-01-15 tracey goto done;
1938 8087c3c5 2020-01-15 tracey }
1939 8087c3c5 2020-01-15 tracey
1940 8087c3c5 2020-01-15 tracey if (strcmp(commit_author, commit_committer) == 0) {
1941 8087c3c5 2020-01-15 tracey free(commit_committer_disp);
1942 8087c3c5 2020-01-15 tracey commit_committer_disp = strdup("");
1943 8087c3c5 2020-01-15 tracey }
1944 8087c3c5 2020-01-15 tracey
1945 8087c3c5 2020-01-15 tracey error = got_object_commit_get_logmsg(&commit_log0, commit);
1946 8087c3c5 2020-01-15 tracey if (error)
1947 8087c3c5 2020-01-15 tracey goto done;
1948 87f9ebf5 2020-01-15 tracey
1949 87f9ebf5 2020-01-15 tracey commit_log = commit_log0;
1950 87f9ebf5 2020-01-15 tracey while (*commit_log == '\n')
1951 87f9ebf5 2020-01-15 tracey commit_log++;
1952 8087c3c5 2020-01-15 tracey
1953 8087c3c5 2020-01-15 tracey switch(log_type) {
1954 8087c3c5 2020-01-15 tracey case (LOGBRIEF):
1955 8087c3c5 2020-01-15 tracey newline = strchr(commit_log, '\n');
1956 8087c3c5 2020-01-15 tracey if (newline)
1957 8087c3c5 2020-01-15 tracey *newline = '\0';
1958 8087c3c5 2020-01-15 tracey
1959 8087c3c5 2020-01-15 tracey if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1960 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1, gw_trans->repo_name,
1961 8087c3c5 2020-01-15 tracey id_str1, gw_trans->repo_name, id_str1,
1962 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1)) == -1) {
1963 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1964 8087c3c5 2020-01-15 tracey goto done;
1965 4ceb8155 2020-01-15 tracey }
1966 8087c3c5 2020-01-15 tracey
1967 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_row, logbriefs_row,
1968 8087c3c5 2020-01-15 tracey commit_age_diff, commit_author, commit_log,
1969 8087c3c5 2020-01-15 tracey logbriefs_navs_html)) == -1) {
1970 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1971 8087c3c5 2020-01-15 tracey goto done;
1972 8087c3c5 2020-01-15 tracey }
1973 8087c3c5 2020-01-15 tracey
1974 8087c3c5 2020-01-15 tracey free(logbriefs_navs_html);
1975 8087c3c5 2020-01-15 tracey logbriefs_navs_html = NULL;
1976 8087c3c5 2020-01-15 tracey break;
1977 8087c3c5 2020-01-15 tracey case (LOGFULL):
1978 8087c3c5 2020-01-15 tracey if ((asprintf(&logbriefs_navs_html, logbriefs_navs,
1979 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1, gw_trans->repo_name,
1980 8087c3c5 2020-01-15 tracey id_str1, gw_trans->repo_name, id_str1,
1981 8087c3c5 2020-01-15 tracey gw_trans->repo_name, id_str1)) == -1) {
1982 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1983 8087c3c5 2020-01-15 tracey goto done;
1984 8087c3c5 2020-01-15 tracey }
1985 8087c3c5 2020-01-15 tracey
1986 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_row, logs_row, commit_commit_disp,
1987 8087c3c5 2020-01-15 tracey commit_author_disp, commit_committer_disp,
1988 8087c3c5 2020-01-15 tracey commit_age_long_disp, gw_html_escape(commit_log),
1989 8087c3c5 2020-01-15 tracey logbriefs_navs_html)) == -1) {
1990 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
1991 8087c3c5 2020-01-15 tracey goto done;
1992 8087c3c5 2020-01-15 tracey }
1993 8087c3c5 2020-01-15 tracey
1994 8087c3c5 2020-01-15 tracey free(logbriefs_navs_html);
1995 8087c3c5 2020-01-15 tracey logbriefs_navs_html = NULL;
1996 8087c3c5 2020-01-15 tracey break;
1997 b772de24 2020-01-15 tracey case (LOGTAG):
1998 077f6c5a 2020-01-15 tracey log_tag_html = strdup("tag log here");
1999 b772de24 2020-01-15 tracey
2000 077f6c5a 2020-01-15 tracey if ((asprintf(&commit_row, log_tag_row,
2001 077f6c5a 2020-01-15 tracey gw_html_escape(commit_log), log_tag_html)) == -1) {
2002 b772de24 2020-01-15 tracey error = got_error_from_errno("asprintf");
2003 b772de24 2020-01-15 tracey goto done;
2004 b772de24 2020-01-15 tracey }
2005 b772de24 2020-01-15 tracey
2006 077f6c5a 2020-01-15 tracey free(log_tag_html);
2007 b772de24 2020-01-15 tracey break;
2008 ec46ccd7 2020-01-15 tracey case (LOGBLAME):
2009 ec46ccd7 2020-01-15 tracey log_blame_html = gw_get_file_blame(gw_trans,
2010 ec46ccd7 2020-01-15 tracey start_commit);
2011 ec46ccd7 2020-01-15 tracey
2012 ec46ccd7 2020-01-15 tracey if ((asprintf(&commit_row, log_blame_row,
2013 ec46ccd7 2020-01-15 tracey gw_html_escape(commit_log), log_blame_html)) == -1) {
2014 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
2015 ec46ccd7 2020-01-15 tracey goto done;
2016 ec46ccd7 2020-01-15 tracey }
2017 ec46ccd7 2020-01-15 tracey
2018 ec46ccd7 2020-01-15 tracey free(log_blame_html);
2019 ec46ccd7 2020-01-15 tracey break;
2020 8087c3c5 2020-01-15 tracey case (LOGTREE):
2021 bcbc97d8 2020-01-15 tracey log_tree_html = gw_get_repo_tree(gw_trans,
2022 bcbc97d8 2020-01-15 tracey start_commit);
2023 8087c3c5 2020-01-15 tracey
2024 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_row, log_tree_row,
2025 87f9ebf5 2020-01-15 tracey gw_html_escape(commit_log), log_tree_html)) == -1) {
2026 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2027 8087c3c5 2020-01-15 tracey goto done;
2028 8087c3c5 2020-01-15 tracey }
2029 8087c3c5 2020-01-15 tracey
2030 8087c3c5 2020-01-15 tracey free(log_tree_html);
2031 8087c3c5 2020-01-15 tracey break;
2032 8087c3c5 2020-01-15 tracey case (LOGCOMMIT):
2033 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_log_disp, commit_log_html,
2034 8087c3c5 2020-01-15 tracey gw_html_escape(commit_log))) == -1) {
2035 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2036 8087c3c5 2020-01-15 tracey goto done;
2037 8087c3c5 2020-01-15 tracey }
2038 8087c3c5 2020-01-15 tracey
2039 8087c3c5 2020-01-15 tracey log_commit_html = strdup("commit here");
2040 8087c3c5 2020-01-15 tracey
2041 8087c3c5 2020-01-15 tracey if ((asprintf(&commit_row, log_commit_row,
2042 8087c3c5 2020-01-15 tracey commit_diff_disp, commit_commit_disp,
2043 87f9ebf5 2020-01-15 tracey commit_tree_disp, commit_author_disp,
2044 87f9ebf5 2020-01-15 tracey commit_committer_disp, commit_age_long_disp,
2045 87f9ebf5 2020-01-15 tracey commit_log_disp, log_commit_html)) == -1) {
2046 8087c3c5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2047 8087c3c5 2020-01-15 tracey goto done;
2048 8087c3c5 2020-01-15 tracey }
2049 8087c3c5 2020-01-15 tracey free(commit_log_disp);
2050 8087c3c5 2020-01-15 tracey free(log_commit_html);
2051 8087c3c5 2020-01-15 tracey
2052 8087c3c5 2020-01-15 tracey break;
2053 87f9ebf5 2020-01-15 tracey case (LOGDIFF):
2054 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_log_disp, commit_log_html,
2055 87f9ebf5 2020-01-15 tracey gw_html_escape(commit_log))) == -1) {
2056 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2057 87f9ebf5 2020-01-15 tracey goto done;
2058 87f9ebf5 2020-01-15 tracey }
2059 87f9ebf5 2020-01-15 tracey
2060 ec46ccd7 2020-01-15 tracey if (has_parent)
2061 ec46ccd7 2020-01-15 tracey log_diff_html = gw_get_repo_diff(gw_trans,
2062 ec46ccd7 2020-01-15 tracey commit_commit, commit_parent);
2063 ec46ccd7 2020-01-15 tracey else
2064 ec46ccd7 2020-01-15 tracey log_diff_html = gw_get_repo_diff(gw_trans,
2065 ec46ccd7 2020-01-15 tracey commit_commit, NULL);
2066 87f9ebf5 2020-01-15 tracey
2067 87f9ebf5 2020-01-15 tracey if ((asprintf(&commit_row, log_diff_row,
2068 87f9ebf5 2020-01-15 tracey commit_diff_disp, commit_commit_disp,
2069 87f9ebf5 2020-01-15 tracey commit_tree_disp, commit_author_disp,
2070 87f9ebf5 2020-01-15 tracey commit_committer_disp, commit_age_long_disp,
2071 87f9ebf5 2020-01-15 tracey commit_log_disp, log_diff_html)) == -1) {
2072 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2073 87f9ebf5 2020-01-15 tracey goto done;
2074 87f9ebf5 2020-01-15 tracey }
2075 87f9ebf5 2020-01-15 tracey free(commit_log_disp);
2076 87f9ebf5 2020-01-15 tracey free(log_diff_html);
2077 87f9ebf5 2020-01-15 tracey
2078 87f9ebf5 2020-01-15 tracey break;
2079 8087c3c5 2020-01-15 tracey default:
2080 8087c3c5 2020-01-15 tracey return NULL;
2081 9d84e7dd 2020-01-15 tracey }
2082 474370cb 2020-01-15 tracey
2083 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, commit_row);
2084 8087c3c5 2020-01-15 tracey
2085 8087c3c5 2020-01-15 tracey free(commit_parent);
2086 8087c3c5 2020-01-15 tracey free(commit_diff_disp);
2087 87f9ebf5 2020-01-15 tracey free(commit_tree_disp);
2088 8087c3c5 2020-01-15 tracey free(commit_age_diff);
2089 8087c3c5 2020-01-15 tracey free(commit_age_diff_disp);
2090 8087c3c5 2020-01-15 tracey free(commit_age_long);
2091 8087c3c5 2020-01-15 tracey free(commit_age_long_disp);
2092 474370cb 2020-01-15 tracey free(commit_author);
2093 8087c3c5 2020-01-15 tracey free(commit_author_disp);
2094 8087c3c5 2020-01-15 tracey free(commit_committer);
2095 8087c3c5 2020-01-15 tracey free(commit_committer_disp);
2096 9d84e7dd 2020-01-15 tracey free(commit_log0);
2097 474370cb 2020-01-15 tracey free(commit_row);
2098 8087c3c5 2020-01-15 tracey free(refs_str_disp);
2099 8087c3c5 2020-01-15 tracey free(refs_str);
2100 8087c3c5 2020-01-15 tracey refs_str = NULL;
2101 8087c3c5 2020-01-15 tracey free(id_str1);
2102 8087c3c5 2020-01-15 tracey id_str1 = NULL;
2103 8087c3c5 2020-01-15 tracey free(id_str2);
2104 8087c3c5 2020-01-15 tracey id_str2 = NULL;
2105 474370cb 2020-01-15 tracey
2106 474370cb 2020-01-15 tracey if (error || (limit && --limit == 0))
2107 474370cb 2020-01-15 tracey break;
2108 474370cb 2020-01-15 tracey }
2109 c6b62706 2020-01-15 tracey
2110 8087c3c5 2020-01-15 tracey if (error)
2111 8087c3c5 2020-01-15 tracey goto done;
2112 474370cb 2020-01-15 tracey
2113 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2114 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2115 6c6c85af 2020-01-15 tracey logs = strdup(buf_get(diffbuf));
2116 6c6c85af 2020-01-15 tracey }
2117 8087c3c5 2020-01-15 tracey done:
2118 8087c3c5 2020-01-15 tracey buf_free(diffbuf);
2119 2e676fc5 2020-01-15 tracey free(in_repo_path);
2120 8087c3c5 2020-01-15 tracey if (commit != NULL)
2121 8087c3c5 2020-01-15 tracey got_object_commit_close(commit);
2122 8087c3c5 2020-01-15 tracey if (search_pattern)
2123 8087c3c5 2020-01-15 tracey regfree(&regex);
2124 8087c3c5 2020-01-15 tracey if (graph)
2125 8087c3c5 2020-01-15 tracey got_commit_graph_close(graph);
2126 474370cb 2020-01-15 tracey if (repo) {
2127 474370cb 2020-01-15 tracey error = got_repo_close(repo);
2128 ec46ccd7 2020-01-15 tracey if (error)
2129 474370cb 2020-01-15 tracey return NULL;
2130 474370cb 2020-01-15 tracey }
2131 8087c3c5 2020-01-15 tracey if (error) {
2132 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, "Error: ");
2133 8087c3c5 2020-01-15 tracey khttp_puts(gw_trans->gw_req, error->msg);
2134 8087c3c5 2020-01-15 tracey return NULL;
2135 8087c3c5 2020-01-15 tracey } else
2136 8087c3c5 2020-01-15 tracey return logs;
2137 2c251c14 2020-01-15 tracey }
2138 2c251c14 2020-01-15 tracey
2139 2c251c14 2020-01-15 tracey static char *
2140 87f9ebf5 2020-01-15 tracey gw_get_repo_tags(struct trans *gw_trans, int limit, int tag_type)
2141 8d4d2453 2020-01-15 tracey {
2142 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
2143 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
2144 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
2145 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
2146 87f9ebf5 2020-01-15 tracey char *tags = NULL, *tag_row = NULL, *tags_navs_disp = NULL,
2147 87f9ebf5 2020-01-15 tracey *age = NULL;
2148 87f9ebf5 2020-01-15 tracey char *newline;
2149 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
2150 87f9ebf5 2020-01-15 tracey size_t newsize;
2151 8d4d2453 2020-01-15 tracey
2152 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2153 ec46ccd7 2020-01-15 tracey if (error)
2154 6c6c85af 2020-01-15 tracey return NULL;
2155 6c6c85af 2020-01-15 tracey SIMPLEQ_INIT(&refs);
2156 87f9ebf5 2020-01-15 tracey
2157 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2158 ec46ccd7 2020-01-15 tracey if (error)
2159 87f9ebf5 2020-01-15 tracey goto done;
2160 87f9ebf5 2020-01-15 tracey
2161 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
2162 87f9ebf5 2020-01-15 tracey if (error)
2163 87f9ebf5 2020-01-15 tracey goto done;
2164 87f9ebf5 2020-01-15 tracey
2165 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2166 87f9ebf5 2020-01-15 tracey const char *refname;
2167 87f9ebf5 2020-01-15 tracey char *refstr, *tag_log0, *tag_log, *id_str;
2168 87f9ebf5 2020-01-15 tracey time_t tagger_time;
2169 87f9ebf5 2020-01-15 tracey struct got_object_id *id;
2170 87f9ebf5 2020-01-15 tracey struct got_tag_object *tag;
2171 87f9ebf5 2020-01-15 tracey
2172 87f9ebf5 2020-01-15 tracey refname = got_ref_get_name(re->ref);
2173 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/tags/", 10) != 0)
2174 87f9ebf5 2020-01-15 tracey continue;
2175 87f9ebf5 2020-01-15 tracey refname += 10;
2176 87f9ebf5 2020-01-15 tracey refstr = got_ref_to_str(re->ref);
2177 87f9ebf5 2020-01-15 tracey if (refstr == NULL) {
2178 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
2179 87f9ebf5 2020-01-15 tracey goto done;
2180 87f9ebf5 2020-01-15 tracey }
2181 87f9ebf5 2020-01-15 tracey
2182 87f9ebf5 2020-01-15 tracey error = got_ref_resolve(&id, repo, re->ref);
2183 87f9ebf5 2020-01-15 tracey if (error)
2184 87f9ebf5 2020-01-15 tracey goto done;
2185 87f9ebf5 2020-01-15 tracey error = got_object_open_as_tag(&tag, repo, id);
2186 87f9ebf5 2020-01-15 tracey free(id);
2187 87f9ebf5 2020-01-15 tracey if (error)
2188 87f9ebf5 2020-01-15 tracey goto done;
2189 87f9ebf5 2020-01-15 tracey
2190 87f9ebf5 2020-01-15 tracey tagger_time = got_object_tag_get_tagger_time(tag);
2191 87f9ebf5 2020-01-15 tracey
2192 87f9ebf5 2020-01-15 tracey error = got_object_id_str(&id_str,
2193 87f9ebf5 2020-01-15 tracey got_object_tag_get_object_id(tag));
2194 87f9ebf5 2020-01-15 tracey if (error)
2195 87f9ebf5 2020-01-15 tracey goto done;
2196 87f9ebf5 2020-01-15 tracey
2197 87f9ebf5 2020-01-15 tracey tag_log0 = strdup(got_object_tag_get_message(tag));
2198 87f9ebf5 2020-01-15 tracey
2199 87f9ebf5 2020-01-15 tracey if (tag_log0 == NULL) {
2200 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("strdup");
2201 87f9ebf5 2020-01-15 tracey goto done;
2202 87f9ebf5 2020-01-15 tracey }
2203 87f9ebf5 2020-01-15 tracey
2204 87f9ebf5 2020-01-15 tracey tag_log = tag_log0;
2205 87f9ebf5 2020-01-15 tracey while (*tag_log == '\n')
2206 87f9ebf5 2020-01-15 tracey tag_log++;
2207 87f9ebf5 2020-01-15 tracey
2208 87f9ebf5 2020-01-15 tracey switch (tag_type) {
2209 87f9ebf5 2020-01-15 tracey case TAGBRIEF:
2210 87f9ebf5 2020-01-15 tracey newline = strchr(tag_log, '\n');
2211 87f9ebf5 2020-01-15 tracey if (newline)
2212 87f9ebf5 2020-01-15 tracey *newline = '\0';
2213 87f9ebf5 2020-01-15 tracey
2214 87f9ebf5 2020-01-15 tracey if ((asprintf(&age, "%s", gw_get_time_str(tagger_time,
2215 87f9ebf5 2020-01-15 tracey TM_DIFF))) == -1) {
2216 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2217 87f9ebf5 2020-01-15 tracey goto done;
2218 87f9ebf5 2020-01-15 tracey }
2219 87f9ebf5 2020-01-15 tracey
2220 87f9ebf5 2020-01-15 tracey if ((asprintf(&tags_navs_disp, tags_navs,
2221 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str, gw_trans->repo_name,
2222 87f9ebf5 2020-01-15 tracey id_str, gw_trans->repo_name, id_str,
2223 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, id_str)) == -1) {
2224 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2225 87f9ebf5 2020-01-15 tracey goto done;
2226 87f9ebf5 2020-01-15 tracey }
2227 87f9ebf5 2020-01-15 tracey
2228 87f9ebf5 2020-01-15 tracey if ((asprintf(&tag_row, tags_row, age, refname, tag_log,
2229 87f9ebf5 2020-01-15 tracey tags_navs_disp)) == -1) {
2230 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2231 87f9ebf5 2020-01-15 tracey goto done;
2232 87f9ebf5 2020-01-15 tracey }
2233 87f9ebf5 2020-01-15 tracey
2234 87f9ebf5 2020-01-15 tracey free(tags_navs_disp);
2235 87f9ebf5 2020-01-15 tracey break;
2236 87f9ebf5 2020-01-15 tracey case TAGFULL:
2237 87f9ebf5 2020-01-15 tracey break;
2238 87f9ebf5 2020-01-15 tracey default:
2239 87f9ebf5 2020-01-15 tracey break;
2240 87f9ebf5 2020-01-15 tracey }
2241 87f9ebf5 2020-01-15 tracey
2242 6c6c85af 2020-01-15 tracey got_object_tag_close(tag);
2243 87f9ebf5 2020-01-15 tracey
2244 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tag_row);
2245 87f9ebf5 2020-01-15 tracey
2246 6c6c85af 2020-01-15 tracey free(id_str);
2247 6c6c85af 2020-01-15 tracey free(refstr);
2248 6c6c85af 2020-01-15 tracey free(age);
2249 87f9ebf5 2020-01-15 tracey free(tag_log0);
2250 87f9ebf5 2020-01-15 tracey free(tag_row);
2251 87f9ebf5 2020-01-15 tracey
2252 87f9ebf5 2020-01-15 tracey if (error || (limit && --limit == 0))
2253 87f9ebf5 2020-01-15 tracey break;
2254 87f9ebf5 2020-01-15 tracey }
2255 6c6c85af 2020-01-15 tracey
2256 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2257 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2258 6c6c85af 2020-01-15 tracey tags = strdup(buf_get(diffbuf));
2259 6c6c85af 2020-01-15 tracey }
2260 87f9ebf5 2020-01-15 tracey done:
2261 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
2262 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
2263 87f9ebf5 2020-01-15 tracey if (repo)
2264 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
2265 87f9ebf5 2020-01-15 tracey if (error)
2266 87f9ebf5 2020-01-15 tracey return NULL;
2267 87f9ebf5 2020-01-15 tracey else
2268 87f9ebf5 2020-01-15 tracey return tags;
2269 ec46ccd7 2020-01-15 tracey }
2270 ec46ccd7 2020-01-15 tracey
2271 ec46ccd7 2020-01-15 tracey struct blame_line {
2272 ec46ccd7 2020-01-15 tracey int annotated;
2273 ec46ccd7 2020-01-15 tracey char *id_str;
2274 ec46ccd7 2020-01-15 tracey char *committer;
2275 ec46ccd7 2020-01-15 tracey char datebuf[11]; /* YYYY-MM-DD + NUL */
2276 ec46ccd7 2020-01-15 tracey };
2277 ec46ccd7 2020-01-15 tracey
2278 ec46ccd7 2020-01-15 tracey struct blame_cb_args {
2279 ec46ccd7 2020-01-15 tracey struct blame_line *lines;
2280 ec46ccd7 2020-01-15 tracey int nlines;
2281 ec46ccd7 2020-01-15 tracey int nlines_prec;
2282 ec46ccd7 2020-01-15 tracey int lineno_cur;
2283 ec46ccd7 2020-01-15 tracey off_t *line_offsets;
2284 ec46ccd7 2020-01-15 tracey FILE *f;
2285 ec46ccd7 2020-01-15 tracey struct got_repository *repo;
2286 2e676fc5 2020-01-15 tracey struct trans *gw_trans;
2287 2e676fc5 2020-01-15 tracey struct buf *blamebuf;
2288 ec46ccd7 2020-01-15 tracey };
2289 ec46ccd7 2020-01-15 tracey
2290 ec46ccd7 2020-01-15 tracey static const struct got_error *
2291 ec46ccd7 2020-01-15 tracey blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2292 ec46ccd7 2020-01-15 tracey {
2293 ec46ccd7 2020-01-15 tracey const struct got_error *err = NULL;
2294 ec46ccd7 2020-01-15 tracey struct blame_cb_args *a = arg;
2295 ec46ccd7 2020-01-15 tracey struct blame_line *bline;
2296 ec46ccd7 2020-01-15 tracey char *line = NULL;
2297 2e676fc5 2020-01-15 tracey size_t linesize = 0, newsize;
2298 ec46ccd7 2020-01-15 tracey struct got_commit_object *commit = NULL;
2299 ec46ccd7 2020-01-15 tracey off_t offset;
2300 ec46ccd7 2020-01-15 tracey struct tm tm;
2301 ec46ccd7 2020-01-15 tracey time_t committer_time;
2302 ec46ccd7 2020-01-15 tracey
2303 ec46ccd7 2020-01-15 tracey if (nlines != a->nlines ||
2304 ec46ccd7 2020-01-15 tracey (lineno != -1 && lineno < 1) || lineno > a->nlines)
2305 ec46ccd7 2020-01-15 tracey return got_error(GOT_ERR_RANGE);
2306 ec46ccd7 2020-01-15 tracey
2307 ec46ccd7 2020-01-15 tracey if (lineno == -1)
2308 ec46ccd7 2020-01-15 tracey return NULL; /* no change in this commit */
2309 ec46ccd7 2020-01-15 tracey
2310 ec46ccd7 2020-01-15 tracey /* Annotate this line. */
2311 ec46ccd7 2020-01-15 tracey bline = &a->lines[lineno - 1];
2312 ec46ccd7 2020-01-15 tracey if (bline->annotated)
2313 ec46ccd7 2020-01-15 tracey return NULL;
2314 ec46ccd7 2020-01-15 tracey err = got_object_id_str(&bline->id_str, id);
2315 ec46ccd7 2020-01-15 tracey if (err)
2316 ec46ccd7 2020-01-15 tracey return err;
2317 ec46ccd7 2020-01-15 tracey
2318 ec46ccd7 2020-01-15 tracey err = got_object_open_as_commit(&commit, a->repo, id);
2319 ec46ccd7 2020-01-15 tracey if (err)
2320 ec46ccd7 2020-01-15 tracey goto done;
2321 ec46ccd7 2020-01-15 tracey
2322 ec46ccd7 2020-01-15 tracey bline->committer = strdup(got_object_commit_get_committer(commit));
2323 ec46ccd7 2020-01-15 tracey if (bline->committer == NULL) {
2324 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("strdup");
2325 ec46ccd7 2020-01-15 tracey goto done;
2326 ec46ccd7 2020-01-15 tracey }
2327 ec46ccd7 2020-01-15 tracey
2328 ec46ccd7 2020-01-15 tracey committer_time = got_object_commit_get_committer_time(commit);
2329 ec46ccd7 2020-01-15 tracey if (localtime_r(&committer_time, &tm) == NULL)
2330 ec46ccd7 2020-01-15 tracey return got_error_from_errno("localtime_r");
2331 ec46ccd7 2020-01-15 tracey if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2332 ec46ccd7 2020-01-15 tracey &tm) >= sizeof(bline->datebuf)) {
2333 ec46ccd7 2020-01-15 tracey err = got_error(GOT_ERR_NO_SPACE);
2334 ec46ccd7 2020-01-15 tracey goto done;
2335 ec46ccd7 2020-01-15 tracey }
2336 ec46ccd7 2020-01-15 tracey bline->annotated = 1;
2337 ec46ccd7 2020-01-15 tracey
2338 ec46ccd7 2020-01-15 tracey /* Print lines annotated so far. */
2339 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2340 ec46ccd7 2020-01-15 tracey if (!bline->annotated)
2341 ec46ccd7 2020-01-15 tracey goto done;
2342 ec46ccd7 2020-01-15 tracey
2343 ec46ccd7 2020-01-15 tracey offset = a->line_offsets[a->lineno_cur - 1];
2344 ec46ccd7 2020-01-15 tracey if (fseeko(a->f, offset, SEEK_SET) == -1) {
2345 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("fseeko");
2346 ec46ccd7 2020-01-15 tracey goto done;
2347 ec46ccd7 2020-01-15 tracey }
2348 ec46ccd7 2020-01-15 tracey
2349 ec46ccd7 2020-01-15 tracey while (bline->annotated) {
2350 2e676fc5 2020-01-15 tracey char *smallerthan, *at, *nl, *committer, *blame_row = NULL;
2351 ec46ccd7 2020-01-15 tracey size_t len;
2352 ec46ccd7 2020-01-15 tracey
2353 ec46ccd7 2020-01-15 tracey if (getline(&line, &linesize, a->f) == -1) {
2354 ec46ccd7 2020-01-15 tracey if (ferror(a->f))
2355 ec46ccd7 2020-01-15 tracey err = got_error_from_errno("getline");
2356 ec46ccd7 2020-01-15 tracey break;
2357 ec46ccd7 2020-01-15 tracey }
2358 ec46ccd7 2020-01-15 tracey
2359 ec46ccd7 2020-01-15 tracey committer = bline->committer;
2360 ec46ccd7 2020-01-15 tracey smallerthan = strchr(committer, '<');
2361 ec46ccd7 2020-01-15 tracey if (smallerthan && smallerthan[1] != '\0')
2362 ec46ccd7 2020-01-15 tracey committer = smallerthan + 1;
2363 ec46ccd7 2020-01-15 tracey at = strchr(committer, '@');
2364 ec46ccd7 2020-01-15 tracey if (at)
2365 ec46ccd7 2020-01-15 tracey *at = '\0';
2366 ec46ccd7 2020-01-15 tracey len = strlen(committer);
2367 ec46ccd7 2020-01-15 tracey if (len >= 9)
2368 ec46ccd7 2020-01-15 tracey committer[8] = '\0';
2369 ec46ccd7 2020-01-15 tracey
2370 ec46ccd7 2020-01-15 tracey nl = strchr(line, '\n');
2371 ec46ccd7 2020-01-15 tracey if (nl)
2372 ec46ccd7 2020-01-15 tracey *nl = '\0';
2373 2e676fc5 2020-01-15 tracey asprintf(&blame_row, log_blame_line, a->nlines_prec,
2374 2e676fc5 2020-01-15 tracey a->lineno_cur, bline->id_str, bline->datebuf, committer,
2375 2e676fc5 2020-01-15 tracey line);
2376 ec46ccd7 2020-01-15 tracey a->lineno_cur++;
2377 2e676fc5 2020-01-15 tracey err = buf_puts(&newsize, a->blamebuf, blame_row);
2378 2e676fc5 2020-01-15 tracey if (err)
2379 2e676fc5 2020-01-15 tracey return err;
2380 2e676fc5 2020-01-15 tracey
2381 ec46ccd7 2020-01-15 tracey bline = &a->lines[a->lineno_cur - 1];
2382 2e676fc5 2020-01-15 tracey free(blame_row);
2383 ec46ccd7 2020-01-15 tracey }
2384 ec46ccd7 2020-01-15 tracey done:
2385 ec46ccd7 2020-01-15 tracey if (commit)
2386 ec46ccd7 2020-01-15 tracey got_object_commit_close(commit);
2387 ec46ccd7 2020-01-15 tracey free(line);
2388 ec46ccd7 2020-01-15 tracey return err;
2389 8d4d2453 2020-01-15 tracey }
2390 bcbc97d8 2020-01-15 tracey
2391 bcbc97d8 2020-01-15 tracey static char*
2392 ec46ccd7 2020-01-15 tracey gw_get_file_blame(struct trans *gw_trans, char *commit_str)
2393 bcbc97d8 2020-01-15 tracey {
2394 bcbc97d8 2020-01-15 tracey const struct got_error *error = NULL;
2395 bcbc97d8 2020-01-15 tracey struct got_repository *repo = NULL;
2396 ec46ccd7 2020-01-15 tracey struct got_object_id *obj_id = NULL;
2397 ec46ccd7 2020-01-15 tracey struct got_object_id *commit_id = NULL;
2398 ec46ccd7 2020-01-15 tracey struct got_blob_object *blob = NULL;
2399 2e676fc5 2020-01-15 tracey char *blame_html = NULL, *path = NULL, *in_repo_path = NULL,
2400 119bf4ed 2020-01-15 tracey *folder = NULL;
2401 ec46ccd7 2020-01-15 tracey struct blame_cb_args bca;
2402 119bf4ed 2020-01-15 tracey int i, obj_type;
2403 ec46ccd7 2020-01-15 tracey size_t filesize;
2404 ec46ccd7 2020-01-15 tracey
2405 ec46ccd7 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2406 ec46ccd7 2020-01-15 tracey if (error)
2407 ec46ccd7 2020-01-15 tracey goto done;
2408 ec46ccd7 2020-01-15 tracey
2409 2e676fc5 2020-01-15 tracey if (gw_trans->repo_folder != NULL) {
2410 2e676fc5 2020-01-15 tracey if ((asprintf(&folder, "%s/", gw_trans->repo_folder)) == -1) {
2411 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2412 2e676fc5 2020-01-15 tracey goto done;
2413 2e676fc5 2020-01-15 tracey }
2414 2e676fc5 2020-01-15 tracey } else
2415 2e676fc5 2020-01-15 tracey folder = strdup("");
2416 2e676fc5 2020-01-15 tracey
2417 2e676fc5 2020-01-15 tracey if ((asprintf(&path, "%s%s", folder, gw_trans->repo_file)) == -1) {
2418 2e676fc5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2419 2e676fc5 2020-01-15 tracey goto done;
2420 2e676fc5 2020-01-15 tracey }
2421 2e676fc5 2020-01-15 tracey free(folder);
2422 2e676fc5 2020-01-15 tracey
2423 2e676fc5 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, path, 1);
2424 ec46ccd7 2020-01-15 tracey if (error)
2425 ec46ccd7 2020-01-15 tracey goto done;
2426 ec46ccd7 2020-01-15 tracey
2427 ec46ccd7 2020-01-15 tracey error = resolve_commit_arg(&commit_id, commit_str, repo);
2428 ec46ccd7 2020-01-15 tracey if (error)
2429 ec46ccd7 2020-01-15 tracey goto done;
2430 ec46ccd7 2020-01-15 tracey
2431 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2432 ec46ccd7 2020-01-15 tracey if (error)
2433 ec46ccd7 2020-01-15 tracey goto done;
2434 2e676fc5 2020-01-15 tracey
2435 ec46ccd7 2020-01-15 tracey if (obj_id == NULL) {
2436 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_NO_OBJ);
2437 ec46ccd7 2020-01-15 tracey goto done;
2438 ec46ccd7 2020-01-15 tracey }
2439 ec46ccd7 2020-01-15 tracey
2440 ec46ccd7 2020-01-15 tracey error = got_object_get_type(&obj_type, repo, obj_id);
2441 ec46ccd7 2020-01-15 tracey if (error)
2442 ec46ccd7 2020-01-15 tracey goto done;
2443 ec46ccd7 2020-01-15 tracey
2444 ec46ccd7 2020-01-15 tracey if (obj_type != GOT_OBJ_TYPE_BLOB) {
2445 ec46ccd7 2020-01-15 tracey error = got_error(GOT_ERR_OBJ_TYPE);
2446 ec46ccd7 2020-01-15 tracey goto done;
2447 ec46ccd7 2020-01-15 tracey }
2448 ec46ccd7 2020-01-15 tracey
2449 ec46ccd7 2020-01-15 tracey error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2450 ec46ccd7 2020-01-15 tracey if (error)
2451 ec46ccd7 2020-01-15 tracey goto done;
2452 ec46ccd7 2020-01-15 tracey
2453 2e676fc5 2020-01-15 tracey error = buf_alloc(&bca.blamebuf, 0);
2454 2e676fc5 2020-01-15 tracey if (error)
2455 2e676fc5 2020-01-15 tracey goto done;
2456 2e676fc5 2020-01-15 tracey
2457 ec46ccd7 2020-01-15 tracey bca.f = got_opentemp();
2458 ec46ccd7 2020-01-15 tracey if (bca.f == NULL) {
2459 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("got_opentemp");
2460 ec46ccd7 2020-01-15 tracey goto done;
2461 ec46ccd7 2020-01-15 tracey }
2462 ec46ccd7 2020-01-15 tracey error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2463 ec46ccd7 2020-01-15 tracey &bca.line_offsets, bca.f, blob);
2464 ec46ccd7 2020-01-15 tracey if (error || bca.nlines == 0)
2465 ec46ccd7 2020-01-15 tracey goto done;
2466 ec46ccd7 2020-01-15 tracey
2467 ec46ccd7 2020-01-15 tracey /* Don't include \n at EOF in the blame line count. */
2468 ec46ccd7 2020-01-15 tracey if (bca.line_offsets[bca.nlines - 1] == filesize)
2469 ec46ccd7 2020-01-15 tracey bca.nlines--;
2470 ec46ccd7 2020-01-15 tracey
2471 ec46ccd7 2020-01-15 tracey bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2472 ec46ccd7 2020-01-15 tracey if (bca.lines == NULL) {
2473 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("calloc");
2474 ec46ccd7 2020-01-15 tracey goto done;
2475 ec46ccd7 2020-01-15 tracey }
2476 ec46ccd7 2020-01-15 tracey bca.lineno_cur = 1;
2477 ec46ccd7 2020-01-15 tracey bca.nlines_prec = 0;
2478 ec46ccd7 2020-01-15 tracey i = bca.nlines;
2479 ec46ccd7 2020-01-15 tracey while (i > 0) {
2480 ec46ccd7 2020-01-15 tracey i /= 10;
2481 ec46ccd7 2020-01-15 tracey bca.nlines_prec++;
2482 ec46ccd7 2020-01-15 tracey }
2483 ec46ccd7 2020-01-15 tracey bca.repo = repo;
2484 2e676fc5 2020-01-15 tracey bca.gw_trans = gw_trans;
2485 ec46ccd7 2020-01-15 tracey
2486 ec46ccd7 2020-01-15 tracey error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca, NULL,
2487 ec46ccd7 2020-01-15 tracey NULL);
2488 2e676fc5 2020-01-15 tracey if (buf_len(bca.blamebuf) > 0) {
2489 2e676fc5 2020-01-15 tracey error = buf_putc(bca.blamebuf, '\0');
2490 2e676fc5 2020-01-15 tracey blame_html = strdup(buf_get(bca.blamebuf));
2491 2e676fc5 2020-01-15 tracey }
2492 ec46ccd7 2020-01-15 tracey done:
2493 2e676fc5 2020-01-15 tracey free(bca.blamebuf);
2494 2e676fc5 2020-01-15 tracey free(in_repo_path);
2495 2e676fc5 2020-01-15 tracey free(commit_id);
2496 2e676fc5 2020-01-15 tracey free(obj_id);
2497 2e676fc5 2020-01-15 tracey free(path);
2498 2e676fc5 2020-01-15 tracey
2499 2e676fc5 2020-01-15 tracey if (blob)
2500 2e676fc5 2020-01-15 tracey error = got_object_blob_close(blob);
2501 2e676fc5 2020-01-15 tracey if (repo)
2502 2e676fc5 2020-01-15 tracey error = got_repo_close(repo);
2503 ec46ccd7 2020-01-15 tracey if (error)
2504 ec46ccd7 2020-01-15 tracey return NULL;
2505 2e676fc5 2020-01-15 tracey if (bca.lines) {
2506 2e676fc5 2020-01-15 tracey for (i = 0; i < bca.nlines; i++) {
2507 2e676fc5 2020-01-15 tracey struct blame_line *bline = &bca.lines[i];
2508 2e676fc5 2020-01-15 tracey free(bline->id_str);
2509 2e676fc5 2020-01-15 tracey free(bline->committer);
2510 2e676fc5 2020-01-15 tracey }
2511 2e676fc5 2020-01-15 tracey free(bca.lines);
2512 2e676fc5 2020-01-15 tracey }
2513 2e676fc5 2020-01-15 tracey free(bca.line_offsets);
2514 2e676fc5 2020-01-15 tracey if (bca.f && fclose(bca.f) == EOF && error == NULL)
2515 2e676fc5 2020-01-15 tracey error = got_error_from_errno("fclose");
2516 2e676fc5 2020-01-15 tracey if (error)
2517 2e676fc5 2020-01-15 tracey return NULL;
2518 ec46ccd7 2020-01-15 tracey else
2519 ec46ccd7 2020-01-15 tracey return blame_html;
2520 ec46ccd7 2020-01-15 tracey }
2521 ec46ccd7 2020-01-15 tracey
2522 ec46ccd7 2020-01-15 tracey static char*
2523 ec46ccd7 2020-01-15 tracey gw_get_repo_tree(struct trans *gw_trans, char *commit_str)
2524 ec46ccd7 2020-01-15 tracey {
2525 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
2526 ec46ccd7 2020-01-15 tracey struct got_repository *repo = NULL;
2527 bcbc97d8 2020-01-15 tracey struct got_object_id *tree_id = NULL, *commit_id = NULL;
2528 bcbc97d8 2020-01-15 tracey struct got_tree_object *tree = NULL;
2529 bcbc97d8 2020-01-15 tracey struct buf *diffbuf = NULL;
2530 bcbc97d8 2020-01-15 tracey size_t newsize;
2531 bcbc97d8 2020-01-15 tracey char *tree_html = NULL, *path = NULL, *in_repo_path = NULL,
2532 bcbc97d8 2020-01-15 tracey *tree_row = NULL, *id_str;
2533 bcbc97d8 2020-01-15 tracey int nentries, i;
2534 8d4d2453 2020-01-15 tracey
2535 bcbc97d8 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2536 ec46ccd7 2020-01-15 tracey if (error)
2537 bcbc97d8 2020-01-15 tracey return NULL;
2538 bcbc97d8 2020-01-15 tracey
2539 bcbc97d8 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2540 ec46ccd7 2020-01-15 tracey if (error)
2541 bcbc97d8 2020-01-15 tracey goto done;
2542 bcbc97d8 2020-01-15 tracey
2543 bcbc97d8 2020-01-15 tracey error = got_repo_map_path(&in_repo_path, repo, gw_trans->repo_path, 1);
2544 ec46ccd7 2020-01-15 tracey if (error)
2545 bcbc97d8 2020-01-15 tracey goto done;
2546 bcbc97d8 2020-01-15 tracey
2547 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL)
2548 ec46ccd7 2020-01-15 tracey path = strdup(gw_trans->repo_folder);
2549 ec46ccd7 2020-01-15 tracey else if (in_repo_path) {
2550 bcbc97d8 2020-01-15 tracey free(path);
2551 bcbc97d8 2020-01-15 tracey path = in_repo_path;
2552 bcbc97d8 2020-01-15 tracey }
2553 bcbc97d8 2020-01-15 tracey
2554 ec46ccd7 2020-01-15 tracey if (commit_str == NULL) {
2555 ec46ccd7 2020-01-15 tracey struct got_reference *head_ref;
2556 ec46ccd7 2020-01-15 tracey error = got_ref_open(&head_ref, repo, gw_trans->headref, 0);
2557 ec46ccd7 2020-01-15 tracey if (error)
2558 ec46ccd7 2020-01-15 tracey goto done;
2559 ec46ccd7 2020-01-15 tracey
2560 ec46ccd7 2020-01-15 tracey error = got_ref_resolve(&commit_id, repo, head_ref);
2561 ec46ccd7 2020-01-15 tracey got_ref_close(head_ref);
2562 ec46ccd7 2020-01-15 tracey
2563 ec46ccd7 2020-01-15 tracey } else
2564 ec46ccd7 2020-01-15 tracey error = resolve_commit_arg(&commit_id, commit_str, repo);
2565 bcbc97d8 2020-01-15 tracey if (error)
2566 bcbc97d8 2020-01-15 tracey goto done;
2567 bcbc97d8 2020-01-15 tracey
2568 ec46ccd7 2020-01-15 tracey error = got_object_id_by_path(&tree_id, repo, commit_id, path);
2569 bcbc97d8 2020-01-15 tracey if (error)
2570 bcbc97d8 2020-01-15 tracey goto done;
2571 bcbc97d8 2020-01-15 tracey
2572 bcbc97d8 2020-01-15 tracey error = got_object_open_as_tree(&tree, repo, tree_id);
2573 bcbc97d8 2020-01-15 tracey if (error)
2574 bcbc97d8 2020-01-15 tracey goto done;
2575 bcbc97d8 2020-01-15 tracey
2576 bcbc97d8 2020-01-15 tracey nentries = got_object_tree_get_nentries(tree);
2577 bcbc97d8 2020-01-15 tracey
2578 bcbc97d8 2020-01-15 tracey for (i = 0; i < nentries; i++) {
2579 bcbc97d8 2020-01-15 tracey struct got_tree_entry *te;
2580 ec46ccd7 2020-01-15 tracey const char *modestr = "";
2581 ec46ccd7 2020-01-15 tracey char *id = NULL, *url_html = NULL;
2582 bcbc97d8 2020-01-15 tracey
2583 bcbc97d8 2020-01-15 tracey te = got_object_tree_get_entry(tree, i);
2584 bcbc97d8 2020-01-15 tracey
2585 bcbc97d8 2020-01-15 tracey error = got_object_id_str(&id_str, got_tree_entry_get_id(te));
2586 bcbc97d8 2020-01-15 tracey if (error)
2587 bcbc97d8 2020-01-15 tracey goto done;
2588 bcbc97d8 2020-01-15 tracey
2589 ec46ccd7 2020-01-15 tracey if ((asprintf(&id, "%s", id_str)) == -1) {
2590 bcbc97d8 2020-01-15 tracey error = got_error_from_errno("asprintf");
2591 bcbc97d8 2020-01-15 tracey free(id_str);
2592 bcbc97d8 2020-01-15 tracey goto done;
2593 bcbc97d8 2020-01-15 tracey }
2594 bcbc97d8 2020-01-15 tracey
2595 bcbc97d8 2020-01-15 tracey mode_t mode = got_tree_entry_get_mode(te);
2596 bcbc97d8 2020-01-15 tracey
2597 bcbc97d8 2020-01-15 tracey if (got_object_tree_entry_is_submodule(te))
2598 bcbc97d8 2020-01-15 tracey modestr = "$";
2599 bcbc97d8 2020-01-15 tracey else if (S_ISLNK(mode))
2600 bcbc97d8 2020-01-15 tracey modestr = "@";
2601 bcbc97d8 2020-01-15 tracey else if (S_ISDIR(mode))
2602 bcbc97d8 2020-01-15 tracey modestr = "/";
2603 bcbc97d8 2020-01-15 tracey else if (mode & S_IXUSR)
2604 bcbc97d8 2020-01-15 tracey modestr = "*";
2605 bcbc97d8 2020-01-15 tracey
2606 ec46ccd7 2020-01-15 tracey char *build_folder = NULL;
2607 ec46ccd7 2020-01-15 tracey if (S_ISDIR(got_tree_entry_get_mode(te))) {
2608 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL) {
2609 ec46ccd7 2020-01-15 tracey if ((asprintf(&build_folder, "%s/%s",
2610 ec46ccd7 2020-01-15 tracey gw_trans->repo_folder,
2611 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te))) == -1) {
2612 ec46ccd7 2020-01-15 tracey error =
2613 ec46ccd7 2020-01-15 tracey got_error_from_errno("asprintf");
2614 ec46ccd7 2020-01-15 tracey goto done;
2615 ec46ccd7 2020-01-15 tracey }
2616 ec46ccd7 2020-01-15 tracey } else {
2617 ec46ccd7 2020-01-15 tracey if (asprintf(&build_folder, "%s",
2618 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te)) == -1)
2619 ec46ccd7 2020-01-15 tracey goto done;
2620 ec46ccd7 2020-01-15 tracey }
2621 ec46ccd7 2020-01-15 tracey
2622 ec46ccd7 2020-01-15 tracey if ((asprintf(&url_html, folder_html,
2623 ec46ccd7 2020-01-15 tracey gw_trans->repo_name, gw_trans->action_name,
2624 ec46ccd7 2020-01-15 tracey gw_trans->commit, build_folder,
2625 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te), modestr)) == -1) {
2626 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
2627 ec46ccd7 2020-01-15 tracey goto done;
2628 ec46ccd7 2020-01-15 tracey }
2629 ec46ccd7 2020-01-15 tracey } else {
2630 ec46ccd7 2020-01-15 tracey if (gw_trans->repo_folder != NULL) {
2631 ec46ccd7 2020-01-15 tracey if ((asprintf(&build_folder, "%s",
2632 ec46ccd7 2020-01-15 tracey gw_trans->repo_folder)) == -1) {
2633 ec46ccd7 2020-01-15 tracey error =
2634 ec46ccd7 2020-01-15 tracey got_error_from_errno("asprintf");
2635 ec46ccd7 2020-01-15 tracey goto done;
2636 ec46ccd7 2020-01-15 tracey }
2637 ec46ccd7 2020-01-15 tracey } else
2638 ec46ccd7 2020-01-15 tracey build_folder = strdup("");
2639 ec46ccd7 2020-01-15 tracey
2640 ec46ccd7 2020-01-15 tracey if ((asprintf(&url_html, file_html, gw_trans->repo_name,
2641 ec46ccd7 2020-01-15 tracey "blame", gw_trans->commit,
2642 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te), build_folder,
2643 ec46ccd7 2020-01-15 tracey got_tree_entry_get_name(te), modestr)) == -1) {
2644 ec46ccd7 2020-01-15 tracey error = got_error_from_errno("asprintf");
2645 ec46ccd7 2020-01-15 tracey goto done;
2646 ec46ccd7 2020-01-15 tracey }
2647 ec46ccd7 2020-01-15 tracey }
2648 ec46ccd7 2020-01-15 tracey free(build_folder);
2649 ec46ccd7 2020-01-15 tracey
2650 ec46ccd7 2020-01-15 tracey if (error)
2651 ec46ccd7 2020-01-15 tracey goto done;
2652 ec46ccd7 2020-01-15 tracey
2653 ec46ccd7 2020-01-15 tracey if ((asprintf(&tree_row, trees_row, "", url_html)) == -1) {
2654 bcbc97d8 2020-01-15 tracey error = got_error_from_errno("asprintf");
2655 bcbc97d8 2020-01-15 tracey goto done;
2656 bcbc97d8 2020-01-15 tracey }
2657 bcbc97d8 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, tree_row);
2658 ec46ccd7 2020-01-15 tracey if (error)
2659 ec46ccd7 2020-01-15 tracey goto done;
2660 ec46ccd7 2020-01-15 tracey
2661 bcbc97d8 2020-01-15 tracey free(id);
2662 bcbc97d8 2020-01-15 tracey free(id_str);
2663 ec46ccd7 2020-01-15 tracey free(url_html);
2664 ec46ccd7 2020-01-15 tracey free(tree_row);
2665 bcbc97d8 2020-01-15 tracey }
2666 bcbc97d8 2020-01-15 tracey
2667 bcbc97d8 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2668 bcbc97d8 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2669 bcbc97d8 2020-01-15 tracey tree_html = strdup(buf_get(diffbuf));
2670 bcbc97d8 2020-01-15 tracey }
2671 bcbc97d8 2020-01-15 tracey done:
2672 bcbc97d8 2020-01-15 tracey if (tree)
2673 bcbc97d8 2020-01-15 tracey got_object_tree_close(tree);
2674 2e676fc5 2020-01-15 tracey if (repo)
2675 2e676fc5 2020-01-15 tracey got_repo_close(repo);
2676 bcbc97d8 2020-01-15 tracey
2677 2e676fc5 2020-01-15 tracey free(in_repo_path);
2678 bcbc97d8 2020-01-15 tracey free(tree_id);
2679 bcbc97d8 2020-01-15 tracey free(diffbuf);
2680 bcbc97d8 2020-01-15 tracey if (error)
2681 bcbc97d8 2020-01-15 tracey return NULL;
2682 bcbc97d8 2020-01-15 tracey else
2683 bcbc97d8 2020-01-15 tracey return tree_html;
2684 bcbc97d8 2020-01-15 tracey }
2685 bcbc97d8 2020-01-15 tracey
2686 8d4d2453 2020-01-15 tracey static char *
2687 8d4d2453 2020-01-15 tracey gw_get_repo_heads(struct trans *gw_trans)
2688 8d4d2453 2020-01-15 tracey {
2689 87f9ebf5 2020-01-15 tracey const struct got_error *error = NULL;
2690 87f9ebf5 2020-01-15 tracey struct got_repository *repo = NULL;
2691 87f9ebf5 2020-01-15 tracey struct got_reflist_head refs;
2692 87f9ebf5 2020-01-15 tracey struct got_reflist_entry *re;
2693 87f9ebf5 2020-01-15 tracey char *heads, *head_row = NULL, *head_navs_disp = NULL, *age = NULL;
2694 87f9ebf5 2020-01-15 tracey struct buf *diffbuf = NULL;
2695 87f9ebf5 2020-01-15 tracey size_t newsize;
2696 8d4d2453 2020-01-15 tracey
2697 6c6c85af 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2698 ec46ccd7 2020-01-15 tracey if (error)
2699 6c6c85af 2020-01-15 tracey return NULL;
2700 87f9ebf5 2020-01-15 tracey
2701 87f9ebf5 2020-01-15 tracey error = got_repo_open(&repo, gw_trans->repo_path, NULL);
2702 ec46ccd7 2020-01-15 tracey if (error)
2703 87f9ebf5 2020-01-15 tracey goto done;
2704 87f9ebf5 2020-01-15 tracey
2705 87f9ebf5 2020-01-15 tracey SIMPLEQ_INIT(&refs);
2706 87f9ebf5 2020-01-15 tracey error = got_ref_list(&refs, repo, "refs/heads", got_ref_cmp_by_name,
2707 87f9ebf5 2020-01-15 tracey NULL);
2708 87f9ebf5 2020-01-15 tracey if (error)
2709 87f9ebf5 2020-01-15 tracey goto done;
2710 87f9ebf5 2020-01-15 tracey
2711 87f9ebf5 2020-01-15 tracey SIMPLEQ_FOREACH(re, &refs, entry) {
2712 87f9ebf5 2020-01-15 tracey char *refname;
2713 87f9ebf5 2020-01-15 tracey
2714 87f9ebf5 2020-01-15 tracey refname = strdup(got_ref_get_name(re->ref));
2715 87f9ebf5 2020-01-15 tracey if (refname == NULL) {
2716 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("got_ref_to_str");
2717 87f9ebf5 2020-01-15 tracey goto done;
2718 87f9ebf5 2020-01-15 tracey }
2719 87f9ebf5 2020-01-15 tracey
2720 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) != 0) {
2721 87f9ebf5 2020-01-15 tracey free(refname);
2722 87f9ebf5 2020-01-15 tracey continue;
2723 87f9ebf5 2020-01-15 tracey }
2724 87f9ebf5 2020-01-15 tracey
2725 87f9ebf5 2020-01-15 tracey age = gw_get_repo_age(gw_trans, gw_trans->gw_dir->path, refname,
2726 87f9ebf5 2020-01-15 tracey TM_DIFF);
2727 87f9ebf5 2020-01-15 tracey
2728 87f9ebf5 2020-01-15 tracey if ((asprintf(&head_navs_disp, heads_navs, gw_trans->repo_name,
2729 87f9ebf5 2020-01-15 tracey refname, gw_trans->repo_name, refname,
2730 87f9ebf5 2020-01-15 tracey gw_trans->repo_name, refname, gw_trans->repo_name,
2731 87f9ebf5 2020-01-15 tracey refname)) == -1) {
2732 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2733 87f9ebf5 2020-01-15 tracey goto done;
2734 87f9ebf5 2020-01-15 tracey }
2735 87f9ebf5 2020-01-15 tracey
2736 87f9ebf5 2020-01-15 tracey if (strncmp(refname, "refs/heads/", 11) == 0)
2737 87f9ebf5 2020-01-15 tracey refname += 11;
2738 87f9ebf5 2020-01-15 tracey
2739 87f9ebf5 2020-01-15 tracey if ((asprintf(&head_row, heads_row, age, refname,
2740 87f9ebf5 2020-01-15 tracey head_navs_disp)) == -1) {
2741 87f9ebf5 2020-01-15 tracey error = got_error_from_errno("asprintf");
2742 87f9ebf5 2020-01-15 tracey goto done;
2743 87f9ebf5 2020-01-15 tracey }
2744 87f9ebf5 2020-01-15 tracey
2745 6c6c85af 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, head_row);
2746 87f9ebf5 2020-01-15 tracey
2747 87f9ebf5 2020-01-15 tracey free(head_navs_disp);
2748 87f9ebf5 2020-01-15 tracey free(head_row);
2749 87f9ebf5 2020-01-15 tracey }
2750 87f9ebf5 2020-01-15 tracey
2751 6c6c85af 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2752 6c6c85af 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2753 6c6c85af 2020-01-15 tracey heads = strdup(buf_get(diffbuf));
2754 6c6c85af 2020-01-15 tracey }
2755 87f9ebf5 2020-01-15 tracey done:
2756 87f9ebf5 2020-01-15 tracey buf_free(diffbuf);
2757 87f9ebf5 2020-01-15 tracey got_ref_list_free(&refs);
2758 87f9ebf5 2020-01-15 tracey if (repo)
2759 87f9ebf5 2020-01-15 tracey got_repo_close(repo);
2760 87f9ebf5 2020-01-15 tracey if (error)
2761 87f9ebf5 2020-01-15 tracey return NULL;
2762 87f9ebf5 2020-01-15 tracey else
2763 87f9ebf5 2020-01-15 tracey return heads;
2764 8d4d2453 2020-01-15 tracey }
2765 8d4d2453 2020-01-15 tracey
2766 8d4d2453 2020-01-15 tracey static char *
2767 2c251c14 2020-01-15 tracey gw_get_got_link(struct trans *gw_trans)
2768 2c251c14 2020-01-15 tracey {
2769 2c251c14 2020-01-15 tracey char *link;
2770 2c251c14 2020-01-15 tracey
2771 2c251c14 2020-01-15 tracey if ((asprintf(&link, got_link, gw_trans->gw_conf->got_logo_url,
2772 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_logo)) == -1)
2773 2c251c14 2020-01-15 tracey return NULL;
2774 2c251c14 2020-01-15 tracey
2775 2c251c14 2020-01-15 tracey return link;
2776 2c251c14 2020-01-15 tracey }
2777 2c251c14 2020-01-15 tracey
2778 2c251c14 2020-01-15 tracey static char *
2779 2c251c14 2020-01-15 tracey gw_get_site_link(struct trans *gw_trans)
2780 2c251c14 2020-01-15 tracey {
2781 2c251c14 2020-01-15 tracey char *link, *repo = "", *action = "";
2782 2c251c14 2020-01-15 tracey
2783 2c251c14 2020-01-15 tracey if (gw_trans->repo_name != NULL)
2784 2c251c14 2020-01-15 tracey if ((asprintf(&repo, " / <a href='?path=%s&action=summary'>%s" \
2785 2c251c14 2020-01-15 tracey "</a>", gw_trans->repo_name, gw_trans->repo_name)) == -1)
2786 2c251c14 2020-01-15 tracey return NULL;
2787 2c251c14 2020-01-15 tracey
2788 2c251c14 2020-01-15 tracey if (gw_trans->action_name != NULL)
2789 2c251c14 2020-01-15 tracey if ((asprintf(&action, " / %s", gw_trans->action_name)) == -1)
2790 2c251c14 2020-01-15 tracey return NULL;
2791 2c251c14 2020-01-15 tracey
2792 2c251c14 2020-01-15 tracey if ((asprintf(&link, site_link, GOTWEB,
2793 2c251c14 2020-01-15 tracey gw_trans->gw_conf->got_site_link, repo, action)) == -1)
2794 2c251c14 2020-01-15 tracey return NULL;
2795 2c251c14 2020-01-15 tracey
2796 2c251c14 2020-01-15 tracey return link;
2797 2c251c14 2020-01-15 tracey }
2798 2c251c14 2020-01-15 tracey
2799 2c251c14 2020-01-15 tracey static char *
2800 ec46ccd7 2020-01-15 tracey color_diff_line(char *buf)
2801 ec46ccd7 2020-01-15 tracey {
2802 ec46ccd7 2020-01-15 tracey const struct got_error *error = NULL;
2803 ec46ccd7 2020-01-15 tracey char *colorized_line = NULL, *div_diff_line_div = NULL, *color = NULL;
2804 ec46ccd7 2020-01-15 tracey struct buf *diffbuf = NULL;
2805 ec46ccd7 2020-01-15 tracey size_t newsize;
2806 ec46ccd7 2020-01-15 tracey
2807 ec46ccd7 2020-01-15 tracey error = buf_alloc(&diffbuf, 0);
2808 ec46ccd7 2020-01-15 tracey if (error)
2809 ec46ccd7 2020-01-15 tracey return NULL;
2810 ec46ccd7 2020-01-15 tracey
2811 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "-", 1) == 0)
2812 ec46ccd7 2020-01-15 tracey color = "diff_minus";
2813 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "+", 1) == 0)
2814 ec46ccd7 2020-01-15 tracey color = "diff_plus";
2815 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "@@", 2) == 0)
2816 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
2817 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "@@", 2) == 0)
2818 ec46ccd7 2020-01-15 tracey color = "diff_chunk_header";
2819 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "commit +", 8) == 0)
2820 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2821 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "commit -", 8) == 0)
2822 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2823 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "blob +", 6) == 0)
2824 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2825 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "blob -", 6) == 0)
2826 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2827 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "file +", 6) == 0)
2828 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2829 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "file -", 6) == 0)
2830 ec46ccd7 2020-01-15 tracey color = "diff_meta";
2831 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "from:", 5) == 0)
2832 ec46ccd7 2020-01-15 tracey color = "diff_author";
2833 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "via:", 4) == 0)
2834 ec46ccd7 2020-01-15 tracey color = "diff_author";
2835 ec46ccd7 2020-01-15 tracey if (strncmp(buf, "date:", 5) == 0)
2836 ec46ccd7 2020-01-15 tracey color = "diff_date";
2837 ec46ccd7 2020-01-15 tracey
2838 ec46ccd7 2020-01-15 tracey if ((asprintf(&div_diff_line_div, div_diff_line, color)) == -1)
2839 ec46ccd7 2020-01-15 tracey return NULL;
2840 ec46ccd7 2020-01-15 tracey
2841 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, div_diff_line_div);
2842 ec46ccd7 2020-01-15 tracey if (error)
2843 ec46ccd7 2020-01-15 tracey return NULL;
2844 ec46ccd7 2020-01-15 tracey
2845 ec46ccd7 2020-01-15 tracey error = buf_puts(&newsize, diffbuf, buf);
2846 ec46ccd7 2020-01-15 tracey if (error)
2847 ec46ccd7 2020-01-15 tracey return NULL;
2848 ec46ccd7 2020-01-15 tracey
2849 ec46ccd7 2020-01-15 tracey if (buf_len(diffbuf) > 0) {
2850 ec46ccd7 2020-01-15 tracey error = buf_putc(diffbuf, '\0');
2851 ec46ccd7 2020-01-15 tracey colorized_line = strdup(buf_get(diffbuf));
2852 ec46ccd7 2020-01-15 tracey }
2853 ec46ccd7 2020-01-15 tracey
2854 ec46ccd7 2020-01-15 tracey free(diffbuf);
2855 ec46ccd7 2020-01-15 tracey free(div_diff_line_div);
2856 ec46ccd7 2020-01-15 tracey return colorized_line;
2857 ec46ccd7 2020-01-15 tracey }
2858 ec46ccd7 2020-01-15 tracey
2859 ec46ccd7 2020-01-15 tracey static char *
2860 2c251c14 2020-01-15 tracey gw_html_escape(const char *html)
2861 2c251c14 2020-01-15 tracey {
2862 2c251c14 2020-01-15 tracey char *escaped_str = NULL, *buf;
2863 2c251c14 2020-01-15 tracey char c[1];
2864 6c6c85af 2020-01-15 tracey size_t sz, i, buff_sz = 2048;
2865 2c251c14 2020-01-15 tracey
2866 6c6c85af 2020-01-15 tracey if ((buf = calloc(buff_sz, sizeof(char *))) == NULL)
2867 2c251c14 2020-01-15 tracey return NULL;
2868 2c251c14 2020-01-15 tracey
2869 2c251c14 2020-01-15 tracey if (html == NULL)
2870 2c251c14 2020-01-15 tracey return NULL;
2871 2c251c14 2020-01-15 tracey else
2872 2c251c14 2020-01-15 tracey if ((sz = strlen(html)) == 0)
2873 2c251c14 2020-01-15 tracey return NULL;
2874 2c251c14 2020-01-15 tracey
2875 6c6c85af 2020-01-15 tracey /* only work with buff_sz */
2876 6c6c85af 2020-01-15 tracey if (buff_sz < sz)
2877 6c6c85af 2020-01-15 tracey sz = buff_sz;
2878 2c251c14 2020-01-15 tracey
2879 2c251c14 2020-01-15 tracey for (i = 0; i < sz; i++) {
2880 2c251c14 2020-01-15 tracey c[0] = html[i];
2881 2c251c14 2020-01-15 tracey switch (c[0]) {
2882 2c251c14 2020-01-15 tracey case ('>'):
2883 2c251c14 2020-01-15 tracey strcat(buf, "&gt;");
2884 2c251c14 2020-01-15 tracey break;
2885 2c251c14 2020-01-15 tracey case ('&'):
2886 2c251c14 2020-01-15 tracey strcat(buf, "&amp;");
2887 2c251c14 2020-01-15 tracey break;
2888 2c251c14 2020-01-15 tracey case ('<'):
2889 2c251c14 2020-01-15 tracey strcat(buf, "&lt;");
2890 2c251c14 2020-01-15 tracey break;
2891 2c251c14 2020-01-15 tracey case ('"'):
2892 2c251c14 2020-01-15 tracey strcat(buf, "&quot;");
2893 2c251c14 2020-01-15 tracey break;
2894 2c251c14 2020-01-15 tracey case ('\''):
2895 2c251c14 2020-01-15 tracey strcat(buf, "&apos;");
2896 2c251c14 2020-01-15 tracey break;
2897 2c251c14 2020-01-15 tracey case ('\n'):
2898 2c251c14 2020-01-15 tracey strcat(buf, "<br />");
2899 2c251c14 2020-01-15 tracey default:
2900 2c251c14 2020-01-15 tracey strcat(buf, &c[0]);
2901 2c251c14 2020-01-15 tracey break;
2902 2c251c14 2020-01-15 tracey }
2903 2c251c14 2020-01-15 tracey }
2904 2c251c14 2020-01-15 tracey asprintf(&escaped_str, "%s", buf);
2905 2c251c14 2020-01-15 tracey free(buf);
2906 2c251c14 2020-01-15 tracey return escaped_str;
2907 2c251c14 2020-01-15 tracey }
2908 2c251c14 2020-01-15 tracey
2909 2c251c14 2020-01-15 tracey int
2910 2c251c14 2020-01-15 tracey main()
2911 2c251c14 2020-01-15 tracey {
2912 2c251c14 2020-01-15 tracey const struct got_error *error = NULL;
2913 2c251c14 2020-01-15 tracey struct trans *gw_trans;
2914 2c251c14 2020-01-15 tracey struct gw_dir *dir = NULL, *tdir;
2915 2c251c14 2020-01-15 tracey const char *page = "index";
2916 4ceb8155 2020-01-15 tracey int gw_malloc = 1;
2917 2c251c14 2020-01-15 tracey
2918 2c251c14 2020-01-15 tracey if ((gw_trans = malloc(sizeof(struct trans))) == NULL)
2919 2c251c14 2020-01-15 tracey errx(1, "malloc");
2920 2c251c14 2020-01-15 tracey
2921 2c251c14 2020-01-15 tracey if ((gw_trans->gw_req = malloc(sizeof(struct kreq))) == NULL)
2922 2c251c14 2020-01-15 tracey errx(1, "malloc");
2923 2c251c14 2020-01-15 tracey
2924 2c251c14 2020-01-15 tracey if ((gw_trans->gw_html_req = malloc(sizeof(struct khtmlreq))) == NULL)
2925 2c251c14 2020-01-15 tracey errx(1, "malloc");
2926 2c251c14 2020-01-15 tracey
2927 2c251c14 2020-01-15 tracey if ((gw_trans->gw_tmpl = malloc(sizeof(struct ktemplate))) == NULL)
2928 2c251c14 2020-01-15 tracey errx(1, "malloc");
2929 2c251c14 2020-01-15 tracey
2930 ec46ccd7 2020-01-15 tracey if (KCGI_OK != khttp_parse(gw_trans->gw_req, gw_keys, KEY__ZMAX,
2931 2c251c14 2020-01-15 tracey &page, 1, 0))
2932 2c251c14 2020-01-15 tracey errx(1, "khttp_parse");
2933 2c251c14 2020-01-15 tracey
2934 2c251c14 2020-01-15 tracey if ((gw_trans->gw_conf =
2935 2c251c14 2020-01-15 tracey malloc(sizeof(struct gotweb_conf))) == NULL) {
2936 4ceb8155 2020-01-15 tracey gw_malloc = 0;
2937 387a29ba 2020-01-15 tracey error = got_error_from_errno("malloc");
2938 2c251c14 2020-01-15 tracey goto err;
2939 2c251c14 2020-01-15 tracey }
2940 2c251c14 2020-01-15 tracey
2941 ec46ccd7 2020-01-15 tracey if (pledge("stdio rpath wpath cpath proc exec sendfd unveil",
2942 ec46ccd7 2020-01-15 tracey NULL) == -1) {
2943 46b9c89b 2020-01-15 tracey error = got_error_from_errno("pledge");
2944 46b9c89b 2020-01-15 tracey goto err;
2945 46b9c89b 2020-01-15 tracey }
2946 46b9c89b 2020-01-15 tracey
2947 2c251c14 2020-01-15 tracey TAILQ_INIT(&gw_trans->gw_dirs);
2948 2c251c14 2020-01-15 tracey
2949 2c251c14 2020-01-15 tracey gw_trans->page = 0;
2950 2c251c14 2020-01-15 tracey gw_trans->repos_total = 0;
2951 2c251c14 2020-01-15 tracey gw_trans->repo_path = NULL;
2952 2c251c14 2020-01-15 tracey gw_trans->commit = NULL;
2953 8087c3c5 2020-01-15 tracey gw_trans->headref = strdup(GOT_REF_HEAD);
2954 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_HTML;
2955 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->key = templs;
2956 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->keysz = TEMPL__MAX;
2957 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->arg = gw_trans;
2958 2c251c14 2020-01-15 tracey gw_trans->gw_tmpl->cb = gw_template;
2959 2c251c14 2020-01-15 tracey error = parse_conf(GOTWEB_CONF, gw_trans->gw_conf);
2960 2c251c14 2020-01-15 tracey
2961 2c251c14 2020-01-15 tracey err:
2962 2c251c14 2020-01-15 tracey if (error) {
2963 2c251c14 2020-01-15 tracey gw_trans->mime = KMIME_TEXT_PLAIN;
2964 2c251c14 2020-01-15 tracey gw_trans->action = GW_ERR;
2965 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
2966 2c251c14 2020-01-15 tracey goto done;
2967 2c251c14 2020-01-15 tracey }
2968 2c251c14 2020-01-15 tracey
2969 2c251c14 2020-01-15 tracey error = gw_parse_querystring(gw_trans);
2970 2c251c14 2020-01-15 tracey if (error)
2971 2c251c14 2020-01-15 tracey goto err;
2972 2c251c14 2020-01-15 tracey
2973 2c251c14 2020-01-15 tracey gw_display_index(gw_trans, error);
2974 2c251c14 2020-01-15 tracey
2975 2c251c14 2020-01-15 tracey done:
2976 2c251c14 2020-01-15 tracey if (gw_malloc) {
2977 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_repos_path);
2978 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_www_path);
2979 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_name);
2980 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_owner);
2981 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_site_link);
2982 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo);
2983 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf->got_logo_url);
2984 2c251c14 2020-01-15 tracey free(gw_trans->gw_conf);
2985 2c251c14 2020-01-15 tracey free(gw_trans->commit);
2986 2c251c14 2020-01-15 tracey free(gw_trans->repo_path);
2987 2c251c14 2020-01-15 tracey free(gw_trans->repo_name);
2988 2c251c14 2020-01-15 tracey free(gw_trans->repo_file);
2989 2c251c14 2020-01-15 tracey free(gw_trans->action_name);
2990 8087c3c5 2020-01-15 tracey free(gw_trans->headref);
2991 2c251c14 2020-01-15 tracey
2992 2c251c14 2020-01-15 tracey TAILQ_FOREACH_SAFE(dir, &gw_trans->gw_dirs, entry, tdir) {
2993 2c251c14 2020-01-15 tracey free(dir->name);
2994 2c251c14 2020-01-15 tracey free(dir->description);
2995 2c251c14 2020-01-15 tracey free(dir->age);
2996 2c251c14 2020-01-15 tracey free(dir->url);
2997 2c251c14 2020-01-15 tracey free(dir->path);
2998 2c251c14 2020-01-15 tracey free(dir);
2999 2c251c14 2020-01-15 tracey }
3000 2c251c14 2020-01-15 tracey
3001 2c251c14 2020-01-15 tracey }
3002 2c251c14 2020-01-15 tracey
3003 2c251c14 2020-01-15 tracey khttp_free(gw_trans->gw_req);
3004 2c251c14 2020-01-15 tracey return EXIT_SUCCESS;
3005 2c251c14 2020-01-15 tracey }