2008-08-05 23:20:24 +00:00
|
|
|
/* ui-clone.c: functions for http cloning, based on
|
|
|
|
* git's http-backend.c by Shawn O. Pearce
|
|
|
|
*
|
2014-01-08 14:10:49 +00:00
|
|
|
* Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
|
2008-08-05 23:20:24 +00:00
|
|
|
*
|
|
|
|
* Licensed under GNU General Public License v2
|
|
|
|
* (see COPYING for full license text)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cgit.h"
|
2013-04-06 10:37:59 +00:00
|
|
|
#include "ui-clone.h"
|
2008-08-05 23:20:24 +00:00
|
|
|
#include "html.h"
|
|
|
|
#include "ui-shared.h"
|
|
|
|
|
2015-07-28 08:42:01 +00:00
|
|
|
static int print_ref_info(const char *refname, const struct object_id *oid,
|
2008-08-05 23:20:24 +00:00
|
|
|
int flags, void *cb_data)
|
|
|
|
{
|
|
|
|
struct object *obj;
|
|
|
|
|
2015-07-28 08:42:01 +00:00
|
|
|
if (!(obj = parse_object(oid->hash)))
|
2008-08-05 23:20:24 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-07-28 08:42:01 +00:00
|
|
|
htmlf("%s\t%s\n", oid_to_hex(oid), refname);
|
2013-02-01 20:08:51 +00:00
|
|
|
if (obj->type == OBJ_TAG) {
|
2008-08-05 23:20:24 +00:00
|
|
|
if (!(obj = deref_tag(obj, refname, 0)))
|
|
|
|
return 0;
|
2016-01-05 06:38:53 +00:00
|
|
|
htmlf("%s\t%s^{}\n", oid_to_hex(&obj->oid), refname);
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
static void print_pack_info(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
|
|
|
struct packed_git *pack;
|
2015-02-09 11:27:44 +00:00
|
|
|
char *offset;
|
2008-08-05 23:20:24 +00:00
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
ctx.page.mimetype = "text/plain";
|
|
|
|
ctx.page.filename = "objects/info/packs";
|
|
|
|
cgit_print_http_headers();
|
2008-08-05 23:20:24 +00:00
|
|
|
prepare_packed_git();
|
2015-02-09 11:27:44 +00:00
|
|
|
for (pack = packed_git; pack; pack = pack->next) {
|
|
|
|
if (pack->pack_local) {
|
|
|
|
offset = strrchr(pack->pack_name, '/');
|
|
|
|
if (offset && offset[1] != '\0')
|
|
|
|
++offset;
|
|
|
|
else
|
|
|
|
offset = pack->pack_name;
|
|
|
|
htmlf("P %s\n", offset);
|
|
|
|
}
|
|
|
|
}
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 08:42:01 +00:00
|
|
|
static void send_file(const char *path)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (stat(path, &st)) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
2015-08-14 11:47:03 +00:00
|
|
|
cgit_print_error_page(404, "Not found", "Not found");
|
2008-08-05 23:20:24 +00:00
|
|
|
break;
|
|
|
|
case EACCES:
|
2015-08-14 11:47:03 +00:00
|
|
|
cgit_print_error_page(403, "Forbidden", "Forbidden");
|
2008-08-05 23:20:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-08-14 11:47:03 +00:00
|
|
|
cgit_print_error_page(400, "Bad request", "Bad request");
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2014-01-15 20:53:15 +00:00
|
|
|
ctx.page.mimetype = "application/octet-stream";
|
|
|
|
ctx.page.filename = path;
|
2015-02-09 06:25:00 +00:00
|
|
|
skip_prefix(path, ctx.repo->path, &ctx.page.filename);
|
|
|
|
skip_prefix(ctx.page.filename, "/", &ctx.page.filename);
|
2014-01-15 20:53:15 +00:00
|
|
|
cgit_print_http_headers();
|
2008-08-05 23:20:24 +00:00
|
|
|
html_include(path);
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
void cgit_clone_info(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
2015-01-15 18:47:42 +00:00
|
|
|
if (!ctx.qry.path || strcmp(ctx.qry.path, "refs")) {
|
2015-08-14 11:47:03 +00:00
|
|
|
cgit_print_error_page(400, "Bad request", "Bad request");
|
2008-08-05 23:20:24 +00:00
|
|
|
return;
|
2015-01-15 18:47:42 +00:00
|
|
|
}
|
2008-08-05 23:20:24 +00:00
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
ctx.page.mimetype = "text/plain";
|
|
|
|
ctx.page.filename = "info/refs";
|
|
|
|
cgit_print_http_headers();
|
|
|
|
for_each_ref(print_ref_info, NULL);
|
2016-06-25 22:24:20 +00:00
|
|
|
/* NOTE: we pass an empty prefix because we want to enumerate everything
|
|
|
|
not just all refs under $namespace/refs/ */
|
|
|
|
cgit_for_each_namespaced_ref_in("", print_ref_info, NULL);
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
void cgit_clone_objects(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
2014-01-15 20:53:15 +00:00
|
|
|
if (!ctx.qry.path) {
|
2015-08-14 11:47:03 +00:00
|
|
|
cgit_print_error_page(400, "Bad request", "Bad request");
|
2008-08-05 23:20:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
if (!strcmp(ctx.qry.path, "info/packs")) {
|
|
|
|
print_pack_info();
|
2008-08-05 23:20:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
send_file(git_path("objects/%s", ctx.qry.path));
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 20:53:15 +00:00
|
|
|
void cgit_clone_head(void)
|
2008-08-05 23:20:24 +00:00
|
|
|
{
|
2016-06-25 22:24:20 +00:00
|
|
|
if (get_git_namespace()) {
|
|
|
|
unsigned char unused[20];
|
|
|
|
char *namespaced_head = NULL;
|
|
|
|
const char *ref;
|
|
|
|
namespaced_head = mkpathdup("%sHEAD", get_git_namespace());
|
|
|
|
/* NOTE: RESOLVE_REF_NO_RECURSE is required to prevent it resolving HEAD
|
|
|
|
into a ref outside of the namespace. */
|
|
|
|
ref = resolve_ref_unsafe(namespaced_head, RESOLVE_REF_NO_RECURSE, unused, NULL);
|
|
|
|
if (!ref) {
|
|
|
|
cgit_print_error_page(404, "Not found", "Not found");
|
|
|
|
free(namespaced_head);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cgit_print_http_headers();
|
|
|
|
htmlf("ref: %s\n", strip_namespace(ref));
|
|
|
|
free(namespaced_head);
|
|
|
|
} else {
|
|
|
|
send_file(git_path("%s", "HEAD"));
|
|
|
|
}
|
2008-08-05 23:20:24 +00:00
|
|
|
}
|