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