Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 1 | /* cmd.c: the cgit command dispatcher |
| 2 | * |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 3 | * Copyright (C) 2006-2017 cgit Development Team <cgit@lists.zx2c4.com> |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | #include "cgit.h" |
| 10 | #include "cmd.h" |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 11 | #include "cache.h" |
| 12 | #include "ui-shared.h" |
Lars Hjemli | b2a3d31 | 2008-05-21 08:17:54 +0200 | [diff] [blame] | 13 | #include "ui-atom.h" |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 14 | #include "ui-blame.h" |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 15 | #include "ui-blob.h" |
Lars Hjemli | 02a545e | 2008-08-06 01:20:24 +0200 | [diff] [blame] | 16 | #include "ui-clone.h" |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 17 | #include "ui-commit.h" |
| 18 | #include "ui-diff.h" |
| 19 | #include "ui-log.h" |
| 20 | #include "ui-patch.h" |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 21 | #include "ui-plain.h" |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 22 | #include "ui-refs.h" |
| 23 | #include "ui-repolist.h" |
| 24 | #include "ui-snapshot.h" |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 25 | #include "ui-stats.h" |
Lars Hjemli | c5984a9 | 2008-03-24 16:38:47 +0100 | [diff] [blame] | 26 | #include "ui-summary.h" |
| 27 | #include "ui-tag.h" |
| 28 | #include "ui-tree.h" |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 29 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 30 | static void HEAD_fn(void) |
Lars Hjemli | 02a545e | 2008-08-06 01:20:24 +0200 | [diff] [blame] | 31 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 32 | cgit_clone_head(); |
Lars Hjemli | 02a545e | 2008-08-06 01:20:24 +0200 | [diff] [blame] | 33 | } |
| 34 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 35 | static void atom_fn(void) |
Lars Hjemli | b2a3d31 | 2008-05-21 08:17:54 +0200 | [diff] [blame] | 36 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 37 | cgit_print_atom(ctx.qry.head, ctx.qry.path, ctx.cfg.max_atom_items); |
Lars Hjemli | b2a3d31 | 2008-05-21 08:17:54 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 40 | static void about_fn(void) |
Lars Hjemli | 71adba1 | 2008-04-29 01:09:41 +0200 | [diff] [blame] | 41 | { |
Jason A. Donenfeld | 03de473 | 2015-08-14 15:54:32 +0200 | [diff] [blame] | 42 | if (ctx.repo) { |
Jason A. Donenfeld | 09a3aa5 | 2016-02-26 13:14:43 +0100 | [diff] [blame] | 43 | size_t path_info_len = ctx.env.path_info ? strlen(ctx.env.path_info) : 0; |
Jason A. Donenfeld | 03de473 | 2015-08-14 15:54:32 +0200 | [diff] [blame] | 44 | if (!ctx.qry.path && |
| 45 | ctx.qry.url[strlen(ctx.qry.url) - 1] != '/' && |
Jason A. Donenfeld | 09a3aa5 | 2016-02-26 13:14:43 +0100 | [diff] [blame] | 46 | (!path_info_len || ctx.env.path_info[path_info_len - 1] != '/')) { |
Christian Hesse | 6f2e440 | 2015-10-09 13:15:49 +0200 | [diff] [blame] | 47 | char *currenturl = cgit_currenturl(); |
| 48 | char *redirect = fmtalloc("%s/", currenturl); |
| 49 | cgit_redirect(redirect, true); |
| 50 | free(currenturl); |
| 51 | free(redirect); |
Jason A. Donenfeld | 94c02bb | 2016-02-22 16:33:49 +0100 | [diff] [blame] | 52 | } else if (ctx.repo->readme.nr) |
Jason A. Donenfeld | 03de473 | 2015-08-14 15:54:32 +0200 | [diff] [blame] | 53 | cgit_print_repo_readme(ctx.qry.path); |
Jason A. Donenfeld | 94c02bb | 2016-02-22 16:33:49 +0100 | [diff] [blame] | 54 | else if (ctx.repo->homepage) |
| 55 | cgit_redirect(ctx.repo->homepage, false); |
| 56 | else { |
| 57 | char *currenturl = cgit_currenturl(); |
| 58 | char *redirect = fmtalloc("%s../", currenturl); |
| 59 | cgit_redirect(redirect, false); |
| 60 | free(currenturl); |
| 61 | free(redirect); |
| 62 | } |
Jason A. Donenfeld | 03de473 | 2015-08-14 15:54:32 +0200 | [diff] [blame] | 63 | } else |
Lars Hjemli | 71adba1 | 2008-04-29 01:09:41 +0200 | [diff] [blame] | 64 | cgit_print_site_readme(); |
| 65 | } |
| 66 | |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 67 | static void blame_fn(void) |
| 68 | { |
| 69 | if (ctx.cfg.enable_blame) |
| 70 | cgit_print_blame(); |
| 71 | else |
| 72 | cgit_print_error_page(403, "Forbidden", "Blame is disabled"); |
| 73 | } |
| 74 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 75 | static void blob_fn(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 76 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 77 | cgit_print_blob(ctx.qry.sha1, ctx.qry.path, ctx.qry.head, 0); |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 80 | static void commit_fn(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 81 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 82 | cgit_print_commit(ctx.qry.sha1, ctx.qry.path); |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 85 | static void diff_fn(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 86 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 87 | cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1, 0); |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 90 | static void rawdiff_fn(void) |
Lukas Fleischer | 445f6ae | 2013-08-14 10:50:33 +0200 | [diff] [blame] | 91 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 92 | cgit_print_diff(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1, 1); |
Lukas Fleischer | 445f6ae | 2013-08-14 10:50:33 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 95 | static void info_fn(void) |
Lars Hjemli | 02a545e | 2008-08-06 01:20:24 +0200 | [diff] [blame] | 96 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 97 | cgit_clone_info(); |
Lars Hjemli | 02a545e | 2008-08-06 01:20:24 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 100 | static void log_fn(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 101 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 102 | cgit_print_log(ctx.qry.sha1, ctx.qry.ofs, ctx.cfg.max_commit_count, |
| 103 | ctx.qry.grep, ctx.qry.search, ctx.qry.path, 1, |
| 104 | ctx.repo->enable_commit_graph, |
| 105 | ctx.repo->commit_sort); |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 108 | static void ls_cache_fn(void) |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 109 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 110 | ctx.page.mimetype = "text/plain"; |
| 111 | ctx.page.filename = "ls-cache.txt"; |
| 112 | cgit_print_http_headers(); |
| 113 | cache_ls(ctx.cfg.cache_root); |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 116 | static void objects_fn(void) |
Lars Hjemli | 02a545e | 2008-08-06 01:20:24 +0200 | [diff] [blame] | 117 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 118 | cgit_clone_objects(); |
Lars Hjemli | 02a545e | 2008-08-06 01:20:24 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 121 | static void repolist_fn(void) |
Lars Hjemli | 9000bbf | 2008-04-28 12:10:13 +0200 | [diff] [blame] | 122 | { |
| 123 | cgit_print_repolist(); |
| 124 | } |
| 125 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 126 | static void patch_fn(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 127 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 128 | cgit_print_patch(ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path); |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 129 | } |
| 130 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 131 | static void plain_fn(void) |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 132 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 133 | cgit_print_plain(); |
Lars Hjemli | e5da4bc | 2008-08-06 10:53:50 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 136 | static void refs_fn(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 137 | { |
| 138 | cgit_print_refs(); |
| 139 | } |
| 140 | |
John Keeping | 46c7163 | 2018-06-18 14:22:02 +0800 | [diff] [blame^] | 141 | static void source_fn(void) |
| 142 | { |
| 143 | cgit_print_tree(ctx.qry.sha1, ctx.qry.path); |
| 144 | } |
| 145 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 146 | static void snapshot_fn(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 147 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 148 | cgit_print_snapshot(ctx.qry.head, ctx.qry.sha1, ctx.qry.path, |
Lukas Fleischer | 3e9578e | 2014-02-08 14:37:29 +0100 | [diff] [blame] | 149 | ctx.qry.nohead); |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 152 | static void stats_fn(void) |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 153 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 154 | cgit_show_stats(); |
Lars Hjemli | f86a23f | 2008-12-06 17:38:19 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 157 | static void summary_fn(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 158 | { |
| 159 | cgit_print_summary(); |
| 160 | } |
| 161 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 162 | static void tag_fn(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 163 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 164 | cgit_print_tag(ctx.qry.sha1); |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 167 | static void tree_fn(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 168 | { |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 169 | cgit_print_tree(ctx.qry.sha1, ctx.qry.path); |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 170 | } |
| 171 | |
John Keeping | a420c7c | 2015-08-14 12:47:21 +0100 | [diff] [blame] | 172 | #define def_cmd(name, want_repo, want_vpath, is_clone) \ |
John Keeping | 73ef856 | 2015-08-14 15:20:23 +0100 | [diff] [blame] | 173 | {#name, name##_fn, want_repo, want_vpath, is_clone} |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 174 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 175 | struct cgit_cmd *cgit_get_cmd(void) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 176 | { |
| 177 | static struct cgit_cmd cmds[] = { |
John Keeping | a420c7c | 2015-08-14 12:47:21 +0100 | [diff] [blame] | 178 | def_cmd(HEAD, 1, 0, 1), |
| 179 | def_cmd(atom, 1, 0, 0), |
Jason A. Donenfeld | 03de473 | 2015-08-14 15:54:32 +0200 | [diff] [blame] | 180 | def_cmd(about, 0, 0, 0), |
Jeff Smith | c1cd290 | 2017-10-01 23:39:08 -0500 | [diff] [blame] | 181 | def_cmd(blame, 1, 1, 0), |
John Keeping | a420c7c | 2015-08-14 12:47:21 +0100 | [diff] [blame] | 182 | def_cmd(blob, 1, 0, 0), |
| 183 | def_cmd(commit, 1, 1, 0), |
| 184 | def_cmd(diff, 1, 1, 0), |
| 185 | def_cmd(info, 1, 0, 1), |
| 186 | def_cmd(log, 1, 1, 0), |
| 187 | def_cmd(ls_cache, 0, 0, 0), |
| 188 | def_cmd(objects, 1, 0, 1), |
| 189 | def_cmd(patch, 1, 1, 0), |
| 190 | def_cmd(plain, 1, 0, 0), |
| 191 | def_cmd(rawdiff, 1, 1, 0), |
| 192 | def_cmd(refs, 1, 0, 0), |
| 193 | def_cmd(repolist, 0, 0, 0), |
| 194 | def_cmd(snapshot, 1, 0, 0), |
John Keeping | 46c7163 | 2018-06-18 14:22:02 +0800 | [diff] [blame^] | 195 | def_cmd(source, 1, 1, 0), |
John Keeping | a420c7c | 2015-08-14 12:47:21 +0100 | [diff] [blame] | 196 | def_cmd(stats, 1, 1, 0), |
| 197 | def_cmd(summary, 1, 0, 0), |
| 198 | def_cmd(tag, 1, 0, 0), |
| 199 | def_cmd(tree, 1, 1, 0), |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 200 | }; |
| 201 | int i; |
| 202 | |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 203 | if (ctx.qry.page == NULL) { |
| 204 | if (ctx.repo) |
| 205 | ctx.qry.page = "summary"; |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 206 | else |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 207 | ctx.qry.page = "repolist"; |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 210 | for (i = 0; i < sizeof(cmds)/sizeof(*cmds); i++) |
Lukas Fleischer | f60ffa1 | 2014-01-15 21:53:15 +0100 | [diff] [blame] | 211 | if (!strcmp(ctx.qry.page, cmds[i].name)) |
Lars Hjemli | e0e4478 | 2008-03-24 01:09:39 +0100 | [diff] [blame] | 212 | return &cmds[i]; |
| 213 | return NULL; |
| 214 | } |