feat(cgi): switch branches and tags with ?ref
Read the ref query parameter, validate it, pass it to got for tree, blob, raw, and log views, and render a no-JavaScript branch/tag bar. Preserve the ref in repository links and cover dev/main switching and traversal rejection in tests. Assisted-by: OpenCode:DeepSeek-V4.1-Flash
This commit is contained in:
1 parent
4be6c78264
commit
0e54cb8a02
4 files changed
+156
-6
No files matched your search
@@ -64,6 +64,8 @@ storage.
|
||||
- C99 + POSIX only (`-D_POSIX_C_SOURCE=200809L`), no third-party libraries.
|
||||
Build with `-Wall -Wextra -Wpedantic -Werror`.
|
||||
- Keep `make -C cgi check` green. Tests may use `git` only to create fixtures.
|
||||
- The CGI's only navigation state is `?ref=`; validate it like a revision (no
|
||||
`..`, no leading dash) and preserve it in repository links.
|
||||
- `tidy -q -e` must accept generated pages; add table `summary` attributes rather
|
||||
than suppressing warnings.
|
||||
|
||||
|
||||
@@ -143,6 +143,8 @@ There is no silent git fallback for the migrated init path.
|
||||
invocation and no `git` fallback.
|
||||
- Routes: `/`, `/repo/<name>/tree[/path]`, `/repo/<name>/blob/<path>`,
|
||||
`/repo/<name>/raw/<path>`, `/repo/<name>/log`, `/repo/<name>/commit/<rev>`.
|
||||
- Branch and tag switching uses `?ref=refs/heads/<name>` (or `refs/tags/<name>`)
|
||||
on tree, blob, raw, and log pages; no JavaScript is involved.
|
||||
|
||||
## Why the backend is not rewritten in C
|
||||
|
||||
|
||||
+126
-6
@@ -70,6 +70,7 @@ struct path {
|
||||
};
|
||||
|
||||
static const char *g_script;
|
||||
static const char *g_ref = "HEAD";
|
||||
static const char *g_root;
|
||||
static const char *g_got;
|
||||
static int g_head_only;
|
||||
@@ -262,6 +263,10 @@ href_repo(struct buf *b, const char *action, const char *name,
|
||||
b_putc(b, '/');
|
||||
b_url_path(b, path);
|
||||
}
|
||||
if (g_ref != NULL && strcmp(g_ref, "HEAD") != 0) {
|
||||
b_puts(b, "?ref=");
|
||||
b_url_path(b, g_ref);
|
||||
}
|
||||
b_puts(b, "\">");
|
||||
}
|
||||
|
||||
@@ -547,6 +552,30 @@ url_decode(const char *s)
|
||||
return out;
|
||||
}
|
||||
|
||||
static char *
|
||||
query_value(const char *name)
|
||||
{
|
||||
const char *qs = getenv("QUERY_STRING");
|
||||
char *copy, *save, *pair, *eq, *value = NULL;
|
||||
|
||||
if (qs == NULL || *qs == '\0')
|
||||
return NULL;
|
||||
copy = xstrdup(qs);
|
||||
for (pair = strtok_r(copy, "&", &save); pair != NULL;
|
||||
pair = strtok_r(NULL, "&", &save)) {
|
||||
eq = strchr(pair, '=');
|
||||
if (eq == NULL)
|
||||
continue;
|
||||
*eq = '\0';
|
||||
if (strcmp(pair, name) == 0) {
|
||||
value = url_decode(eq + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(copy);
|
||||
return value;
|
||||
}
|
||||
|
||||
static int
|
||||
split_path(struct path *p, const char *decoded)
|
||||
{
|
||||
@@ -887,6 +916,75 @@ run_got(char *const argv[], struct buf *out, struct buf *err)
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void
|
||||
refbar(struct buf *b, const char *repo_path)
|
||||
{
|
||||
char *args[8];
|
||||
struct buf out, err;
|
||||
char *line, *next;
|
||||
int rc, any = 0;
|
||||
|
||||
b_init(&out);
|
||||
b_init(&err);
|
||||
args[0] = (char *)g_got;
|
||||
args[1] = (char *)"ref";
|
||||
args[2] = (char *)"-l";
|
||||
args[3] = (char *)"-r";
|
||||
args[4] = (char *)repo_path;
|
||||
args[5] = NULL;
|
||||
rc = run_got(args, &out, &err);
|
||||
b_free(&err);
|
||||
if (rc != 0 || out.p == NULL) {
|
||||
b_free(&out);
|
||||
return;
|
||||
}
|
||||
b_puts(b, "<p><font size=\"-1\">ref:");
|
||||
line = out.p;
|
||||
while (line != NULL && *line != '\0') {
|
||||
const char *display;
|
||||
char *colon;
|
||||
|
||||
next = strchr(line, '\n');
|
||||
if (next != NULL)
|
||||
*next++ = '\0';
|
||||
colon = strchr(line, ':');
|
||||
if (colon == NULL) {
|
||||
line = next;
|
||||
continue;
|
||||
}
|
||||
*colon = '\0';
|
||||
if (strncmp(line, "refs/heads/", 11) == 0)
|
||||
display = line + 11;
|
||||
else if (strncmp(line, "refs/tags/", 10) == 0)
|
||||
display = line + 10;
|
||||
else {
|
||||
line = next;
|
||||
continue;
|
||||
}
|
||||
if (any)
|
||||
b_puts(b, " | ");
|
||||
else
|
||||
b_putc(b, ' ');
|
||||
if (strcmp(line, g_ref) == 0) {
|
||||
b_puts(b, "<b>");
|
||||
b_html(b, display);
|
||||
b_puts(b, "</b>");
|
||||
} else {
|
||||
b_puts(b, "<a href=\"?ref=");
|
||||
b_url_path(b, line);
|
||||
b_puts(b, "\">");
|
||||
b_html(b, display);
|
||||
b_puts(b, "</a>");
|
||||
}
|
||||
any = 1;
|
||||
line = next;
|
||||
}
|
||||
if (!any)
|
||||
b_puts(b, " <i>none</i>");
|
||||
b_puts(b, "</font></p>\n");
|
||||
b_free(&out);
|
||||
}
|
||||
|
||||
static void
|
||||
css(struct buf *b)
|
||||
{
|
||||
@@ -1241,7 +1339,7 @@ page_tree(struct buf *b, const char *repo, const char *repo_path,
|
||||
args[i++] = (char *)"-r";
|
||||
args[i++] = (char *)repo_path;
|
||||
args[i++] = (char *)"-c";
|
||||
args[i++] = (char *)"HEAD";
|
||||
args[i++] = (char *)g_ref;
|
||||
args[i++] = (char *)"-i";
|
||||
if (path != NULL && *path != '\0')
|
||||
args[i++] = (char *)path;
|
||||
@@ -1261,6 +1359,7 @@ page_tree(struct buf *b, const char *repo, const char *repo_path,
|
||||
b_puts(b, " ");
|
||||
href_repo(b, "log", repo, NULL);
|
||||
b_puts(b, "[history]</a></p>\n");
|
||||
refbar(b, repo_path);
|
||||
if (out.len == 0) {
|
||||
b_puts(b, "<p><i>empty directory</i></p>\n");
|
||||
} else {
|
||||
@@ -1376,7 +1475,7 @@ static int
|
||||
got_cat(const char *repo_path, const char *path, struct buf *out,
|
||||
struct buf *err)
|
||||
{
|
||||
return got_cat_ref(repo_path, "HEAD", path, out, err);
|
||||
return got_cat_ref(repo_path, g_ref, path, out, err);
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -1410,6 +1509,7 @@ page_blob(struct buf *b, const char *repo, const char *repo_path,
|
||||
b_puts(b, "[raw]</a> ");
|
||||
href_repo(b, "log", repo, NULL);
|
||||
b_puts(b, "[history]</a></p>\n");
|
||||
refbar(b, repo_path);
|
||||
if (binary) {
|
||||
b_printf(b, "<p><i>Binary file, %lu bytes.</i></p>\n",
|
||||
(unsigned long)out.len);
|
||||
@@ -1435,7 +1535,7 @@ static void
|
||||
page_log(struct buf *b, const char *repo, const char *repo_path,
|
||||
struct repo *repos, size_t nrepos)
|
||||
{
|
||||
char *args[8];
|
||||
char *args[10];
|
||||
struct buf out, err;
|
||||
char *line, *next;
|
||||
int rc;
|
||||
@@ -1448,7 +1548,9 @@ page_log(struct buf *b, const char *repo, const char *repo_path,
|
||||
args[3] = (char *)repo_path;
|
||||
args[4] = (char *)"-l";
|
||||
args[5] = (char *)"50";
|
||||
args[6] = NULL;
|
||||
args[6] = (char *)"-c";
|
||||
args[7] = (char *)g_ref;
|
||||
args[8] = NULL;
|
||||
rc = run_got(args, &out, &err);
|
||||
if (rc != 0) {
|
||||
page_error_body(b, "History unavailable",
|
||||
@@ -1467,7 +1569,9 @@ page_log(struct buf *b, const char *repo, const char *repo_path,
|
||||
b_html(b, repo);
|
||||
b_puts(b, "</a> / history ");
|
||||
href_repo(b, "tree", repo, NULL);
|
||||
b_puts(b, "[files]</a></p>\n<pre>");
|
||||
b_puts(b, "[files]</a></p>\n");
|
||||
refbar(b, repo_path);
|
||||
b_puts(b, "<pre>");
|
||||
line = out.p;
|
||||
while (line != NULL && *line != '\0') {
|
||||
size_t len;
|
||||
@@ -2065,7 +2169,7 @@ main(void)
|
||||
const char *pages_host, *http_host;
|
||||
char hostbuf[PATHBUF];
|
||||
char *decoded = NULL, *repo_path = NULL, *repo_name = NULL;
|
||||
char *path = NULL;
|
||||
char *path = NULL, *ref = NULL;
|
||||
char script_buf[PATHBUF];
|
||||
struct path p;
|
||||
struct repo *repos;
|
||||
@@ -2114,6 +2218,21 @@ main(void)
|
||||
return serve_pages();
|
||||
}
|
||||
|
||||
ref = query_value("ref");
|
||||
if (ref != NULL && *ref != '\0') {
|
||||
if (!valid_rev(ref)) {
|
||||
b_init(&body);
|
||||
page_error_body(&body, "Bad request",
|
||||
"Invalid ref.", NULL);
|
||||
send_buf(400, "text/html; charset=UTF-8", &body,
|
||||
g_head_only);
|
||||
b_free(&body);
|
||||
free(ref);
|
||||
return 0;
|
||||
}
|
||||
g_ref = ref;
|
||||
}
|
||||
|
||||
decoded_env = env_or("PATH_INFO", "/");
|
||||
decoded = url_decode(decoded_env);
|
||||
if (decoded == NULL) {
|
||||
@@ -2216,6 +2335,7 @@ done:
|
||||
free(path);
|
||||
free(repo_path);
|
||||
free(repo_name);
|
||||
free(ref);
|
||||
free(decoded);
|
||||
free(p.raw);
|
||||
free_repos(repos, nrepos);
|
||||
|
||||
@@ -34,6 +34,13 @@ run() {
|
||||
PATH_INFO="$1" "$bin"
|
||||
}
|
||||
|
||||
run_ref() {
|
||||
env -i PATH=/usr/bin:/bin HOME="$tmp" \
|
||||
GOTEA_REPO_ROOT="$tmp/repos" GOT_BIN="${GOT_BIN:-/usr/bin/got}" \
|
||||
SCRIPT_NAME=/cgi-bin/gotea REQUEST_METHOD=GET \
|
||||
QUERY_STRING="$2" PATH_INFO="$1" "$bin"
|
||||
}
|
||||
|
||||
run_palette() {
|
||||
env -i PATH=/usr/bin:/bin HOME="$tmp" \
|
||||
GOTEA_REPO_ROOT="$tmp/repos" GOT_BIN="${GOT_BIN:-/usr/bin/got}" \
|
||||
@@ -54,6 +61,13 @@ git add .
|
||||
GIT_AUTHOR_NAME='Test User' GIT_AUTHOR_EMAIL='test@example.com' \
|
||||
GIT_COMMITTER_NAME='Test User' GIT_COMMITTER_EMAIL='test@example.com' \
|
||||
git commit -qm 'initial import'
|
||||
git checkout -q -b dev
|
||||
printf 'dev branch\n' > dev.txt
|
||||
git add dev.txt
|
||||
GIT_AUTHOR_NAME='Test User' GIT_AUTHOR_EMAIL='test@example.com' \
|
||||
GIT_COMMITTER_NAME='Test User' GIT_COMMITTER_EMAIL='test@example.com' \
|
||||
git commit -qm 'dev branch'
|
||||
git checkout -q main
|
||||
cd "$cgi"
|
||||
hash=$(git -C "$tmp/src" rev-parse HEAD)
|
||||
git clone -q --bare "$tmp/src" "$tmp/repos/owner/demo.git"
|
||||
@@ -77,6 +91,18 @@ expect_contains "$tree" '/repo/owner/demo/blob/README.md' "tree: blob link"
|
||||
expect_contains "$tree" '[history]' "tree: history link"
|
||||
echo "PASS tree"
|
||||
|
||||
devtree=$(run_ref /repo/owner/demo/tree ref=refs/heads/dev)
|
||||
expect_contains "$devtree" 'dev.txt' "ref: dev tree"
|
||||
expect_contains "$devtree" '?ref=refs/heads/dev' "ref: links keep ref"
|
||||
expect_contains "$devtree" '<b>dev</b>' "ref: current branch marked"
|
||||
maintree=$(run /repo/owner/demo/tree)
|
||||
expect_no "$maintree" 'dev.txt' "ref: main stays main"
|
||||
devblob=$(run_ref /repo/owner/demo/blob/dev.txt ref=refs/heads/dev)
|
||||
expect_contains "$devblob" 'dev branch' "ref: blob from dev"
|
||||
badref=$(run_ref /repo/owner/demo/tree ref=../x)
|
||||
expect_contains "$badref" 'Status: 400' "ref: traversal rejected"
|
||||
echo "PASS refs"
|
||||
|
||||
sub=$(run /repo/owner/demo/tree/src)
|
||||
expect_contains "$sub" 'main.c' "subtree: file"
|
||||
expect_contains "$sub" 'logo.bin' "subtree: binary file"
|
||||
|
||||
Reference in new issue
Block a user